2 * Copyright (C) 2001 Sistina Software (UK) Limited.
4 * This file is released under the LGPL.
7 #include "libdm-targets.h"
8 #include "libdm-common.h"
11 # include "libdm-compat.h"
22 #include <sys/ioctl.h>
26 # include <linux/limits.h>
27 # include <linux/kdev_t.h>
28 # include <linux/dm-ioctl.h>
30 # define MAJOR(x) major((x))
31 # define MINOR(x) minor((x))
32 # define MKDEV(x,y) makedev((x),(y))
36 * Ensure build compatibility.
37 * The hard-coded versions here are the highest present
38 * in the _cmd_data arrays.
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.
46 /* dm major version no for running kernel */
47 static int _dm_version = DM_VERSION_MAJOR;
48 static int _log_suppress = 0;
50 static int _control_fd = -1;
51 static int _version_checked = 0;
52 static int _version_ok = 1;
55 * Support both old and new major numbers to ease the transition.
56 * Clumsy, but only temporary.
58 #if DM_VERSION_MAJOR == 4 && defined(DM_COMPAT)
59 const int _dm_compat = 1;
61 const int _dm_compat = 0;
66 static struct cmd_data _cmd_data_v4[] = {
67 {"create", DM_DEV_CREATE, {4, 0, 0}},
68 {"reload", DM_TABLE_LOAD, {4, 0, 0}},
69 {"remove", DM_DEV_REMOVE, {4, 0, 0}},
70 {"remove_all", DM_REMOVE_ALL, {4, 0, 0}},
71 {"suspend", DM_DEV_SUSPEND, {4, 0, 0}},
72 {"resume", DM_DEV_SUSPEND, {4, 0, 0}},
73 {"info", DM_DEV_STATUS, {4, 0, 0}},
74 {"deps", DM_TABLE_DEPS, {4, 0, 0}},
75 {"rename", DM_DEV_RENAME, {4, 0, 0}},
76 {"version", DM_VERSION, {4, 0, 0}},
77 {"status", DM_TABLE_STATUS, {4, 0, 0}},
78 {"table", DM_TABLE_STATUS, {4, 0, 0}},
79 {"waitevent", DM_DEV_WAIT, {4, 0, 0}},
80 {"names", DM_LIST_DEVICES, {4, 0, 0}},
81 {"clear", DM_TABLE_CLEAR, {4, 0, 0}},
82 {"mknodes", DM_DEV_STATUS, {4, 0, 0}},
86 #define ALIGNMENT_V1 sizeof(int)
89 /* FIXME Rejig library to record & use errno instead */
90 #ifndef DM_EXISTS_FLAG
91 # define DM_EXISTS_FLAG 0x00000004
94 static void *_align(void *ptr, unsigned int a)
96 register unsigned long agn = --a;
98 return (void *) (((unsigned long) ptr + agn) & ~agn);
101 static int _open_control(void)
103 char control[PATH_MAX];
105 if (_control_fd != -1)
108 snprintf(control, sizeof(control), "%s/control", dm_dir());
110 if ((_control_fd = open(control, O_RDWR)) < 0) {
111 log_error("%s: open failed: %s", control, strerror(errno));
112 log_error("Is device-mapper driver missing from kernel?");
119 void dm_task_destroy(struct dm_task *dmt)
121 struct target *t, *n;
123 for (t = dmt->head; t; t = n) {
146 * Protocol Version 1 compatibility functions.
151 static int _dm_task_get_driver_version_v1(struct dm_task *dmt, char *version,
161 v = dmt->dmi.v1->version;
162 snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]);
166 /* Unmarshall the target info returned from a status call */
167 static int _unmarshal_status_v1(struct dm_task *dmt, struct dm_ioctl_v1 *dmi)
169 char *outbuf = (char *) dmi + dmi->data_start;
170 char *outptr = outbuf;
172 struct dm_target_spec_v1 *spec;
174 for (i = 0; i < dmi->target_count; i++) {
175 spec = (struct dm_target_spec_v1 *) outptr;
177 if (!dm_task_add_target(dmt, spec->sector_start,
178 (uint64_t) spec->length,
180 outptr + sizeof(*spec)))
183 outptr = outbuf + spec->next;
189 static int _dm_format_dev_v1(char *buf, int bufsize, uint32_t dev_major,
197 r = snprintf(buf, bufsize, "%03x:%03x", dev_major, dev_minor);
198 if (r < 0 || r > bufsize - 1)
204 static int _dm_task_get_info_v1(struct dm_task *dmt, struct dm_info *info)
209 memset(info, 0, sizeof(*info));
211 info->exists = dmt->dmi.v1->flags & DM_EXISTS_FLAG ? 1 : 0;
215 info->suspended = dmt->dmi.v1->flags & DM_SUSPEND_FLAG ? 1 : 0;
216 info->read_only = dmt->dmi.v1->flags & DM_READONLY_FLAG ? 1 : 0;
217 info->target_count = dmt->dmi.v1->target_count;
218 info->open_count = dmt->dmi.v1->open_count;
220 info->major = MAJOR(dmt->dmi.v1->dev);
221 info->minor = MINOR(dmt->dmi.v1->dev);
222 info->live_table = 1;
223 info->inactive_table = 0;
228 static const char *_dm_task_get_name_v1(struct dm_task *dmt)
230 return (dmt->dmi.v1->name);
233 static const char *_dm_task_get_uuid_v1(struct dm_task *dmt)
235 return (dmt->dmi.v1->uuid);
238 static struct dm_deps *_dm_task_get_deps_v1(struct dm_task *dmt)
240 log_error("deps version 1 no longer supported by libdevmapper");
244 static struct dm_names *_dm_task_get_names_v1(struct dm_task *dmt)
246 return (struct dm_names *) (((void *) dmt->dmi.v1) +
247 dmt->dmi.v1->data_start);
250 static void *_add_target_v1(struct target *t, void *out, void *end)
253 struct dm_target_spec_v1 sp;
254 size_t sp_size = sizeof(struct dm_target_spec_v1);
256 const char no_space[] = "Ran out of memory building ioctl parameter";
265 sp.sector_start = t->start;
266 sp.length = t->length;
267 strncpy(sp.target_type, t->type, sizeof(sp.target_type));
269 len = strlen(t->params);
271 if ((out + len + 1) >= end) {
274 log_error("t->params= '%s'", t->params);
277 strcpy((char *) out, t->params);
280 /* align next block */
281 out = _align(out, ALIGNMENT_V1);
283 sp.next = out - out_sp;
285 memcpy(out_sp, &sp, sp_size);
290 static struct dm_ioctl_v1 *_flatten_v1(struct dm_task *dmt)
292 const size_t min_size = 16 * 1024;
293 const int (*version)[3];
295 struct dm_ioctl_v1 *dmi;
297 size_t len = sizeof(struct dm_ioctl_v1);
301 for (t = dmt->head; t; t = t->next) {
302 len += sizeof(struct dm_target_spec_v1);
303 len += strlen(t->params) + 1 + ALIGNMENT_V1;
307 if (count && dmt->newname) {
308 log_error("targets and newname are incompatible");
313 len += strlen(dmt->newname) + 1;
316 * Give len a minimum size so that we have space to store
317 * dependencies or status information.
322 if (!(dmi = malloc(len)))
327 version = &_cmd_data_v1[dmt->type].version;
329 dmi->version[0] = (*version)[0];
330 dmi->version[1] = (*version)[1];
331 dmi->version[2] = (*version)[2];
333 dmi->data_size = len;
334 dmi->data_start = sizeof(struct dm_ioctl_v1);
337 strncpy(dmi->name, dmt->dev_name, sizeof(dmi->name));
339 if (dmt->type == DM_DEVICE_SUSPEND)
340 dmi->flags |= DM_SUSPEND_FLAG;
342 dmi->flags |= DM_READONLY_FLAG;
344 if (dmt->minor >= 0) {
345 if (dmt->major <= 0) {
346 log_error("Missing major number for persistent device");
349 dmi->flags |= DM_PERSISTENT_DEV_FLAG;
350 dmi->dev = MKDEV(dmt->major, dmt->minor);
354 strncpy(dmi->uuid, dmt->uuid, sizeof(dmi->uuid));
356 dmi->target_count = count;
358 b = (void *) (dmi + 1);
359 e = (void *) ((char *) dmi + len);
361 for (t = dmt->head; t; t = t->next)
362 if (!(b = _add_target_v1(t, b, e)))
366 strcpy(b, dmt->newname);
375 static int _dm_names_v1(struct dm_ioctl_v1 *dmi)
377 const char *dev_dir = dm_dir();
380 struct dirent *dirent;
382 struct dm_names *names, *old_names = NULL;
383 void *end = (void *) dmi + dmi->data_size;
387 if (!(d = opendir(dev_dir))) {
388 log_error("%s: opendir failed: %s", dev_dir, strerror(errno));
392 names = (struct dm_names *) ((void *) dmi + dmi->data_start);
394 names->dev = 0; /* Flags no data */
396 while ((dirent = readdir(d))) {
397 name = dirent->d_name;
399 if (name[0] == '.' || !strcmp(name, "control"))
403 old_names->next = (uint32_t) ((void *) names -
405 snprintf(path, sizeof(path), "%s/%s", dev_dir, name);
406 if (stat(path, &buf)) {
407 log_error("%s: stat failed: %s", path, strerror(errno));
410 if (!S_ISBLK(buf.st_mode))
412 names->dev = (uint64_t) buf.st_rdev;
415 if (((void *) (names + 1) + len + 1) >= end) {
416 log_error("Insufficient buffer space for device list");
421 strcpy(names->name, name);
424 names = _align((void *) ++names + len + 1, ALIGNMENT);
428 log_error("%s: closedir failed: %s", dev_dir, strerror(errno));
433 static int _dm_task_run_v1(struct dm_task *dmt)
435 struct dm_ioctl_v1 *dmi;
436 unsigned int command;
438 dmi = _flatten_v1(dmt);
440 log_error("Couldn't create ioctl argument");
444 if (!_open_control())
447 if ((unsigned) dmt->type >=
448 (sizeof(_cmd_data_v1) / sizeof(*_cmd_data_v1))) {
449 log_error("Internal error: unknown device-mapper task %d",
454 command = _cmd_data_v1[dmt->type].cmd;
456 if (dmt->type == DM_DEVICE_TABLE)
457 dmi->flags |= DM_STATUS_TABLE_FLAG;
459 log_debug("dm %s %s %s %s", _cmd_data_v1[dmt->type].name, dmi->name,
460 dmi->uuid, dmt->newname ? dmt->newname : "");
461 if (dmt->type == DM_DEVICE_LIST) {
462 if (!_dm_names_v1(dmi))
464 } else if (ioctl(_control_fd, command, dmi) < 0) {
466 log_verbose("device-mapper ioctl cmd %d failed: %s",
467 _IOC_NR(command), strerror(errno));
469 log_error("device-mapper ioctl cmd %d failed: %s",
470 _IOC_NR(command), strerror(errno));
475 case DM_DEVICE_CREATE:
476 add_dev_node(dmt->dev_name, MAJOR(dmi->dev), MINOR(dmi->dev));
479 case DM_DEVICE_REMOVE:
480 rm_dev_node(dmt->dev_name);
483 case DM_DEVICE_RENAME:
484 rename_dev_node(dmt->dev_name, dmt->newname);
487 case DM_DEVICE_MKNODES:
488 if (dmi->flags & DM_EXISTS_FLAG)
489 add_dev_node(dmt->dev_name, MAJOR(dmi->dev),
492 rm_dev_node(dmt->dev_name);
495 case DM_DEVICE_STATUS:
496 case DM_DEVICE_TABLE:
497 if (!_unmarshal_status_v1(dmt, dmi))
501 case DM_DEVICE_SUSPEND:
502 case DM_DEVICE_RESUME:
503 dmt->type = DM_DEVICE_INFO;
504 if (!dm_task_run(dmt))
506 free(dmi); /* We'll use what info returned */
521 * Protocol Version 4 functions.
524 int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
529 if (_dm_version == 1)
530 return _dm_task_get_driver_version_v1(dmt, version, size);
538 v = dmt->dmi.v4->version;
539 snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]);
543 static int _check_version(char *version, size_t size, int log_suppress)
545 struct dm_task *task;
548 if (!(task = dm_task_create(DM_DEVICE_VERSION))) {
549 log_error("Failed to get device-mapper version");
557 r = dm_task_run(task);
558 dm_task_get_driver_version(task, version, size);
559 dm_task_destroy(task);
566 * Find out device-mapper's major version number the first time
567 * this is called and whether or not we support it.
569 int dm_check_version(void)
571 char libversion[64], dmversion[64];
572 const char *compat = "";
574 if (_version_checked)
577 _version_checked = 1;
579 if (_check_version(dmversion, sizeof(dmversion), _dm_compat))
585 log_verbose("device-mapper ioctl protocol version %d failed. "
586 "Trying protocol version 1.", _dm_version);
588 if (_check_version(dmversion, sizeof(dmversion), 0)) {
589 log_verbose("Using device-mapper ioctl protocol version 1");
595 dm_get_library_version(libversion, sizeof(libversion));
597 log_error("Incompatible libdevmapper %s%s and kernel driver %s",
598 libversion, compat, dmversion);
605 void *dm_get_next_target(struct dm_task *dmt, void *next,
606 uint64_t *start, uint64_t *length,
607 char **target_type, char **params)
609 struct target *t = (struct target *) next;
619 *target_type = t->type;
625 /* Unmarshall the target info returned from a status call */
626 static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
628 char *outbuf = (char *) dmi + dmi->data_start;
629 char *outptr = outbuf;
631 struct dm_target_spec *spec;
633 for (i = 0; i < dmi->target_count; i++) {
634 spec = (struct dm_target_spec *) outptr;
635 if (!dm_task_add_target(dmt, spec->sector_start,
638 outptr + sizeof(*spec)))
641 outptr = outbuf + spec->next;
647 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
653 if (_dm_version == 1)
654 return _dm_format_dev_v1(buf, bufsize, dev_major, dev_minor);
660 r = snprintf(buf, bufsize, "%03u:%03u", dev_major, dev_minor);
661 if (r < 0 || r > bufsize - 1)
667 int dm_task_get_info(struct dm_task *dmt, struct dm_info *info)
670 if (_dm_version == 1)
671 return _dm_task_get_info_v1(dmt, info);
677 memset(info, 0, sizeof(*info));
679 info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
683 info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0;
684 info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0;
685 info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0;
686 info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ?
688 info->target_count = dmt->dmi.v4->target_count;
689 info->open_count = dmt->dmi.v4->open_count;
690 info->event_nr = dmt->dmi.v4->event_nr;
691 info->major = MAJOR(dmt->dmi.v4->dev);
692 info->minor = MINOR(dmt->dmi.v4->dev);
697 const char *dm_task_get_name(struct dm_task *dmt)
700 if (_dm_version == 1)
701 return _dm_task_get_name_v1(dmt);
704 return (dmt->dmi.v4->name);
707 const char *dm_task_get_uuid(struct dm_task *dmt)
710 if (_dm_version == 1)
711 return _dm_task_get_uuid_v1(dmt);
714 return (dmt->dmi.v4->uuid);
717 struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
720 if (_dm_version == 1)
721 return _dm_task_get_deps_v1(dmt);
724 return (struct dm_deps *) (((void *) dmt->dmi.v4) +
725 dmt->dmi.v4->data_start);
728 struct dm_names *dm_task_get_names(struct dm_task *dmt)
731 if (_dm_version == 1)
732 return _dm_task_get_names_v1(dmt);
735 return (struct dm_names *) (((void *) dmt->dmi.v4) +
736 dmt->dmi.v4->data_start);
739 int dm_task_set_ro(struct dm_task *dmt)
745 int dm_task_set_newname(struct dm_task *dmt, const char *newname)
747 if (!(dmt->newname = strdup(newname))) {
748 log_error("dm_task_set_newname: strdup(%s) failed", newname);
755 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
757 dmt->event_nr = event_nr;
762 struct target *create_target(uint64_t start, uint64_t len, const char *type,
765 struct target *t = malloc(sizeof(*t));
768 log_error("create_target: malloc(%d) failed", sizeof(*t));
772 memset(t, 0, sizeof(*t));
774 if (!(t->params = strdup(params))) {
775 log_error("create_target: strdup(params) failed");
779 if (!(t->type = strdup(type))) {
780 log_error("create_target: strdup(type) failed");
795 static void *_add_target(struct target *t, void *out, void *end)
798 struct dm_target_spec sp;
799 size_t sp_size = sizeof(struct dm_target_spec);
801 const char no_space[] = "Ran out of memory building ioctl parameter";
810 sp.sector_start = t->start;
811 sp.length = t->length;
812 strncpy(sp.target_type, t->type, sizeof(sp.target_type));
814 len = strlen(t->params);
816 if ((out + len + 1) >= end) {
819 log_error("t->params= '%s'", t->params);
822 strcpy((char *) out, t->params);
825 /* align next block */
826 out = _align(out, ALIGNMENT);
828 sp.next = out - out_sp;
829 memcpy(out_sp, &sp, sp_size);
834 static struct dm_ioctl *_flatten(struct dm_task *dmt)
836 const size_t min_size = 16 * 1024;
837 const int (*version)[3];
839 struct dm_ioctl *dmi;
841 size_t len = sizeof(struct dm_ioctl);
845 for (t = dmt->head; t; t = t->next) {
846 len += sizeof(struct dm_target_spec);
847 len += strlen(t->params) + 1 + ALIGNMENT;
851 if (count && dmt->newname) {
852 log_error("targets and newname are incompatible");
857 len += strlen(dmt->newname) + 1;
860 * Give len a minimum size so that we have space to store
861 * dependencies or status information.
866 if (!(dmi = malloc(len)))
871 version = &_cmd_data_v4[dmt->type].version;
873 dmi->version[0] = (*version)[0];
874 dmi->version[1] = (*version)[1];
875 dmi->version[2] = (*version)[2];
877 dmi->data_size = len;
878 dmi->data_start = sizeof(struct dm_ioctl);
881 strncpy(dmi->name, dmt->dev_name, sizeof(dmi->name));
883 if (dmt->type == DM_DEVICE_SUSPEND)
884 dmi->flags |= DM_SUSPEND_FLAG;
886 dmi->flags |= DM_READONLY_FLAG;
888 if (dmt->minor >= 0) {
889 if (dmt->major <= 0) {
890 log_error("Missing major number for persistent device");
893 dmi->flags |= DM_PERSISTENT_DEV_FLAG;
894 dmi->dev = MKDEV(dmt->major, dmt->minor);
898 strncpy(dmi->uuid, dmt->uuid, sizeof(dmi->uuid));
900 dmi->target_count = count;
901 dmi->event_nr = dmt->event_nr;
903 b = (void *) (dmi + 1);
904 e = (void *) ((char *) dmi + len);
906 for (t = dmt->head; t; t = t->next)
907 if (!(b = _add_target(t, b, e)))
911 strcpy(b, dmt->newname);
920 static int _create_and_load_v4(struct dm_task *dmt)
922 struct dm_task *task;
925 /* Use new task struct to create the device */
926 if (!(task = dm_task_create(DM_DEVICE_CREATE))) {
927 log_error("Failed to create device-mapper task struct");
931 /* Copy across relevant fields */
932 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
933 dm_task_destroy(task);
937 if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
938 dm_task_destroy(task);
942 task->major = dmt->major;
943 task->minor = dmt->minor;
945 r = dm_task_run(task);
946 dm_task_destroy(task);
950 /* Next load the table */
951 if (!(task = dm_task_create(DM_DEVICE_RELOAD))) {
952 log_error("Failed to create device-mapper task struct");
956 /* Copy across relevant fields */
957 if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
958 dm_task_destroy(task);
962 task->read_only = dmt->read_only;
963 task->head = dmt->head;
964 task->tail = dmt->tail;
966 r = dm_task_run(task);
970 dm_task_destroy(task);
974 /* Use the original structure last so the info will be correct */
975 dmt->type = DM_DEVICE_RESUME;
979 r = dm_task_run(dmt);
984 int dm_task_run(struct dm_task *dmt)
986 struct dm_ioctl *dmi = NULL;
987 unsigned int command;
990 if (_dm_version == 1)
991 return _dm_task_run_v1(dmt);
994 if ((unsigned) dmt->type >=
995 (sizeof(_cmd_data_v4) / sizeof(*_cmd_data_v4))) {
996 log_error("Internal error: unknown device-mapper task %d",
1001 command = _cmd_data_v4[dmt->type].cmd;
1003 /* Old-style creation had a table supplied */
1004 if (dmt->type == DM_DEVICE_CREATE && dmt->head)
1005 return _create_and_load_v4(dmt);
1007 if (!_open_control())
1010 dmi = _flatten(dmt);
1012 log_error("Couldn't create ioctl argument");
1016 if (dmt->type == DM_DEVICE_TABLE)
1017 dmi->flags |= DM_STATUS_TABLE_FLAG;
1019 dmi->flags |= DM_EXISTS_FLAG; /* FIXME */
1020 log_debug("dm %s %s %s %s", _cmd_data_v4[dmt->type].name, dmi->name,
1021 dmi->uuid, dmt->newname ? dmt->newname : "");
1022 if (ioctl(_control_fd, command, dmi) < 0) {
1023 if (errno == ENXIO && ((dmt->type == DM_DEVICE_INFO) ||
1024 (dmt->type == DM_DEVICE_MKNODES))) {
1025 dmi->flags &= ~DM_EXISTS_FLAG; /* FIXME */
1029 log_verbose("device-mapper ioctl cmd %d failed: %s",
1030 _IOC_NR(command), strerror(errno));
1032 log_error("device-mapper ioctl cmd %d failed: %s",
1033 _IOC_NR(command), strerror(errno));
1038 switch (dmt->type) {
1039 case DM_DEVICE_CREATE:
1040 add_dev_node(dmt->dev_name, MAJOR(dmi->dev), MINOR(dmi->dev));
1043 case DM_DEVICE_REMOVE:
1044 rm_dev_node(dmt->dev_name);
1047 case DM_DEVICE_RENAME:
1048 rename_dev_node(dmt->dev_name, dmt->newname);
1051 case DM_DEVICE_MKNODES:
1052 if (dmi->flags & DM_EXISTS_FLAG)
1053 add_dev_node(dmt->dev_name, MAJOR(dmi->dev),
1056 rm_dev_node(dmt->dev_name);
1059 case DM_DEVICE_STATUS:
1060 case DM_DEVICE_TABLE:
1061 case DM_DEVICE_WAITEVENT:
1062 if (!_unmarshal_status(dmt, dmi))
1075 void dm_lib_release(void)
1077 if (_control_fd != -1) {
1084 void dm_lib_exit(void)
1086 if (_control_fd != -1) {
1091 _version_checked = 0;