xref: /netbsd-src/external/gpl2/lvm2/dist/libdm/ioctl/libdm-nbsd-iface.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*      $NetBSD: libdm-nbsd-iface.c,v 1.6 2009/12/09 00:15:51 haad Exp $        */
2 
3 /*
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  *
8  * This file is part of the device-mapper userspace tools.
9  *
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.
13  *
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
17  */
18 
19 #include "dmlib.h"
20 #include "libdm-targets.h"
21 #include "libdm-common.h"
22 #include "libdm-netbsd.h"
23 
24 #include <sys/ioctl.h>
25 #include <sys/sysctl.h>
26 
27 #include <fcntl.h>
28 #include <dirent.h>
29 #include <limits.h>
30 
31 #include <dev/dm/netbsd-dm.h>
32 
33 #include <dm-ioctl.h>
34 
35 #ifdef RUMP_ACTION
36 #include <rump/rump.h>
37 #include <rump/rump_syscalls.h>
38 #endif
39 
40 /*
41  * Ensure build compatibility.
42  * The hard-coded versions here are the highest present
43  * in the _cmd_data arrays.
44  */
45 
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.
49 #endif
50 
51 /* dm major version no for running kernel */
52 static unsigned _dm_version_minor = 0;
53 static unsigned _dm_version_patchlevel = 0;
54 
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;
59 
60 /* *INDENT-OFF* */
61 
62 /*
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
66  */
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}},
86 #endif
87 #ifdef DM_TARGET_MSG
88 	{"message",	DM_TARGET_MSG,		{4, 2, 0}},
89 #endif
90 #ifdef DM_DEV_SET_GEOMETRY
91 	{"setgeometry",	DM_DEV_SET_GEOMETRY,	{4, 6, 0}},
92 #endif
93 };
94 /* *INDENT-ON* */
95 
96 /*
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;
101  */
102 
103 static int _control_device_number(uint32_t *major, uint32_t *minor)
104 {
105 
106 	nbsd_get_dm_major(major, DM_CHAR_MAJOR);
107 
108 	*minor = 0;
109 
110 	return 1;
111 }
112 
113 /*
114  * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong
115  */
116 static int _control_exists(const char *control, uint32_t major, uint32_t minor)
117 {
118 	struct stat buf;
119 
120 	if (stat(control, &buf) < 0) {
121 		if (errno != ENOENT)
122 			log_sys_error("stat", control);
123 		return 0;
124 	}
125 
126 	if (!S_ISCHR(buf.st_mode)) {
127 		log_verbose("%s: Wrong inode type", control);
128 		if (!unlink(control))
129 			return 0;
130 		log_sys_error("unlink", control);
131 		return -1;
132 	}
133 
134 	if (major && buf.st_rdev != MKDEV(major, minor)) {
135 		log_verbose("%s: Wrong device number: (%u, %u) instead of "
136 			    "(%u, %u)", control,
137 			    MAJOR(buf.st_mode), MINOR(buf.st_mode),
138 			    major, minor);
139 		if (!unlink(control))
140 			return 0;
141 		log_sys_error("unlink", control);
142 		return -1;
143 	}
144 
145 	return 1;
146 }
147 
148 static int _create_control(const char *control, uint32_t major, uint32_t minor)
149 {
150 	int ret;
151 	mode_t old_umask;
152 
153 	if (!major)
154 		return 0;
155 
156 	old_umask = umask(0022);
157 	ret = dm_create_dir(dm_dir());
158 	umask(old_umask);
159 
160 	if (!ret)
161 		return 0;
162 
163 	log_verbose("Creating device %s (%u, %u)", control, major, minor);
164 
165 	if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR,
166 		  MKDEV(major, minor)) < 0)  {
167 		log_sys_error("mknod", control);
168 		return 0;
169 	}
170 
171 
172 	return 1;
173 }
174 
175 /* Check if major is device-mapper block device major number */
176 int dm_is_dm_major(uint32_t major)
177 {
178 	uint32_t dm_major;
179 
180 	nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
181 
182 	if (major == dm_major)
183 		return 1;
184 
185 	return 0;
186 }
187 
188 /* Open control device if doesn't exist create it. */
189 static int _open_control(void)
190 {
191 	char control[PATH_MAX];
192 	uint32_t major = 0, minor = 0;
193 
194 	if (_control_fd != -1)
195 		return 1;
196 
197 #ifdef RUMP_ACTION
198 	rump_init();
199 #endif
200 	snprintf(control, sizeof(control), "%s/control", dm_dir());
201 
202 	if (!_control_device_number(&major, &minor))
203 		log_error("Is device-mapper driver missing from kernel?");
204 
205 	if (!_control_exists(control, major, minor) &&
206 	    !_create_control(control, major, minor))
207 		goto error;
208 
209 	if ((_control_fd = open(control, O_RDWR)) < 0) {
210 		log_sys_error("open", control);
211 		goto error;
212 	}
213 
214 	return 1;
215 
216 error:
217 	log_error("Failure to communicate with kernel device-mapper driver.");
218 	return 0;
219 }
220 
221 /*
222  * Destroy dm task structure there are some dynamically alocated values there.
223  * name, uuid, head, tail list.
224  */
225 void dm_task_destroy(struct dm_task *dmt)
226 {
227 	struct target *t, *n;
228 
229 	for (t = dmt->head; t; t = n) {
230 		n = t->next;
231 		dm_free(t->params);
232 		dm_free(t->type);
233 		dm_free(t);
234 	}
235 
236 	if (dmt->dev_name)
237 		dm_free(dmt->dev_name);
238 
239 	if (dmt->newname)
240 		dm_free(dmt->newname);
241 
242 	if (dmt->message)
243 		dm_free(dmt->message);
244 
245 	if (dmt->dmi.v4)
246 		dm_free(dmt->dmi.v4);
247 
248 	if (dmt->uuid)
249 		dm_free(dmt->uuid);
250 
251 	dm_free(dmt);
252 
253 }
254 
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)
257 {
258 	unsigned *v;
259 
260 	if (!dmt->dmi.v4) {
261 		version[0] = '\0';
262 		return 0;
263 	}
264 
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];
269 
270 	return 1;
271 }
272 
273 /* Get kernel driver protocol version and comapre it with library version. */
274 static int _check_version(char *version, size_t size)
275 {
276 	struct dm_task *task;
277 	int r;
278 
279 	if (!(task = dm_task_create(DM_DEVICE_VERSION))) {
280 		log_error("Failed to get device-mapper version");
281 		version[0] = '\0';
282 		return 0;
283 	}
284 
285 	r = dm_task_run(task);
286 	dm_task_get_driver_version(task, version, size);
287 	dm_task_destroy(task);
288 
289 	return r;
290 }
291 
292 /*
293  * Find out device-mapper's major version number the first time
294  * this is called and whether or not we support it.
295  */
296 int dm_check_version(void)
297 {
298 	char dmversion[64];
299 
300 	if (_version_checked)
301 		return _version_ok;
302 
303 	_version_checked = 1;
304 
305 	if (_check_version(dmversion, sizeof(dmversion)))
306 		return 1;
307 
308 
309 	return 0;
310 }
311 
312 int dm_cookie_supported(void)
313 {
314 	return (0);
315 }
316 
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)
321 {
322 	struct target *t = (struct target *) next;
323 
324 	if (!t)
325 		t = dmt->head;
326 
327 	if (!t)
328 		return NULL;
329 
330 	*start = t->start;
331 	*length = t->length;
332 	*target_type = t->type;
333 	*params = t->params;
334 
335 	return t->next;
336 }
337 
338 /* Unmarshall the target info returned from a status call */
339 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
340 {
341 	char *outbuf = (char *) dmi + dmi->data_start;
342 	char *outptr = outbuf;
343 	uint32_t i;
344 	struct dm_target_spec *spec;
345 
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,
349 					spec->length,
350 					spec->target_type,
351 					outptr + sizeof(*spec))) {
352 			return 0;
353 		}
354 
355 		outptr = outbuf + spec->next;
356 	}
357 
358 	return 1;
359 }
360 
361 /*
362  * @dev_major is major number of char device
363  *
364  * I have to find it's block device number and lookup dev in
365  * device database to find device path.
366  *
367  */
368 
369 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
370 		  uint32_t dev_minor)
371 {
372 	int r;
373 	uint32_t major, dm_major;
374 	char *name;
375 	mode_t mode;
376 	dev_t dev;
377 	size_t val_len,i;
378 	struct kinfo_drivers *kd;
379 
380 	mode = 0;
381 
382 	nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
383 
384 	if (bufsize < 8)
385 		return 0;
386 
387 	if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
388 		printf("sysctlbyname failed");
389 		return 0;
390 	}
391 
392 	if ((kd = malloc (val_len)) == NULL){
393 		printf("malloc kd info error\n");
394 		return 0;
395 	}
396 
397 	if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
398 		printf("sysctlbyname failed kd");
399 		return 0;
400 	}
401 
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;
405 			break;
406 		}
407 	}
408 
409 	dev = MKDEV(major,dev_minor);
410 
411 	mode |= S_IFBLK;
412 
413 	name = devname(dev,mode);
414 
415 	r = snprintf(buf, (size_t) bufsize, "/dev/%s",name);
416 
417 	free(kd);
418 
419 	if (r < 0 || r > bufsize - 1 || name == NULL)
420 		return 0;
421 
422 	return 1;
423 }
424 
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)
427 {
428 	if (!dmt->dmi.v4)
429 		return 0;
430 
431 	memset(info, 0, sizeof(*info));
432 
433 	info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
434 	if (!info->exists)
435 		return 1;
436 
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 ?
441 	    1 : 0;
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;
445 
446 	nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */
447 	info->minor = MINOR(dmt->dmi.v4->dev);
448 
449 	return 1;
450 }
451 
452 /* Unsupported on NetBSD */
453 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
454 {
455 	*read_ahead = DM_READ_AHEAD_NONE;
456 	return 1;
457 }
458 
459 const char *dm_task_get_name(const struct dm_task *dmt)
460 {
461 
462 	return (dmt->dmi.v4->name);
463 }
464 
465 const char *dm_task_get_uuid(const struct dm_task *dmt)
466 {
467 
468 	return (dmt->dmi.v4->uuid);
469 }
470 
471 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
472 {
473 	return (struct dm_deps *) (((void *) dmt->dmi.v4) +
474 				   dmt->dmi.v4->data_start);
475 }
476 
477 struct dm_names *dm_task_get_names(struct dm_task *dmt)
478 {
479 	return (struct dm_names *) (((void *) dmt->dmi.v4) +
480 				    dmt->dmi.v4->data_start);
481 }
482 
483 struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
484 {
485 	return (struct dm_versions *) (((void *) dmt->dmi.v4) +
486 				       dmt->dmi.v4->data_start);
487 }
488 
489 int dm_task_set_ro(struct dm_task *dmt)
490 {
491 	dmt->read_only = 1;
492 	return 1;
493 }
494 
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)
498 {
499 	return 1;
500 }
501 
502 int dm_task_suppress_identical_reload(struct dm_task *dmt)
503 {
504 	dmt->suppress_identical_reload = 1;
505 	return 1;
506 }
507 
508 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
509 {
510 	if (!(dmt->newname = dm_strdup(newname))) {
511 		log_error("dm_task_set_newname: strdup(%s) failed", newname);
512 		return 0;
513 	}
514 
515 	return 1;
516 }
517 
518 int dm_task_set_message(struct dm_task *dmt, const char *message)
519 {
520 	if (!(dmt->message = dm_strdup(message))) {
521 		log_error("dm_task_set_message: strdup(%s) failed", message);
522 		return 0;
523 	}
524 
525 	return 1;
526 }
527 
528 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
529 {
530 	dmt->sector = sector;
531 
532 	return 1;
533 }
534 
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)
538 {
539 	return 0;
540 }
541 
542 int dm_task_no_flush(struct dm_task *dmt)
543 {
544 	dmt->no_flush = 1;
545 
546 	return 1;
547 }
548 
549 int dm_task_no_open_count(struct dm_task *dmt)
550 {
551 	dmt->no_open_count = 1;
552 
553 	return 1;
554 }
555 
556 int dm_task_skip_lockfs(struct dm_task *dmt)
557 {
558 	dmt->skip_lockfs = 1;
559 
560 	return 1;
561 }
562 
563 int dm_task_query_inactive_table(struct dm_task *dmt)
564 {
565 	dmt->query_inactive_table = 1;
566 
567 	return 1;
568 }
569 
570 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
571 {
572 	dmt->event_nr = event_nr;
573 
574 	return 1;
575 }
576 
577 /* Allocate one target(table description) entry. */
578 struct target *create_target(uint64_t start, uint64_t len, const char *type,
579 			     const char *params)
580 {
581 	struct target *t = dm_malloc(sizeof(*t));
582 
583 	if (!t) {
584 		log_error("create_target: malloc(%" PRIsize_t ") failed",
585 			  sizeof(*t));
586 		return NULL;
587 	}
588 
589 	memset(t, 0, sizeof(*t));
590 
591 	if (!(t->params = dm_strdup(params))) {
592 		log_error("create_target: strdup(params) failed");
593 		goto bad;
594 	}
595 
596 	if (!(t->type = dm_strdup(type))) {
597 		log_error("create_target: strdup(type) failed");
598 		goto bad;
599 	}
600 
601 	t->start = start;
602 	t->length = len;
603 	return t;
604 
605       bad:
606 	dm_free(t->params);
607 	dm_free(t->type);
608 	dm_free(t);
609 	return NULL;
610 }
611 
612 /* Parse given dm task structure to proplib dictionary.  */
613 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict)
614 {
615 	prop_array_t cmd_array;
616 	prop_dictionary_t target_spec;
617 
618 	struct target *t;
619 
620 	size_t len;
621 	char type[DM_MAX_TYPE_NAME];
622 
623 	uint32_t major, flags;
624 	int count = 0;
625 	const int (*version)[3];
626 
627 	flags = 0;
628 	version = &_cmd_data_v4[dmt->type].version;
629 
630 	cmd_array = prop_array_create();
631 
632 	for (t = dmt->head; t; t = t->next) {
633 		target_spec = prop_dictionary_create();
634 
635 		prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start);
636 		prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length);
637 
638 		strlcpy(type,t->type,DM_MAX_TYPE_NAME);
639 
640 		prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type);
641 		prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params);
642 
643 		prop_array_set(cmd_array,count,target_spec);
644 
645 		prop_object_release(target_spec);
646 
647 		count++;
648 	}
649 
650 
651 	if (count && (dmt->sector || dmt->message)) {
652 		log_error("targets and message are incompatible");
653 		return -1;
654 	}
655 
656 	if (count && dmt->newname) {
657 		log_error("targets and newname are incompatible");
658 		return -1;
659 	}
660 
661 	if (count && dmt->geometry) {
662 		log_error("targets and geometry are incompatible");
663 		return -1;
664 	}
665 
666 	if (dmt->newname && (dmt->sector || dmt->message)) {
667 		log_error("message and newname are incompatible");
668 		return -1;
669 	}
670 
671 	if (dmt->newname && dmt->geometry) {
672 		log_error("geometry and newname are incompatible");
673 		return -1;
674 	}
675 
676 	if (dmt->geometry && (dmt->sector || dmt->message)) {
677 		log_error("geometry and message are incompatible");
678 		return -1;
679 	}
680 
681 	if (dmt->sector && !dmt->message) {
682 		log_error("message is required with sector");
683 		return -1;
684 	}
685 
686 	if (dmt->newname)
687 		len += strlen(dmt->newname) + 1;
688 
689 	if (dmt->message)
690 		len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1;
691 
692 	if (dmt->geometry)
693 		len += strlen(dmt->geometry) + 1;
694 
695 	nbsd_dmi_add_version((*version), dm_dict);
696 
697 	nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
698 	/*
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.
701 	 */
702 	if (dmt->major != major && dmt->major != -1)
703 		return -1;
704 
705 	if (dmt->minor >= 0) {
706 		flags |= DM_PERSISTENT_DEV_FLAG;
707 
708 		prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor);
709 	}
710 
711 	/* Set values to dictionary. */
712 	if (dmt->dev_name)
713 		prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name);
714 
715 	if (dmt->uuid)
716 		prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid);
717 
718 	if (dmt->type == DM_DEVICE_SUSPEND)
719 		flags |= DM_SUSPEND_FLAG;
720 	if (dmt->no_flush)
721 		flags |= DM_NOFLUSH_FLAG;
722 	if (dmt->read_only)
723 		flags |= DM_READONLY_FLAG;
724 	if (dmt->skip_lockfs)
725 		flags |= DM_SKIP_LOCKFS_FLAG;
726 
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;
732 	}
733 
734 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
735 
736 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr);
737 
738 	if (dmt->newname)
739 		prop_array_set_cstring(cmd_array, 0, dmt->newname);
740 
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);
744 
745 	return 0;
746 }
747 
748 static int _process_mapper_dir(struct dm_task *dmt)
749 {
750 	struct dirent *dirent;
751 	DIR *d;
752 	const char *dir;
753 	int r = 1;
754 
755 	dir = dm_dir();
756 	if (!(d = opendir(dir))) {
757 		log_sys_error("opendir", dir);
758 		return 0;
759 	}
760 
761 	while ((dirent = readdir(d))) {
762 		if (!strcmp(dirent->d_name, ".") ||
763 		    !strcmp(dirent->d_name, "..") ||
764 		    !strcmp(dirent->d_name, "control"))
765 			continue;
766 		dm_task_set_name(dmt, dirent->d_name);
767 		dm_task_run(dmt);
768 	}
769 
770 	if (closedir(d))
771 		log_sys_error("closedir", dir);
772 
773 	return r;
774 }
775 
776 /* Get list of all devices. */
777 static int _process_all_v4(struct dm_task *dmt)
778 {
779 	struct dm_task *task;
780 	struct dm_names *names;
781 	unsigned next = 0;
782 	int r = 1;
783 
784 	if (!(task = dm_task_create(DM_DEVICE_LIST)))
785 		return 0;
786 
787 	if (!dm_task_run(task)) {
788 		r = 0;
789 		goto out;
790 	}
791 
792 	if (!(names = dm_task_get_names(task))) {
793 		r = 0;
794 		goto out;
795 	}
796 
797 	if (!names->dev)
798 		goto out;
799 
800 	do {
801 		names = (void *) names + next;
802 		if (!dm_task_set_name(dmt, names->name)) {
803 			r = 0;
804 			goto out;
805 		}
806 		if (!dm_task_run(dmt))
807 			r = 0;
808 		next = names->next;
809 	} while (next);
810 
811       out:
812 	dm_task_destroy(task);
813 	return r;
814 }
815 
816 static int _mknodes_v4(struct dm_task *dmt)
817 {
818 	(void) _process_mapper_dir(dmt);
819 
820 	return _process_all_v4(dmt);
821 }
822 
823 /* Create new device and load table to it. */
824 static int _create_and_load_v4(struct dm_task *dmt)
825 {
826 	struct dm_task *task;
827 	int r;
828 
829 	printf("create and load called \n");
830 
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");
834 		return 0;
835 	}
836 
837 	/* Copy across relevant fields */
838 	if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
839 		dm_task_destroy(task);
840 		return 0;
841 	}
842 
843 	if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
844 		dm_task_destroy(task);
845 		return 0;
846 	}
847 
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;
853 
854 	r = dm_task_run(task);
855 	dm_task_destroy(task);
856 	if (!r)
857 		return r;
858 
859 	/* Next load the table */
860 	if (!(task = dm_task_create(DM_DEVICE_RELOAD))) {
861 		log_error("Failed to create device-mapper task struct");
862 		return 0;
863 	}
864 
865 	/* Copy across relevant fields */
866 	if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
867 		dm_task_destroy(task);
868 		return 0;
869 	}
870 
871 	task->read_only = dmt->read_only;
872 	task->head = dmt->head;
873 	task->tail = dmt->tail;
874 
875 	r = dm_task_run(task);
876 
877 	task->head = NULL;
878 	task->tail = NULL;
879 	dm_task_destroy(task);
880 	if (!r)
881 		goto revert;
882 
883 	/* Use the original structure last so the info will be correct */
884 	dmt->type = DM_DEVICE_RESUME;
885 	dm_free(dmt->uuid);
886 	dmt->uuid = NULL;
887 
888 	r = dm_task_run(dmt);
889 
890 	if (r)
891 		return r;
892 
893       revert:
894  	dmt->type = DM_DEVICE_REMOVE;
895 	dm_free(dmt->uuid);
896 	dmt->uuid = NULL;
897 
898 	if (!dm_task_run(dmt))
899 		log_error("Failed to revert device creation.");
900 
901 	return r;
902 }
903 
904 uint64_t dm_task_get_existing_table_size(struct dm_task *dmt)
905 {
906 	return dmt->existing_table_size;
907 }
908 
909 static int _reload_with_suppression_v4(struct dm_task *dmt)
910 {
911 	struct dm_task *task;
912 	struct target *t1, *t2;
913 	int r;
914 
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");
918 		return 0;
919 	}
920 
921 	/* Copy across relevant fields */
922 	if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
923 		dm_task_destroy(task);
924 		return 0;
925 	}
926 
927 	if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
928 		dm_task_destroy(task);
929 		return 0;
930 	}
931 
932 	task->major = dmt->major;
933 	task->minor = dmt->minor;
934 
935 	r = dm_task_run(task);
936 
937 	if (!r) {
938 		dm_task_destroy(task);
939 		return r;
940 	}
941 
942 	/* Store existing table size */
943 	t2 = task->head;
944 	while (t2 && t2->next)
945 		t2 = t2->next;
946 	dmt->existing_table_size = t2 ? t2->start + t2->length : 0;
947 
948 	if ((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0 != dmt->read_only)
949 		goto no_match;
950 
951 	t1 = dmt->head;
952 	t2 = task->head;
953 
954 	while (t1 && t2) {
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)))
961 			goto no_match;
962 		t1 = t1->next;
963 		t2 = t2->next;
964 	}
965 
966 	if (!t1 && !t2) {
967 		dmt->dmi.v4 = task->dmi.v4;
968 		task->dmi.v4 = NULL;
969 		dm_task_destroy(task);
970 		return 1;
971 	}
972 
973 no_match:
974 	dm_task_destroy(task);
975 
976 	/* Now do the original reload */
977 	dmt->suppress_identical_reload = 0;
978 	r = dm_task_run(dmt);
979 
980 	return r;
981 }
982 
983 /*
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.
989  */
990 static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command)
991 {
992 	struct dm_ioctl *dmi;
993 	prop_dictionary_t dm_dict_in, dm_dict_out;
994 
995 	uint32_t flags;
996 
997 	dm_dict_in = NULL;
998 
999 	dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1000 	dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */
1001 
1002 	/* Set command name to dictionary */
1003 	prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1004 	    _cmd_data_v4[dmt->type].name);
1005 
1006 	/* Parse dmi from libdevmapper to dictionary */
1007 	if (_flatten(dmt, dm_dict_in) < 0)
1008 		goto bad;
1009 
1010 	prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags);
1011 
1012 	if (dmt->type == DM_DEVICE_TABLE)
1013 		flags |= DM_STATUS_TABLE_FLAG;
1014 
1015 	if (dmt->no_open_count)
1016 		flags |= DM_SKIP_BDGET_FLAG;
1017 
1018 	flags |= DM_EXISTS_FLAG;
1019 
1020 	/* Set flags to dictionary. */
1021 	prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags);
1022 
1023 	prop_dictionary_externalize_to_file(dm_dict_in,"/tmp/test_in");
1024 
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. */
1029 #ifdef RUMP_ACTION
1030 	struct plistref prefp;
1031 	int err;
1032 	prop_dictionary_externalize_to_pref(dm_dict_in, &prefp);
1033 
1034 	if (rump_sys_ioctl(_control_fd, NETBSD_DM_IOCTL, &prefp) != 0) {
1035 
1036 		dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
1037 #else
1038 	if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1039 		NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1040 #endif
1041 		if (errno == ENOENT &&
1042 		    ((dmt->type == DM_DEVICE_INFO) ||
1043 			(dmt->type == DM_DEVICE_MKNODES) ||
1044 			(dmt->type == DM_DEVICE_STATUS))) {
1045 
1046 			/*
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;
1050 			 */
1051 
1052 			dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd);
1053 
1054 			dmi->flags &= ~DM_EXISTS_FLAG;
1055 
1056 			prop_object_release(dm_dict_in);
1057 			prop_object_release(dm_dict_out);
1058 
1059 			goto out;
1060 		} else {
1061 			log_error("ioctl %s call failed with errno %d\n",
1062 					  _cmd_data_v4[dmt->type].name, errno);
1063 
1064 			prop_object_release(dm_dict_in);
1065 			prop_object_release(dm_dict_out);
1066 
1067 			goto bad;
1068 		}
1069 	}
1070 
1071 #ifdef RUMP_ACTION
1072 	dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
1073 #endif
1074 	prop_dictionary_externalize_to_file(dm_dict_out,"/tmp/test_out");
1075 
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);
1078 
1079 	prop_object_release(dm_dict_in);
1080 	prop_object_release(dm_dict_out);
1081 out:
1082 	return dmi;
1083 bad:
1084 	return NULL;
1085 }
1086 
1087 /* Create new edvice nodes in mapper/ dir. */
1088 void dm_task_update_nodes(void)
1089 {
1090 	update_devs();
1091 }
1092 
1093 /* Run dm command which is descirbed in dm_task structure. */
1094 int dm_task_run(struct dm_task *dmt)
1095 {
1096 	struct dm_ioctl *dmi;
1097 	unsigned command;
1098 
1099 	if ((unsigned) dmt->type >=
1100 	    (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) {
1101 		log_error("Internal error: unknown device-mapper task %d",
1102 			  dmt->type);
1103 		return 0;
1104 	}
1105 
1106 	command = _cmd_data_v4[dmt->type].cmd;
1107 
1108 	/* Old-style creation had a table supplied */
1109 	if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1110 		return _create_and_load_v4(dmt);
1111 
1112 	if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name &&
1113 	    !dmt->uuid && dmt->major <= 0)
1114 		return _mknodes_v4(dmt);
1115 
1116 	if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
1117 		return _reload_with_suppression_v4(dmt);
1118 
1119 	if (!_open_control())
1120 		return 0;
1121 
1122 	if (!(dmi = _do_dm_ioctl(dmt, command)))
1123 		return 0;
1124 
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);
1129 		break;
1130 
1131 	case DM_DEVICE_REMOVE:
1132 		/* FIXME Kernel needs to fill in dmi->name */
1133 		if (dmt->dev_name)
1134 			rm_dev_node(dmt->dev_name, 0);
1135 		break;
1136 
1137 	case DM_DEVICE_RENAME:
1138 		/* FIXME Kernel needs to fill in dmi->name */
1139 		if (dmt->dev_name)
1140 			rename_dev_node(dmt->dev_name, dmt->newname, 0);
1141 		break;
1142 
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);
1147 		break;
1148 
1149 	case DM_DEVICE_MKNODES:
1150 		if (dmi->flags & DM_EXISTS_FLAG)
1151 			add_dev_node(dmi->name, MAJOR(dmi->dev),
1152 				     MINOR(dmi->dev),
1153 			    dmt->uid, dmt->gid, dmt->mode, 0);
1154 		else if (dmt->dev_name)
1155 			rm_dev_node(dmt->dev_name, 0);
1156 		break;
1157 
1158 	case DM_DEVICE_STATUS:
1159 	case DM_DEVICE_TABLE:
1160 	case DM_DEVICE_WAITEVENT:
1161 		if (!_unmarshal_status(dmt, dmi))
1162 			goto bad;
1163 		break;
1164 	}
1165 
1166 	/* Was structure reused? */
1167 	if (dmt->dmi.v4)
1168 		dm_free(dmt->dmi.v4);
1169 
1170 	dmt->dmi.v4 = dmi;
1171 	return 1;
1172 
1173       bad:
1174 	dm_free(dmi);
1175 	return 0;
1176 }
1177 
1178 void dm_lib_release(void)
1179 {
1180 	if (_control_fd != -1) {
1181 		close(_control_fd);
1182 		_control_fd = -1;
1183 	}
1184 	update_devs();
1185 }
1186 
1187 void dm_lib_exit(void)
1188 {
1189 	dm_lib_release();
1190 	dm_dump_memory();
1191 	_version_ok = 1;
1192 	_version_checked = 0;
1193 }
1194