1 /*      $NetBSD: libdm-nbsd-iface.c,v 1.7 2010/03/12 16:24:40 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 static char *
362 get_dev_name(char *d_name, uint32_t d_major, uint32_t d_minor)
363 {
364 	static char d_buf[MAXPATHLEN];
365 	struct dirent *dire;
366 	struct stat st;
367 	DIR *dev_dir;
368 
369 	int err;
370 	char *name;
371 
372 	dev_dir = opendir("/dev");
373 
374 	while ((dire = readdir(dev_dir)) != NULL) {
375 
376 		if (strstr(dire->d_name, d_name) == NULL)
377 			continue;
378 
379 		snprintf(d_buf, MAXPATHLEN, "/dev/%s", dire->d_name);
380 
381 		if ((err = stat(d_buf, &st)) < 0)
382 			printf("stat failed with %d\n", err);
383 
384 		if (st.st_mode & S_IFBLK){
385 			if ((major(st.st_rdev) == d_major) && (minor(st.st_rdev) == d_minor)) {
386 				strncpy(d_buf, dire->d_name, strlen(dire->d_name) + 1);
387 				name = d_buf;
388 				break;
389 			}
390 		}
391 
392 		memset(d_buf, '0', sizeof(d_buf));
393 	}
394 
395 	(void)closedir(dev_dir);
396 
397 	return name;
398 }
399 
400 /*
401  * @dev_major is major number of char device
402  *
403  * I have to find it's block device number and lookup dev in
404  * device database to find device path.
405  *
406  */
407 
408 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
409 		  uint32_t dev_minor)
410 {
411 	int r;
412 	uint32_t major, dm_major;
413 	char *name;
414 	mode_t mode;
415 	dev_t dev;
416 	size_t val_len,i;
417 	struct kinfo_drivers *kd;
418 
419 	mode = 0;
420 
421 	nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
422 
423 	if (bufsize < 8)
424 		return 0;
425 
426 	if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
427 		printf("sysctlbyname failed");
428 		return 0;
429 	}
430 
431 	if ((kd = malloc (val_len)) == NULL){
432 		printf("malloc kd info error\n");
433 		return 0;
434 	}
435 
436 	if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
437 		printf("sysctlbyname failed kd");
438 		return 0;
439 	}
440 
441 	for (i = 0, val_len /= sizeof(*kd); i < val_len; i++){
442 		if (kd[i].d_cmajor == dev_major) {
443 			major = kd[i].d_bmajor;
444 			break;
445 		}
446 	}
447 
448 	dev = MKDEV(major,dev_minor);
449 
450 	mode |= S_IFBLK;
451 
452 	if ((name = devname(dev,mode)) == NULL)
453 		name = get_dev_name(kd[i].d_name, major, dev_minor);
454 
455 	r = snprintf(buf, (size_t) bufsize, "/dev/%s",name);
456 
457 	free(kd);
458 
459 	if (r < 0 || r > bufsize - 1 || name == NULL)
460 		return 0;
461 
462 	return 1;
463 }
464 
465 /* Fill info from dm_ioctl structure. Look at DM_EXISTS_FLAG*/
466 int dm_task_get_info(struct dm_task *dmt, struct dm_info *info)
467 {
468 	if (!dmt->dmi.v4)
469 		return 0;
470 
471 	memset(info, 0, sizeof(*info));
472 
473 	info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
474 	if (!info->exists)
475 		return 1;
476 
477 	info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0;
478 	info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0;
479 	info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0;
480 	info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ?
481 	    1 : 0;
482 	info->target_count = dmt->dmi.v4->target_count;
483 	info->open_count = dmt->dmi.v4->open_count;
484 	info->event_nr = dmt->dmi.v4->event_nr;
485 
486 	nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */
487 	info->minor = MINOR(dmt->dmi.v4->dev);
488 
489 	return 1;
490 }
491 
492 /* Unsupported on NetBSD */
493 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
494 {
495 	*read_ahead = DM_READ_AHEAD_NONE;
496 	return 1;
497 }
498 
499 const char *dm_task_get_name(const struct dm_task *dmt)
500 {
501 
502 	return (dmt->dmi.v4->name);
503 }
504 
505 const char *dm_task_get_uuid(const struct dm_task *dmt)
506 {
507 
508 	return (dmt->dmi.v4->uuid);
509 }
510 
511 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
512 {
513 	return (struct dm_deps *) (((void *) dmt->dmi.v4) +
514 				   dmt->dmi.v4->data_start);
515 }
516 
517 struct dm_names *dm_task_get_names(struct dm_task *dmt)
518 {
519 	return (struct dm_names *) (((void *) dmt->dmi.v4) +
520 				    dmt->dmi.v4->data_start);
521 }
522 
523 struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
524 {
525 	return (struct dm_versions *) (((void *) dmt->dmi.v4) +
526 				       dmt->dmi.v4->data_start);
527 }
528 
529 int dm_task_set_ro(struct dm_task *dmt)
530 {
531 	dmt->read_only = 1;
532 	return 1;
533 }
534 
535 /* Unsupported on NetBSD */
536 int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
537 			   uint32_t read_ahead_flags)
538 {
539 	return 1;
540 }
541 
542 int dm_task_suppress_identical_reload(struct dm_task *dmt)
543 {
544 	dmt->suppress_identical_reload = 1;
545 	return 1;
546 }
547 
548 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
549 {
550 	if (!(dmt->newname = dm_strdup(newname))) {
551 		log_error("dm_task_set_newname: strdup(%s) failed", newname);
552 		return 0;
553 	}
554 
555 	return 1;
556 }
557 
558 int dm_task_set_message(struct dm_task *dmt, const char *message)
559 {
560 	if (!(dmt->message = dm_strdup(message))) {
561 		log_error("dm_task_set_message: strdup(%s) failed", message);
562 		return 0;
563 	}
564 
565 	return 1;
566 }
567 
568 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
569 {
570 	dmt->sector = sector;
571 
572 	return 1;
573 }
574 
575 /* Unsupported in NetBSD */
576 int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders,
577     const char *heads, const char *sectors, const char *start)
578 {
579 	return 0;
580 }
581 
582 int dm_task_no_flush(struct dm_task *dmt)
583 {
584 	dmt->no_flush = 1;
585 
586 	return 1;
587 }
588 
589 int dm_task_no_open_count(struct dm_task *dmt)
590 {
591 	dmt->no_open_count = 1;
592 
593 	return 1;
594 }
595 
596 int dm_task_skip_lockfs(struct dm_task *dmt)
597 {
598 	dmt->skip_lockfs = 1;
599 
600 	return 1;
601 }
602 
603 int dm_task_query_inactive_table(struct dm_task *dmt)
604 {
605 	dmt->query_inactive_table = 1;
606 
607 	return 1;
608 }
609 
610 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
611 {
612 	dmt->event_nr = event_nr;
613 
614 	return 1;
615 }
616 
617 /* Allocate one target(table description) entry. */
618 struct target *create_target(uint64_t start, uint64_t len, const char *type,
619 			     const char *params)
620 {
621 	struct target *t = dm_malloc(sizeof(*t));
622 
623 	if (!t) {
624 		log_error("create_target: malloc(%" PRIsize_t ") failed",
625 			  sizeof(*t));
626 		return NULL;
627 	}
628 
629 	memset(t, 0, sizeof(*t));
630 
631 	if (!(t->params = dm_strdup(params))) {
632 		log_error("create_target: strdup(params) failed");
633 		goto bad;
634 	}
635 
636 	if (!(t->type = dm_strdup(type))) {
637 		log_error("create_target: strdup(type) failed");
638 		goto bad;
639 	}
640 
641 	t->start = start;
642 	t->length = len;
643 	return t;
644 
645       bad:
646 	dm_free(t->params);
647 	dm_free(t->type);
648 	dm_free(t);
649 	return NULL;
650 }
651 
652 /* Parse given dm task structure to proplib dictionary.  */
653 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict)
654 {
655 	prop_array_t cmd_array;
656 	prop_dictionary_t target_spec;
657 
658 	struct target *t;
659 
660 	size_t len;
661 	char type[DM_MAX_TYPE_NAME];
662 
663 	uint32_t major, flags;
664 	int count = 0;
665 	const int (*version)[3];
666 
667 	flags = 0;
668 	version = &_cmd_data_v4[dmt->type].version;
669 
670 	cmd_array = prop_array_create();
671 
672 	for (t = dmt->head; t; t = t->next) {
673 		target_spec = prop_dictionary_create();
674 
675 		prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start);
676 		prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length);
677 
678 		strlcpy(type,t->type,DM_MAX_TYPE_NAME);
679 
680 		prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type);
681 		prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params);
682 
683 		prop_array_set(cmd_array,count,target_spec);
684 
685 		prop_object_release(target_spec);
686 
687 		count++;
688 	}
689 
690 
691 	if (count && (dmt->sector || dmt->message)) {
692 		log_error("targets and message are incompatible");
693 		return -1;
694 	}
695 
696 	if (count && dmt->newname) {
697 		log_error("targets and newname are incompatible");
698 		return -1;
699 	}
700 
701 	if (count && dmt->geometry) {
702 		log_error("targets and geometry are incompatible");
703 		return -1;
704 	}
705 
706 	if (dmt->newname && (dmt->sector || dmt->message)) {
707 		log_error("message and newname are incompatible");
708 		return -1;
709 	}
710 
711 	if (dmt->newname && dmt->geometry) {
712 		log_error("geometry and newname are incompatible");
713 		return -1;
714 	}
715 
716 	if (dmt->geometry && (dmt->sector || dmt->message)) {
717 		log_error("geometry and message are incompatible");
718 		return -1;
719 	}
720 
721 	if (dmt->sector && !dmt->message) {
722 		log_error("message is required with sector");
723 		return -1;
724 	}
725 
726 	if (dmt->newname)
727 		len += strlen(dmt->newname) + 1;
728 
729 	if (dmt->message)
730 		len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1;
731 
732 	if (dmt->geometry)
733 		len += strlen(dmt->geometry) + 1;
734 
735 	nbsd_dmi_add_version((*version), dm_dict);
736 
737 	nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
738 	/*
739 	 * Only devices with major which is equal to netbsd dm major
740 	 * dm devices in NetBSD can't have more majors then one assigned to dm.
741 	 */
742 	if (dmt->major != major && dmt->major != -1)
743 		return -1;
744 
745 	if (dmt->minor >= 0) {
746 		flags |= DM_PERSISTENT_DEV_FLAG;
747 
748 		prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor);
749 	}
750 
751 	/* Set values to dictionary. */
752 	if (dmt->dev_name)
753 		prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name);
754 
755 	if (dmt->uuid)
756 		prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid);
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 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
775 
776 	prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr);
777 
778 	if (dmt->newname)
779 		prop_array_set_cstring(cmd_array, 0, dmt->newname);
780 
781 	/* Add array for all COMMAND specific data. */
782 	prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
783 	prop_object_release(cmd_array);
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 	prop_dictionary_t dm_dict_in, dm_dict_out;
1034 
1035 	uint32_t flags;
1036 
1037 	dm_dict_in = NULL;
1038 
1039 	dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1040 	dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */
1041 
1042 	/* Set command name to dictionary */
1043 	prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1044 	    _cmd_data_v4[dmt->type].name);
1045 
1046 	/* Parse dmi from libdevmapper to dictionary */
1047 	if (_flatten(dmt, dm_dict_in) < 0)
1048 		goto bad;
1049 
1050 	prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags);
1051 
1052 	if (dmt->type == DM_DEVICE_TABLE)
1053 		flags |= DM_STATUS_TABLE_FLAG;
1054 
1055 	if (dmt->no_open_count)
1056 		flags |= DM_SKIP_BDGET_FLAG;
1057 
1058 	flags |= DM_EXISTS_FLAG;
1059 
1060 	/* Set flags to dictionary. */
1061 	prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags);
1062 
1063 	prop_dictionary_externalize_to_file(dm_dict_in,"/tmp/test_in");
1064 
1065 	log_very_verbose("Ioctl type  %s --- flags %d",_cmd_data_v4[dmt->type].name,flags);
1066 	//printf("name %s, major %d minor %d\n uuid %s\n",
1067         //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt));
1068 	/* Send dictionary to kernel and wait for reply. */
1069 #ifdef RUMP_ACTION
1070 	struct plistref prefp;
1071 	int err;
1072 	prop_dictionary_externalize_to_pref(dm_dict_in, &prefp);
1073 
1074 	if (rump_sys_ioctl(_control_fd, NETBSD_DM_IOCTL, &prefp) != 0) {
1075 
1076 		dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
1077 #else
1078 	if (prop_dictionary_sendrecv_ioctl(dm_dict_in,_control_fd,
1079 		NETBSD_DM_IOCTL,&dm_dict_out) != 0) {
1080 #endif
1081 		if (errno == ENOENT &&
1082 		    ((dmt->type == DM_DEVICE_INFO) ||
1083 			(dmt->type == DM_DEVICE_MKNODES) ||
1084 			(dmt->type == DM_DEVICE_STATUS))) {
1085 
1086 			/*
1087 			 * Linux version doesn't fail when ENOENT is returned
1088 			 * for nonexisting device after info, deps, mknodes call.
1089 			 * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0;
1090 			 */
1091 
1092 			dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd);
1093 
1094 			dmi->flags &= ~DM_EXISTS_FLAG;
1095 
1096 			prop_object_release(dm_dict_in);
1097 			prop_object_release(dm_dict_out);
1098 
1099 			goto out;
1100 		} else {
1101 			log_error("ioctl %s call failed with errno %d\n",
1102 					  _cmd_data_v4[dmt->type].name, errno);
1103 
1104 			prop_object_release(dm_dict_in);
1105 			prop_object_release(dm_dict_out);
1106 
1107 			goto bad;
1108 		}
1109 	}
1110 
1111 #ifdef RUMP_ACTION
1112 	dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
1113 #endif
1114 	prop_dictionary_externalize_to_file(dm_dict_out,"/tmp/test_out");
1115 
1116 	/* Parse kernel dictionary to dmi structure and return it to libdevmapper. */
1117 	dmi = nbsd_dm_dict_to_dmi(dm_dict_out,_cmd_data_v4[dmt->type].cmd);
1118 
1119 	prop_object_release(dm_dict_in);
1120 	prop_object_release(dm_dict_out);
1121 out:
1122 	return dmi;
1123 bad:
1124 	return NULL;
1125 }
1126 
1127 /* Create new edvice nodes in mapper/ dir. */
1128 void dm_task_update_nodes(void)
1129 {
1130 	update_devs();
1131 }
1132 
1133 /* Run dm command which is descirbed in dm_task structure. */
1134 int dm_task_run(struct dm_task *dmt)
1135 {
1136 	struct dm_ioctl *dmi;
1137 	unsigned command;
1138 
1139 	if ((unsigned) dmt->type >=
1140 	    (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) {
1141 		log_error("Internal error: unknown device-mapper task %d",
1142 			  dmt->type);
1143 		return 0;
1144 	}
1145 
1146 	command = _cmd_data_v4[dmt->type].cmd;
1147 
1148 	/* Old-style creation had a table supplied */
1149 	if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1150 		return _create_and_load_v4(dmt);
1151 
1152 	if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name &&
1153 	    !dmt->uuid && dmt->major <= 0)
1154 		return _mknodes_v4(dmt);
1155 
1156 	if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
1157 		return _reload_with_suppression_v4(dmt);
1158 
1159 	if (!_open_control())
1160 		return 0;
1161 
1162 	if (!(dmi = _do_dm_ioctl(dmt, command)))
1163 		return 0;
1164 
1165 	switch (dmt->type) {
1166 	case DM_DEVICE_CREATE:
1167 		add_dev_node(dmt->dev_name, MAJOR(dmi->dev), MINOR(dmi->dev),
1168 		    dmt->uid, dmt->gid, dmt->mode, 0);
1169 		break;
1170 
1171 	case DM_DEVICE_REMOVE:
1172 		/* FIXME Kernel needs to fill in dmi->name */
1173 		if (dmt->dev_name)
1174 			rm_dev_node(dmt->dev_name, 0);
1175 		break;
1176 
1177 	case DM_DEVICE_RENAME:
1178 		/* FIXME Kernel needs to fill in dmi->name */
1179 		if (dmt->dev_name)
1180 			rename_dev_node(dmt->dev_name, dmt->newname, 0);
1181 		break;
1182 
1183 	case DM_DEVICE_RESUME:
1184 		/* FIXME Kernel needs to fill in dmi->name */
1185 		set_dev_node_read_ahead(dmt->dev_name, dmt->read_ahead,
1186 					dmt->read_ahead_flags);
1187 		break;
1188 
1189 	case DM_DEVICE_MKNODES:
1190 		if (dmi->flags & DM_EXISTS_FLAG)
1191 			add_dev_node(dmi->name, MAJOR(dmi->dev),
1192 				     MINOR(dmi->dev),
1193 			    dmt->uid, dmt->gid, dmt->mode, 0);
1194 		else if (dmt->dev_name)
1195 			rm_dev_node(dmt->dev_name, 0);
1196 		break;
1197 
1198 	case DM_DEVICE_STATUS:
1199 	case DM_DEVICE_TABLE:
1200 	case DM_DEVICE_WAITEVENT:
1201 		if (!_unmarshal_status(dmt, dmi))
1202 			goto bad;
1203 		break;
1204 	}
1205 
1206 	/* Was structure reused? */
1207 	if (dmt->dmi.v4)
1208 		dm_free(dmt->dmi.v4);
1209 
1210 	dmt->dmi.v4 = dmi;
1211 	return 1;
1212 
1213       bad:
1214 	dm_free(dmi);
1215 	return 0;
1216 }
1217 
1218 void dm_lib_release(void)
1219 {
1220 	if (_control_fd != -1) {
1221 		close(_control_fd);
1222 		_control_fd = -1;
1223 	}
1224 	update_devs();
1225 }
1226 
1227 void dm_lib_exit(void)
1228 {
1229 	dm_lib_release();
1230 	dm_dump_memory();
1231 	_version_ok = 1;
1232 	_version_checked = 0;
1233 }
1234