Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / ioctl / libdm-nbsd-iface.c
blob9faf11bbcb1bc82fcbe9befd94a992b7eb7ad1f6
1 /* $NetBSD: libdm-nbsd-iface.c,v 1.5 2009/12/05 11:42:24 haad Exp $ */
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.
8 * This file is part of the device-mapper userspace tools.
10 * This copyrighted material is made available to anyone wishing to use,
11 * modify, copy, or redistribute it subject to the terms and conditions
12 * of the GNU Lesser General Public License v.2.1.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "dmlib.h"
20 #include "libdm-targets.h"
21 #include "libdm-common.h"
22 #include "libdm-netbsd.h"
24 #include <sys/ioctl.h>
25 #include <sys/sysctl.h>
27 #include <fcntl.h>
28 #include <dirent.h>
29 #include <limits.h>
31 #include <dev/dm/netbsd-dm.h>
33 #include <dm-ioctl.h>
35 #ifdef RUMP_ACTION
36 #include <rump/rump.h>
37 #include <rump/rump_syscalls.h>
38 #endif
41 * Ensure build compatibility.
42 * The hard-coded versions here are the highest present
43 * in the _cmd_data arrays.
46 #if !((DM_VERSION_MAJOR == 1 && DM_VERSION_MINOR >= 0) || \
47 (DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 0))
48 #error The version of dm-ioctl.h included is incompatible.
49 #endif
51 /* dm major version no for running kernel */
52 static unsigned _dm_version_minor = 0;
53 static unsigned _dm_version_patchlevel = 0;
55 static int _control_fd = -1;
56 static int _version_checked = 0;
57 static int _version_ok = 1;
58 static unsigned _ioctl_buffer_double_factor = 0;
60 /* *INDENT-OFF* */
63 * XXX Remove this structure and write another own one
64 * I don't understand why ioctl calls has different
65 * names then dm task type
67 static struct cmd_data _cmd_data_v4[] = {
68 {"create", DM_DEV_CREATE, {4, 0, 0}},
69 {"reload", DM_TABLE_LOAD, {4, 0, 0}}, /* DM_DEVICE_RELOAD */
70 {"remove", DM_DEV_REMOVE, {4, 0, 0}},
71 {"remove_all", DM_REMOVE_ALL, {4, 0, 0}},
72 {"suspend", DM_DEV_SUSPEND, {4, 0, 0}},
73 {"resume", DM_DEV_SUSPEND, {4, 0, 0}},
74 {"info", DM_DEV_STATUS, {4, 0, 0}},
75 {"deps", DM_TABLE_DEPS, {4, 0, 0}}, /* DM_DEVICE_DEPS */
76 {"rename", DM_DEV_RENAME, {4, 0, 0}},
77 {"version", DM_VERSION, {4, 0, 0}},
78 {"status", DM_TABLE_STATUS, {4, 0, 0}},
79 {"table", DM_TABLE_STATUS, {4, 0, 0}}, /* DM_DEVICE_TABLE */
80 {"waitevent", DM_DEV_WAIT, {4, 0, 0}},
81 {"names", DM_LIST_DEVICES, {4, 0, 0}},
82 {"clear", DM_TABLE_CLEAR, {4, 0, 0}},
83 {"mknodes", DM_DEV_STATUS, {4, 0, 0}},
84 #ifdef DM_LIST_VERSIONS
85 {"targets", DM_LIST_VERSIONS, {4, 1, 0}},
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
94 /* *INDENT-ON* */
97 * In NetBSD we use sysctl to get kernel drivers info. control device
98 * has predefined minor number 0 and major number = char major number
99 * of dm driver. First slot is therefore ocupied with control device
100 * and minor device starts from 1;
103 static int _control_device_number(uint32_t *major, uint32_t *minor)
106 nbsd_get_dm_major(major, DM_CHAR_MAJOR);
108 *minor = 0;
110 return 1;
114 * Returns 1 if exists; 0 if it doesn't; -1 if it's wrong
116 static int _control_exists(const char *control, uint32_t major, uint32_t minor)
118 struct stat buf;
120 if (stat(control, &buf) < 0) {
121 if (errno != ENOENT)
122 log_sys_error("stat", control);
123 return 0;
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;
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;
145 return 1;
148 static int _create_control(const char *control, uint32_t major, uint32_t minor)
150 int ret;
151 mode_t old_umask;
153 if (!major)
154 return 0;
156 old_umask = umask(0022);
157 ret = dm_create_dir(dm_dir());
158 umask(old_umask);
160 if (!ret)
161 return 0;
163 log_verbose("Creating device %s (%u, %u)", control, major, minor);
165 if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR,
166 MKDEV(major, minor)) < 0) {
167 log_sys_error("mknod", control);
168 return 0;
172 return 1;
175 /* Check if major is device-mapper block device major number */
176 int dm_is_dm_major(uint32_t major)
178 uint32_t dm_major;
180 nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
182 if (major == dm_major)
183 return 1;
185 return 0;
188 /* Open control device if doesn't exist create it. */
189 static int _open_control(void)
191 char control[PATH_MAX];
192 uint32_t major = 0, minor = 0;
194 if (_control_fd != -1)
195 return 1;
197 #ifdef RUMP_ACTION
198 rump_init();
199 #endif
200 snprintf(control, sizeof(control), "%s/control", dm_dir());
202 if (!_control_device_number(&major, &minor))
203 log_error("Is device-mapper driver missing from kernel?");
205 if (!_control_exists(control, major, minor) &&
206 !_create_control(control, major, minor))
207 goto error;
209 if ((_control_fd = open(control, O_RDWR)) < 0) {
210 log_sys_error("open", control);
211 goto error;
214 return 1;
216 error:
217 log_error("Failure to communicate with kernel device-mapper driver.");
218 return 0;
222 * Destroy dm task structure there are some dynamically alocated values there.
223 * name, uuid, head, tail list.
225 void dm_task_destroy(struct dm_task *dmt)
227 struct target *t, *n;
229 for (t = dmt->head; t; t = n) {
230 n = t->next;
231 dm_free(t->params);
232 dm_free(t->type);
233 dm_free(t);
236 if (dmt->dev_name)
237 dm_free(dmt->dev_name);
239 if (dmt->newname)
240 dm_free(dmt->newname);
242 if (dmt->message)
243 dm_free(dmt->message);
245 if (dmt->dmi.v4)
246 dm_free(dmt->dmi.v4);
248 if (dmt->uuid)
249 dm_free(dmt->uuid);
251 dm_free(dmt);
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)
258 unsigned *v;
260 if (!dmt->dmi.v4) {
261 version[0] = '\0';
262 return 0;
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];
270 return 1;
273 /* Get kernel driver protocol version and comapre it with library version. */
274 static int _check_version(char *version, size_t size)
276 struct dm_task *task;
277 int r;
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;
285 r = dm_task_run(task);
286 dm_task_get_driver_version(task, version, size);
287 dm_task_destroy(task);
289 return r;
293 * Find out device-mapper's major version number the first time
294 * this is called and whether or not we support it.
296 int dm_check_version(void)
298 char dmversion[64];
300 if (_version_checked)
301 return _version_ok;
303 _version_checked = 1;
305 if (_check_version(dmversion, sizeof(dmversion)))
306 return 1;
309 return 0;
312 int dm_cookie_supported(void)
314 return (0);
317 /* Get next target(table description) from list pointed by dmt->head. */
318 void *dm_get_next_target(struct dm_task *dmt, void *next,
319 uint64_t *start, uint64_t *length,
320 char **target_type, char **params)
322 struct target *t = (struct target *) next;
324 if (!t)
325 t = dmt->head;
327 if (!t)
328 return NULL;
330 *start = t->start;
331 *length = t->length;
332 *target_type = t->type;
333 *params = t->params;
335 return t->next;
338 /* Unmarshall the target info returned from a status call */
339 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
341 char *outbuf = (char *) dmi + dmi->data_start;
342 char *outptr = outbuf;
343 uint32_t i;
344 struct dm_target_spec *spec;
346 for (i = 0; i < dmi->target_count; i++) {
347 spec = (struct dm_target_spec *) outptr;
348 if (!dm_task_add_target(dmt, spec->sector_start,
349 spec->length,
350 spec->target_type,
351 outptr + sizeof(*spec))) {
352 return 0;
355 outptr = outbuf + spec->next;
358 return 1;
362 * @dev_major is major number of char device
364 * I have to find it's block device number and lookup dev in
365 * device database to find device path.
369 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
370 uint32_t dev_minor)
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;
380 mode = 0;
382 nbsd_get_dm_major(&dm_major, DM_BLOCK_MAJOR);
384 if (bufsize < 8)
385 return 0;
387 if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
388 printf("sysctlbyname failed");
389 return 0;
392 if ((kd = malloc (val_len)) == NULL){
393 printf("malloc kd info error\n");
394 return 0;
397 if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
398 printf("sysctlbyname failed kd");
399 return 0;
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;
409 dev = MKDEV(major,dev_minor);
411 mode |= S_IFBLK;
413 name = devname(dev,mode);
415 r = snprintf(buf, (size_t) bufsize, "/dev/%s",name);
417 free(kd);
419 if (r < 0 || r > bufsize - 1 || name == NULL)
420 return 0;
422 return 1;
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)
428 if (!dmt->dmi.v4)
429 return 0;
431 memset(info, 0, sizeof(*info));
433 info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
434 if (!info->exists)
435 return 1;
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;
446 nbsd_get_dm_major(&info->major, DM_BLOCK_MAJOR); /* get netbsd dm device major number */
447 info->minor = MINOR(dmt->dmi.v4->dev);
449 return 1;
452 /* Unsupported on NetBSD */
453 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
455 *read_ahead = DM_READ_AHEAD_NONE;
456 return 1;
459 const char *dm_task_get_name(const struct dm_task *dmt)
462 return (dmt->dmi.v4->name);
465 const char *dm_task_get_uuid(const struct dm_task *dmt)
468 return (dmt->dmi.v4->uuid);
471 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
473 return (struct dm_deps *) (((void *) dmt->dmi.v4) +
474 dmt->dmi.v4->data_start);
477 struct dm_names *dm_task_get_names(struct dm_task *dmt)
479 return (struct dm_names *) (((void *) dmt->dmi.v4) +
480 dmt->dmi.v4->data_start);
483 struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
485 return (struct dm_versions *) (((void *) dmt->dmi.v4) +
486 dmt->dmi.v4->data_start);
489 int dm_task_set_ro(struct dm_task *dmt)
491 dmt->read_only = 1;
492 return 1;
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)
499 return 1;
502 int dm_task_suppress_identical_reload(struct dm_task *dmt)
504 dmt->suppress_identical_reload = 1;
505 return 1;
508 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
510 if (!(dmt->newname = dm_strdup(newname))) {
511 log_error("dm_task_set_newname: strdup(%s) failed", newname);
512 return 0;
515 return 1;
518 int dm_task_set_message(struct dm_task *dmt, const char *message)
520 if (!(dmt->message = dm_strdup(message))) {
521 log_error("dm_task_set_message: strdup(%s) failed", message);
522 return 0;
525 return 1;
528 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
530 dmt->sector = sector;
532 return 1;
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)
539 return 0;
542 int dm_task_no_flush(struct dm_task *dmt)
544 dmt->no_flush = 1;
546 return 1;
549 int dm_task_no_open_count(struct dm_task *dmt)
551 dmt->no_open_count = 1;
553 return 1;
556 int dm_task_skip_lockfs(struct dm_task *dmt)
558 dmt->skip_lockfs = 1;
560 return 1;
563 int dm_task_query_inactive_table(struct dm_task *dmt)
565 dmt->query_inactive_table = 1;
567 return 1;
570 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
572 dmt->event_nr = event_nr;
574 return 1;
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)
581 struct target *t = dm_malloc(sizeof(*t));
583 if (!t) {
584 log_error("create_target: malloc(%" PRIsize_t ") failed",
585 sizeof(*t));
586 return NULL;
589 memset(t, 0, sizeof(*t));
591 if (!(t->params = dm_strdup(params))) {
592 log_error("create_target: strdup(params) failed");
593 goto bad;
596 if (!(t->type = dm_strdup(type))) {
597 log_error("create_target: strdup(type) failed");
598 goto bad;
601 t->start = start;
602 t->length = len;
603 return t;
605 bad:
606 dm_free(t->params);
607 dm_free(t->type);
608 dm_free(t);
609 return NULL;
612 /* Parse given dm task structure to proplib dictionary. */
613 static int _flatten(struct dm_task *dmt, prop_dictionary_t dm_dict)
615 prop_array_t cmd_array;
616 prop_dictionary_t target_spec;
618 struct target *t;
620 size_t len;
621 char type[DM_MAX_TYPE_NAME];
623 uint32_t major, flags;
624 int count = 0;
625 const int (*version)[3];
627 flags = 0;
628 version = &_cmd_data_v4[dmt->type].version;
630 cmd_array = prop_array_create();
632 for (t = dmt->head; t; t = t->next) {
633 target_spec = prop_dictionary_create();
635 prop_dictionary_set_uint64(target_spec,DM_TABLE_START,t->start);
636 prop_dictionary_set_uint64(target_spec,DM_TABLE_LENGTH,t->length);
638 strlcpy(type,t->type,DM_MAX_TYPE_NAME);
640 prop_dictionary_set_cstring(target_spec,DM_TABLE_TYPE,type);
641 prop_dictionary_set_cstring(target_spec,DM_TABLE_PARAMS,t->params);
643 prop_array_set(cmd_array,count,target_spec);
645 prop_object_release(target_spec);
647 count++;
651 if (count && (dmt->sector || dmt->message)) {
652 log_error("targets and message are incompatible");
653 return -1;
656 if (count && dmt->newname) {
657 log_error("targets and newname are incompatible");
658 return -1;
661 if (count && dmt->geometry) {
662 log_error("targets and geometry are incompatible");
663 return -1;
666 if (dmt->newname && (dmt->sector || dmt->message)) {
667 log_error("message and newname are incompatible");
668 return -1;
671 if (dmt->newname && dmt->geometry) {
672 log_error("geometry and newname are incompatible");
673 return -1;
676 if (dmt->geometry && (dmt->sector || dmt->message)) {
677 log_error("geometry and message are incompatible");
678 return -1;
681 if (dmt->sector && !dmt->message) {
682 log_error("message is required with sector");
683 return -1;
686 if (dmt->newname)
687 len += strlen(dmt->newname) + 1;
689 if (dmt->message)
690 len += sizeof(struct dm_target_msg) + strlen(dmt->message) + 1;
692 if (dmt->geometry)
693 len += strlen(dmt->geometry) + 1;
695 nbsd_dmi_add_version((*version), dm_dict);
697 nbsd_get_dm_major(&major, DM_BLOCK_MAJOR);
699 * Only devices with major which is equal to netbsd dm major
700 * dm devices in NetBSD can't have more majors then one assigned to dm.
702 if (dmt->major != major && dmt->major != -1)
703 return -1;
705 if (dmt->minor >= 0) {
706 flags |= DM_PERSISTENT_DEV_FLAG;
708 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_MINOR, dmt->minor);
711 /* Set values to dictionary. */
712 if (dmt->dev_name)
713 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_NAME, dmt->dev_name);
715 if (dmt->uuid)
716 prop_dictionary_set_cstring(dm_dict, DM_IOCTL_UUID, dmt->uuid);
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;
727 if (dmt->query_inactive_table) {
728 if (_dm_version_minor < 16)
729 log_warn("WARNING: Inactive table query unsupported "
730 "by kernel. It will use live table.");
731 flags |= DM_QUERY_INACTIVE_TABLE_FLAG;
734 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_FLAGS, flags);
736 prop_dictionary_set_uint32(dm_dict, DM_IOCTL_EVENT, dmt->event_nr);
738 if (dmt->newname)
739 prop_array_set_cstring(cmd_array, 0, dmt->newname);
741 /* Add array for all COMMAND specific data. */
742 prop_dictionary_set(dm_dict, DM_IOCTL_CMD_DATA, cmd_array);
743 prop_object_release(cmd_array);
745 return 0;
748 static int _process_mapper_dir(struct dm_task *dmt)
750 struct dirent *dirent;
751 DIR *d;
752 const char *dir;
753 int r = 1;
755 dir = dm_dir();
756 if (!(d = opendir(dir))) {
757 log_sys_error("opendir", dir);
758 return 0;
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);
770 if (closedir(d))
771 log_sys_error("closedir", dir);
773 return r;
776 /* Get list of all devices. */
777 static int _process_all_v4(struct dm_task *dmt)
779 struct dm_task *task;
780 struct dm_names *names;
781 unsigned next = 0;
782 int r = 1;
784 if (!(task = dm_task_create(DM_DEVICE_LIST)))
785 return 0;
787 if (!dm_task_run(task)) {
788 r = 0;
789 goto out;
792 if (!(names = dm_task_get_names(task))) {
793 r = 0;
794 goto out;
797 if (!names->dev)
798 goto out;
800 do {
801 names = (void *) names + next;
802 if (!dm_task_set_name(dmt, names->name)) {
803 r = 0;
804 goto out;
806 if (!dm_task_run(dmt))
807 r = 0;
808 next = names->next;
809 } while (next);
811 out:
812 dm_task_destroy(task);
813 return r;
816 static int _mknodes_v4(struct dm_task *dmt)
818 (void) _process_mapper_dir(dmt);
820 return _process_all_v4(dmt);
823 /* Create new device and load table to it. */
824 static int _create_and_load_v4(struct dm_task *dmt)
826 struct dm_task *task;
827 int r;
829 printf("create and load called \n");
831 /* Use new task struct to create the device */
832 if (!(task = dm_task_create(DM_DEVICE_CREATE))) {
833 log_error("Failed to create device-mapper task struct");
834 return 0;
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;
843 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
844 dm_task_destroy(task);
845 return 0;
848 task->major = dmt->major;
849 task->minor = dmt->minor;
850 task->uid = dmt->uid;
851 task->gid = dmt->gid;
852 task->mode = dmt->mode;
854 r = dm_task_run(task);
855 dm_task_destroy(task);
856 if (!r)
857 return r;
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;
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;
871 task->read_only = dmt->read_only;
872 task->head = dmt->head;
873 task->tail = dmt->tail;
875 r = dm_task_run(task);
877 task->head = NULL;
878 task->tail = NULL;
879 dm_task_destroy(task);
880 if (!r)
881 goto revert;
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;
888 r = dm_task_run(dmt);
890 if (r)
891 return r;
893 revert:
894 dmt->type = DM_DEVICE_REMOVE;
895 dm_free(dmt->uuid);
896 dmt->uuid = NULL;
898 if (!dm_task_run(dmt))
899 log_error("Failed to revert device creation.");
901 return r;
904 uint64_t dm_task_get_existing_table_size(struct dm_task *dmt)
906 return dmt->existing_table_size;
909 static int _reload_with_suppression_v4(struct dm_task *dmt)
911 struct dm_task *task;
912 struct target *t1, *t2;
913 int r;
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;
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;
927 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
928 dm_task_destroy(task);
929 return 0;
932 task->major = dmt->major;
933 task->minor = dmt->minor;
935 r = dm_task_run(task);
937 if (!r) {
938 dm_task_destroy(task);
939 return r;
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;
948 if ((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0 != dmt->read_only)
949 goto no_match;
951 t1 = dmt->head;
952 t2 = task->head;
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;
966 if (!t1 && !t2) {
967 dmt->dmi.v4 = task->dmi.v4;
968 task->dmi.v4 = NULL;
969 dm_task_destroy(task);
970 return 1;
973 no_match:
974 dm_task_destroy(task);
976 /* Now do the original reload */
977 dmt->suppress_identical_reload = 0;
978 r = dm_task_run(dmt);
980 return r;
984 * This function is heart of NetBSD libdevmapper-> device-mapper kernel protocol
985 * It creates proplib_dictionary from dm task structure and sends it to NetBSD
986 * kernel driver. After succesfull ioctl it create dmi structure from returned
987 * proplib dictionary. This way I keep number of changes in NetBSD version of
988 * libdevmapper as small as posible.
990 static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command)
992 struct dm_ioctl *dmi;
993 prop_dictionary_t dm_dict_in, dm_dict_out;
995 uint32_t flags;
997 dm_dict_in = NULL;
999 dm_dict_in = prop_dictionary_create(); /* Dictionary send to kernel */
1000 dm_dict_out = prop_dictionary_create(); /* Dictionary received from kernel */
1002 /* Set command name to dictionary */
1003 prop_dictionary_set_cstring(dm_dict_in, DM_IOCTL_COMMAND,
1004 _cmd_data_v4[dmt->type].name);
1006 /* Parse dmi from libdevmapper to dictionary */
1007 if (_flatten(dmt, dm_dict_in) < 0)
1008 goto bad;
1010 prop_dictionary_get_uint32(dm_dict_in, DM_IOCTL_FLAGS, &flags);
1012 if (dmt->type == DM_DEVICE_TABLE)
1013 flags |= DM_STATUS_TABLE_FLAG;
1015 if (dmt->no_open_count)
1016 flags |= DM_SKIP_BDGET_FLAG;
1018 flags |= DM_EXISTS_FLAG;
1020 /* Set flags to dictionary. */
1021 prop_dictionary_set_uint32(dm_dict_in,DM_IOCTL_FLAGS,flags);
1023 prop_dictionary_externalize_to_file(dm_dict_in,"/tmp/test_in");
1025 log_very_verbose("Ioctl type %s --- flags %d",_cmd_data_v4[dmt->type].name,flags);
1026 //printf("name %s, major %d minor %d\n uuid %s\n",
1027 //dm_task_get_name(dmt), dmt->minor, dmt->major, dm_task_get_uuid(dmt));
1028 /* Send dictionary to kernel and wait for reply. */
1029 #ifdef RUMP_ACTION
1030 struct plistref prefp;
1031 int err;
1032 prop_dictionary_externalize_to_pref(dm_dict_in, &prefp);
1034 if (rump_sys_ioctl(_control_fd, NETBSD_DM_IOCTL, &prefp) != 0) {
1036 dm_dict_out = prop_dictionary_internalize(prefp.pref_plist);
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))) {
1047 * Linux version doesn't fail when ENOENT is returned
1048 * for nonexisting device after info, deps, mknodes call.
1049 * It returns dmi sent to kernel with DM_EXISTS_FLAG = 0;
1052 dmi = nbsd_dm_dict_to_dmi(dm_dict_in,_cmd_data_v4[dmt->type].cmd);
1054 dmi->flags &= ~DM_EXISTS_FLAG;
1056 prop_object_release(dm_dict_in);
1057 prop_object_release(dm_dict_out);
1059 goto out;
1060 } else {
1061 log_error("ioctl %s call failed with errno %d\n",
1062 _cmd_data_v4[dmt->type].name, errno);
1064 prop_object_release(dm_dict_in);
1065 prop_object_release(dm_dict_out);
1067 goto bad;
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");
1076 /* Parse kernel dictionary to dmi structure and return it to libdevmapper. */
1077 dmi = nbsd_dm_dict_to_dmi(dm_dict_out,_cmd_data_v4[dmt->type].cmd);
1079 prop_object_release(dm_dict_in);
1080 prop_object_release(dm_dict_out);
1081 out:
1082 return dmi;
1083 bad:
1084 return NULL;
1087 /* Create new edvice nodes in mapper/ dir. */
1088 void dm_task_update_nodes(void)
1090 update_devs();
1093 /* Run dm command which is descirbed in dm_task structure. */
1094 int dm_task_run(struct dm_task *dmt)
1096 struct dm_ioctl *dmi;
1097 unsigned command;
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;
1106 command = _cmd_data_v4[dmt->type].cmd;
1108 /* Old-style creation had a table supplied */
1109 if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1110 return _create_and_load_v4(dmt);
1112 if (dmt->type == DM_DEVICE_MKNODES && !dmt->dev_name &&
1113 !dmt->uuid && dmt->major <= 0)
1114 return _mknodes_v4(dmt);
1116 if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
1117 return _reload_with_suppression_v4(dmt);
1119 if (!_open_control())
1120 return 0;
1122 if (!(dmi = _do_dm_ioctl(dmt, command)))
1123 return 0;
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;
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;
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;
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;
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;
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;
1166 /* Was structure reused? */
1167 if (dmt->dmi.v4)
1168 dm_free(dmt->dmi.v4);
1170 dmt->dmi.v4 = dmi;
1171 return 1;
1173 bad:
1174 dm_free(dmi);
1175 return 0;
1178 void dm_lib_release(void)
1180 if (_control_fd != -1) {
1181 close(_control_fd);
1182 _control_fd = -1;
1184 update_devs();
1187 void dm_lib_exit(void)
1189 dm_lib_release();
1190 dm_dump_memory();
1191 _version_ok = 1;
1192 _version_checked = 0;