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