#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;
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5
¤ Dauer der Verarbeitung: 0.23 Sekunden
(vorverarbeitet)
¤
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.