1 /* $NetBSD: libdm-nbsd-iface.c,v 1.5 2009/12/05 11:42:24 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.
8 * This file is part of the device-mapper userspace tools.
10 * This copyrighted material is made available to anyone wishing to use,
11 * modify, copy, or redistribute it subject to the terms and conditions
12 * of the GNU Lesser General Public License v.2.1.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "libdm-targets.h"
21 #include "libdm-common.h"
22 #include "libdm-netbsd.h"
24 #include <sys/ioctl.h>
25 #include <sys/sysctl.h>
31 #include <dev/dm/netbsd-dm.h>
36 #include <rump/rump.h>
37 #include <rump/rump_syscalls.h>
41 * Ensure build compatibility.
42 * The hard-coded versions here are the highest present
43 * in the _cmd_data arrays.
46 #if !((DM_VERSION_MAJOR == 1 && DM_VERSION_MINOR >= 0) || \
47 (DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 0))
48 #error The version of dm-ioctl.h included is incompatible.
51 /* dm major version no for running kernel */
52 static unsigned _dm_version_minor
= 0;
53 static unsigned _dm_version_patchlevel
= 0;
55 static int _control_fd
= -1;
56 static int _version_checked
= 0;
57 static int _version_ok
= 1;
58 static unsigned _ioctl_buffer_double_factor
= 0;
63 * XXX Remove this structure and write another own one
64 * I don't understand why ioctl calls has different
65 * names then dm task type
67 static struct cmd_data _cmd_data_v4
[] = {
68 {"create", DM_DEV_CREATE
, {4, 0, 0}},
69 {"reload", DM_TABLE_LOAD
, {4, 0, 0}}, /* DM_DEVICE_RELOAD */
70 {"remove", DM_DEV_REMOVE
, {4, 0, 0}},
71 {"remove_all", DM_REMOVE_ALL
, {4, 0, 0}},
72 {"suspend", DM_DEV_SUSPEND
, {4, 0, 0}},
73 {"resume", DM_DEV_SUSPEND
, {4, 0, 0}},
74 {"info", DM_DEV_STATUS
, {4, 0, 0}},
75 {"deps", DM_TABLE_DEPS
, {4, 0, 0}}, /* DM_DEVICE_DEPS */
76 {"rename", DM_DEV_RENAME
, {4, 0, 0}},
77 {"version", DM_VERSION
, {4, 0, 0}},
78 {"status", DM_TABLE_STATUS
, {4, 0, 0}},
79 {"table", DM_TABLE_STATUS
, {4, 0, 0}}, /* DM_DEVICE_TABLE */
80 {"waitevent", DM_DEV_WAIT
, {4, 0, 0}},
81 {"names", DM_LIST_DEVICES
, {4, 0, 0}},
82 {"clear", DM_TABLE_CLEAR
, {4, 0, 0}},
83 {"mknodes", DM_DEV_STATUS
, {4, 0, 0}},
84 #ifdef DM_LIST_VERSIONS
85 {"targets", DM_LIST_VERSIONS
, {4, 1, 0}},
88 {"message", DM_TARGET_MSG
, {4, 2, 0}},
90 #ifdef DM_DEV_SET_GEOMETRY
91 {"setgeometry", DM_DEV_SET_GEOMETRY
, {4, 6, 0}},
97 * In NetBSD we use sysctl to get kernel drivers info. control device
98 * has predefined minor number 0 and major number = char major number
99 * of dm driver. First slot is therefore ocupied with control device
100 * and minor device starts from 1;
103 static int _control_device_number(uint32_t *major
, uint32_t *minor
)
106 nbsd_get_dm_major(major
, DM_CHAR_MAJOR
);
114 * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong
116 static int _control_exists(const char *control
, uint32_t major
, uint32_t minor
)
120 if (stat(control
, &buf
) < 0) {
122 log_sys_error("stat", control
);
126 if (!S_ISCHR(buf
.st_mode
)) {
127 log_verbose("%s: Wrong inode type", control
);
128 if (!unlink(control
))
130 log_sys_error("unlink", control
);
134 if (major
&& buf
.st_rdev
!= MKDEV(major
, minor
)) {
135 log_verbose("%s: Wrong device number: (%u, %u) instead of "
137 MAJOR(buf
.st_mode
), MINOR(buf
.st_mode
),
139 if (!unlink(control
))
141 log_sys_error("unlink", control
);
148 static int _create_control(const char *control
, uint32_t major
, uint32_t minor
)
156 old_umask
= umask(0022);
157 ret
= dm_create_dir(dm_dir());
163 log_verbose("Creating device %s (%u, %u)", control
, major
, minor
);
165 if (mknod(control
, S_IFCHR
| S_IRUSR
| S_IWUSR
,
166 MKDEV(major
, minor
)) < 0) {
167 log_sys_error("mknod", control
);
175 /* Check if major is device-mapper block device major number */
176 int dm_is_dm_major(uint32_t major
)
180 nbsd_get_dm_major(&dm_major
, DM_BLOCK_MAJOR
);
182 if (major
== dm_major
)
188 /* Open control device if doesn't exist create it. */
189 static int _open_control(void)
191 char control
[PATH_MAX
];
192 uint32_t major
= 0, minor
= 0;
194 if (_control_fd
!= -1)
200 snprintf(control
, sizeof(control
), "%s/control", dm_dir());
202 if (!_control_device_number(&major
, &minor
))
203 log_error("Is device-mapper driver missing from kernel?");
205 if (!_control_exists(control
, major
, minor
) &&
206 !_create_control(control
, major
, minor
))
209 if ((_control_fd
= open(control
, O_RDWR
)) < 0) {
210 log_sys_error("open", control
);
217 log_error("Failure to communicate with kernel device-mapper driver.");
222 * Destroy dm task structure there are some dynamically alocated values there.
223 * name, uuid, head, tail list.
225 void dm_task_destroy(struct dm_task
*dmt
)
227 struct target
*t
, *n
;
229 for (t
= dmt
->head
; t
; t
= n
) {
237 dm_free(dmt
->dev_name
);
240 dm_free(dmt
->newname
);
243 dm_free(dmt
->message
);
246 dm_free(dmt
->dmi
.v4
);
255 /* Get kernel driver version from dm_ioctl structure. */
256 int dm_task_get_driver_version(struct dm_task
*dmt
, char *version
, size_t size
)
265 v
= dmt
->dmi
.v4
->version
;
266 snprintf(version
, size
, "%u.%u.%u", v
[0], v
[1], v
[2]);
267 _dm_version_minor
= v
[1];
268 _dm_version_patchlevel
= v
[2];
273 /* Get kernel driver protocol version and comapre it with library version. */
274 static int _check_version(char *version
, size_t size
)
276 struct dm_task
*task
;
279 if (!(task
= dm_task_create(DM_DEVICE_VERSION
))) {
280 log_error("Failed to get device-mapper version");
285 r
= dm_task_run(task
);
286 dm_task_get_driver_version(task
, version
, size
);
287 dm_task_destroy(task
);
293 * Find out device-mapper's major version number the first time
294 * this is called and whether or not we support it.
296 int dm_check_version(void)
300 if (_version_checked
)
303 _version_checked
= 1;
305 if (_check_version(dmversion
, sizeof(dmversion
)))
312 int dm_cookie_supported(void)
317 /* Get next target(table description) from list pointed by dmt->head. */
318 void *dm_get_next_target(struct dm_task
*dmt
, void *next
,
319 uint64_t *start
, uint64_t *length
,
320 char **target_type
, char **params
)
322 struct target
*t
= (struct target
*) next
;
332 *target_type
= t
->type
;
338 /* Unmarshall the target info returned from a status call */
339 static int _unmarshal_status(struct dm_task
*dmt
, struct dm_ioctl
*dmi
)
341 char *outbuf
= (char *) dmi
+ dmi
->data_start
;
342 char *outptr
= outbuf
;
344 struct dm_target_spec
*spec
;
346 for (i
= 0; i
< dmi
->target_count
; i
++) {
347 spec
= (struct dm_target_spec
*) outptr
;
348 if (!dm_task_add_target(dmt
, spec
->sector_start
,
351 outptr
+ sizeof(*spec
))) {
355 outptr
= outbuf
+ spec
->next
;
362 * @dev_major is major number of char device
364 * I have to find it's block device number and lookup dev in
365 * device database to find device path.
369 int dm_format_dev(char *buf
, int bufsize
, uint32_t dev_major
,
373 uint32_t major
, dm_major
;
378 struct kinfo_drivers
*kd
;
382 nbsd_get_dm_major(&dm_major
, DM_BLOCK_MAJOR
);
387 if (sysctlbyname("kern.drivers",NULL
,&val_len
,NULL
,0) < 0) {
388 printf("sysctlbyname failed");
392 if ((kd
= malloc (val_len
)) == NULL
){
393 printf("malloc kd info error\n");
397 if (sysctlbyname("kern.drivers", kd
, &val_len
, NULL
, 0) < 0) {
398 printf("sysctlbyname failed kd");
402 for (i
= 0, val_len
/= sizeof(*kd
); i
< val_len
; i
++){
403 if (kd
[i
].d_cmajor
== dev_major
) {
404 major
= kd
[i
].d_bmajor
;
409 dev
= MKDEV(major
,dev_minor
);
413 name
= devname(dev
,mode
);
415 r
= snprintf(buf
, (size_t) bufsize
, "/dev/%s",name
);
419 if (r
< 0 || r
> bufsize
- 1 || name
== NULL
)
425 /* Fill info from dm_ioctl structure. Look at DM_EXISTS_FLAG*/
426 int dm_task_get_info(struct dm_task
*dmt
, struct dm_info
*info
)
431 memset(info
, 0, sizeof(*info
));
433 info
->exists
= dmt
->dmi
.v4
->flags
& DM_EXISTS_FLAG
? 1 : 0;
437 info
->suspended
= dmt
->dmi
.v4
->flags
& DM_SUSPEND_FLAG
? 1 : 0;
438 info
->read_only
= dmt
->dmi
.v4
->flags
& DM_READONLY_FLAG
? 1 : 0;
439 info
->live_table
= dmt
->dmi
.v4
->flags
& DM_ACTIVE_PRESENT_FLAG
? 1 : 0;
440 info
->inactive_table
= dmt
->dmi
.v4
->flags
& DM_INACTIVE_PRESENT_FLAG
?
442 info
->target_count
= dmt
->dmi
.v4
->target_count
;
443 info
->open_count
= dmt
->dmi
.v4
->open_count
;
444 info
->event_nr
= dmt
->dmi
.v4
->event_nr
;
446 nbsd_get_dm_major(&info
->major
, DM_BLOCK_MAJOR
); /* get netbsd dm device major number */
447 info
->minor
= MINOR(dmt
->dmi
.v4
->dev
);
452 /* Unsupported on NetBSD */
453 uint32_t dm_task_get_read_ahead(const struct dm_task
*dmt
, uint32_t *read_ahead
)
455 *read_ahead
= DM_READ_AHEAD_NONE
;
459 const char *dm_task_get_name(const struct dm_task
*dmt
)
462 return (dmt
->dmi
.v4
->name
);
465 const char *dm_task_get_uuid(const struct dm_task
*dmt
)
468 return (dmt
->dmi
.v4
->uuid
);
471 struct dm_deps
*dm_task_get_deps(struct dm_task
*dmt
)
473 return (struct dm_deps
*) (((void *) dmt
->dmi
.v4
) +
474 dmt
->dmi
.v4
->data_start
);
477 struct dm_names
*dm_task_get_names(struct dm_task
*dmt
)
479 return (struct dm_names
*) (((void *) dmt
->dmi
.v4
) +
480 dmt
->dmi
.v4
->data_start
);
483 struct dm_versions
*dm_task_get_versions(struct dm_task
*dmt
)
485 return (struct dm_versions
*) (((void *) dmt
->dmi
.v4
) +
486 dmt
->dmi
.v4
->data_start
);
489 int dm_task_set_ro(struct dm_task
*dmt
)
495 /* Unsupported on NetBSD */
496 int dm_task_set_read_ahead(struct dm_task
*dmt
, uint32_t read_ahead
,
497 uint32_t read_ahead_flags
)
502 int dm_task_suppress_identical_reload(struct dm_task
*dmt
)
504 dmt
->suppress_identical_reload
= 1;
508 int dm_task_set_newname(struct dm_task
*dmt
, const char *newname
)
510 if (!(dmt
->newname
= dm_strdup(newname
))) {
511 log_error("dm_task_set_newname: strdup(%s) failed", newname
);
518 int dm_task_set_message(struct dm_task
*dmt
, const char *message
)
520 if (!(dmt
->message
= dm_strdup(message
))) {
521 log_error("dm_task_set_message: strdup(%s) failed", message
);
528 int dm_task_set_sector(struct dm_task
*dmt
, uint64_t sector
)
530 dmt
->sector
= sector
;
535 /* Unsupported in NetBSD */
536 int dm_task_set_geometry(struct dm_task
*dmt
, const char *cylinders
,
537 const char *heads
, const char *sectors
, const char *start
)
542 int dm_task_no_flush(struct dm_task
*dmt
)
549 int dm_task_no_open_count(struct dm_task
*dmt
)
551 dmt
->no_open_count
= 1;
556 int dm_task_skip_lockfs(struct dm_task
*dmt
)
558 dmt
->skip_lockfs
= 1;
563 int dm_task_query_inactive_table(struct dm_task
*dmt
)
565 dmt
->query_inactive_table
= 1;
570 int dm_task_set_event_nr(struct dm_task
*dmt
, uint32_t event_nr
)
572 dmt
->event_nr
= event_nr
;
577 /* Allocate one target(table description) entry. */
578 struct target
*create_target(uint64_t start
, uint64_t len
, const char *type
,
581 struct target
*t
= dm_malloc(sizeof(*t
));
584 log_error("create_target: malloc(%" PRIsize_t
") failed",
589 memset(t
, 0, sizeof(*t
));
591 if (!(t
->params
= dm_strdup(params
))) {
592 log_error("create_target: strdup(params) failed");
596 if (!(t
->type
= dm_strdup(type
))) {
597 log_error("create_target: strdup(type) failed");
612 /* Parse given dm task structure to proplib dictionary. */
613 static int _flatten(struct dm_task
*dmt
, prop_dictionary_t dm_dict
)
615 prop_array_t cmd_array
;
616 prop_dictionary_t target_spec
;
621 char type
[DM_MAX_TYPE_NAME
];
623 uint32_t major
, flags
;
625 const int (*version
)[3];
628 version
= &_cmd_data_v4
[dmt
->type
].version
;
630 cmd_array
= prop_array_create();
632 for (t
= dmt
->head
; t
; t
= t
->next
) {
633 target_spec
= prop_dictionary_create();
635 prop_dictionary_set_uint64(target_spec
,DM_TABLE_START
,t
->start
);
636 prop_dictionary_set_uint64(target_spec
,DM_TABLE_LENGTH
,t
->length
);
638 strlcpy(type
,t
->type
,DM_MAX_TYPE_NAME
);
640 prop_dictionary_set_cstring(target_spec
,DM_TABLE_TYPE
,type
);
641 prop_dictionary_set_cstring(target_spec
,DM_TABLE_PARAMS
,t
->params
);
643 prop_array_set(cmd_array
,count
,target_spec
);
645 prop_object_release(target_spec
);
651 if (count
&& (dmt
->sector
|| dmt
->message
)) {
652 log_error("targets and message are incompatible");
656 if (count
&& dmt
->newname
) {
657 log_error("targets and newname are incompatible");
661 if (count
&& dmt
->geometry
) {
662 log_error("targets and geometry are incompatible");
666 if (dmt
->newname
&& (dmt
->sector
|| dmt
->message
)) {
667 log_error("message and newname are incompatible");
671 if (dmt
->newname
&& dmt
->geometry
) {
672 log_error("geometry and newname are incompatible");
676 if (dmt
->geometry
&& (dmt
->sector
|| dmt
->message
)) {
677 log_error("geometry and message are incompatible");
681 if (dmt
->sector
&& !dmt
->message
) {
682 log_error("message is required with sector");
687 len
+= strlen(dmt
->newname
) + 1;
690 len
+= sizeof(struct dm_target_msg
) + strlen(dmt
->message
) + 1;
693 len
+= strlen(dmt
->geometry
) + 1;
695 nbsd_dmi_add_version((*version
), dm_dict
);
697 nbsd_get_dm_major(&major
, DM_BLOCK_MAJOR
);
699 * Only devices with major which is equal to netbsd dm major
700 * dm devices in NetBSD can't have more majors then one assigned to dm.
702 if (dmt
->major
!= major
&& dmt
->major
!= -1)
705 if (dmt
->minor
>= 0) {
706 flags
|= DM_PERSISTENT_DEV_FLAG
;
708 prop_dictionary_set_uint32(dm_dict
, DM_IOCTL_MINOR
, dmt
->minor
);
711 /* Set values to dictionary. */
713 prop_dictionary_set_cstring(dm_dict
, DM_IOCTL_NAME
, dmt
->dev_name
);
716 prop_dictionary_set_cstring(dm_dict
, DM_IOCTL_UUID
, dmt
->uuid
);
718 if (dmt
->type
== DM_DEVICE_SUSPEND
)
719 flags
|= DM_SUSPEND_FLAG
;
721 flags
|= DM_NOFLUSH_FLAG
;
723 flags
|= DM_READONLY_FLAG
;
724 if (dmt
->skip_lockfs
)
725 flags
|= DM_SKIP_LOCKFS_FLAG
;
727 if (dmt
->query_inactive_table
) {
728 if (_dm_version_minor
< 16)
729 log_warn("WARNING: Inactive table query unsupported "
730 "by kernel. It will use live table.");
731 flags
|= DM_QUERY_INACTIVE_TABLE_FLAG
;
734 prop_dictionary_set_uint32(dm_dict
, DM_IOCTL_FLAGS
, flags
);
736 prop_dictionary_set_uint32(dm_dict
, DM_IOCTL_EVENT
, dmt
->event_nr
);
739 prop_array_set_cstring(cmd_array
, 0, dmt
->newname
);
741 /* Add array for all COMMAND specific data. */
742 prop_dictionary_set(dm_dict
, DM_IOCTL_CMD_DATA
, cmd_array
);
743 prop_object_release(cmd_array
);
748 static int _process_mapper_dir(struct dm_task
*dmt
)
750 struct dirent
*dirent
;
756 if (!(d
= opendir(dir
))) {
757 log_sys_error("opendir", dir
);
761 while ((dirent
= readdir(d
))) {
762 if (!strcmp(dirent
->d_name
, ".") ||
763 !strcmp(dirent
->d_name
, "..") ||
764 !strcmp(dirent
->d_name
, "control"))
766 dm_task_set_name(dmt
, dirent
->d_name
);
771 log_sys_error("closedir", dir
);
776 /* Get list of all devices. */
777 static int _process_all_v4(struct dm_task
*dmt
)
779 struct dm_task
*task
;
780 struct dm_names
*names
;
784 if (!(task
= dm_task_create(DM_DEVICE_LIST
)))
787 if (!dm_task_run(task
)) {
792 if (!(names
= dm_task_get_names(task
))) {
801 names
= (void *) names
+ next
;
802 if (!dm_task_set_name(dmt
, names
->name
)) {
806 if (!dm_task_run(dmt
))
812 dm_task_destroy(task
);
816 static int _mknodes_v4(struct dm_task
*dmt
)
818 (void) _process_mapper_dir(dmt
);
820 return _process_all_v4(dmt
);
823 /* Create new device and load table to it. */
824 static int _create_and_load_v4(struct dm_task
*dmt
)
826 struct dm_task
*task
;
829 printf("create and load called \n");
831 /* Use new task struct to create the device */
832 if (!(task
= dm_task_create(DM_DEVICE_CREATE
))) {
833 log_error("Failed to create device-mapper task struct");
837 /* Copy across relevant fields */
838 if (dmt
->dev_name
&& !dm_task_set_name(task
, dmt
->dev_name
)) {
839 dm_task_destroy(task
);
843 if (dmt
->uuid
&& !dm_task_set_uuid(task
, dmt
->uuid
)) {
844 dm_task_destroy(task
);
848 task
->major
= dmt
->major
;
849 task
->minor
= dmt
->minor
;
850 task
->uid
= dmt
->uid
;
851 task
->gid
= dmt
->gid
;
852 task
->mode
= dmt
->mode
;
854 r
= dm_task_run(task
);
855 dm_task_destroy(task
);
859 /* Next load the table */
860 if (!(task
= dm_task_create(DM_DEVICE_RELOAD
))) {
861 log_error("Failed to create device-mapper task struct");
865 /* Copy across relevant fields */
866 if (dmt
->dev_name
&& !dm_task_set_name(task
, dmt
->dev_name
)) {
867 dm_task_destroy(task
);
871 task
->read_only
= dmt
->read_only
;
872 task
->head
= dmt
->head
;
873 task
->tail
= dmt
->tail
;
875 r
= dm_task_run(task
);
879 dm_task_destroy(task
);
883 /* Use the original structure last so the info will be correct */
884 dmt
->type
= DM_DEVICE_RESUME
;
888 r
= dm_task_run(dmt
);
894 dmt
->type
= DM_DEVICE_REMOVE
;
898 if (!dm_task_run(dmt
))
899 log_error("Failed to revert device creation.");
904 uint64_t dm_task_get_existing_table_size(struct dm_task
*dmt
)
906 return dmt
->existing_table_size
;
909 static int _reload_with_suppression_v4(struct dm_task
*dmt
)
911 struct dm_task
*task
;
912 struct target
*t1
, *t2
;
915 /* New task to get existing table information */
916 if (!(task
= dm_task_create(DM_DEVICE_TABLE
))) {
917 log_error("Failed to create device-mapper task struct");
921 /* Copy across relevant fields */
922 if (dmt
->dev_name
&& !dm_task_set_name(task
, dmt
->dev_name
)) {
923 dm_task_destroy(task
);
927 if (dmt
->uuid
&& !dm_task_set_uuid(task
, dmt
->uuid
)) {
928 dm_task_destroy(task
);
932 task
->major
= dmt
->major
;
933 task
->minor
= dmt
->minor
;
935 r
= dm_task_run(task
);
938 dm_task_destroy(task
);
942 /* Store existing table size */
944 while (t2
&& t2
->next
)
946 dmt
->existing_table_size
= t2
? t2
->start
+ t2
->length
: 0;
948 if ((task
->dmi
.v4
->flags
& DM_READONLY_FLAG
) ? 1 : 0 != dmt
->read_only
)
955 while (t2
->params
[strlen(t2
->params
) - 1] == ' ')
956 t2
->params
[strlen(t2
->params
) - 1] = '\0';
957 if ((t1
->start
!= t2
->start
) ||
958 (t1
->length
!= t2
->length
) ||
959 (strcmp(t1
->type
, t2
->type
)) ||
960 (strcmp(t1
->params
, t2
->params
)))
967 dmt
->dmi
.v4
= task
->dmi
.v4
;
969 dm_task_destroy(task
);
974 dm_task_destroy(task
);
976 /* Now do the original reload */
977 dmt
->suppress_identical_reload
= 0;
978 r
= dm_task_run(dmt
);
984 * This function is heart of NetBSD libdevmapper-> device-mapper kernel protocol
985 * It creates proplib_dictionary from dm task structure and sends it to NetBSD
986 * kernel driver. After succesfull ioctl it create dmi structure from returned
987 * proplib dictionary. This way I keep number of changes in NetBSD version of
988 * libdevmapper as small as posible.
990 static struct dm_ioctl
*_do_dm_ioctl(struct dm_task
*dmt
, unsigned command
)
992 struct dm_ioctl
*dmi
;
993 prop_dictionary_t dm_dict_in
, dm_dict_out
;
999 dm_dict_in
= prop_dictionary_create(); /* Dictionary send to kernel */
1000 dm_dict_out
= prop_dictionary_create(); /* Dictionary received from kernel */
1002 /* Set command name to dictionary */
1003 prop_dictionary_set_cstring(dm_dict_in
, DM_IOCTL_COMMAND
,
1004 _cmd_data_v4
[dmt
->type
].name
);
1006 /* Parse dmi from libdevmapper to dictionary */
1007 if (_flatten(dmt
, dm_dict_in
) < 0)
1010 prop_dictionary_get_uint32(dm_dict_in
, DM_IOCTL_FLAGS
, &flags
);
1012 if (dmt
->type
== DM_DEVICE_TABLE
)
1013 flags
|= DM_STATUS_TABLE_FLAG
;
1015 if (dmt
->no_open_count
)
1016 flags
|= DM_SKIP_BDGET_FLAG
;
1018 flags
|= DM_EXISTS_FLAG
;
1020 /* Set flags to dictionary. */
1021 prop_dictionary_set_uint32(dm_dict_in
,DM_IOCTL_FLAGS
,flags
);
1023 prop_dictionary_externalize_to_file(dm_dict_in
,"/tmp/test_in");
1025 log_very_verbose("Ioctl type %s --- flags %d",_cmd_data_v4
[dmt
->type
].name
,flags
);
1026 //printf("name %s, major %d minor %d\n uuid %s\n",
1027 //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt));
1028 /* Send dictionary to kernel and wait for reply. */
1030 struct plistref prefp
;
1032 prop_dictionary_externalize_to_pref(dm_dict_in
, &prefp
);
1034 if (rump_sys_ioctl(_control_fd
, NETBSD_DM_IOCTL
, &prefp
) != 0) {
1036 dm_dict_out
= prop_dictionary_internalize(prefp
.pref_plist
);
1038 if (prop_dictionary_sendrecv_ioctl(dm_dict_in
,_control_fd
,
1039 NETBSD_DM_IOCTL
,&dm_dict_out
) != 0) {
1041 if (errno
== ENOENT
&&
1042 ((dmt
->type
== DM_DEVICE_INFO
) ||
1043 (dmt
->type
== DM_DEVICE_MKNODES
) ||
1044 (dmt
->type
== DM_DEVICE_STATUS
))) {
1047 * Linux version doesn't fail when ENOENT is returned
1048 * for nonexisting device after info, deps, mknodes call.
1049 * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0;
1052 dmi
= nbsd_dm_dict_to_dmi(dm_dict_in
,_cmd_data_v4
[dmt
->type
].cmd
);
1054 dmi
->flags
&= ~DM_EXISTS_FLAG
;
1056 prop_object_release(dm_dict_in
);
1057 prop_object_release(dm_dict_out
);
1061 log_error("ioctl %s call failed with errno %d\n",
1062 _cmd_data_v4
[dmt
->type
].name
, errno
);
1064 prop_object_release(dm_dict_in
);
1065 prop_object_release(dm_dict_out
);
1072 dm_dict_out
= prop_dictionary_internalize(prefp
.pref_plist
);
1074 prop_dictionary_externalize_to_file(dm_dict_out
,"/tmp/test_out");
1076 /* Parse kernel dictionary to dmi structure and return it to libdevmapper. */
1077 dmi
= nbsd_dm_dict_to_dmi(dm_dict_out
,_cmd_data_v4
[dmt
->type
].cmd
);
1079 prop_object_release(dm_dict_in
);
1080 prop_object_release(dm_dict_out
);
1087 /* Create new edvice nodes in mapper/ dir. */
1088 void dm_task_update_nodes(void)
1093 /* Run dm command which is descirbed in dm_task structure. */
1094 int dm_task_run(struct dm_task
*dmt
)
1096 struct dm_ioctl
*dmi
;
1099 if ((unsigned) dmt
->type
>=
1100 (sizeof(_cmd_data_v4
) / sizeof(*_cmd_data_v4
))) {
1101 log_error("Internal error: unknown device-mapper task %d",
1106 command
= _cmd_data_v4
[dmt
->type
].cmd
;
1108 /* Old-style creation had a table supplied */
1109 if (dmt
->type
== DM_DEVICE_CREATE
&& dmt
->head
)
1110 return _create_and_load_v4(dmt
);
1112 if (dmt
->type
== DM_DEVICE_MKNODES
&& !dmt
->dev_name
&&
1113 !dmt
->uuid
&& dmt
->major
<= 0)
1114 return _mknodes_v4(dmt
);
1116 if ((dmt
->type
== DM_DEVICE_RELOAD
) && dmt
->suppress_identical_reload
)
1117 return _reload_with_suppression_v4(dmt
);
1119 if (!_open_control())
1122 if (!(dmi
= _do_dm_ioctl(dmt
, command
)))
1125 switch (dmt
->type
) {
1126 case DM_DEVICE_CREATE
:
1127 add_dev_node(dmt
->dev_name
, MAJOR(dmi
->dev
), MINOR(dmi
->dev
),
1128 dmt
->uid
, dmt
->gid
, dmt
->mode
, 0);
1131 case DM_DEVICE_REMOVE
:
1132 /* FIXME Kernel needs to fill in dmi->name */
1134 rm_dev_node(dmt
->dev_name
, 0);
1137 case DM_DEVICE_RENAME
:
1138 /* FIXME Kernel needs to fill in dmi->name */
1140 rename_dev_node(dmt
->dev_name
, dmt
->newname
, 0);
1143 case DM_DEVICE_RESUME
:
1144 /* FIXME Kernel needs to fill in dmi->name */
1145 set_dev_node_read_ahead(dmt
->dev_name
, dmt
->read_ahead
,
1146 dmt
->read_ahead_flags
);
1149 case DM_DEVICE_MKNODES
:
1150 if (dmi
->flags
& DM_EXISTS_FLAG
)
1151 add_dev_node(dmi
->name
, MAJOR(dmi
->dev
),
1153 dmt
->uid
, dmt
->gid
, dmt
->mode
, 0);
1154 else if (dmt
->dev_name
)
1155 rm_dev_node(dmt
->dev_name
, 0);
1158 case DM_DEVICE_STATUS
:
1159 case DM_DEVICE_TABLE
:
1160 case DM_DEVICE_WAITEVENT
:
1161 if (!_unmarshal_status(dmt
, dmi
))
1166 /* Was structure reused? */
1168 dm_free(dmt
->dmi
.v4
);
1178 void dm_lib_release(void)
1180 if (_control_fd
!= -1) {
1187 void dm_lib_exit(void)
1192 _version_checked
= 0;