[PATCH 6/9] iscsid: Refactor code for iser reuse

Refactor character device handling code as well as
connection allocation code in order to be able to reuse that in iser later on.

Signed-off-by: Yan Burman <yanb@mellanox.com>



git-svn-id: http://svn.code.sf.net/p/scst/svn/branches/iser@5234 d57e44dd-8a1f-0410-8b47-8ef2f437770f
This commit is contained in:
Vladislav Bolkhovitin
2014-01-28 04:18:35 +00:00
parent 39d0b2983a
commit bded9b5943
4 changed files with 106 additions and 67 deletions

View File

@@ -28,59 +28,17 @@
#include "iscsid.h"
#define CTL_DEVICE "/dev/iscsi-scst-ctl"
#define CTL_DEVICE "iscsi-scst-ctl"
int kernel_open(void)
{
FILE *f;
char devname[256];
char buf[256];
int devn;
int ctlfd = -1;
int err;
struct iscsi_kern_register_info reg;
if (!(f = fopen("/proc/devices", "r"))) {
err = -errno;
perror("Cannot open control path to the driver");
goto out_err;
}
devn = 0;
while (!feof(f)) {
if (!fgets(buf, sizeof(buf), f)) {
break;
}
if (sscanf(buf, "%d %s", &devn, devname) != 2) {
continue;
}
if (!strcmp(devname, "iscsi-scst-ctl")) {
break;
}
devn = 0;
}
fclose(f);
if (!devn) {
err = -ENOENT;
printf("cannot find iscsictl in /proc/devices - "
"make sure the module is loaded\n");
goto out_err;
}
unlink(CTL_DEVICE);
if (mknod(CTL_DEVICE, (S_IFCHR | 0600), (devn << 8))) {
err = -errno;
printf("cannot create %s %s\n", CTL_DEVICE, strerror(errno));
goto out_err;
}
ctlfd = open(CTL_DEVICE, O_RDWR);
if (ctlfd < 0) {
err = -errno;
printf("cannot open %s %s\n", CTL_DEVICE, strerror(errno));
goto out_err;
}
ctlfd = create_and_open_dev(CTL_DEVICE, 0);
if (ctlfd < 0)
goto out;
memset(&reg, 0, sizeof(reg));
reg.version = (uintptr_t)ISCSI_SCST_INTERFACE_VERSION;
@@ -104,7 +62,6 @@ out:
out_close:
close(ctlfd);
out_err:
ctlfd = err;
goto out;
}

View File

@@ -170,6 +170,39 @@ static void create_listen_socket(struct pollfd *array)
exit(1);
}
static struct connection *alloc_and_init_conn(int fd)
{
struct pollfd *pollfd;
struct connection *conn = NULL;
int i;
for (i = 0; i < INCOMING_MAX; i++) {
if (!incoming[i])
break;
}
if (i >= INCOMING_MAX) {
log_error("Unable to find incoming slot? %d\n", i);
goto out;
}
conn = conn_alloc();
if (!conn) {
log_error("Fail to allocate %s", "conn\n");
goto out;
}
conn->fd = fd;
incoming[i] = conn;
pollfd = &poll_array[POLL_INCOMING + i];
pollfd->fd = fd;
pollfd->events = POLLIN;
pollfd->revents = 0;
out:
return conn;
}
static int transmit_sock(int fd, bool start)
{
int opt = start;
@@ -184,9 +217,8 @@ static void accept_connection(int listen)
struct sockaddr_in6 sin6;
} from, to;
socklen_t namesize;
struct pollfd *pollfd;
struct connection *conn;
int fd, i, rc;
int fd, rc;
char initiator_addr[ISCSI_PORTAL_LEN], initiator_port[NI_MAXSERV];
char target_portal[ISCSI_PORTAL_LEN], target_portal_port[NI_MAXSERV];
@@ -245,36 +277,20 @@ static void accept_connection(int listen)
goto out_close;
}
for (i = 0; i < INCOMING_MAX; i++) {
if (!incoming[i])
break;
}
if (i >= INCOMING_MAX) {
log_error("Unable to find incoming slot? %d\n", i);
conn = alloc_and_init_conn(fd);
if (!conn)
goto out_close;
}
if (!(conn = conn_alloc())) {
log_error("Fail to allocate %s", "conn\n");
goto out_close;
}
conn->fd = fd;
conn->target_portal = strdup(target_portal);
if (conn->target_portal == NULL) {
log_error("Unable to duplicate target portal %s", target_portal);
goto out_free;
}
incoming[i] = conn;
conn->transmit = transmit_sock;
conn_read_pdu(conn);
set_non_blocking(fd);
pollfd = &poll_array[POLL_INCOMING + i];
pollfd->fd = fd;
pollfd->events = POLLIN;
pollfd->revents = 0;
incoming_cnt++;

View File

@@ -18,9 +18,74 @@
#include <netinet/tcp.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "iscsid.h"
int create_and_open_dev(const char *dev, int readonly)
{
FILE *f;
char devname[256];
char buf[256];
int devn;
int ctlfd = -1;
int err;
int flags;
f = fopen("/proc/devices", "r");
if (!f) {
err = -errno;
perror("Cannot open control path to the driver");
goto out;
}
devn = 0;
while (!feof(f)) {
if (!fgets(buf, sizeof(buf), f))
break;
if (sscanf(buf, "%d %s", &devn, devname) != 2)
continue;
if (!strcmp(devname, dev))
break;
devn = 0;
}
fclose(f);
if (!devn) {
err = -ENOENT;
printf("cannot find %s in /proc/devices - "
"make sure the module is loaded\n", dev);
goto out;
}
sprintf(devname, "/dev/%s", dev);
unlink(devname);
if (mknod(devname, (S_IFCHR | 0600), (devn << 8))) {
err = -errno;
printf("cannot create %s %s\n", devname, strerror(errno));
goto out;
}
if (readonly)
flags = O_RDONLY;
else
flags = O_RDWR;
err = ctlfd = open(devname, flags);
if (ctlfd < 0) {
err = -errno;
printf("cannot open %s %s\n", devname, strerror(errno));
goto out;
}
out:
return err;
}
void set_non_blocking(int fd)
{
int res = fcntl(fd, F_GETFL);

View File

@@ -108,5 +108,6 @@ static inline int list_length_is_one(const struct __qelem *head)
extern void set_non_blocking(int fd);
extern void sock_set_keepalive(int sock, int timeout);
extern int create_and_open_dev(const char *dev, int readonly);
#endif