#ifdef CONFIG_64BIT /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI * structures are incorrect, as the timespec structure from userspace * is 4 bytes too small. We define these alternatives here to teach * the kernel about the 32-bit struct packing.
*/ struct btrfs_ioctl_timespec_32 {
__u64 sec;
__u32 nsec;
} __attribute__ ((__packed__));
struct btrfs_ioctl_received_subvol_args_32 { char uuid[BTRFS_UUID_SIZE]; /* in */
__u64 stransid; /* in */
__u64 rtransid; /* out */ struct btrfs_ioctl_timespec_32 stime; /* in */ struct btrfs_ioctl_timespec_32 rtime; /* out */
__u64 flags; /* in */
__u64 reserved[16]; /* in */
} __attribute__ ((__packed__));
/* Mask out flags that are inappropriate for the given type of inode. */ staticunsignedint btrfs_mask_fsflags_for_type(conststruct inode *inode, unsignedint flags)
{ if (S_ISDIR(inode->i_mode)) return flags; elseif (S_ISREG(inode->i_mode)) return flags & ~FS_DIRSYNC_FL; else return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
}
/* * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS * ioctl.
*/ staticunsignedint btrfs_inode_flags_to_fsflags(conststruct btrfs_inode *inode)
{ unsignedint iflags = 0;
u32 flags = inode->flags;
u32 ro_flags = inode->ro_flags;
if (flags & BTRFS_INODE_SYNC)
iflags |= FS_SYNC_FL; if (flags & BTRFS_INODE_IMMUTABLE)
iflags |= FS_IMMUTABLE_FL; if (flags & BTRFS_INODE_APPEND)
iflags |= FS_APPEND_FL; if (flags & BTRFS_INODE_NODUMP)
iflags |= FS_NODUMP_FL; if (flags & BTRFS_INODE_NOATIME)
iflags |= FS_NOATIME_FL; if (flags & BTRFS_INODE_DIRSYNC)
iflags |= FS_DIRSYNC_FL; if (flags & BTRFS_INODE_NODATACOW)
iflags |= FS_NOCOW_FL; if (ro_flags & BTRFS_INODE_RO_VERITY)
iflags |= FS_VERITY_FL;
/* * Check if @flags are a supported and valid set of FS_*_FL flags and that * the old and new flags are not conflicting
*/ staticint check_fsflags(unsignedint old_flags, unsignedint flags)
{ if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
FS_NOATIME_FL | FS_NODUMP_FL | \
FS_SYNC_FL | FS_DIRSYNC_FL | \
FS_NOCOMP_FL | FS_COMPR_FL |
FS_NOCOW_FL)) return -EOPNOTSUPP;
/* COMPR and NOCOMP on new/old are valid */ if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) return -EINVAL;
if ((flags & FS_COMPR_FL) && (flags & FS_NOCOW_FL)) return -EINVAL;
/* NOCOW and compression options are mutually exclusive */ if ((old_flags & FS_NOCOW_FL) && (flags & (FS_COMPR_FL | FS_NOCOMP_FL))) return -EINVAL; if ((flags & FS_NOCOW_FL) && (old_flags & (FS_COMPR_FL | FS_NOCOMP_FL))) return -EINVAL;
/* * Set flags/xflags from the internal inode flags. The remaining items of * fsxattr are zeroed.
*/ int btrfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
{ conststruct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
/* If coming from FS_IOC_FSSETXATTR then skip unconverted flags */ if (!fa->flags_valid) { /* 1 item for the inode */
trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) return PTR_ERR(trans); goto update_flags;
}
if (fsflags & FS_DIRSYNC_FL)
inode_flags |= BTRFS_INODE_DIRSYNC; else
inode_flags &= ~BTRFS_INODE_DIRSYNC; if (fsflags & FS_NOCOW_FL) { if (S_ISREG(inode->vfs_inode.i_mode)) { /* * It's safe to turn csums off here, no extents exist. * Otherwise we want the flag to reflect the real COW * status of the file and will not set it.
*/ if (inode->vfs_inode.i_size == 0)
inode_flags |= BTRFS_INODE_NODATACOW |
BTRFS_INODE_NODATASUM;
} else {
inode_flags |= BTRFS_INODE_NODATACOW;
}
} else { /* * Revert back under same assumptions as above
*/ if (S_ISREG(inode->vfs_inode.i_mode)) { if (inode->vfs_inode.i_size == 0)
inode_flags &= ~(BTRFS_INODE_NODATACOW |
BTRFS_INODE_NODATASUM);
} else {
inode_flags &= ~BTRFS_INODE_NODATACOW;
}
}
/* * The COMPRESS flag can only be changed by users, while the NOCOMPRESS * flag may be changed automatically if compression code won't make * things smaller.
*/ if (fsflags & FS_NOCOMP_FL) {
inode_flags &= ~BTRFS_INODE_COMPRESS;
inode_flags |= BTRFS_INODE_NOCOMPRESS;
} elseif (fsflags & FS_COMPR_FL) {
if (IS_SWAPFILE(&inode->vfs_inode)) return -ETXTBSY;
/* * btrfs_trim_block_group() depends on space cache, which is not * available in zoned filesystem. So, disallow fitrim on a zoned * filesystem for now.
*/ if (btrfs_is_zoned(fs_info)) return -EOPNOTSUPP;
/* * If the fs is mounted with nologreplay, which requires it to be * mounted in RO mode as well, we can not allow discard on free space * inside block groups, because log trees refer to extents that are not * pinned in a block group's free space cache (pinning the extents is * precisely the first phase of replaying a log tree).
*/ if (btrfs_test_opt(fs_info, NOLOGREPLAY)) return -EROFS;
if (!num_devices) return -EOPNOTSUPP; if (copy_from_user(&range, arg, sizeof(range))) return -EFAULT;
/* * NOTE: Don't truncate the range using super->total_bytes. Bytenr of * block group is in the logical address space, which can be any * sectorsize aligned bytenr in the range [0, U64_MAX].
*/ if (range.len < fs_info->sectorsize) return -EINVAL;
range.minlen = max(range.minlen, minlen);
ret = btrfs_trim_fs(fs_info, &range);
if (copy_to_user(arg, &range, sizeof(range))) return -EFAULT;
return ret;
}
/* * Calculate the number of transaction items to reserve for creating a subvolume * or snapshot, not including the inode, directory entries, or parent directory.
*/ staticunsignedint create_subvol_num_items(conststruct btrfs_qgroup_inherit *inherit)
{ /* * 1 to add root block * 1 to add root item * 1 to add root ref * 1 to add root backref * 1 to add UUID item * 1 to add qgroup info * 1 to add qgroup limit * * Ideally the last two would only be accounted if qgroups are enabled, * but that can change between now and the time we would insert them.
*/ unsignedint num_items = 7;
if (inherit) { /* 2 to add qgroup relations for each inherited qgroup */
num_items += 2 * inherit->num_qgroups;
} return num_items;
}
root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); if (!root_item) return -ENOMEM;
ret = btrfs_get_free_objectid(fs_info->tree_root, &objectid); if (ret) goto out_root_item;
/* * Don't create subvolume whose level is not zero. Or qgroup will be * screwed up since it assumes subvolume qgroup's level to be 0.
*/ if (btrfs_qgroup_level(objectid)) {
ret = -ENOSPC; goto out_root_item;
}
ret = get_anon_bdev(&anon_dev); if (ret < 0) goto out_root_item;
new_inode_args.inode = btrfs_new_subvol_inode(idmap, dir); if (!new_inode_args.inode) {
ret = -ENOMEM; goto out_anon_dev;
}
ret = btrfs_new_inode_prepare(&new_inode_args, &trans_num_items); if (ret) goto out_inode;
trans_num_items += create_subvol_num_items(inherit);
btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
ret = btrfs_subvolume_reserve_metadata(root, &block_rsv,
trans_num_items, false); if (ret) goto out_new_inode_args;
qgroup_reserved = block_rsv.qgroup_rsv_reserved;
trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto out_release_rsv;
}
btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
qgroup_reserved = 0;
trans->block_rsv = &block_rsv;
trans->bytes_reserved = block_rsv.size;
ret = btrfs_qgroup_inherit(trans, 0, objectid, btrfs_root_id(root), inherit); if (ret) goto out;
key.objectid = objectid;
key.type = BTRFS_ROOT_ITEM_KEY;
key.offset = 0;
ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
root_item); if (ret) { int ret2;
/* * Since we don't abort the transaction in this case, free the * tree block so that we don't leak space and leave the * filesystem in an inconsistent state (an extent item in the * extent tree with a backreference for a root that does not * exists).
*/
btrfs_tree_lock(leaf);
btrfs_clear_buffer_dirty(trans, leaf);
btrfs_tree_unlock(leaf);
ret2 = btrfs_free_tree_block(trans, objectid, leaf, 0, 1); if (ret2 < 0)
btrfs_abort_transaction(trans, ret2);
free_extent_buffer(leaf); goto out;
}
free_extent_buffer(leaf);
leaf = NULL;
new_root = btrfs_get_new_fs_root(fs_info, objectid, &anon_dev); if (IS_ERR(new_root)) {
ret = PTR_ERR(new_root);
btrfs_abort_transaction(trans, ret); goto out;
} /* anon_dev is owned by new_root now. */
anon_dev = 0;
BTRFS_I(new_inode_args.inode)->root = new_root; /* ... and new_root is owned by new_inode_args.inode now. */
ret = btrfs_record_root_in_trans(trans, new_root); if (ret) {
btrfs_abort_transaction(trans, ret); goto out;
}
ret = btrfs_uuid_tree_add(trans, root_item->uuid,
BTRFS_UUID_KEY_SUBVOL, objectid); if (ret) {
btrfs_abort_transaction(trans, ret); goto out;
}
btrfs_record_new_subvolume(trans, BTRFS_I(dir));
ret = btrfs_create_new_inode(trans, &new_inode_args); if (ret) {
btrfs_abort_transaction(trans, ret); goto out;
}
/* We do not support snapshotting right now. */ if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
btrfs_warn(fs_info, "extent tree v2 doesn't support snapshotting yet"); return -EOPNOTSUPP;
}
if (btrfs_root_refs(&root->root_item) == 0) return -ENOENT;
if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) return -EINVAL;
if (atomic_read(&root->nr_swapfiles)) {
btrfs_warn(fs_info, "cannot snapshot subvolume with active swapfile"); return -ETXTBSY;
}
pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); if (!pending_snapshot) return -ENOMEM;
ret = get_anon_bdev(&pending_snapshot->anon_dev); if (ret < 0) goto free_pending;
pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item),
GFP_KERNEL);
pending_snapshot->path = btrfs_alloc_path(); if (!pending_snapshot->root_item || !pending_snapshot->path) {
ret = -ENOMEM; goto free_pending;
}
block_rsv = &pending_snapshot->block_rsv;
btrfs_init_block_rsv(block_rsv, BTRFS_BLOCK_RSV_TEMP); /* * 1 to add dir item * 1 to add dir index * 1 to update parent inode item
*/
trans_num_items = create_subvol_num_items(inherit) + 3;
ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, block_rsv,
trans_num_items, false); if (ret) goto free_pending;
qgroup_reserved = block_rsv->qgroup_rsv_reserved;
trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto fail;
}
ret = btrfs_record_root_in_trans(trans, BTRFS_I(dir)->root); if (ret) {
btrfs_end_transaction(trans); goto fail;
}
btrfs_qgroup_convert_reserved_meta(root, qgroup_reserved);
qgroup_reserved = 0;
trans->pending_snapshot = pending_snapshot;
ret = btrfs_commit_transaction(trans); if (ret) goto fail;
ret = pending_snapshot->error; if (ret) goto fail;
ret = btrfs_orphan_cleanup(pending_snapshot->snap); if (ret) goto fail;
inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); if (IS_ERR(inode)) {
ret = PTR_ERR(inode); goto fail;
}
d_instantiate(dentry, inode);
ret = 0;
pending_snapshot->anon_dev = 0;
fail: /* Prevent double freeing of anon_dev */ if (ret && pending_snapshot->snap)
pending_snapshot->snap->anon_dev = 0;
btrfs_put_root(pending_snapshot->snap);
btrfs_block_rsv_release(fs_info, block_rsv, (u64)-1, NULL); if (qgroup_reserved)
btrfs_qgroup_free_meta_prealloc(root, qgroup_reserved);
free_pending: if (pending_snapshot->anon_dev)
free_anon_bdev(pending_snapshot->anon_dev);
kfree(pending_snapshot->root_item);
btrfs_free_path(pending_snapshot->path);
kfree(pending_snapshot);
return ret;
}
/* copy of may_delete in fs/namei.c() * Check whether we can remove a link victim from directory dir, check * whether the type of victim is right. * 1. We can't do it if dir is read-only (done in permission()) * 2. We should have write and exec permissions on dir * 3. We can't remove anything from append-only dir * 4. We can't do anything with immutable dir (done in permission()) * 5. If the sticky bit on dir is set we should either * a. be owner of dir, or * b. be owner of victim, or * c. have CAP_FOWNER capability * 6. If the victim is append-only or immutable we can't do anything with * links pointing to it. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. * 9. We can't remove a root or mountpoint. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by * nfs_async_unlink().
*/
staticint btrfs_may_delete(struct mnt_idmap *idmap, struct inode *dir, struct dentry *victim, int isdir)
{ int ret;
if (d_really_is_negative(victim)) return -ENOENT;
/* The @victim is not inside @dir. */ if (d_inode(victim->d_parent) != dir) return -EINVAL;
audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
ret = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); if (ret) return ret; if (IS_APPEND(dir)) return -EPERM; if (check_sticky(idmap, dir, d_inode(victim)) ||
IS_APPEND(d_inode(victim)) || IS_IMMUTABLE(d_inode(victim)) ||
IS_SWAPFILE(d_inode(victim))) return -EPERM; if (isdir) { if (!d_is_dir(victim)) return -ENOTDIR; if (IS_ROOT(victim)) return -EBUSY;
} elseif (d_is_dir(victim)) return -EISDIR; if (IS_DEADDIR(dir)) return -ENOENT; if (victim->d_flags & DCACHE_NFSFS_RENAMED) return -EBUSY; return 0;
}
/* copy of may_create in fs/namei.c() */ staticinlineint btrfs_may_create(struct mnt_idmap *idmap, struct inode *dir, conststruct dentry *child)
{ if (d_really_is_positive(child)) return -EEXIST; if (IS_DEADDIR(dir)) return -ENOENT; if (!fsuidgid_has_mapping(dir->i_sb, idmap)) return -EOVERFLOW; return inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
}
/* * Create a new subvolume below @parent. This is largely modeled after * sys_mkdirat and vfs_mkdir, but we only do a single component lookup * inside this filesystem so it's quite a bit simpler.
*/ static noinline int btrfs_mksubvol(struct dentry *parent, struct mnt_idmap *idmap, struct qstr *qname, struct btrfs_root *snap_src, bool readonly, struct btrfs_qgroup_inherit *inherit)
{ struct inode *dir = d_inode(parent); struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); struct dentry *dentry; struct fscrypt_str name_str = FSTR_INIT((char *)qname->name, qname->len); int ret;
ret = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); if (ret == -EINTR) return ret;
dentry = lookup_one(idmap, qname, parent);
ret = PTR_ERR(dentry); if (IS_ERR(dentry)) goto out_unlock;
ret = btrfs_may_create(idmap, dir, dentry); if (ret) goto out_dput;
/* * even if this name doesn't exist, we may get hash collisions. * check for them now when we can safely fail
*/
ret = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, dir->i_ino, &name_str); if (ret) goto out_dput;
down_read(&fs_info->subvol_sem);
if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) goto out_up_read;
if (snap_src)
ret = create_snapshot(snap_src, dir, dentry, readonly, inherit); else
ret = create_subvol(idmap, dir, dentry, inherit);
/* * Force new buffered writes to reserve space even when NOCOW is * possible. This is to avoid later writeback (running dealloc) to * fallback to COW mode and unexpectedly fail with ENOSPC.
*/
btrfs_drew_read_lock(&root->snapshot_lock);
ret = btrfs_start_delalloc_snapshot(root, false); if (ret) goto out;
/* * All previous writes have started writeback in NOCOW mode, so now * we force future writes to fallback to COW mode during snapshot * creation.
*/
atomic_inc(&root->snapshot_force_cow);
btrfs_wait_ordered_extents(root, U64_MAX, NULL);
ret = btrfs_mksubvol(parent, idmap, qname, root, readonly, inherit);
/* * Try to start exclusive operation @type or cancel it if it's running. * * Return: * 0 - normal mode, newly claimed op started * >0 - normal mode, something else is running, * return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS to user space * ECANCELED - cancel mode, successful cancel * ENOTCONN - cancel mode, operation not running anymore
*/ staticint exclop_start_or_cancel_reloc(struct btrfs_fs_info *fs_info, enum btrfs_exclusive_operation type, bool cancel)
{ if (!cancel) { /* Start normal op */ if (!btrfs_exclop_start(fs_info, type)) return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; /* Exclusive operation is now claimed */ return 0;
}
/* Cancel running op */ if (btrfs_exclop_start_try_lock(fs_info, type)) { /* * This blocks any exclop finish from setting it to NONE, so we * request cancellation. Either it runs and we will wait for it, * or it has finished and no waiting will happen.
*/
atomic_inc(&fs_info->reloc_cancel_req);
btrfs_exclop_start_unlock(fs_info);
if (test_bit(BTRFS_FS_RELOC_RUNNING, &fs_info->flags))
wait_on_bit(&fs_info->flags, BTRFS_FS_RELOC_RUNNING,
TASK_INTERRUPTIBLE);
return -ECANCELED;
}
/* Something else is running or none */ return -ENOTCONN;
}
ret = mnt_want_write_file(file); if (ret) return ret;
/* * Read the arguments before checking exclusivity to be able to * distinguish regular resize and cancel
*/
vol_args = memdup_user(arg, sizeof(*vol_args)); if (IS_ERR(vol_args)) {
ret = PTR_ERR(vol_args); goto out_drop;
}
ret = btrfs_check_ioctl_vol_args_path(vol_args); if (ret < 0) goto out_free;
sizestr = vol_args->name;
cancel = (strcmp("cancel", sizestr) == 0);
ret = exclop_start_or_cancel_reloc(fs_info, BTRFS_EXCLOP_RESIZE, cancel); if (ret) goto out_free; /* Exclusive operation is now claimed */
devstr = strchr(sizestr, ':'); if (devstr) {
sizestr = devstr + 1;
*devstr = '\0';
devstr = vol_args->name;
ret = kstrtoull(devstr, 10, &devid); if (ret) goto out_finish; if (!devid) {
ret = -EINVAL; goto out_finish;
}
btrfs_info(fs_info, "resizing devid %llu", devid);
}
args.devid = devid;
device = btrfs_find_device(fs_info->fs_devices, &args); if (!device) {
btrfs_info(fs_info, "resizer unable to find device %llu",
devid);
ret = -ENODEV; goto out_finish;
}
if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
btrfs_info(fs_info, "resizer unable to apply on readonly device %llu",
devid);
ret = -EPERM; goto out_finish;
}
if (!strcmp(sizestr, "max"))
new_size = bdev_nr_bytes(device->bdev); else { char *retptr;
if (sizestr[0] == '-') {
mod = -1;
sizestr++;
} elseif (sizestr[0] == '+') {
mod = 1;
sizestr++;
}
new_size = memparse(sizestr, &retptr); if (*retptr != '\0' || new_size == 0) {
ret = -EINVAL; goto out_finish;
}
}
if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
ret = -EPERM; goto out_finish;
}
old_size = btrfs_device_get_total_bytes(device);
if (mod < 0) { if (new_size > old_size) {
ret = -EINVAL; goto out_finish;
}
new_size = old_size - new_size;
} elseif (mod > 0) { if (new_size > ULLONG_MAX - old_size) {
ret = -ERANGE; goto out_finish;
}
new_size = old_size + new_size;
}
if (new_size < SZ_256M) {
ret = -EINVAL; goto out_finish;
} if (new_size > bdev_nr_bytes(device->bdev)) {
ret = -EFBIG; goto out_finish;
}
if (new_size > old_size) { struct btrfs_trans_handle *trans;
trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto out_finish;
}
ret = btrfs_grow_device(trans, device, new_size);
btrfs_commit_transaction(trans);
} elseif (new_size < old_size) {
ret = btrfs_shrink_device(device, new_size);
} /* equal, nothing need to do */
if (subvol) {
ret = btrfs_mksubvol(file_dentry(file), idmap, &qname, NULL,
readonly, inherit);
} else { CLASS(fd, src)(fd); struct inode *src_inode; if (fd_empty(src)) {
ret = -EINVAL; goto out_drop_write;
}
src_inode = file_inode(fd_file(src)); if (src_inode->i_sb != file_inode(file)->i_sb) {
btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, "Snapshot src from another FS");
ret = -EXDEV;
} elseif (!inode_owner_or_capable(idmap, src_inode)) { /* * Subvolume creation is not restricted, but snapshots * are limited to own subvolumes only
*/
ret = -EPERM;
} elseif (btrfs_ino(BTRFS_I(src_inode)) != BTRFS_FIRST_FREE_OBJECTID) { /* * Snapshots must be made with the src_inode referring * to the subvolume inode, otherwise the permission * checking above is useless because we may have * permission on a lower directory but not the subvol * itself.
*/
ret = -EINVAL;
} else {
ret = btrfs_mksnapshot(file_dentry(file), idmap, &qname,
BTRFS_I(src_inode)->root,
readonly, inherit);
}
}
out_drop_write:
mnt_drop_write_file(file);
out: return ret;
}
static noinline int btrfs_ioctl_snap_create(struct file *file, void __user *arg, int subvol)
{ struct btrfs_ioctl_vol_args *vol_args; int ret;
if (!S_ISDIR(file_inode(file)->i_mode)) return -ENOTDIR;
vol_args = memdup_user(arg, sizeof(*vol_args)); if (IS_ERR(vol_args)) return PTR_ERR(vol_args);
ret = btrfs_check_ioctl_vol_args_path(vol_args); if (ret < 0) goto out;
ret = __btrfs_ioctl_snap_create(file, file_mnt_idmap(file),
vol_args->name, vol_args->fd, subvol, false, NULL);
/* * Copy search result header. If we fault then loop again so we * can fault in the pages and -EFAULT there if there's a * problem. Otherwise we'll fault and then copy the buffer in * properly this next time through
*/ if (copy_to_user_nofault(ubuf + *sk_offset, &sh, sizeof(sh))) {
ret = 0; goto out;
}
*sk_offset += sizeof(sh);
if (item_len) { char __user *up = ubuf + *sk_offset; /* * Copy the item, same behavior as above, but reset the * * sk_offset so we copy the full thing again.
*/ if (read_extent_buffer_to_user_nofault(leaf, up,
item_off, item_len)) {
ret = 0;
*sk_offset -= sizeof(sh); goto out;
}
*sk_offset += item_len;
}
(*num_found)++;
if (ret) /* -EOVERFLOW from above */ goto out;
if (*num_found >= sk->nr_items) {
ret = 1; goto out;
}
}
advance_key:
ret = 0;
test.objectid = sk->max_objectid;
test.type = sk->max_type;
test.offset = sk->max_offset; if (btrfs_comp_cpu_keys(key, &test) >= 0)
ret = 1; elseif (key->offset < (u64)-1)
key->offset++; elseif (key->type < (u8)-1) {
key->offset = 0;
key->type++;
} elseif (key->objectid < (u64)-1) {
key->offset = 0;
key->type = 0;
key->objectid++;
} else
ret = 1;
out: /* * 0: all items from this leaf copied, continue with next * 1: * more items can be copied, but unused buffer is too small * * all items were found * Either way, it will stops the loop which iterates to the next * leaf * -EOVERFLOW: item was to large for buffer * -EFAULT: could not copy extent buffer back to userspace
*/ return ret;
}
path = btrfs_alloc_path(); if (!path) return -ENOMEM;
if (sk->tree_id == 0) { /* Search the root that we got passed. */
root = btrfs_grab_root(root);
} else { /* Look up the root from the arguments. */
root = btrfs_get_fs_root(info, sk->tree_id, true); if (IS_ERR(root)) {
btrfs_free_path(path); return PTR_ERR(root);
}
}
while (1) { /* * Ensure that the whole user buffer is faulted in at sub-page * granularity, otherwise the loop may live-lock.
*/ if (fault_in_subpage_writeable(ubuf + sk_offset, *buf_size - sk_offset)) {
ret = -EFAULT; break;
}
ret = btrfs_search_forward(root, &key, path, sk->min_transid); if (ret) break;
ret = copy_to_sk(path, &key, sk, buf_size, ubuf,
&sk_offset, &num_found);
btrfs_release_path(path); if (ret) break;
} /* Normalize return values from btrfs_search_forward() and copy_to_sk(). */ if (ret > 0)
ret = 0;
if (copy_from_user(&sk, &uargs->key, sizeof(sk))) return -EFAULT;
buf_size = sizeof(uargs->buf);
ret = search_ioctl(root, &sk, &buf_size, uargs->buf);
/* * In the origin implementation an overflow is handled by returning a * search header with a len of zero, so reset ret.
*/ if (ret == -EOVERFLOW)
ret = 0;
if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk)))
ret = -EFAULT; return ret;
}
/* copy search header and buffer size */ if (copy_from_user(&args, uarg, sizeof(args))) return -EFAULT;
buf_size = args.buf_size;
/* limit result size to 16MB */ if (buf_size > buf_limit)
buf_size = buf_limit;
ret = search_ioctl(root, &args.key, &buf_size,
(char __user *)(&uarg->buf[0])); if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key)))
ret = -EFAULT; elseif (ret == -EOVERFLOW &&
copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size)))
ret = -EFAULT;
return ret;
}
/* * Search INODE_REFs to identify path name of 'dirid' directory * in a 'tree_id' tree. and sets path name to 'name'.
*/ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
u64 tree_id, u64 dirid, char *name)
{ struct btrfs_root *root; struct btrfs_key key; char *ptr; int ret = -1; int slot; int len; int total_len = 0; struct btrfs_inode_ref *iref; struct extent_buffer *l; struct btrfs_path *path;
if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
name[0]='\0'; return 0;
}
path = btrfs_alloc_path(); if (!path) return -ENOMEM;
ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
root = btrfs_get_fs_root(info, tree_id, true); if (IS_ERR(root)) {
ret = PTR_ERR(root);
root = NULL; goto out;
}
path = btrfs_alloc_path(); if (!path) return -ENOMEM;
/* * If the bottom subvolume does not exist directly under upper_limit, * construct the path in from the bottom up.
*/ if (dirid != upper_limit) {
ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
root = btrfs_get_fs_root(fs_info, treeid, true); if (IS_ERR(root)) {
ret = PTR_ERR(root); goto out;
}
/* Check the read+exec permission of this directory */
ret = btrfs_previous_item(root, path, dirid,
BTRFS_INODE_ITEM_KEY); if (ret < 0) { goto out_put;
} elseif (ret > 0) {
ret = -ENOENT; goto out_put;
}
leaf = path->nodes[0];
slot = path->slots[0];
btrfs_item_key_to_cpu(leaf, &key2, slot); if (key2.objectid != dirid) {
ret = -ENOENT; goto out_put;
}
/* * We don't need the path anymore, so release it and * avoid deadlocks and lockdep warnings in case * btrfs_iget() needs to lookup the inode from its root * btree and lock the same leaf.
*/
btrfs_release_path(path);
temp_inode = btrfs_iget(key2.objectid, root); if (IS_ERR(temp_inode)) {
ret = PTR_ERR(temp_inode); goto out_put;
}
ret = inode_permission(idmap, &temp_inode->vfs_inode,
MAY_READ | MAY_EXEC);
iput(&temp_inode->vfs_inode); if (ret) {
ret = -EACCES; goto out_put;
}
if (key.offset == upper_limit) break; if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) {
ret = -EACCES; goto out_put;
}
static noinline int btrfs_ioctl_ino_lookup(struct btrfs_root *root, void __user *argp)
{ struct btrfs_ioctl_ino_lookup_args *args; int ret = 0;
args = memdup_user(argp, sizeof(*args)); if (IS_ERR(args)) return PTR_ERR(args);
/* * Unprivileged query to obtain the containing subvolume root id. The * path is reset so it's consistent with btrfs_search_path_in_tree.
*/ if (args->treeid == 0)
args->treeid = btrfs_root_id(root);
if (!capable(CAP_SYS_ADMIN)) {
ret = -EPERM; goto out;
}
ret = btrfs_search_path_in_tree(root->fs_info,
args->treeid, args->objectid,
args->name);
out: if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
ret = -EFAULT;
kfree(args); return ret;
}
/* * Version of ino_lookup ioctl (unprivileged) * * The main differences from ino_lookup ioctl are: * * 1. Read + Exec permission will be checked using inode_permission() during * path construction. -EACCES will be returned in case of failure. * 2. Path construction will be stopped at the inode number which corresponds * to the fd with which this ioctl is called. If constructed path does not * exist under fd's inode, -EACCES will be returned. * 3. The name of bottom subvolume is also searched and filled.
*/ staticint btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp)
{ struct btrfs_ioctl_ino_lookup_user_args *args; struct inode *inode; int ret;
args = memdup_user(argp, sizeof(*args)); if (IS_ERR(args)) return PTR_ERR(args);
inode = file_inode(file);
if (args->dirid == BTRFS_FIRST_FREE_OBJECTID &&
btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { /* * The subvolume does not exist under fd with which this is * called
*/
kfree(args); return -EACCES;
}
ret = btrfs_search_path_in_tree_user(file_mnt_idmap(file), inode, args);
if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
ret = -EFAULT;
kfree(args); return ret;
}
/* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ staticint btrfs_ioctl_get_subvol_info(struct inode *inode, void __user *argp)
{ struct btrfs_ioctl_get_subvol_info_args *subvol_info; struct btrfs_fs_info *fs_info; struct btrfs_root *root; struct btrfs_path *path; struct btrfs_key key; struct btrfs_root_item *root_item; struct btrfs_root_ref *rref; struct extent_buffer *leaf; unsignedlong item_off; unsignedlong item_len; int slot; int ret = 0;
path = btrfs_alloc_path(); if (!path) return -ENOMEM;
ret = btrfs_next_item(root, path); if (ret < 0) { goto out;
} elseif (ret > 0) {
ret = -EUCLEAN; goto out;
}
}
out:
btrfs_free_path(path);
if (!ret || ret == -EOVERFLOW) {
rootrefs->num_items = found; /* update min_treeid for next search */ if (found)
rootrefs->min_treeid =
rootrefs->rootref[found - 1].treeid + 1; if (copy_to_user(argp, rootrefs, sizeof(*rootrefs)))
ret = -EFAULT;
}
/* We don't support snapshots with extent tree v2 yet. */ if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
btrfs_err(fs_info, "extent tree v2 doesn't support snapshot deletion yet"); return -EOPNOTSUPP;
}
if (destroy_v2) {
vol_args2 = memdup_user(arg, sizeof(*vol_args2)); if (IS_ERR(vol_args2)) return PTR_ERR(vol_args2);
if (vol_args2->flags & ~BTRFS_SUBVOL_DELETE_ARGS_MASK) {
ret = -EOPNOTSUPP; goto out;
}
/* * If SPEC_BY_ID is not set, we are looking for the subvolume by * name, same as v1 currently does.
*/ if (!(vol_args2->flags & BTRFS_SUBVOL_SPEC_BY_ID)) {
ret = btrfs_check_ioctl_vol_args2_subvol_name(vol_args2); if (ret < 0) goto out;
subvol_name = vol_args2->name;
ret = mnt_want_write_file(file); if (ret) goto out;
} else { struct inode *old_dir;
if (vol_args2->subvolid < BTRFS_FIRST_FREE_OBJECTID) {
ret = -EINVAL; goto out;
}
ret = mnt_want_write_file(file); if (ret) goto out;
dentry = btrfs_get_dentry(fs_info->sb,
BTRFS_FIRST_FREE_OBJECTID,
vol_args2->subvolid, 0); if (IS_ERR(dentry)) {
ret = PTR_ERR(dentry); goto out_drop_write;
}
/* * Change the default parent since the subvolume being * deleted can be outside of the current mount point.
*/
parent = btrfs_get_parent(dentry);
/* * At this point dentry->d_name can point to '/' if the * subvolume we want to destroy is outsite of the * current mount point, so we need to release the * current dentry and execute the lookup to return a new * one with ->d_name pointing to the * <mount point>/subvol_name.
*/
dput(dentry); if (IS_ERR(parent)) {
ret = PTR_ERR(parent); goto out_drop_write;
}
old_dir = dir;
dir = d_inode(parent);
/* * If v2 was used with SPEC_BY_ID, a new parent was * allocated since the subvolume can be outside of the * current mount point. Later on we need to release this * new parent dentry.
*/
destroy_parent = true;
/* * On idmapped mounts, deletion via subvolid is * restricted to subvolumes that are immediate * ancestors of the inode referenced by the file * descriptor in the ioctl. Otherwise the idmapping * could potentially be abused to delete subvolumes * anywhere in the filesystem the user wouldn't be able * to delete without an idmapped mount.
*/ if (old_dir != dir && idmap != &nop_mnt_idmap) {
ret = -EOPNOTSUPP; goto free_parent;
}
subvol_name_ptr = btrfs_get_subvol_name_from_objectid(
fs_info, vol_args2->subvolid); if (IS_ERR(subvol_name_ptr)) {
ret = PTR_ERR(subvol_name_ptr); goto free_parent;
} /* subvol_name_ptr is already nul terminated */
subvol_name = (char *)kbasename(subvol_name_ptr);
}
} else {
vol_args = memdup_user(arg, sizeof(*vol_args)); if (IS_ERR(vol_args)) return PTR_ERR(vol_args);
ret = btrfs_check_ioctl_vol_args_path(vol_args); if (ret < 0) goto out;
subvol_name = vol_args->name;
ret = mnt_want_write_file(file); if (ret) goto out;
}
if (strchr(subvol_name, '/') ||
strcmp(subvol_name, "..") == 0) {
ret = -EINVAL; goto free_subvol_name;
}
if (!S_ISDIR(dir->i_mode)) {
ret = -ENOTDIR; goto free_subvol_name;
}
ret = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); if (ret == -EINTR) goto free_subvol_name;
dentry = lookup_one(idmap, &QSTR(subvol_name), parent); if (IS_ERR(dentry)) {
ret = PTR_ERR(dentry); goto out_unlock_dir;
}
if (d_really_is_negative(dentry)) {
ret = -ENOENT; goto out_dput;
}
inode = d_inode(dentry);
dest = BTRFS_I(inode)->root; if (!capable(CAP_SYS_ADMIN)) { /* * Regular user. Only allow this with a special mount * option, when the user has write+exec access to the * subvol root, and when rmdir(2) would have been * allowed. * * Note that this is _not_ check that the subvol is * empty or doesn't contain data that we wouldn't * otherwise be able to delete. * * Users who want to delete empty subvols should try * rmdir(2).
*/
ret = -EPERM; if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) goto out_dput;
/* * Do not allow deletion if the parent dir is the same * as the dir to be deleted. That means the ioctl * must be called on the dentry referencing the root * of the subvol, not a random directory contained * within it.
*/
ret = -EINVAL; if (root == dest) goto out_dput;
ret = inode_permission(idmap, inode, MAY_WRITE | MAY_EXEC); if (ret) goto out_dput;
}
/* check if subvolume may be deleted by a user */
ret = btrfs_may_delete(idmap, dir, dentry, 1); if (ret) goto out_dput;
if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) {
ret = -EINVAL; goto out_dput;
}
btrfs_inode_lock(BTRFS_I(inode), 0);
ret = btrfs_delete_subvolume(BTRFS_I(dir), dentry);
btrfs_inode_unlock(BTRFS_I(inode), 0); if (!ret)
d_delete_notify(dir, dentry);
ret = mnt_want_write_file(file); if (ret) return ret;
if (btrfs_root_readonly(root)) {
ret = -EROFS; goto out;
}
switch (inode->i_mode & S_IFMT) { case S_IFDIR: if (!capable(CAP_SYS_ADMIN)) {
ret = -EPERM; goto out;
}
ret = btrfs_defrag_root(root); break; case S_IFREG: /* * Note that this does not check the file descriptor for write * access. This prevents defragmenting executables that are * running and allows defrag on files open in read-only mode.
*/ if (!capable(CAP_SYS_ADMIN) &&
inode_permission(&nop_mnt_idmap, inode, MAY_WRITE)) {
ret = -EPERM; goto out;
}
/* * Don't allow defrag on pre-content watched files, as it could * populate the page cache with 0's via readahead.
*/ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
ret = -EINVAL; goto out;
}
if (argp) { if (copy_from_user(&range, argp, sizeof(range))) {
ret = -EFAULT; goto out;
} if (range.flags & ~BTRFS_DEFRAG_RANGE_FLAGS_SUPP) {
ret = -EOPNOTSUPP; goto out;
} if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) &&
(range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
ret = -EINVAL; goto out;
} /* Compression or no-compression require to start the IO. */ if ((range.flags & BTRFS_DEFRAG_RANGE_COMPRESS) ||
(range.flags & BTRFS_DEFRAG_RANGE_NOCOMPRESS)) {
range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
range.extent_thresh = (u32)-1;
}
} else { /* the rest are all set to zero by kzalloc */
range.len = (u64)-1;
}
ret = btrfs_defrag_file(BTRFS_I(file_inode(file)), &file->f_ra,
&range, BTRFS_OLDEST_GENERATION, 0); if (ret > 0)
ret = 0; break; default:
ret = -EINVAL;
}
out:
mnt_drop_write_file(file); return ret;
}
if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
btrfs_err(fs_info, "device add not supported on extent tree v2 yet"); return -EINVAL;
}
if (fs_info->fs_devices->temp_fsid) {
btrfs_err(fs_info, "device add not supported on cloned temp-fsid mount"); return -EINVAL;
}
if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_ADD)) { if (!btrfs_exclop_start_try_lock(fs_info, BTRFS_EXCLOP_DEV_ADD)) return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
/* * We can do the device add because we have a paused balanced, * change the exclusive op type and remember we should bring * back the paused balance
*/
fs_info->exclusive_operation = BTRFS_EXCLOP_DEV_ADD;
btrfs_exclop_start_unlock(fs_info);
restore_op = true;
}
vol_args = memdup_user(arg, sizeof(*vol_args)); if (IS_ERR(vol_args)) {
ret = PTR_ERR(vol_args); goto out;
}
ret = btrfs_check_ioctl_vol_args_path(vol_args); if (ret < 0) goto out_free;
ret = btrfs_init_new_device(fs_info, vol_args->name);
if (!ret)
btrfs_info(fs_info, "disk added %s", vol_args->name);
ret = mnt_want_write_file(file); if (ret) return ret;
if (copy_from_user(&objectid, argp, sizeof(objectid))) {
ret = -EFAULT; goto out;
}
if (!objectid)
objectid = BTRFS_FS_TREE_OBJECTID;
new_root = btrfs_get_fs_root(fs_info, objectid, true); if (IS_ERR(new_root)) {
ret = PTR_ERR(new_root); goto out;
} if (!btrfs_is_fstree(btrfs_root_id(new_root))) {
ret = -ENOENT; goto out_free;
}
path = btrfs_alloc_path(); if (!path) {
ret = -ENOMEM; goto out_free;
}
trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto out_free;
}
dir_id = btrfs_super_root_dir(fs_info->super_copy);
di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
dir_id, &name, 1); if (IS_ERR_OR_NULL(di)) {
btrfs_release_path(path);
btrfs_end_transaction(trans);
btrfs_err(fs_info, "Umm, you don't have the default diritem, this isn't going to work");
ret = -ENOENT; goto out_free;
}
/* we generally have at most 6 or so space infos, one for each raid * level. So, a whole page should be more than enough for everyone
*/ if (alloc_size > PAGE_SIZE) return -ENOMEM;
space_args.total_spaces = 0;
dest = kmalloc(alloc_size, GFP_KERNEL); if (!dest) return -ENOMEM;
dest_orig = dest;
/* now we have a buffer to copy into */ for (i = 0; i < num_types; i++) { struct btrfs_space_info *tmp;
if (!slot_count) break;
info = NULL;
list_for_each_entry(tmp, &fs_info->space_info, list) { if (tmp->flags == types[i]) {
info = tmp; break;
}
}
if (!info) continue;
down_read(&info->groups_sem); for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { if (!list_empty(&info->block_groups[c])) {
get_block_group_info(&info->block_groups[c],
&space);
memcpy(dest, &space, sizeof(space));
dest++;
space_args.total_spaces++;
slot_count--;
} if (!slot_count) break;
}
up_read(&info->groups_sem);
}
/* * Add global block reserve
*/ if (slot_count) { struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
/* * Start orphan cleanup here for the given root in case it hasn't been * started already by other means. Errors are handled in the other * functions during transaction commit.
*/
btrfs_orphan_cleanup(root);
trans = btrfs_attach_transaction_barrier(root); if (IS_ERR(trans)) { if (PTR_ERR(trans) != -ENOENT) return PTR_ERR(trans);
/* No running transaction, don't bother */
transid = btrfs_get_last_trans_committed(root->fs_info); goto out;
}
transid = trans->transid;
btrfs_commit_transaction_async(trans);
out: if (argp) if (copy_to_user(argp, &transid, sizeof(transid))) return -EFAULT; return 0;
}
static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, void __user *argp)
{ /* By default wait for the current transaction. */
u64 transid = 0;
if (argp) if (copy_from_user(&transid, argp, sizeof(transid))) return -EFAULT;
/* * Copy scrub args to user space even if btrfs_scrub_dev() returned an * error. This is important as it allows user space to know how much * progress scrub has done. For example, if scrub is canceled we get * -ECANCELED from btrfs_scrub_dev() and return that error back to user * space. Later user space can inspect the progress from the structure * btrfs_ioctl_scrub_args and resume scrub from where it left off * previously (btrfs-progs does this). * If we fail to copy the btrfs_ioctl_scrub_args structure to user space * then return -EFAULT to signal the structure was not copied or it may * be corrupt and unreliable due to a partial copy.
*/ if (copy_to_user(arg, sa, sizeof(*sa)))
ret = -EFAULT;
if (!(sa->flags & BTRFS_SCRUB_READONLY))
mnt_drop_write_file(file);
out:
kfree(sa); return ret;
}
staticlong btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
{ if (!capable(CAP_SYS_ADMIN)) return -EPERM;
if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
btrfs_err(fs_info, "device replace not supported on extent tree v2 yet"); return -EINVAL;
}
p = memdup_user(arg, sizeof(*p)); if (IS_ERR(p)) return PTR_ERR(p);
switch (p->cmd) { case BTRFS_IOCTL_DEV_REPLACE_CMD_START: if (sb_rdonly(fs_info->sb)) {
ret = -EROFS; goto out;
} if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_DEV_REPLACE)) {
ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
} else {
ret = btrfs_dev_replace_by_ioctl(fs_info, p);
btrfs_exclop_finish(fs_info);
} break; case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
btrfs_dev_replace_status(fs_info, p);
ret = 0; break; case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
p->result = btrfs_dev_replace_cancel(fs_info);
ret = 0; break; default:
ret = -EINVAL; break;
}
if ((ret == 0 || ret == -ECANCELED) && copy_to_user(arg, p, sizeof(*p)))
ret = -EFAULT;
out:
kfree(p); return ret;
}
staticlong btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg)
{ int ret = 0; int i;
u64 rel_ptr; int size; struct btrfs_ioctl_ino_path_args *ipa = NULL; struct inode_fs_paths *ipath = NULL; struct btrfs_path *path;
if (!capable(CAP_DAC_READ_SEARCH)) return -EPERM;
path = btrfs_alloc_path(); if (!path) {
ret = -ENOMEM; goto out;
}
ipa = memdup_user(arg, sizeof(*ipa)); if (IS_ERR(ipa)) {
ret = PTR_ERR(ipa);
ipa = NULL; goto out;
}
staticlong btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, void __user *arg, int version)
{ int ret = 0; int size; struct btrfs_ioctl_logical_ino_args *loi; struct btrfs_data_container *inodes = NULL; bool ignore_offset;
if (!capable(CAP_SYS_ADMIN)) return -EPERM;
loi = memdup_user(arg, sizeof(*loi)); if (IS_ERR(loi)) return PTR_ERR(loi);
if (version == 1) {
ignore_offset = false;
size = min_t(u32, loi->size, SZ_64K);
} else { /* All reserved bits must be 0 for now */ if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) {
ret = -EINVAL; goto out_loi;
} /* Only accept flags we have defined so far */ if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) {
ret = -EINVAL; goto out_loi;
}
ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET;
size = min_t(u32, loi->size, SZ_16M);
}
inodes = init_data_container(size); if (IS_ERR(inodes)) {
ret = PTR_ERR(inodes); goto out_loi;
}
ret = iterate_inodes_from_logical(loi->logical, fs_info, inodes, ignore_offset); if (ret == -EINVAL)
ret = -ENOENT; if (ret < 0) goto out;
ret = copy_to_user((void __user *)(unsignedlong)loi->inodes, inodes,
size); if (ret)
ret = -EFAULT;
/* * Try to acquire fs_info::balance_mutex as well as set BTRFS_EXLCOP_BALANCE as * required. * * @fs_info: the filesystem * @excl_acquired: ptr to boolean value which is set to false in case balance * is being resumed * * Return 0 on success in which case both fs_info::balance is acquired as well * as exclusive ops are blocked. In case of failure return an error code.
*/ staticint btrfs_try_lock_balance(struct btrfs_fs_info *fs_info, bool *excl_acquired)
{ int ret;
/* * Exclusive operation is locked. Three possibilities: * (1) some other op is running * (2) balance is running * (3) balance is paused -- special case (think resume)
*/ while (1) { if (btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
*excl_acquired = true;
mutex_lock(&fs_info->balance_mutex); return 0;
}
mutex_lock(&fs_info->balance_mutex); if (fs_info->balance_ctl) { /* This is either (2) or (3) */ if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { /* This is (2) */
ret = -EINPROGRESS; goto out_failure;
} else {
mutex_unlock(&fs_info->balance_mutex); /* * Lock released to allow other waiters to * continue, we'll reexamine the status again.
*/
mutex_lock(&fs_info->balance_mutex);
if (fs_info->balance_ctl &&
!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { /* This is (3) */
*excl_acquired = false; return 0;
}
}
} else { /* This is (1) */
ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; goto out_failure;
}
bctl->flags = bargs->flags;
do_balance: /* * Ownership of bctl and exclusive operation goes to btrfs_balance. * bctl is freed in reset_balance_state, or, if restriper was paused * all the way until unmount, in free_fs_info. The flag should be * cleared after reset_balance_state.
*/
need_unlock = false;
ret = btrfs_balance(fs_info, bctl, bargs);
bctl = NULL;
if (ret == 0 || ret == -ECANCELED) { if (copy_to_user(arg, bargs, sizeof(*bargs)))
ret = -EFAULT;
}
ret = mnt_want_write_file(file); if (ret) return ret;
sa = memdup_user(arg, sizeof(*sa)); if (IS_ERR(sa)) {
ret = PTR_ERR(sa); goto drop_write;
}
switch (sa->cmd) { case BTRFS_QUOTA_CTL_ENABLE: case BTRFS_QUOTA_CTL_ENABLE_SIMPLE_QUOTA:
down_write(&fs_info->subvol_sem);
ret = btrfs_quota_enable(fs_info, sa);
up_write(&fs_info->subvol_sem); break; case BTRFS_QUOTA_CTL_DISABLE: /* * Lock the cleaner mutex to prevent races with concurrent * relocation, because relocation may be building backrefs for * blocks of the quota root while we are deleting the root. This * is like dropping fs roots of deleted snapshots/subvolumes, we * need the same protection. * * This also prevents races between concurrent tasks trying to * disable quotas, because we will unlock and relock * qgroup_ioctl_lock across BTRFS_FS_QUOTA_ENABLED changes. * * We take this here because we have the dependency of * * inode_lock -> subvol_sem * * because of rename. With relocation we can prealloc extents, * so that makes the dependency chain * * cleaner_mutex -> inode_lock -> subvol_sem * * so we must take the cleaner_mutex here before we take the * subvol_sem. The deadlock can't actually happen, but this * quiets lockdep.
*/
mutex_lock(&fs_info->cleaner_mutex);
down_write(&fs_info->subvol_sem);
ret = btrfs_quota_disable(fs_info);
up_write(&fs_info->subvol_sem);
mutex_unlock(&fs_info->cleaner_mutex); break; default:
ret = -EINVAL; break;
}
if (!btrfs_qgroup_enabled(fs_info)) return -ENOTCONN;
ret = mnt_want_write_file(file); if (ret) return ret;
sa = memdup_user(arg, sizeof(*sa)); if (IS_ERR(sa)) {
ret = PTR_ERR(sa); goto drop_write;
}
if (sa->assign) {
prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL); if (!prealloc) {
ret = -ENOMEM; goto out;
}
}
trans = btrfs_join_transaction(root); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto out;
}
/* * Prealloc ownership is moved to the relation handler, there it's used * or freed on error.
*/ if (sa->assign) {
ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst, prealloc);
prealloc = NULL;
} else {
ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst);
}
/* update qgroup status and info */
mutex_lock(&fs_info->qgroup_ioctl_lock);
err = btrfs_run_qgroups(trans);
mutex_unlock(&fs_info->qgroup_ioctl_lock); if (err < 0)
btrfs_warn(fs_info, "qgroup status update failed after %s relation, marked as inconsistent",
sa->assign ? "adding" : "deleting");
err = btrfs_end_transaction(trans); if (err && !ret)
ret = err;
if (copy_from_user(label, arg, sizeof(label))) return -EFAULT;
if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
btrfs_err(fs_info, "unable to set label with more than %d bytes",
BTRFS_LABEL_SIZE - 1); return -EINVAL;
}
ret = mnt_want_write_file(file); if (ret) return ret;
trans = btrfs_start_transaction(root, 0); if (IS_ERR(trans)) {
ret = PTR_ERR(trans); goto out_unlock;
}
spin_lock(&fs_info->super_lock);
strscpy(super_block->label, label);
spin_unlock(&fs_info->super_lock);
ret = btrfs_commit_transaction(trans);
/* * Context that's attached to an encoded read io_uring command, in cmd->pdu. It * contains the fields in btrfs_uring_read_extent that are necessary to finish * off and cleanup the I/O in btrfs_uring_read_finished.
*/ struct btrfs_uring_priv { struct io_uring_cmd *cmd; struct page **pages; unsignedlong nr_pages; struct kiocb iocb; struct iovec *iov; struct iov_iter iter; struct extent_state *cached_state;
u64 count;
u64 start;
u64 lockend; int err; bool compressed;
};
ret = btrfs_encoded_read_regular_fill_pages(inode, disk_bytenr,
disk_io_size, pages, priv); if (ret && ret != -EIOCBQUEUED) goto out_fail;
/* * If we return -EIOCBQUEUED, we're deferring the cleanup to * btrfs_uring_read_finished(), which will handle unlocking the extent * and inode and freeing the allocations.
*/
/* * We're returning to userspace with the inode lock held, and that's * okay - it'll get unlocked in a worker thread. Call * btrfs_lockdep_inode_release() to avoid confusing lockdep.
*/
btrfs_lockdep_inode_release(inode, i_rwsem);
if (copy_from_user(&args, argp, sizeof(args))) return -EFAULT;
switch (args.mode) { case BTRFS_SUBVOL_SYNC_WAIT_FOR_QUEUED: /* * Wait for the first one deleted that waits until all previous * are cleaned.
*/
spin_lock(&fs_info->trans_lock); if (!list_empty(&fs_info->dead_roots)) {
root = list_last_entry(&fs_info->dead_roots, struct btrfs_root, root_list);
args.subvolid = btrfs_root_id(root);
found = true;
}
spin_unlock(&fs_info->trans_lock); if (!found) return -ENOENT;
fallthrough; case BTRFS_SUBVOL_SYNC_WAIT_FOR_ONE: if ((0 < args.subvolid && args.subvolid < BTRFS_FIRST_FREE_OBJECTID) ||
BTRFS_LAST_FREE_OBJECTID < args.subvolid) return -EINVAL; break; case BTRFS_SUBVOL_SYNC_COUNT:
spin_lock(&fs_info->trans_lock);
args.count = list_count_nodes(&fs_info->dead_roots);
spin_unlock(&fs_info->trans_lock); if (copy_to_user(argp, &args, sizeof(args))) return -EFAULT; return 0; case BTRFS_SUBVOL_SYNC_PEEK_FIRST:
spin_lock(&fs_info->trans_lock); /* Last in the list was deleted first. */ if (!list_empty(&fs_info->dead_roots)) {
root = list_last_entry(&fs_info->dead_roots, struct btrfs_root, root_list);
args.subvolid = btrfs_root_id(root);
} else {
args.subvolid = 0;
}
spin_unlock(&fs_info->trans_lock); if (copy_to_user(argp, &args, sizeof(args))) return -EFAULT; return 0; case BTRFS_SUBVOL_SYNC_PEEK_LAST:
spin_lock(&fs_info->trans_lock); /* First in the list was deleted last. */ if (!list_empty(&fs_info->dead_roots)) {
root = list_first_entry(&fs_info->dead_roots, struct btrfs_root, root_list);
args.subvolid = btrfs_root_id(root);
} else {
args.subvolid = 0;
}
spin_unlock(&fs_info->trans_lock); if (copy_to_user(argp, &args, sizeof(args))) return -EFAULT; return 0; default: return -EINVAL;
}
/* 32bit limitation: fs_roots_radix key is not wide enough. */ if (sizeof(unsignedlong) != sizeof(u64) && args.subvolid > U32_MAX) return -EOVERFLOW;
while (1) { /* Wait for the specific one. */ if (down_read_interruptible(&fs_info->subvol_sem) == -EINTR) return -EINTR;
refs = -1;
spin_lock(&fs_info->fs_roots_radix_lock);
root = radix_tree_lookup(&fs_info->fs_roots_radix,
(unsignedlong)args.subvolid); if (root) {
spin_lock(&root->root_item_lock);
refs = btrfs_root_refs(&root->root_item);
root_flags = btrfs_root_flags(&root->root_item);
spin_unlock(&root->root_item_lock);
}
spin_unlock(&fs_info->fs_roots_radix_lock);
up_read(&fs_info->subvol_sem);
/* Subvolume does not exist. */ if (!root) return -ENOENT;
/* Subvolume not deleted at all. */ if (refs > 0) return -EEXIST; /* We've waited and now the subvolume is gone. */ if (wait_for_deletion && refs == -1) { /* Return the one we waited for as the last one. */ if (copy_to_user(argp, &args, sizeof(args))) return -EFAULT; return 0;
}
/* Subvolume not found on the first try (deleted or never existed). */ if (refs == -1) return -ENOENT;
wait_for_deletion = true;
ASSERT(root_flags & BTRFS_ROOT_SUBVOL_DEAD);
sched_ret = schedule_timeout_interruptible(HZ); /* Early wake up or error. */ if (sched_ret != 0) return -EINTR;
}
switch (cmd) { case FS_IOC_GETVERSION: return btrfs_ioctl_getversion(inode, argp); case FS_IOC_GETFSLABEL: return btrfs_ioctl_get_fslabel(fs_info, argp); case FS_IOC_SETFSLABEL: return btrfs_ioctl_set_fslabel(file, argp); case FITRIM: return btrfs_ioctl_fitrim(fs_info, argp); case BTRFS_IOC_SNAP_CREATE: return btrfs_ioctl_snap_create(file, argp, 0); case BTRFS_IOC_SNAP_CREATE_V2: return btrfs_ioctl_snap_create_v2(file, argp, 0); case BTRFS_IOC_SUBVOL_CREATE: return btrfs_ioctl_snap_create(file, argp, 1); case BTRFS_IOC_SUBVOL_CREATE_V2: return btrfs_ioctl_snap_create_v2(file, argp, 1); case BTRFS_IOC_SNAP_DESTROY: return btrfs_ioctl_snap_destroy(file, argp, false); case BTRFS_IOC_SNAP_DESTROY_V2: return btrfs_ioctl_snap_destroy(file, argp, true); case BTRFS_IOC_SUBVOL_GETFLAGS: return btrfs_ioctl_subvol_getflags(BTRFS_I(inode), argp); case BTRFS_IOC_SUBVOL_SETFLAGS: return btrfs_ioctl_subvol_setflags(file, argp); case BTRFS_IOC_DEFAULT_SUBVOL: return btrfs_ioctl_default_subvol(file, argp); case BTRFS_IOC_DEFRAG: return btrfs_ioctl_defrag(file, NULL); case BTRFS_IOC_DEFRAG_RANGE: return btrfs_ioctl_defrag(file, argp); case BTRFS_IOC_RESIZE: return btrfs_ioctl_resize(file, argp); case BTRFS_IOC_ADD_DEV: return btrfs_ioctl_add_dev(fs_info, argp); case BTRFS_IOC_RM_DEV: return btrfs_ioctl_rm_dev(file, argp); case BTRFS_IOC_RM_DEV_V2: return btrfs_ioctl_rm_dev_v2(file, argp); case BTRFS_IOC_FS_INFO: return btrfs_ioctl_fs_info(fs_info, argp); case BTRFS_IOC_DEV_INFO: return btrfs_ioctl_dev_info(fs_info, argp); case BTRFS_IOC_TREE_SEARCH: return btrfs_ioctl_tree_search(root, argp); case BTRFS_IOC_TREE_SEARCH_V2: return btrfs_ioctl_tree_search_v2(root, argp); case BTRFS_IOC_INO_LOOKUP: return btrfs_ioctl_ino_lookup(root, argp); case BTRFS_IOC_INO_PATHS: return btrfs_ioctl_ino_to_path(root, argp); case BTRFS_IOC_LOGICAL_INO: return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); case BTRFS_IOC_LOGICAL_INO_V2: return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); case BTRFS_IOC_SPACE_INFO: return btrfs_ioctl_space_info(fs_info, argp); case BTRFS_IOC_SYNC: { int ret;
ret = btrfs_start_delalloc_roots(fs_info, LONG_MAX, false); if (ret) return ret;
ret = btrfs_sync_fs(inode->i_sb, 1); /* * There may be work for the cleaner kthread to do (subvolume * deletion, delayed iputs, defrag inodes, etc), so wake it up.
*/
wake_up_process(fs_info->cleaner_kthread); return ret;
} case BTRFS_IOC_START_SYNC: return btrfs_ioctl_start_sync(root, argp); case BTRFS_IOC_WAIT_SYNC: return btrfs_ioctl_wait_sync(fs_info, argp); case BTRFS_IOC_SCRUB: return btrfs_ioctl_scrub(file, argp); case BTRFS_IOC_SCRUB_CANCEL: return btrfs_ioctl_scrub_cancel(fs_info); case BTRFS_IOC_SCRUB_PROGRESS: return btrfs_ioctl_scrub_progress(fs_info, argp); case BTRFS_IOC_BALANCE_V2: return btrfs_ioctl_balance(file, argp); case BTRFS_IOC_BALANCE_CTL: return btrfs_ioctl_balance_ctl(fs_info, arg); case BTRFS_IOC_BALANCE_PROGRESS: return btrfs_ioctl_balance_progress(fs_info, argp); case BTRFS_IOC_SET_RECEIVED_SUBVOL: return btrfs_ioctl_set_received_subvol(file, argp); #ifdef CONFIG_64BIT case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: return btrfs_ioctl_set_received_subvol_32(file, argp); #endif case BTRFS_IOC_SEND: return _btrfs_ioctl_send(root, argp, false); #ifdefined(CONFIG_64BIT) && defined(CONFIG_COMPAT) case BTRFS_IOC_SEND_32: return _btrfs_ioctl_send(root, argp, true); #endif case BTRFS_IOC_GET_DEV_STATS: return btrfs_ioctl_get_dev_stats(fs_info, argp); case BTRFS_IOC_QUOTA_CTL: return btrfs_ioctl_quota_ctl(file, argp); case BTRFS_IOC_QGROUP_ASSIGN: return btrfs_ioctl_qgroup_assign(file, argp); case BTRFS_IOC_QGROUP_CREATE: return btrfs_ioctl_qgroup_create(file, argp); case BTRFS_IOC_QGROUP_LIMIT: return btrfs_ioctl_qgroup_limit(file, argp); case BTRFS_IOC_QUOTA_RESCAN: return btrfs_ioctl_quota_rescan(file, argp); case BTRFS_IOC_QUOTA_RESCAN_STATUS: return btrfs_ioctl_quota_rescan_status(fs_info, argp); case BTRFS_IOC_QUOTA_RESCAN_WAIT: return btrfs_ioctl_quota_rescan_wait(fs_info); case BTRFS_IOC_DEV_REPLACE: return btrfs_ioctl_dev_replace(fs_info, argp); case BTRFS_IOC_GET_SUPPORTED_FEATURES: return btrfs_ioctl_get_supported_features(argp); case BTRFS_IOC_GET_FEATURES: return btrfs_ioctl_get_features(fs_info, argp); case BTRFS_IOC_SET_FEATURES: return btrfs_ioctl_set_features(file, argp); case BTRFS_IOC_GET_SUBVOL_INFO: return btrfs_ioctl_get_subvol_info(inode, argp); case BTRFS_IOC_GET_SUBVOL_ROOTREF: return btrfs_ioctl_get_subvol_rootref(root, argp); case BTRFS_IOC_INO_LOOKUP_USER: return btrfs_ioctl_ino_lookup_user(file, argp); case FS_IOC_ENABLE_VERITY: return fsverity_ioctl_enable(file, (constvoid __user *)argp); case FS_IOC_MEASURE_VERITY: return fsverity_ioctl_measure(file, argp); case FS_IOC_READ_VERITY_METADATA: return fsverity_ioctl_read_metadata(file, argp); case BTRFS_IOC_ENCODED_READ: return btrfs_ioctl_encoded_read(file, argp, false); case BTRFS_IOC_ENCODED_WRITE: return btrfs_ioctl_encoded_write(file, argp, false); #ifdefined(CONFIG_64BIT) && defined(CONFIG_COMPAT) case BTRFS_IOC_ENCODED_READ_32: return btrfs_ioctl_encoded_read(file, argp, true); case BTRFS_IOC_ENCODED_WRITE_32: return btrfs_ioctl_encoded_write(file, argp, true); #endif case BTRFS_IOC_SUBVOL_SYNC_WAIT: return btrfs_ioctl_subvol_sync(fs_info, argp);
}
return -ENOTTY;
}
#ifdef CONFIG_COMPAT long btrfs_compat_ioctl(struct file *file, unsignedint cmd, unsignedlong arg)
{ /* * These all access 32-bit values anyway so no further * handling is necessary.
*/ switch (cmd) { case FS_IOC32_GETVERSION:
cmd = FS_IOC_GETVERSION; break;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.