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