1 /* $NetBSD: libdm-nbsd-iface.c,v 1.7 2010/03/12 16:24:40 haad Exp $ */
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2008 Adam Hamsik. All rights reserved.
7 * Copyright (C) 2010 Alex Hornung. All rights reserved.
9 * This file is part of the device-mapper userspace tools.
11 * This copyrighted material is made available to anyone wishing to use,
12 * modify, copy, or redistribute it subject to the terms and conditions
13 * of the GNU Lesser General Public License v.2.1.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "libdm-targets.h"
23 #include "libdm-common.h"
24 #include "libdm-netbsd.h"
26 #include <sys/param.h>
27 #include <sys/ioctl.h>
28 #include <sys/sysctl.h>
35 #include <netbsd-dm.h>
39 #include <rump/rump.h>
40 #include <rump/rump_syscalls.h>
44 * Ensure build compatibility.
45 * The hard-coded versions here are the highest present
46 * in the _cmd_data arrays.
49 #if !((DM_VERSION_MAJOR == 1 && DM_VERSION_MINOR >= 0) || \
50 (DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 0))
51 #error The version of dm-ioctl.h included is incompatible.
54 /* dm major version no for running kernel */
55 static unsigned _dm_version_minor = 0;
56 static unsigned _dm_version_patchlevel = 0;
58 static int _control_fd = -1;
59 static int _version_checked = 0;
60 static int _version_ok = 1;
61 static unsigned _ioctl_buffer_double_factor = 0;
66 * XXX Remove this structure and write another own one
67 * I don't understand why ioctl calls has different
68 * names then dm task type
70 static struct cmd_data _cmd_data_v4[] = {
71 {"create", DM_DEV_CREATE, {4, 0, 0}},
72 {"reload", DM_TABLE_LOAD, {4, 0, 0}}, /* DM_DEVICE_RELOAD */
73 {"remove", DM_DEV_REMOVE, {4, 0, 0}},
74 {"remove_all", DM_REMOVE_ALL, {4, 0, 0}},
75 {"suspend", DM_DEV_SUSPEND, {4, 0, 0}},
76 {"resume", DM_DEV_SUSPEND, {4, 0, 0}},
77 {"info", DM_DEV_STATUS, {4, 0, 0}},
78 {"deps", DM_TABLE_DEPS, {4, 0, 0}}, /* DM_DEVICE_DEPS */
79 {"rename", DM_DEV_RENAME, {4, 0, 0}},
80 {"version", DM_VERSION, {4, 0, 0}},
81 {"status", DM_TABLE_STATUS, {4, 0, 0}},
82 {"table", DM_TABLE_STATUS, {4, 0, 0}}, /* DM_DEVICE_TABLE */
83 {"waitevent", DM_DEV_WAIT, {4, 0, 0}},
84 {"names", DM_LIST_DEVICES, {4, 0, 0}},
85 {"clear", DM_TABLE_CLEAR, {4, 0, 0}},
86 {"mknodes", DM_DEV_STATUS, {4, 0, 0}},
87 #ifdef DM_LIST_VERSIONS
88 {"targets", DM_LIST_VERSIONS, {4, 1, 0}},
91 {"message", DM_TARGET_MSG, {4, 2, 0}},
93 #ifdef DM_DEV_SET_GEOMETRY
94 {"setgeometry", DM_DEV_SET_GEOMETRY, {4, 6, 0}},
100 * In NetBSD we use sysctl to get kernel drivers info. control device
101 * has predefined minor number 0 and major number = char major number
102 * of dm driver. First slot is therefore ocupied with control device
103 * and minor device starts from 1;
106 static int _control_device_number(uint32_t *major, uint32_t *minor)
109 nbsd_get_dm_major(major, DM_CHAR_MAJOR);
117 * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong
119 static int _control_exists(const char *control, uint32_t major, uint32_t minor)
123 if (stat(control, &buf) < 0) {
125 log_sys_error("stat", control);
129 if (!S_ISCHR(buf.st_mode)) {
130 log_verbose("%s: Wrong inode type", control);
131 if (!unlink(control))
133 log_sys_error("unlink", control);
137 if (major && buf.st_rdev != MKDEV(major, minor)) {
138 log_verbose("%s: Wrong device number: (%u, %u) instead of "
140 MAJOR(buf.st_mode), MINOR(buf.st_mode),
142 if (!unlink(control))
144 log_sys_error("unlink", control);
151 static int _create_control(const char *control, uint32_t major, uint32_t minor)
159 old_umask = umask(0022);
160 ret = dm_create_dir(dm_dir());
166 log_verbose("Creating device %s (%u, %u)", control, major, minor);
168 if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR,
169 MKDEV(major, minor)) < 0) {
170 log_sys_error("mknod", control);
178 /* Check if major is device-mapper block device major number */
179 int dm_is_dm_major(uint32_t major)
183 nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
185 if (major == dm_major)
191 /* Open control device if doesn't exist create it. */
192 static int _open_control(void)
194 char control[PATH_MAX];
195 uint32_t major = 0, minor = 0;
197 if (_control_fd != -1)
203 snprintf(control, sizeof(control), "%s/control", dm_dir());
205 if (!_control_device_number(&major, &minor))
206 log_error("Is device-mapper driver missing from kernel?");
209 if (!_control_exists(control, major, minor) &&
210 !_create_control(control, major, minor)) {
211 log_error("create_control or control_exists");
216 if ((_control_fd = open(control, O_RDWR)) < 0) {
217 log_sys_error("open", control);
224 log_error("Failure to communicate with kernel device-mapper driver.");
229 * Destroy dm task structure there are some dynamically alocated values there.
230 * name, uuid, head, tail list.
232 void dm_task_destroy(struct dm_task *dmt)
234 struct target *t, *n;
236 for (t = dmt->head; t; t = n) {
244 dm_free(dmt->dev_name);
247 dm_free(dmt->newname);
250 dm_free(dmt->message);
253 dm_free(dmt->dmi.v4);
262 /* Get kernel driver version from dm_ioctl structure. */
263 int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
272 v = dmt->dmi.v4->version;
273 snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]);
274 _dm_version_minor = v[1];
275 _dm_version_patchlevel = v[2];
280 /* Get kernel driver protocol version and comapre it with library version. */
281 static int _check_version(char *version, size_t size)
283 struct dm_task *task;
286 if (!(task = dm_task_create(DM_DEVICE_VERSION))) {
287 log_error("Failed to get device-mapper version");
292 r = dm_task_run(task);
293 dm_task_get_driver_version(task, version, size);
294 dm_task_destroy(task);
300 * Find out device-mapper's major version number the first time
301 * this is called and whether or not we support it.
303 int dm_check_version(void)
307 if (_version_checked)
310 _version_checked = 1;
312 if (_check_version(dmversion, sizeof(dmversion)))
319 int dm_cookie_supported(void)
324 /* Get next target(table description) from list pointed by dmt->head. */
325 void *dm_get_next_target(struct dm_task *dmt, void *next,
326 uint64_t *start, uint64_t *length,
327 char **target_type, char **params)
329 struct target *t = (struct target *) next;
339 *target_type = t->type;
345 /* Unmarshall the target info returned from a status call */
346 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
348 char *outbuf = (char *) dmi + dmi->data_start;
349 char *outptr = outbuf;
351 struct dm_target_spec *spec;
353 for (i = 0; i < dmi->target_count; i++) {
354 spec = (struct dm_target_spec *) outptr;
355 if (!dm_task_add_target(dmt, spec->sector_start,
358 outptr + sizeof(*spec))) {
362 outptr = outbuf + spec->next;
369 get_dev_name(char *d_name, uint32_t d_major, uint32_t d_minor)
371 static char d_buf[MAXPATHLEN];
379 dev_dir = opendir("/dev");
381 while ((dire = readdir(dev_dir)) != NULL) {
383 if (strstr(dire->d_name, d_name) == NULL)
386 snprintf(d_buf, MAXPATHLEN, "/dev/%s", dire->d_name);
388 if ((err = stat(d_buf, &st)) < 0)
389 printf("stat failed with %d", err);
391 if (st.st_mode & S_IFBLK){
392 if ((major(st.st_rdev) == d_major) && (minor(st.st_rdev) == d_minor)) {
393 strncpy(d_buf, dire->d_name, strlen(dire->d_name) + 1);
399 memset(d_buf, '0', sizeof(d_buf));
402 (void)closedir(dev_dir);
408 * @dev_major is major number of char device
410 * I have to find it's block device number and lookup dev in
411 * device database to find device path.
415 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
419 uint32_t major, dm_major;
425 dev = MKDEV(dev_major,dev_minor);
427 log_error("dangerous zone...\n");
431 name = devname(dev,mode);
433 r = snprintf(buf, (size_t) bufsize, "/dev/%s",name);
435 if (r < 0 || r > bufsize - 1 || name == NULL)
441 /* Fill info from dm_ioctl structure. Look at DM_EXISTS_FLAG*/
442 int dm_task_get_info(struct dm_task *dmt, struct dm_info *info)
447 memset(info, 0, sizeof(*info));
449 info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
453 info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0;
454 info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0;
455 info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0;
456 info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ?
458 info->target_count = dmt->dmi.v4->target_count;
459 info->open_count = dmt->dmi.v4->open_count;
460 info->event_nr = dmt->dmi.v4->event_nr;
462 nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */
463 info->minor = MINOR(dmt->dmi.v4->dev);
468 /* Unsupported on NetBSD */
469 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
471 *read_ahead = DM_READ_AHEAD_NONE;
475 const char *dm_task_get_name(const struct dm_task *dmt)
478 return (dmt->dmi.v4->name);
481 const char *dm_task_get_uuid(const struct dm_task *dmt)
484 return (dmt->dmi.v4->uuid);
487 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
489 return (struct dm_deps *) (((void *) dmt->dmi.v4) +
490 dmt->dmi.v4->data_start);
493 struct dm_names *dm_task_get_names(struct dm_task *dmt)
495 return (struct dm_names *) (((void *) dmt->dmi.v4) +
496 dmt->dmi.v4->data_start);
499 struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
501 return (struct dm_versions *) (((void *) dmt->dmi.v4) +
502 dmt->dmi.v4->data_start);
505 int dm_task_set_ro(struct dm_task *dmt)
511 /* Unsupported on NetBSD */
512 int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
513 uint32_t read_ahead_flags)
518 int dm_task_suppress_identical_reload(struct dm_task *dmt)
520 dmt->suppress_identical_reload = 1;
524 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
526 if (!(dmt->newname = dm_strdup(newname))) {
527 log_error("dm_task_set_newname: strdup(%s) failed", newname);
534 int dm_task_set_message(struct dm_task *dmt, const char *message)
536 if (!(dmt->message = dm_strdup(message))) {
537 log_error("dm_task_set_message: strdup(%s) failed", message);
544 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
546 dmt->sector = sector;
551 /* Unsupported in NetBSD */
552 int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders,
553 const char *heads, const char *sectors, const char *start)
558 int dm_task_no_flush(struct dm_task *dmt)
565 int dm_task_no_open_count(struct dm_task *dmt)
567 dmt->no_open_count = 1;
572 int dm_task_skip_lockfs(struct dm_task *dmt)
574 dmt->skip_lockfs = 1;
579 int dm_task_query_inactive_table(struct dm_task *dmt)
581 dmt->query_inactive_table = 1;
586 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
588 dmt->event_nr = event_nr;
593 /* Allocate one target(table description) entry. */
594 struct target *create_target(uint64_t start, uint64_t len, const char *type,
597 struct target *t = dm_malloc(sizeof(*t));
598 log_error("params is at create_target: %s", params);
600 log_error("create_target: malloc(%" PRIsize_t ") failed",
605 memset(t, 0, sizeof(*t));
607 if (!(t->params = dm_strdup(params))) {
608 log_error("create_target: strdup(params) failed");
612 if (!(t->type = dm_strdup(type))) {
613 log_error("create_target: strdup(type) failed");
628 /* Parse given dm task structure to proplib dictionary. */
629 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict)
631 prop_array_t cmd_array;
632 prop_dictionary_t target_spec;
637 char type[DM_MAX_TYPE_NAME];
639 uint32_t major, flags;
642 const int (*version)[3];
645 version = &_cmd_data_v4[dmt->type].version;
647 cmd_array = prop_array_create();
649 for (t = dmt->head; t; t = t->next) {
650 target_spec = prop_dictionary_create();
652 prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start);
653 prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length);
655 strlcpy(type,t->type,DM_MAX_TYPE_NAME);
657 prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type);
658 log_error("t->params: %s\n", t->params);
659 prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params);
661 prop_dictionary_get_cstring(target_spec,
662 DM_TABLE_PARAMS, (char **) &str);
663 log_error("t->params from dict: %s\n", str);
665 prop_array_set(cmd_array,count,target_spec);
667 prop_object_release(target_spec);
673 if (count && (dmt->sector || dmt->message)) {
674 log_error("targets and message are incompatible");
678 if (count && dmt->newname) {
679 log_error("targets and newname are incompatible");
683 if (count && dmt->geometry) {
684 log_error("targets and geometry are incompatible");
688 if (dmt->newname && (dmt->sector || dmt->message)) {
689 log_error("message and newname are incompatible");
693 if (dmt->newname && dmt->geometry) {
694 log_error("geometry and newname are incompatible");
698 if (dmt->geometry && (dmt->sector || dmt->message)) {
699 log_error("geometry and message are incompatible");
703 if (dmt->sector && !dmt->message) {
704 log_error("message is required with sector");
709 len += strlen(dmt->newname) + 1;
712 len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1;
715 len += strlen(dmt->geometry) + 1;
717 nbsd_dmi_add_version((*version), dm_dict);
719 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
721 * Only devices with major which is equal to netbsd dm major
722 * dm devices in NetBSD can't have more majors then one assigned to dm.
724 if (dmt->major != major && dmt->major != -1)
727 if (dmt->minor >= 0) {
728 flags |= DM_PERSISTENT_DEV_FLAG;
730 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor);
733 /* Set values to dictionary. */
735 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name);
738 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid);
740 if (dmt->type == DM_DEVICE_SUSPEND)
741 flags |= DM_SUSPEND_FLAG;
743 flags |= DM_NOFLUSH_FLAG;
745 flags |= DM_READONLY_FLAG;
746 if (dmt->skip_lockfs)
747 flags |= DM_SKIP_LOCKFS_FLAG;
749 if (dmt->query_inactive_table) {
750 if (_dm_version_minor < 16)
751 log_warn("WARNING: Inactive table query unsupported "
752 "by kernel. It will use live table.");
753 flags |= DM_QUERY_INACTIVE_TABLE_FLAG;
756 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
758 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr);
761 prop_array_set_cstring(cmd_array, 0, dmt->newname);
763 /* Add array for all COMMAND specific data. */
764 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
765 prop_object_release(cmd_array);
770 static int _process_mapper_dir(struct dm_task *dmt)
772 struct dirent *dirent;
778 if (!(d = opendir(dir))) {
779 log_sys_error("opendir", dir);
783 while ((dirent = readdir(d))) {
784 if (!strcmp(dirent->d_name, ".") ||
785 !strcmp(dirent->d_name, "..") ||
786 !strcmp(dirent->d_name, "control"))
788 dm_task_set_name(dmt, dirent->d_name);
793 log_sys_error("closedir", dir);
798 /* Get list of all devices. */
799 static int _process_all_v4(struct dm_task *dmt)
801 struct dm_task *task;
802 struct dm_names *names;
806 if (!(task = dm_task_create(DM_DEVICE_LIST)))
809 if (!dm_task_run(task)) {
814 if (!(names = dm_task_get_names(task))) {
823 names = (void *) names + next;
824 if (!dm_task_set_name(dmt, names->name)) {
828 if (!dm_task_run(dmt))
834 dm_task_destroy(task);
838 static int _mknodes_v4(struct dm_task *dmt)
840 (void) _process_mapper_dir(dmt);
842 return _process_all_v4(dmt);
845 /* Create new device and load table to it. */
846 static int _create_and_load_v4(struct dm_task *dmt)
848 struct dm_task *task;
851 printf("create and load called \n");
853 /* Use new task struct to create the device */
854 if (!(task = dm_task_create(DM_DEVICE_CREATE))) {
855 log_error("Failed to create device-mapper task struct");
859 /* Copy across relevant fields */
860 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
861 dm_task_destroy(task);
865 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
866 dm_task_destroy(task);
870 task->major = dmt->major;
871 task->minor = dmt->minor;
872 task->uid = dmt->uid;
873 task->gid = dmt->gid;
874 task->mode = dmt->mode;
876 r = dm_task_run(task);
877 dm_task_destroy(task);
881 /* Next load the table */
882 if (!(task = dm_task_create(DM_DEVICE_RELOAD))) {
883 log_error("Failed to create device-mapper task struct");
887 /* Copy across relevant fields */
888 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
889 dm_task_destroy(task);
893 task->read_only = dmt->read_only;
894 task->head = dmt->head;
895 task->tail = dmt->tail;
897 r = dm_task_run(task);
901 dm_task_destroy(task);
905 /* Use the original structure last so the info will be correct */
906 dmt->type = DM_DEVICE_RESUME;
910 r = dm_task_run(dmt);
916 dmt->type = DM_DEVICE_REMOVE;
920 if (!dm_task_run(dmt))
921 log_error("Failed to revert device creation.");
926 uint64_t dm_task_get_existing_table_size(struct dm_task *dmt)
928 return dmt->existing_table_size;
931 static int _reload_with_suppression_v4(struct dm_task *dmt)
933 struct dm_task *task;
934 struct target *t1, *t2;
937 /* New task to get existing table information */
938 if (!(task = dm_task_create(DM_DEVICE_TABLE))) {
939 log_error("Failed to create device-mapper task struct");
943 /* Copy across relevant fields */
944 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
945 dm_task_destroy(task);
949 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
950 dm_task_destroy(task);
954 task->major = dmt->major;
955 task->minor = dmt->minor;
957 r = dm_task_run(task);
960 dm_task_destroy(task);
964 /* Store existing table size */
966 while (t2 && t2->next)
968 dmt->existing_table_size = t2 ? t2->start + t2->length : 0;
970 if ((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0 != dmt->read_only)
977 while (t2->params[strlen(t2->params) - 1] == ' ')
978 t2->params[strlen(t2->params) - 1] = '\0';
979 if ((t1->start != t2->start) ||
980 (t1->length != t2->length) ||
981 (strcmp(t1->type, t2->type)) ||
982 (strcmp(t1->params, t2->params)))
989 dmt->dmi.v4 = task->dmi.v4;
991 dm_task_destroy(task);
996 dm_task_destroy(task);
998 /* Now do the original reload */
999 dmt->suppress_identical_reload = 0;
1000 r = dm_task_run(dmt);
1006 * This function is heart of NetBSD libdevmapper-> device-mapper kernel protocol
1007 * It creates proplib_dictionary from dm task structure and sends it to NetBSD
1008 * kernel driver. After succesfull ioctl it create dmi structure from returned
1009 * proplib dictionary. This way I keep number of changes in NetBSD version of
1010 * libdevmapper as small as posible.
1012 static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command)
1014 struct dm_ioctl *dmi;
1015 prop_dictionary_t dm_dict_in, dm_dict_out;
1021 dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1022 dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */
1024 /* Set command name to dictionary */
1025 prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1026 _cmd_data_v4[dmt->type].name);
1028 /* Parse dmi from libdevmapper to dictionary */
1029 if (_flatten(dmt, dm_dict_in) < 0)
1032 prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags);
1034 if (dmt->type == DM_DEVICE_TABLE)
1035 flags |= DM_STATUS_TABLE_FLAG;
1037 if (dmt->no_open_count)
1038 flags |= DM_SKIP_BDGET_FLAG;
1040 flags |= DM_EXISTS_FLAG;
1042 /* Set flags to dictionary. */
1043 prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags);
1045 prop_dictionary_externalize_to_file(dm_dict_in,"/tmp/test_in");
1047 log_very_verbose("Ioctl type %s --- flags %d",_cmd_data_v4[dmt->type].name,flags);
1048 //printf("name %s, major %d minor %d\n uuid %s\n",
1049 //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt));
1050 /* Send dictionary to kernel and wait for reply. */
1052 if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1053 NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1055 if (errno == ENOENT &&
1056 ((dmt->type == DM_DEVICE_INFO) ||
1057 (dmt->type == DM_DEVICE_MKNODES) ||
1058 (dmt->type == DM_DEVICE_STATUS))) {
1061 * Linux version doesn't fail when ENOENT is returned
1062 * for nonexisting device after info, deps, mknodes call.
1063 * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0;
1066 dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd);
1068 dmi->flags &= ~DM_EXISTS_FLAG;
1070 prop_object_release(dm_dict_in);
1071 prop_object_release(dm_dict_out);
1075 log_error("ioctl %s call failed with errno %d\n",
1076 _cmd_data_v4[dmt->type].name, errno);
1078 prop_object_release(dm_dict_in);
1079 prop_object_release(dm_dict_out);
1086 dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
1088 prop_dictionary_externalize_to_file(dm_dict_out,"/tmp/test_out");
1090 /* Parse kernel dictionary to dmi structure and return it to libdevmapper. */
1091 dmi = nbsd_dm_dict_to_dmi(dm_dict_out,_cmd_data_v4[dmt->type].cmd);
1093 prop_object_release(dm_dict_in);
1094 prop_object_release(dm_dict_out);
1101 /* Create new edvice nodes in mapper/ dir. */
1102 void dm_task_update_nodes(void)
1107 static void _dm_rename_kern(struct dm_task *dmt)
1109 prop_dictionary_t dm_dict_in, dm_dict_out;
1111 dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1113 /* Set command name to dictionary */
1114 prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1116 prop_dictionary_set_cstring(dm_dict_in, "dev_name", dmt->dev_name);
1117 prop_dictionary_set_cstring(dm_dict_in, "new_name", dmt->newname);
1119 /* Set flags to dictionary. */
1120 prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,0);
1122 if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1123 NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1124 log_error("rename failed");
1128 /* Run dm command which is descirbed in dm_task structure. */
1129 int dm_task_run(struct dm_task *dmt)
1131 struct dm_ioctl *dmi;
1134 if ((unsigned) dmt->type >=
1135 (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) {
1136 log_error("Internal error: unknown device-mapper task %d",
1141 command = _cmd_data_v4[dmt->type].cmd;
1143 /* Old-style creation had a table supplied */
1144 if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1145 return _create_and_load_v4(dmt);
1147 if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name &&
1148 !dmt->uuid && dmt->major <= 0)
1149 return _mknodes_v4(dmt);
1151 if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
1152 return _reload_with_suppression_v4(dmt);
1154 if (!_open_control())
1157 if (!(dmi = _do_dm_ioctl(dmt, command)))
1160 switch (dmt->type) {
1161 case DM_DEVICE_CREATE:
1162 printf("create dmt->dev_name = %s\n", dmt->dev_name);
1163 /* XXX: ideally kernel takes care of this */
1165 add_dev_node(dmt->dev_name, MAJOR(dmi->dev), MINOR(dmi->dev),
1166 dmt->uid, dmt->gid, dmt->mode, 0);
1170 case DM_DEVICE_REMOVE:
1171 printf("remove dmt->dev_name = %s\n", dmt->dev_name);
1172 /* XXX: ideally kernel takes care of this */
1174 /* FIXME Kernel needs to fill in dmi->name */
1176 rm_dev_node(dmt->dev_name, 0);
1180 case DM_DEVICE_RENAME:
1181 /* FIXME Kernel needs to fill in dmi->name */
1182 printf("rename dmt->dev_name = %s\n", dmt->dev_name);
1183 _dm_rename_kern(dmt);
1186 rename_dev_node(dmt->dev_name, dmt->newname, 0);
1190 case DM_DEVICE_RESUME:
1191 /* FIXME Kernel needs to fill in dmi->name */
1192 set_dev_node_read_ahead(dmt->dev_name, dmt->read_ahead,
1193 dmt->read_ahead_flags);
1196 case DM_DEVICE_MKNODES:
1197 printf("mknodes dmt->dev_name = %s\n", dmt->dev_name);
1199 if (dmi->flags & DM_EXISTS_FLAG)
1200 add_dev_node(dmi->name, MAJOR(dmi->dev),
1202 dmt->uid, dmt->gid, dmt->mode, 0);
1203 else if (dmt->dev_name)
1204 rm_dev_node(dmt->dev_name, 0);
1208 case DM_DEVICE_STATUS:
1209 case DM_DEVICE_TABLE:
1210 case DM_DEVICE_WAITEVENT:
1211 if (!_unmarshal_status(dmt, dmi))
1216 /* Was structure reused? */
1218 dm_free(dmt->dmi.v4);
1228 void dm_lib_release(void)
1230 if (_control_fd != -1) {
1237 void dm_lib_exit(void)
1242 _version_checked = 0;