Cumulative patches for blkmapd.
Jim Rees (6):
blkmapd: get rid of config file and instead examine all block devices
blkmapd: don't rescan periodically
blkmapd: don't use atomicio() where it's not needed
ignore passive devices before checking serial numbers
add "-d" option to just do discovery then exit
Various bug fixes and cleanups.
utils/blkmapd/Makefile.am | 3 -
utils/blkmapd/atomicio.c | 54 --------
utils/blkmapd/cfg.c | 248 --------------------------------------
utils/blkmapd/cfg.h | 47 -------
utils/blkmapd/device-discovery.c | 216 +++++++++++++++------------------
utils/blkmapd/device-discovery.h | 5 +-
utils/blkmapd/device-inq.c | 43 +++----
utils/blkmapd/device-process.c | 30 ++---
utils/blkmapd/dm-device.c | 1 +
9 files changed, 129 insertions(+), 518 deletions(-)
delete mode 100644 utils/blkmapd/atomicio.c
delete mode 100644 utils/blkmapd/cfg.c
delete mode 100644 utils/blkmapd/cfg.h
Periodic device rescanning is always going to be wrong, since the interval
will either be too long and miss an important change, or too short and waste
resources. It's not even needed in the normal case, only when the devices
change during the life of a layout. For now, just rescan when the kernel
asks for a layout. A later patch will re-introduce rescanning during the
life of a layout, but will be triggered by configuration change
notifications from udev, rather than by expiration of an arbitrary time
interval.
Signed-off-by: Jim Rees <[email protected]>
Signed-off-by: Benny Halevy <[email protected]>
---
utils/blkmapd/device-discovery.c | 14 ++++++++++----
utils/blkmapd/device-discovery.h | 2 --
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
index 0fc8fd3..71b4d48 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -308,6 +308,15 @@ int bl_disk_inquiry_process(int fd)
head->status = BL_DEVICE_REQUEST_PROC;
switch (head->type) {
case BL_DEVICE_MOUNT:
+ /*
+ * It shouldn't be necessary to discover devices here, since
+ * process_deviceinfo() will re-discover if it can't find
+ * the devices it needs. But in the case of multipath
+ * devices (ones that appear more than once, for example an
+ * active and a standby LUN), this will re-order them in the
+ * correct priority.
+ */
+ bl_discover_devices();
if (!process_deviceinfo(buf, buflen, &major, &minor)) {
head->status = BL_DEVICE_REQUEST_ERR;
goto out;
@@ -329,7 +338,6 @@ int bl_disk_inquiry_process(int fd)
case BL_DEVICE_UMOUNT:
if (!dm_device_remove_all((uint64_t *) buf))
head->status = BL_DEVICE_REQUEST_ERR;
- bl_discover_devices();
break;
default:
head->status = BL_DEVICE_REQUEST_ERR;
@@ -357,7 +365,6 @@ unsigned int bl_process_stop;
int bl_run_disk_inquiry_process(int fd)
{
fd_set rset;
- struct timeval tv;
int ret;
bl_process_stop = 0;
@@ -368,8 +375,7 @@ int bl_run_disk_inquiry_process(int fd)
FD_ZERO(&rset);
FD_SET(fd, &rset);
ret = 0;
- tv.tv_sec = BL_DEVICE_DISCOVERY_INTERVAL;
- switch (select(fd + 1, &rset, NULL, NULL, &tv)) {
+ switch (select(fd + 1, &rset, NULL, NULL, NULL)) {
case -1:
if (errno == EINTR)
continue;
diff --git a/utils/blkmapd/device-discovery.h b/utils/blkmapd/device-discovery.h
index 71c0748..b8d26a6 100644
--- a/utils/blkmapd/device-discovery.h
+++ b/utils/blkmapd/device-discovery.h
@@ -27,8 +27,6 @@
#ifndef BL_DEVICE_DISCOVERY_H
#define BL_DEVICE_DISCOVERY_H
-#define BL_DEVICE_DISCOVERY_INTERVAL 60
-
#include <stdint.h>
#include <syslog.h>
--
1.7.1
Signed-off-by: Jim Rees <[email protected]>
Signed-off-by: Benny Halevy <[email protected]>
---
utils/blkmapd/device-process.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/utils/blkmapd/device-process.c b/utils/blkmapd/device-process.c
index 9e91840..c4e72ea 100644
--- a/utils/blkmapd/device-process.c
+++ b/utils/blkmapd/device-process.c
@@ -121,7 +121,7 @@ read_cmp_blk_sig(const char *dev_name, struct bl_sig_comp *comp,
goto error;
}
- if (atomicio(read, fd, sig, comp->bs_length) != comp->bs_length) {
+ if (read(fd, sig, comp->bs_length) != comp->bs_length) {
BL_LOG_ERR("File %s read error\n", dev_name);
goto error;
}
--
1.7.1
Signed-off-by: Jim Rees <[email protected]>
---
utils/blkmapd/device-discovery.c | 33 +++++++++++++++++----------------
utils/blkmapd/device-inq.c | 19 +++++++++++--------
2 files changed, 28 insertions(+), 24 deletions(-)
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
index 71b4d48..c8bd39c 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -26,24 +26,25 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <linux/kdev_t.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/select.h>
+#include <linux/kdev_t.h>
+#include <scsi/scsi.h>
+#include <scsi/scsi_ioctl.h>
+#include <scsi/sg.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
-#include <scsi/scsi.h>
-#include <scsi/scsi_ioctl.h>
-#include <scsi/sg.h>
#include "device-discovery.h"
@@ -127,7 +128,7 @@ void bl_add_disk(char *filepath)
struct stat sb;
off_t size = 0;
struct bl_serial *serial = NULL;
- enum bl_path_state_e ap_state = BL_PATH_STATE_PASSIVE;
+ enum bl_path_state_e ap_state;
struct bl_disk_path *diskpath = NULL, *path = NULL;
dev_t dev;
@@ -152,6 +153,11 @@ void bl_add_disk(char *filepath)
dev = sb.st_rdev;
serial = bldev_read_serial(fd, filepath);
+ bldev_read_ap_state(fd, &ap_state);
+ close(fd);
+
+ if (ap_state == BL_PATH_STATE_PASSIVE)
+ return;
for (disk = visible_disk_list; disk != NULL; disk = disk->next) {
/* Already scanned or a partition?
@@ -164,13 +170,8 @@ void bl_add_disk(char *filepath)
}
}
- if (disk && diskpath) {
- close(fd);
+ if (disk && diskpath)
return;
- }
-
- bldev_read_ap_state(fd, &ap_state);
- close(fd);
BL_LOG_ERR("%s: %s\n", __func__, filepath);
diff --git a/utils/blkmapd/device-inq.c b/utils/blkmapd/device-inq.c
index ff38fd6..7817c5a 100644
--- a/utils/blkmapd/device-inq.c
+++ b/utils/blkmapd/device-inq.c
@@ -28,23 +28,25 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <dirent.h>
-#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/select.h>
+#include <scsi/scsi.h>
+#include <scsi/scsi_ioctl.h>
+#include <scsi/sg.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
-#include <scsi/scsi.h>
-#include <scsi/scsi_ioctl.h>
-#include <scsi/sg.h>
+
#include "device-discovery.h"
#define DEF_ALLOC_LEN 255
@@ -161,6 +163,7 @@ void bldev_read_ap_state(int fd, enum bl_path_state_e *ap_state_out)
if (status)
goto out;
+ printf("buffer[4]=%d\n", (int) buffer[4]);
if (buffer[4] < 0x02)
*ap_state_out = BL_PATH_STATE_PASSIVE;
out:
--
1.7.1
Signed-off-by: Jim Rees <[email protected]>
---
utils/blkmapd/device-discovery.c | 69 +++++++++++++++++++++----------------
1 files changed, 39 insertions(+), 30 deletions(-)
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
index c8bd39c..6b1f942 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -398,54 +398,63 @@ int bl_run_disk_inquiry_process(int fd)
/* Daemon */
int main(int argc, char **argv)
{
- int fd, opt, fg = 0, ret = 1;
+ int fd = -1, opt, dflag = 0, fg = 0, ret = 1;
struct stat statbuf;
char pidbuf[64];
- while ((opt = getopt(argc, argv, "f")) != -1) {
+ while ((opt = getopt(argc, argv, "df")) != -1) {
switch (opt) {
+ case 'd':
+ dflag = 1;
+ break;
case 'f':
fg = 1;
break;
}
}
- if (!stat(PID_FILE, &statbuf)) {
- fprintf(stderr, "Pid file already existed\n");
- return -1;
- }
+ if (fg) {
+ openlog("blkmapd", LOG_PERROR, 0);
+ } else {
+ if (!stat(PID_FILE, &statbuf)) {
+ fprintf(stderr, "Pid file already existed\n");
+ return -1;
+ }
- if (!fg && daemon(0, 0) != 0) {
- fprintf(stderr, "Daemonize failed\n");
- return -1;
- }
+ if (daemon(0, 0) != 0) {
+ fprintf(stderr, "Daemonize failed\n");
+ return -1;
+ }
- openlog("blkmapd", LOG_PID, 0);
- fd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- BL_LOG_ERR("Create pid file failed\n");
- return -1;
- }
+ openlog("blkmapd", LOG_PID, 0);
+ fd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
+ if (fd < 0) {
+ BL_LOG_ERR("Create pid file failed\n");
+ return -1;
+ }
- if (lockf(fd, F_TLOCK, 0) < 0) {
- BL_LOG_ERR("Lock pid file failed\n");
- close(fd);
- return -1;
- }
- ftruncate(fd, 0);
- sprintf(pidbuf, "%d\n", getpid());
- write(fd, pidbuf, strlen(pidbuf));
-
- /* open pipe file */
- fd = open(BL_PIPE_FILE, O_RDWR);
- if (fd < 0) {
- BL_LOG_ERR("open pipe file error\n");
- return -1;
+ if (lockf(fd, F_TLOCK, 0) < 0) {
+ BL_LOG_ERR("Lock pid file failed\n");
+ close(fd);
+ return -1;
+ }
+ ftruncate(fd, 0);
+ sprintf(pidbuf, "%d\n", getpid());
+ write(fd, pidbuf, strlen(pidbuf));
+
+ /* open pipe file */
+ fd = open(BL_PIPE_FILE, O_RDWR);
+ if (fd < 0) {
+ BL_LOG_ERR("open pipe file error\n");
+ return -1;
+ }
}
while (1) {
/* discover device when needed */
bl_discover_devices();
+ if (dflag)
+ break;
ret = bl_run_disk_inquiry_process(fd);
if (ret < 0) {
--
1.7.1
Only examine actual block devices rather than everything in /dev. Since
we're looking for pnfs signatures, it usually doesn't hurt to examine a
device that isn't part of a layout, and in general it's hard to tell ahead
of time which devices should be considered, so I think having a config file
does more harm than good.
It may be useful later on to be able to blacklist some devices but I'd
rather not do this through yet another config file. Maybe this can be done
via udev rules.
Signed-off-by: Jim Rees <[email protected]>
Signed-off-by: Benny Halevy <[email protected]>
---
utils/blkmapd/Makefile.am | 2 -
utils/blkmapd/cfg.c | 248 --------------------------------------
utils/blkmapd/cfg.h | 47 -------
utils/blkmapd/device-discovery.c | 103 ++++------------
4 files changed, 26 insertions(+), 374 deletions(-)
delete mode 100644 utils/blkmapd/cfg.c
delete mode 100644 utils/blkmapd/cfg.h
diff --git a/utils/blkmapd/Makefile.am b/utils/blkmapd/Makefile.am
index f23c073..1e8720d 100644
--- a/utils/blkmapd/Makefile.am
+++ b/utils/blkmapd/Makefile.am
@@ -7,13 +7,11 @@ sbin_PROGRAMS = blkmapd
blkmapd_SOURCES = \
atomicio.c \
- cfg.c \
device-discovery.c \
device-inq.c \
device-process.c \
dm-device.c \
\
- cfg.h \
device-discovery.h
blkmapd_LDADD = -ldevmapper ../../support/nfs/libnfs.a
diff --git a/utils/blkmapd/cfg.c b/utils/blkmapd/cfg.c
deleted file mode 100644
index dab9d0f..0000000
--- a/utils/blkmapd/cfg.c
+++ /dev/null
@@ -1,248 +0,0 @@
-/*
- * Copyright (c) 2010 EMC Corporation, Haiying Tang <[email protected]>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-#include <linux/errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <fcntl.h>
-#include <ctype.h>
-
-#include "device-discovery.h"
-#include "cfg.h"
-
-char *conf_path = "/etc/blkmapd.conf";
-
-struct scan_root_list *scan_root_list_head;
-
-void bl_release_list(void)
-{
- struct scan_root_list *root = scan_root_list_head;
- struct scan_device_list *disk;
-
- while (root) {
- disk = root->disk;
- while (disk) {
- root->disk = disk->next;
- free(disk->name);
- free(disk);
- disk = root->disk;
- }
- scan_root_list_head = root->next;
- free(root->name);
- free(root);
- root = scan_root_list_head;
- }
-}
-
-struct scan_root_list *bl_alloc_root_list(char *name, int all_disk)
-{
- struct scan_root_list *root;
-
- root = malloc(sizeof(struct scan_root_list));
- if (!root)
- goto nomem;
-
- root->name = strdup(name);
- if (!root->name)
- goto nomem;
- root->next = scan_root_list_head;
- root->all_disk = all_disk;
- scan_root_list_head = root;
- return root;
-
- nomem:
- BL_LOG_ERR("%s: Out of memory!\n", __func__);
- if (root)
- free(root);
- return NULL;
-}
-
-struct scan_device_list *bl_alloc_device_list(struct scan_root_list *root,
- char *name)
-{
- struct scan_device_list *device;
-
- device = malloc(sizeof(struct scan_device_list));
- if (!device)
- goto nomem;
-
- device->name = strdup(name);
- if (!device->name)
- goto nomem;
- device->next = root->disk;
- root->disk = device;
- return device;
-
- nomem:
- BL_LOG_ERR("%s: Out of memory!\n", __func__);
- if (device)
- free(device);
- return NULL;
-}
-
-struct scan_device_list *bl_insert_device_list(struct scan_root_list *root,
- char *name)
-{
- struct scan_device_list *device = root->disk;
-
- /* Check whether this device has been inserted */
- while (device) {
- if (device->name && !strcmp(device->name, name))
- return device;
- device = device->next;
- }
-
- return bl_alloc_device_list(root, name);
-}
-
-struct scan_root_list *bl_insert_root_list(char *name, int all_disk)
-{
- struct scan_root_list *root = scan_root_list_head;
-
- /* Check whether this root has been inserted */
- while (root) {
- if (root->name && !strcmp(root->name, name))
- return root;
- root = root->next;
- }
-
- return bl_alloc_root_list(name, all_disk);
-}
-
-int bl_parse_line(char *line, struct scan_root_list **bl_root)
-{
- char *root, *device, *end;
-
- root = strdup(line);
- end = root + strlen(line);
-
- /* Skip comments */
- if (*root == '#')
- return 0;
-
- /* Trim leading space */
- while (*root != '\0' && isspace(*root))
- root++;
- if (*root == '\0')
- return 0;
-
- /* Trim trailing space and set "end" to last char */
- while ((isspace(*end) || (*end == '\0')) && (end > root))
- end--;
-
- /* For lines ending with '/' or '/','*': add as a dir root */
- if ((*end == '/') ||
- ((*end == '*') && (end - root >= 1) && (*(end - 1) == '/'))) {
- if (*end == '*')
- end--;
- if (*end == '/')
- end--;
- *(end + 1) = '\0';
- *bl_root = bl_insert_root_list(root, 1);
- return 0;
- }
-
- /* Other lines: add as a device */
- device = end;
- while ((*device != '/') && (device > root))
- device--;
- if (device == root) {
- BL_LOG_ERR("%s: invalid config line\n", __func__);
- return -1;
- }
- *device = '\0';
- *bl_root = bl_insert_root_list(root, 0);
- if (*bl_root == NULL)
- return -ENOMEM;
- if (*end == '*')
- end--;
- *(end + 1) = '\0';
- if (bl_insert_device_list(*bl_root, device + 1) == NULL)
- return -ENOMEM;
-
- return 0;
-}
-
-int bl_set_default_conf(void)
-{
- struct scan_root_list *root = NULL;
- int rv;
-
- bl_release_list();
- rv = bl_parse_line("/dev/sd*", &root);
- if (rv < 0)
- return rv;
- rv = bl_parse_line("/dev/mapper/", &root);
- return rv;
-}
-
-int bl_parse_conf(char *buf)
-{
- char *tmp = buf, *line = buf, *end = buf + strlen(buf);
- struct scan_root_list *bl_root = NULL;
- int rv;
-
- while (tmp < end) {
- if (*tmp == '\n') {
- *tmp = '\0';
- rv = bl_parse_line(line, &bl_root);
- if (rv < 0)
- return rv;
- line = tmp + 1;
- }
- tmp++;
- }
-
- return 0;
-}
-
-int bl_cfg_init(void)
-{
- struct scan_root_list *root = NULL;
- FILE *f = NULL;
- char buf[PATH_MAX];
- int rv = 0;
-
- f = fopen(conf_path, "r");
- if (f == NULL)
- rv = bl_set_default_conf();
- else {
- while (fgets(buf, sizeof buf, f) != NULL) {
- rv = bl_parse_line(buf, &root);
- if (rv < 0)
- break;
- }
- }
- if (!scan_root_list_head)
- rv = -EINVAL;
-
- if (f)
- fclose(f);
- return rv;
-}
diff --git a/utils/blkmapd/cfg.h b/utils/blkmapd/cfg.h
deleted file mode 100644
index b9bf930..0000000
--- a/utils/blkmapd/cfg.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * bl-cfg.h
- *
- * Copyright (c) 2010 EMC Corporation, Haiying Tang <[email protected]>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-#ifndef BL_CFG_H
-#define BL_CFG_H
-
-extern char *conf_path;
-extern struct scan_root_list *scan_root_list_head;
-
-struct scan_device_list {
- struct scan_device_list *next;
- char *name;
-};
-
-struct scan_root_list {
- struct scan_root_list *next;
- unsigned int all_disk;
- char *name;
- struct scan_device_list *disk;
-};
-
-int bl_cfg_init(void);
-
-#endif
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
index f42ddc8..0fc8fd3 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -44,8 +44,8 @@
#include <scsi/scsi.h>
#include <scsi/scsi_ioctl.h>
#include <scsi/sg.h>
+
#include "device-discovery.h"
-#include "cfg.h"
#define BL_PIPE_FILE "/var/lib/nfs/rpc_pipefs/bl_device_pipe"
#define PID_FILE "/var/run/blkmapd.pid"
@@ -56,6 +56,7 @@ struct bl_disk_path *bl_get_path(const char *filepath,
struct bl_disk_path *paths)
{
struct bl_disk_path *tmp = paths;
+
while (tmp) {
if (!strcmp(tmp->full_path, filepath))
break;
@@ -130,8 +131,6 @@ void bl_add_disk(char *filepath)
struct bl_disk_path *diskpath = NULL, *path = NULL;
dev_t dev;
- BL_LOG_ERR("%s: %s\n", __func__, filepath);
-
fd = open(filepath, O_RDONLY | O_LARGEFILE);
if (fd < 0)
return;
@@ -173,6 +172,8 @@ void bl_add_disk(char *filepath)
bldev_read_ap_state(fd, &ap_state);
close(fd);
+ BL_LOG_ERR("%s: %s\n", __func__, filepath);
+
/*
* Not sure how to identify a pseudo device created by
* device-mapper, so leave /dev/mapper for now.
@@ -226,75 +227,36 @@ void bl_add_disk(char *filepath)
return;
}
-void bl_devicescan(const char *filename, struct scan_root_list *root)
-{
- /* scan all disks */
- char filepath[PATH_MAX];
- struct scan_device_list *device;
-
- if (!strcmp(filename, ".") || !strcmp(filename, ".."))
- return;
-
- memset(filepath, 0, sizeof(filepath));
- if (strlen(filename) < (PATH_MAX - strlen(root->name) - 2))
- sprintf(filepath, "%s/%s", root->name, filename);
- else {
- BL_LOG_ERR("%s: name too long\n", __func__);
- return;
- }
- if (root->all_disk)
- goto valid;
-
- device = root->disk;
- while (device) {
- /* If device->name is a subset of filename, this disk should be
- * valid for scanning.
- * For example, device->name is "sd", filename is "sda".
- */
- if (device->name
- && !memcmp(filename, device->name, strlen(device->name)))
- goto valid;
- device = device->next;
- }
-
- return;
-
- valid:
- /*
- * sg device is not a real device, but a device created according
- * to each scsi device. It won't be used for pseudo device creation.
- * I moved it here, so that sg devices will not be scanned.
- */
- if (!strncmp(filepath, "/dev/sg", 7))
- return;
- bl_add_disk(filepath);
- return;
-}
-
int bl_discover_devices(void)
{
- DIR *dir;
- struct dirent *dp;
- struct scan_root_list *root = scan_root_list_head;
+ FILE *f;
+ int n;
+ char buf[PATH_MAX], devname[PATH_MAX], fulldevname[PATH_MAX];
/* release previous list */
bl_release_disk();
- /* scan all disks */
- while (root) {
- dir = opendir(root->name);
- if (dir == NULL) {
- root = root->next;
- continue;
- }
+ /* scan all block devices */
+ f = fopen("/proc/partitions", "r");
+ if (f == NULL)
+ return 0;
- while ((dp = readdir(dir)) != NULL)
- bl_devicescan(dp->d_name, root);
-
- root = root->next;
- closedir(dir);
+ while (1) {
+ if (fgets(buf, sizeof buf, f) == NULL)
+ break;
+ n = sscanf(buf, "%*d %*d %*d %31s", devname);
+ if (n != 1)
+ continue;
+ snprintf(fulldevname, sizeof fulldevname, "/sys/block/%s",
+ devname);
+ if (access(fulldevname, F_OK) < 0)
+ continue;
+ snprintf(fulldevname, sizeof fulldevname, "/dev/%s", devname);
+ bl_add_disk(fulldevname);
}
+ fclose(f);
+
return 0;
}
@@ -433,11 +395,8 @@ int main(int argc, char **argv)
struct stat statbuf;
char pidbuf[64];
- while ((opt = getopt(argc, argv, "c:f")) != -1) {
+ while ((opt = getopt(argc, argv, "f")) != -1) {
switch (opt) {
- case 'c':
- conf_path = optarg;
- break;
case 'f':
fg = 1;
break;
@@ -477,16 +436,6 @@ int main(int argc, char **argv)
return -1;
}
- ret = bl_cfg_init();
- if (ret < 0) {
- if (ret == -ENOENT)
- BL_LOG_WARNING("Config file not exist, use default\n");
- else {
- BL_LOG_ERR("Open/read Block pNFS config file error\n");
- return -1;
- }
- }
-
while (1) {
/* discover device when needed */
bl_discover_devices();
--
1.7.1
Don't log EUI errors. These aren't really errors, since we'll fall back to
a different id type, or just use the file name.
Get rid of useless default case.
eliminate fd/pidfd confusion
use INFO syslog level instead of ERR for info messages
Remove atomicio.c from blkmapd now that it's in the support lib
Signed-off-by: Jim Rees <[email protected]>
---
utils/blkmapd/Makefile.am | 1 -
utils/blkmapd/atomicio.c | 54 --------------------------------
utils/blkmapd/device-discovery.c | 63 +++++++++++++++++++++----------------
utils/blkmapd/device-discovery.h | 3 +-
utils/blkmapd/device-inq.c | 26 ++++++---------
utils/blkmapd/device-process.c | 28 ++++++-----------
utils/blkmapd/dm-device.c | 1 +
7 files changed, 59 insertions(+), 117 deletions(-)
delete mode 100644 utils/blkmapd/atomicio.c
diff --git a/utils/blkmapd/Makefile.am b/utils/blkmapd/Makefile.am
index 1e8720d..70e299e 100644
--- a/utils/blkmapd/Makefile.am
+++ b/utils/blkmapd/Makefile.am
@@ -6,7 +6,6 @@ AM_CFLAGS += -D_LARGEFILE64_SOURCE
sbin_PROGRAMS = blkmapd
blkmapd_SOURCES = \
- atomicio.c \
device-discovery.c \
device-inq.c \
device-process.c \
diff --git a/utils/blkmapd/atomicio.c b/utils/blkmapd/atomicio.c
deleted file mode 100644
index 8db626e..0000000
--- a/utils/blkmapd/atomicio.c
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (c) 2002 Marius Aamodt Eriksen <[email protected]>
- * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <errno.h>
-
-/*
- * ensure all of data on socket comes through. f==read || f==write
- */
-ssize_t atomicio(ssize_t(*f) (int, void *, size_t), int fd, void *_s, size_t n)
-{
- char *s = _s;
- ssize_t res, pos = 0;
-
- while (n > pos) {
- res = (f) (fd, s + pos, n - pos);
- switch (res) {
- case -1:
- if (errno == EINTR || errno == EAGAIN)
- continue;
- case 0:
- if (pos != 0)
- return pos;
- return res;
- default:
- pos += res;
- }
- }
- return pos;
-}
diff --git a/utils/blkmapd/device-discovery.c b/utils/blkmapd/device-discovery.c
index 6b1f942..0497a11 100644
--- a/utils/blkmapd/device-discovery.c
+++ b/utils/blkmapd/device-discovery.c
@@ -153,7 +153,7 @@ void bl_add_disk(char *filepath)
dev = sb.st_rdev;
serial = bldev_read_serial(fd, filepath);
- bldev_read_ap_state(fd, &ap_state);
+ ap_state = bldev_read_ap_state(fd);
close(fd);
if (ap_state == BL_PATH_STATE_PASSIVE)
@@ -173,7 +173,7 @@ void bl_add_disk(char *filepath)
if (disk && diskpath)
return;
- BL_LOG_ERR("%s: %s\n", __func__, filepath);
+ BL_LOG_INFO("%s: %s\n", __func__, filepath);
/*
* Not sure how to identify a pseudo device created by
@@ -320,7 +320,7 @@ int bl_disk_inquiry_process(int fd)
bl_discover_devices();
if (!process_deviceinfo(buf, buflen, &major, &minor)) {
head->status = BL_DEVICE_REQUEST_ERR;
- goto out;
+ break;
}
tmp = realloc(head, sizeof(major) + sizeof(minor) +
sizeof(struct pipefs_hdr));
@@ -342,6 +342,7 @@ int bl_disk_inquiry_process(int fd)
break;
default:
head->status = BL_DEVICE_REQUEST_ERR;
+ break;
}
head->totallen = sizeof(struct pipefs_hdr) + len;
@@ -398,7 +399,7 @@ int bl_run_disk_inquiry_process(int fd)
/* Daemon */
int main(int argc, char **argv)
{
- int fd = -1, opt, dflag = 0, fg = 0, ret = 1;
+ int fd, pidfd = -1, opt, dflag = 0, fg = 0, ret = 1;
struct stat statbuf;
char pidbuf[64];
@@ -417,44 +418,47 @@ int main(int argc, char **argv)
openlog("blkmapd", LOG_PERROR, 0);
} else {
if (!stat(PID_FILE, &statbuf)) {
- fprintf(stderr, "Pid file already existed\n");
- return -1;
+ fprintf(stderr, "Pid file %s already existed\n", PID_FILE);
+ exit(1);
}
if (daemon(0, 0) != 0) {
fprintf(stderr, "Daemonize failed\n");
- return -1;
+ exit(1);
}
openlog("blkmapd", LOG_PID, 0);
- fd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- BL_LOG_ERR("Create pid file failed\n");
- return -1;
+ pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
+ if (pidfd < 0) {
+ BL_LOG_ERR("Create pid file %s failed\n", PID_FILE);
+ exit(1);
}
- if (lockf(fd, F_TLOCK, 0) < 0) {
- BL_LOG_ERR("Lock pid file failed\n");
- close(fd);
- return -1;
+ if (lockf(pidfd, F_TLOCK, 0) < 0) {
+ BL_LOG_ERR("Lock pid file %s failed\n", PID_FILE);
+ close(pidfd);
+ exit(1);
}
- ftruncate(fd, 0);
+ ftruncate(pidfd, 0);
sprintf(pidbuf, "%d\n", getpid());
- write(fd, pidbuf, strlen(pidbuf));
+ write(pidfd, pidbuf, strlen(pidbuf));
+ }
- /* open pipe file */
- fd = open(BL_PIPE_FILE, O_RDWR);
- if (fd < 0) {
- BL_LOG_ERR("open pipe file error\n");
- return -1;
- }
+ if (dflag) {
+ bl_discover_devices();
+ exit(0);
+ }
+
+ /* open pipe file */
+ fd = open(BL_PIPE_FILE, O_RDWR);
+ if (fd < 0) {
+ BL_LOG_ERR("open pipe file %s error\n", BL_PIPE_FILE);
+ exit(1);
}
while (1) {
/* discover device when needed */
bl_discover_devices();
- if (dflag)
- break;
ret = bl_run_disk_inquiry_process(fd);
if (ret < 0) {
@@ -462,6 +466,11 @@ int main(int argc, char **argv)
BL_LOG_ERR("inquiry process return %d\n", ret);
}
}
- close(fd);
- return ret;
+
+ if (pidfd >= 0) {
+ close(pidfd);
+ unlink(PID_FILE);
+ }
+
+ exit(ret);
}
diff --git a/utils/blkmapd/device-discovery.h b/utils/blkmapd/device-discovery.h
index b8d26a6..8cf88b8 100644
--- a/utils/blkmapd/device-discovery.h
+++ b/utils/blkmapd/device-discovery.h
@@ -151,9 +151,10 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
extern ssize_t atomicio(ssize_t(*f) (int, void *, size_t),
int fd, void *_s, size_t n);
extern struct bl_serial *bldev_read_serial(int fd, const char *filename);
-extern void bldev_read_ap_state(int fd, enum bl_path_state_e *ap_state_out);
+extern enum bl_path_state_e bldev_read_ap_state(int fd);
extern int bl_discover_devices(void);
+#define BL_LOG_INFO(fmt...) syslog(LOG_INFO, fmt)
#define BL_LOG_WARNING(fmt...) syslog(LOG_WARNING, fmt)
#define BL_LOG_ERR(fmt...) syslog(LOG_ERR, fmt)
#define BL_LOG_DEBUG(fmt...) syslog(LOG_DEBUG, fmt)
diff --git a/utils/blkmapd/device-inq.c b/utils/blkmapd/device-inq.c
index 7817c5a..e1c0128 100644
--- a/utils/blkmapd/device-inq.c
+++ b/utils/blkmapd/device-inq.c
@@ -52,9 +52,10 @@
#define DEF_ALLOC_LEN 255
#define MX_ALLOC_LEN (0xc000 + 0x80)
-struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
+static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
{
struct bl_serial *s;
+
s = malloc(sizeof(*s) + len);
if (s) {
s->data = (char *)&s[1];
@@ -64,7 +65,7 @@ struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
return s;
}
-void bl_free_scsi_string(struct bl_serial *str)
+static void bl_free_scsi_string(struct bl_serial *str)
{
if (str)
free(str);
@@ -107,7 +108,7 @@ static int bldev_inquire_page(int fd, int page, char *buffer, int len)
return -1;
}
-int bldev_inquire_pages(int fd, int page, char **buffer)
+static int bldev_inquire_pages(int fd, int page, char **buffer)
{
int status = 0;
char *tmp;
@@ -152,24 +153,22 @@ int bldev_inquire_pages(int fd, int page, char **buffer)
/* For EMC multipath devices, use VPD page (0xc0) to get status.
* For other devices, return ACTIVE for now
*/
-void bldev_read_ap_state(int fd, enum bl_path_state_e *ap_state_out)
+extern enum bl_path_state_e bldev_read_ap_state(int fd)
{
int status = 0;
- char *buffer;
-
- *ap_state_out = BL_PATH_STATE_ACTIVE;
+ char *buffer = NULL;
+ enum bl_path_state_e ap_state = BL_PATH_STATE_ACTIVE;
status = bldev_inquire_pages(fd, 0xc0, &buffer);
if (status)
goto out;
- printf("buffer[4]=%d\n", (int) buffer[4]);
if (buffer[4] < 0x02)
- *ap_state_out = BL_PATH_STATE_PASSIVE;
+ ap_state = BL_PATH_STATE_PASSIVE;
out:
if (buffer)
free(buffer);
- return;
+ return ap_state;
}
struct bl_serial *bldev_read_serial(int fd, const char *filename)
@@ -200,11 +199,8 @@ struct bl_serial *bldev_read_serial(int fd, const char *filename)
*/
case 2: /* EUI-64 based */
if ((dev_id->len != 8) && (dev_id->len != 12) &&
- (dev_id->len != 16)) {
- BL_LOG_ERR("EUI-64 only decodes 8, "
- "12 and 16\n");
+ (dev_id->len != 16))
break;
- }
case 3: /* NAA */
/* TODO: NAA validity judgement too complicated,
* so just ingore it here.
@@ -221,8 +217,6 @@ struct bl_serial *bldev_read_serial(int fd, const char *filename)
serial_out = bl_create_scsi_string(dev_id->len,
dev_id->data);
break;
- default:
- break;
}
if (current_id == 3)
break;
diff --git a/utils/blkmapd/device-process.c b/utils/blkmapd/device-process.c
index c4e72ea..a543769 100644
--- a/utils/blkmapd/device-process.c
+++ b/utils/blkmapd/device-process.c
@@ -82,7 +82,7 @@ static int decode_blk_signature(uint32_t **pp, uint32_t *end,
* for mapping, then thrown away.
*/
sig->si_comps[i].bs_string = (char *)p;
- BL_LOG_ERR("%s: si_comps[%d]: bs_length %d, bs_string %s\n",
+ BL_LOG_INFO("%s: si_comps[%d]: bs_length %d, bs_string %s\n",
__func__, i, sig->si_comps[i].bs_length,
sig->si_comps[i].bs_string);
p += ((tmp + 3) >> 2);
@@ -126,7 +126,7 @@ read_cmp_blk_sig(const char *dev_name, struct bl_sig_comp *comp,
goto error;
}
- BL_LOG_ERR
+ BL_LOG_INFO
("%s: %s sig: %s, bs_string: %s, bs_length: %d, bs_offset: %lld\n",
__func__, dev_name, sig, comp->bs_string, comp->bs_length,
(long long)bs_offset);
@@ -155,7 +155,7 @@ static int verify_sig(struct bl_disk *disk, struct bl_sig *sig)
bs_offset = comp->bs_offset;
if (bs_offset < 0)
bs_offset += (((int64_t) disk->size) << 9);
- BL_LOG_ERR("%s: bs_offset: %lld\n",
+ BL_LOG_INFO("%s: bs_offset: %lld\n",
__func__, (long long) bs_offset);
ret = read_cmp_blk_sig(disk->valid_path->full_path,
comp, bs_offset);
@@ -180,7 +180,7 @@ static int map_sig_to_device(struct bl_sig *sig, struct bl_volume *vol)
struct bl_disk *lolDisk = disk;
while (lolDisk) {
- BL_LOG_ERR("%s: visible_disk_list: %s\n", __func__,
+ BL_LOG_INFO("%s: visible_disk_list: %s\n", __func__,
lolDisk->valid_path->full_path);
lolDisk = lolDisk->next;
}
@@ -324,16 +324,14 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
uint32_t *p, *end;
struct bl_volume *vols = NULL, **arrays = NULL, **arrays_ptr = NULL;
uint64_t dev = 0;
- int tried = 0;
- restart:
p = (uint32_t *) dev_addr_buf;
end = (uint32_t *) ((char *)p + dev_addr_len);
/* Decode block volume */
BLK_READBUF(p, end, 4);
READ32(num_vols);
if (num_vols <= 0) {
- BL_LOG_WARNING("Error: number of vols: %d\n", num_vols);
+ BL_LOG_ERR("Error: number of vols: %d\n", num_vols);
goto out_err;
}
@@ -363,15 +361,6 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
for (i = 0; i < num_vols; i++) {
vols[i].bv_vols = arrays_ptr;
status = decode_blk_volume(&p, end, vols, i, &count);
- if (status == -ENXIO && (tried <= 5)) {
- sleep(1);
- BL_LOG_DEBUG("%s: discover again!\n", __func__);
- bl_discover_devices();
- tried++;
- free(vols);
- free(arrays);
- goto restart;
- }
if (status)
goto out_err;
arrays_ptr += count;
@@ -383,8 +372,11 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
}
dev = dm_device_create(vols, num_vols);
- *major = MAJOR(dev);
- *minor = MINOR(dev);
+ if (dev) {
+ *major = MAJOR(dev);
+ *minor = MINOR(dev);
+ }
+
out_err:
if (vols)
free(vols);
diff --git a/utils/blkmapd/dm-device.c b/utils/blkmapd/dm-device.c
index 8162706..14ad131 100644
--- a/utils/blkmapd/dm-device.c
+++ b/utils/blkmapd/dm-device.c
@@ -379,6 +379,7 @@ uint64_t dm_device_create(struct bl_volume *vols, int num_vols)
struct bl_dm_table *bl_table_head = NULL;
unsigned int len;
char *dev_name = NULL;
+
/* Create pseudo device here */
while (number < num_vols) {
node = &vols[number];
--
1.7.1
All merged and pushed to git://linux-nfs.org/~bhalevy/pnfs-nfs-utils.git
at tag pnfs-utils-1-2-4-rc1
Thanks!
Benny
On 2010-11-02 22:08, Jim Rees wrote:
> Cumulative patches for blkmapd.
>
> Jim Rees (6):
> blkmapd: get rid of config file and instead examine all block devices
> blkmapd: don't rescan periodically
> blkmapd: don't use atomicio() where it's not needed
> ignore passive devices before checking serial numbers
> add "-d" option to just do discovery then exit
> Various bug fixes and cleanups.
>
> utils/blkmapd/Makefile.am | 3 -
> utils/blkmapd/atomicio.c | 54 --------
> utils/blkmapd/cfg.c | 248 --------------------------------------
> utils/blkmapd/cfg.h | 47 -------
> utils/blkmapd/device-discovery.c | 216 +++++++++++++++------------------
> utils/blkmapd/device-discovery.h | 5 +-
> utils/blkmapd/device-inq.c | 43 +++----
> utils/blkmapd/device-process.c | 30 ++---
> utils/blkmapd/dm-device.c | 1 +
> 9 files changed, 129 insertions(+), 518 deletions(-)
> delete mode 100644 utils/blkmapd/atomicio.c
> delete mode 100644 utils/blkmapd/cfg.c
> delete mode 100644 utils/blkmapd/cfg.h
>