/*
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Adam Hamsik.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dm_ioctl.c,v 1.57 2024/01/14 07:56:53 mlelstv Exp $");
/*
* Locking is used to synchronise between ioctl calls and between dm_table's
* users.
*
* ioctl locking:
* Simple reference counting, to count users of device will be used routines
* dm_dev_busy/dm_dev_unbusy are used for that.
* dm_dev_lookup/dm_dev_rem call dm_dev_busy before return(caller is therefore
* holder of reference_counter last).
*
* ioctl routines which change/remove dm_dev parameters must wait on
* dm_dev::dev_cv and when last user will call dm_dev_unbusy they will wake
* up them.
*
* table_head locking:
* To access table entries dm_table_* routines must be used.
*
* dm_table_get_entry will increment table users reference
* counter. It will return active or inactive table depends
* on uint8_t argument.
*
* dm_table_release must be called for every table_entry from
* dm_table_get_entry. Between these two calls tables can't be switched
* or destroyed.
*
* dm_table_head_init initialize table_entries SLISTS and io_cv.
*
* dm_table_head_destroy destroy cv.
*
* There are two types of users for dm_table_head first type will
* only read list and try to do anything with it e.g. dmstrategy,
* dm_table_size etc. There is another user for table_head which wants
* to change table lists e.g. dm_dev_resume_ioctl, dm_dev_remove_ioctl,
* dm_table_clear_ioctl.
*
* NOTE: It is not allowed to call dm_table_destroy, dm_table_switch_tables
* with hold table reference counter. Table reference counter is hold
* after calling dm_table_get_entry routine. After calling this
* function user must call dm_table_release before any writer table
* operation.
*
* Example: dm_table_get_entry
* dm_table_destroy/dm_table_switch_tables
* This example will lead to deadlock situation because after dm_table_get_entry
* table reference counter is != 0 and dm_table_destroy have to wait on cv until
* reference counter is 0.
*
*/
#define DM_REMOVE_FLAG(flag, name) do { \
prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
flag &= ~name; \
prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
} while (/*CONSTCOND*/0)
#define DM_ADD_FLAG(flag, name) do { \
prop_dictionary_get_uint32(dm_dict,DM_IOCTL_FLAGS,&flag); \
flag |= name; \
prop_dictionary_set_uint32(dm_dict,DM_IOCTL_FLAGS,flag); \
} while (/*CONSTCOND*/0)
static int dm_table_deps(dm_table_entry_t *, prop_array_t);
static int dm_table_init(dm_target_t *, dm_table_entry_t *, char *);
/*
* Print flags sent to the kernel from libdevmapper.
*/
static int
dm_dbg_print_flags(uint32_t flags)
{
aprint_debug("dbg_print --- %d\n", flags);
if (flags & DM_READONLY_FLAG)
aprint_debug("dbg_flags: DM_READONLY_FLAG set In/Out\n");
if (flags & DM_SUSPEND_FLAG)
aprint_debug("dbg_flags: DM_SUSPEND_FLAG set In/Out\n");
if (flags & DM_PERSISTENT_DEV_FLAG)
aprint_debug("dbg_flags: DM_PERSISTENT_DEV_FLAG set In\n");
if (flags & DM_STATUS_TABLE_FLAG)
aprint_debug("dbg_flags: DM_STATUS_TABLE_FLAG set In\n");
if (flags & DM_ACTIVE_PRESENT_FLAG)
aprint_debug("dbg_flags: DM_ACTIVE_PRESENT_FLAG set Out\n");
if (flags & DM_INACTIVE_PRESENT_FLAG)
aprint_debug("dbg_flags: DM_INACTIVE_PRESENT_FLAG set Out\n");
if (flags & DM_BUFFER_FULL_FLAG)
aprint_debug("dbg_flags: DM_BUFFER_FULL_FLAG set Out\n");
if (flags & DM_SKIP_BDGET_FLAG)
aprint_debug("dbg_flags: DM_SKIP_BDGET_FLAG set In\n");
if (flags & DM_SKIP_LOCKFS_FLAG)
aprint_debug("dbg_flags: DM_SKIP_LOCKFS_FLAG set In\n");
if (flags & DM_NOFLUSH_FLAG)
aprint_debug("dbg_flags: DM_NOFLUSH_FLAG set In\n");
return 0;
}
/*
* Get list of all available targets from global
* target list and sent them back to libdevmapper.
*/
int
dm_list_versions_ioctl(prop_dictionary_t dm_dict)
{
prop_array_t target_list;
uint32_t flags;
/*
* Create in-kernel entry for device. Device attributes such as name, uuid are
* taken from proplib dictionary.
*/
int
dm_dev_create_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
const char *name, *uuid;
int r;
uint32_t flags;
device_t devt;
cfdata_t cf;
flags = 0;
name = NULL;
uuid = NULL;
/* Get needed values from dictionary. */
prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
dm_dbg_print_flags(flags);
/* Lookup name and uuid if device already exist quit. */
if ((dmv = dm_dev_lookup(name, uuid, -1)) != NULL) {
DM_ADD_FLAG(flags, DM_EXISTS_FLAG); /* Device already exists */
dm_dev_unbusy(dmv);
return EEXIST;
}
if ((dmv = dm_dev_alloc()) == NULL)
return ENOMEM;
/* Increment device counter After creating device */
atomic_inc_32(&dm_dev_counter);
return r;
}
/*
* Get list of created device-mapper devices from global list and
* send it to kernel.
*
* Output dictionary:
*
* <key>cmd_data</key>
* <array>
* <dict>
* <key>name<key>
* <string>...</string>
*
* <key>dev</key>
* <integer>...</integer>
* </dict>
* </array>
*/
int
dm_dev_list_ioctl(prop_dictionary_t dm_dict)
{
prop_array_t dev_list;
uint32_t flags;
/*
* Rename selected devices old name is in struct dm_ioctl.
* newname is taken from dictionary
*
* <key>cmd_data</key>
* <array>
* <string>...</string>
* </array>
*/
int
dm_dev_rename_ioctl(prop_dictionary_t dm_dict)
{
prop_array_t cmd_array;
dm_dev_t *dmv;
if (strlen(n_name) + 1 > DM_NAME_LEN)
return EINVAL;
if ((dmv = dm_dev_rem(name, uuid, minor)) == NULL) {
DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
return ENOENT;
}
/* change device name */
/*
* XXX How to deal with this change, name only used in
* dm_dev_routines, should I add dm_dev_change_name which will run
* under the dm_dev_list mutex ?
*/
strlcpy(dmv->name, n_name, DM_NAME_LEN);
/*
* Remove device from global list I have to remove active
* and inactive tables first.
*/
int
dm_dev_remove_ioctl(prop_dictionary_t dm_dict)
{
int error;
cfdata_t cf;
dm_dev_t *dmv;
const char *name, *uuid;
uint32_t flags, minor;
device_t devt;
flags = 0;
name = NULL;
uuid = NULL;
/* Get needed values from dictionary. */
prop_dictionary_get_string(dm_dict, DM_IOCTL_NAME, &name);
prop_dictionary_get_string(dm_dict, DM_IOCTL_UUID, &uuid);
prop_dictionary_get_uint32(dm_dict, DM_IOCTL_FLAGS, &flags);
prop_dictionary_get_uint32(dm_dict, DM_IOCTL_MINOR, &minor);
dm_dbg_print_flags(flags);
/*
* This seems as hack to me, probably use routine dm_dev_get_devt to
* atomically get devt from device.
*/
if ((dmv = dm_dev_lookup(name, uuid, minor)) == NULL) {
DM_REMOVE_FLAG(flags, DM_EXISTS_FLAG);
return ENOENT;
}
devt = dmv->devt;
dm_dev_unbusy(dmv);
/*
* This will call dm_detach routine which will actually removes
* device.
*/
cf = device_cfdata(devt);
error = config_detach(devt, DETACH_QUIET);
if (error == 0)
kmem_free(cf, sizeof(*cf));
return error;
}
/*
* Return actual state of device to libdevmapper.
*/
int
dm_dev_status_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
const char *name, *uuid;
uint32_t flags, j, minor;
if (dmv->flags & DM_SUSPEND_FLAG)
DM_ADD_FLAG(flags, DM_SUSPEND_FLAG);
/*
* Add status flags for tables I have to check both active and
* inactive tables.
*/
if ((j = dm_table_get_target_count(&dmv->table_head, DM_TABLE_ACTIVE)))
DM_ADD_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
else
DM_REMOVE_FLAG(flags, DM_ACTIVE_PRESENT_FLAG);
if (dm_table_get_target_count(&dmv->table_head, DM_TABLE_INACTIVE))
DM_ADD_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
else
DM_REMOVE_FLAG(flags, DM_INACTIVE_PRESENT_FLAG);
dm_dev_unbusy(dmv);
return 0;
}
/*
* Set only flag to suggest that device is suspended. This call is
* not supported in NetBSD.
*/
int
dm_dev_suspend_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
const char *name, *uuid;
uint32_t flags, minor;
/* Add flags to dictionary flag after dmv -> dict copy */
DM_ADD_FLAG(flags, DM_EXISTS_FLAG);
return 0;
}
/*
* Simulate Linux behaviour better and switch tables here and not in
* dm_table_load_ioctl.
*/
int
dm_dev_resume_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
const char *name, *uuid;
uint32_t flags, minor;
name = NULL;
uuid = NULL;
flags = 0;
/*
* char *xml; xml = prop_dictionary_externalize(dm_dict);
* printf("%s\n",xml);
*/
/* Destroy inactive table after resume. */
dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
return 0;
}
/*
* Table management routines
* lvm2tools doesn't send name/uuid to kernel with table
* for lookup I have to use minor number.
*/
/*
* Remove inactive table from device. Routines which work's with inactive tables
* doesn't need to synchronise with dmstrategy. They can synchronise themselves with mutex?.
*/
int
dm_table_clear_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
const char *name, *uuid;
uint32_t flags, minor;
/*
* Get list of physical devices for active table.
* Get dev_t from pdev vnode and insert it into cmd_array.
*
* XXX. This function is called from lvm2tools to get information
* about physical devices, too e.g. during vgcreate.
*/
int
dm_table_deps_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
dm_table_t *tbl;
dm_table_entry_t *table_en;
aprint_debug("Getting table deps for device: %s\n", dmv->name);
/*
* if DM_QUERY_INACTIVE_TABLE_FLAG is passed we need to query
* INACTIVE TABLE
*/
if (flags & DM_QUERY_INACTIVE_TABLE_FLAG)
table_type = DM_TABLE_INACTIVE;
else
table_type = DM_TABLE_ACTIVE;
static int
dm_table_deps(dm_table_entry_t *table_en, prop_array_t array)
{
dm_mapping_t *map;
int i, size;
uint64_t rdev, tmp;
size = prop_array_count(array);
TAILQ_FOREACH(map, &table_en->pdev_maps, next) {
rdev = map->data.pdev->pdev_vnode->v_rdev;
for (i = 0; i < size; i++) {
if (prop_array_get_uint64(array, i, &tmp) == true)
if (rdev == tmp)
break; /* exists */
}
/*
* Ignore if the device has already been added by
* other tables.
*/
if (i == size) {
prop_array_add_uint64(array, rdev);
aprint_debug("%s: %d:%d\n", __func__, major(rdev),
minor(rdev));
}
}
return 0;
}
/*
* Load new table/tables to device.
* Call appropriate target init routine to open all physical pdev's and
* link them to device. For other targets mirror, strip, snapshot
* etc. also add dependency devices to upcalls list.
*
* Load table to inactive slot table are switched in dm_device_resume_ioctl.
* This simulates Linux behaviour better there should not be any difference.
*/
int
dm_table_load_ioctl(prop_dictionary_t dm_dict)
{
dm_dev_t *dmv;
dm_table_entry_t *table_en, *last_table;
dm_table_t *tbl;
dm_target_t *target;
/*
* I have to check if this table slot is not used by another table list.
* if it is used I should free them.
*/
if (dmv->flags & DM_INACTIVE_PRESENT_FLAG)
dm_table_destroy(&dmv->table_head, DM_TABLE_INACTIVE);
while ((target_dict = prop_object_iterator_next(iter)) != NULL) {
int ret;
const char *cp;
char *str;
size_t strsz;
prop_dictionary_get_string(target_dict,
DM_TABLE_TYPE, &type);
/*
* If we want to deny table with 2 or more different
* target we should do it here
*/
if (((target = dm_target_lookup(type)) == NULL) &&
((target = dm_target_autoload(type)) == NULL)) {
dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
dm_dev_unbusy(dmv);
prop_object_iterator_release(iter);
return ENOENT;
}
table_en = kmem_alloc(sizeof(dm_table_entry_t), KM_SLEEP);
prop_dictionary_get_uint64(target_dict, DM_TABLE_START,
&table_en->start);
prop_dictionary_get_uint64(target_dict, DM_TABLE_LENGTH,
&table_en->length);
/*
* There is a parameter string after dm_target_spec
* structure which points to /dev/wd0a 284 part of
* table. String str points to this text. This can be
* null and therefore it should be checked before we try to
* use it.
*/
cp = NULL;
prop_dictionary_get_string(target_dict,
DM_TABLE_PARAMS, &cp);
if (cp == NULL)
str = NULL;
else
str = kmem_strdupsize(cp, &strsz, KM_SLEEP);
if (SLIST_EMPTY(tbl) || last_table == NULL)
/* insert this table to head */
SLIST_INSERT_HEAD(tbl, table_en, next);
else
SLIST_INSERT_AFTER(last_table, table_en, next);
/*
* Explicitly clear DM_TABLE_PARAMS to prevent dmsetup(8) from
* printing junk when DM_TABLE_PARAMS was never initialized.
*/
prop_dictionary_set_string(target_dict, DM_TABLE_PARAMS, "");
/*
* For every call I have to set kernel driver version.
* Because I can have commands supported only in other
* newer/later version. This routine is called for every
* ioctl command.
*/
int
dm_check_version(prop_dictionary_t dm_dict)
{
int i;
uint32_t dm_version[3];
prop_array_t ver;
ver = prop_dictionary_get(dm_dict, DM_IOCTL_VERSION);
for (i = 0; i < 3; i++)
prop_array_get_uint32(ver, i, &dm_version[i]);