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