Skip to content

Commit

Permalink
[SCSI] iscsi: rm kernel iscsi handles usage for session and connection
Browse files Browse the repository at this point in the history
from hare@suse.de and michaelc@cs.wisc.edu

hw iscsi like qla4xxx does not allocate a host per session and
for userspace it is difficult to restart iscsid using the
"iscsi handles" for the session and connection, so this
patch just has the class or userspace allocate the id for
the session and connection.

Note: this breaks userspace and requires users to upgrade to the newest
open-iscsi tools. Sorry about his but open-iscsi is still too new to
say we have a stable user-kernel api and we were not good nough
designers to know that other hw iscsi drivers and iscsid itself would
need such changes. Actually we sorta did but at the time we did not
have the HW available to us so we could only guess.

Luckily, the only tools hooking into the class are the open-iscsi ones
or other tools like iscsitart hook into the open-iscsi engine from
userspace or prgroams like anaconda call our tools so they are not affected.

Signed-off-by: Mike Christie <michaelc@cs.wisc.edu>
Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
  • Loading branch information
Mike Christie authored and James Bottomley committed Apr 14, 2006
1 parent 13f7e5a commit b5c7a12
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 50 deletions.
5 changes: 3 additions & 2 deletions drivers/scsi/iscsi_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3254,7 +3254,7 @@ static struct iscsi_transport iscsi_tcp_transport;

static struct iscsi_cls_session *
iscsi_session_create(struct scsi_transport_template *scsit,
uint32_t initial_cmdsn, uint32_t *sid)
uint32_t initial_cmdsn, uint32_t *hostno)
{
struct Scsi_Host *shost;
struct iscsi_session *session;
Expand All @@ -3274,7 +3274,8 @@ iscsi_session_create(struct scsi_transport_template *scsit,
session->exp_cmdsn = initial_cmdsn + 1;
session->max_cmdsn = initial_cmdsn + 1;
session->max_r2t = 1;
*sid = shost->host_no;

*hostno = shost->host_no;

/* initialize SCSI PDU commands pool */
if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
Expand Down
106 changes: 73 additions & 33 deletions drivers/scsi/scsi_transport_iscsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct iscsi_internal {
struct class_device_attribute *session_attrs[ISCSI_SESSION_ATTRS + 1];
};

static int iscsi_session_nr; /* sysfs session id for next new session */

/*
* list of registered transports and lock that must
* be held while accessing list. The iscsi_transport_lock must
Expand Down Expand Up @@ -165,14 +167,23 @@ static DEFINE_SPINLOCK(sesslock);
static LIST_HEAD(connlist);
static DEFINE_SPINLOCK(connlock);

static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle)
static uint32_t iscsi_conn_get_sid(struct iscsi_cls_conn *conn)
{
struct iscsi_cls_session *sess = iscsi_dev_to_session(conn->dev.parent);
return sess->sid;
}

/*
* Returns the matching session to a given sid
*/
static struct iscsi_cls_session *iscsi_session_lookup(uint32_t sid)
{
unsigned long flags;
struct iscsi_cls_session *sess;

spin_lock_irqsave(&sesslock, flags);
list_for_each_entry(sess, &sesslist, sess_list) {
if (sess == iscsi_ptr(handle)) {
if (sess->sid == sid) {
spin_unlock_irqrestore(&sesslock, flags);
return sess;
}
Expand All @@ -181,14 +192,17 @@ static struct iscsi_cls_session *iscsi_session_lookup(uint64_t handle)
return NULL;
}

static struct iscsi_cls_conn *iscsi_conn_lookup(uint64_t handle)
/*
* Returns the matching connection to a given sid / cid tuple
*/
static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
{
unsigned long flags;
struct iscsi_cls_conn *conn;

spin_lock_irqsave(&connlock, flags);
list_for_each_entry(conn, &connlist, conn_list) {
if (conn == iscsi_ptr(handle)) {
if ((conn->cid == cid) && (iscsi_conn_get_sid(conn) == sid)) {
spin_unlock_irqrestore(&connlock, flags);
return conn;
}
Expand Down Expand Up @@ -223,7 +237,7 @@ static int iscsi_is_session_dev(const struct device *dev)
* @shost: scsi host
* @transport: iscsi transport
*
* This can be called from a LLD or iscsi_transport
* This can be called from a LLD or iscsi_transport.
**/
struct iscsi_cls_session *
iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
Expand All @@ -234,14 +248,20 @@ iscsi_create_session(struct Scsi_Host *shost, struct iscsi_transport *transport)
if (!try_module_get(transport->owner))
return NULL;

session = kzalloc(sizeof(*session), GFP_KERNEL);
session = kzalloc(sizeof(*session) + transport->sessiondata_size,
GFP_KERNEL);
if (!session)
goto module_put;
session->transport = transport;

if (transport->sessiondata_size)
session->dd_data = &session[1];

/* this is released in the dev's release function */
scsi_host_get(shost);
snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u", shost->host_no);
session->sid = iscsi_session_nr++;
snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
session->sid);
session->dev.parent = &shost->shost_gendev;
session->dev.release = iscsi_session_release;
err = device_register(&session->dev);
Expand Down Expand Up @@ -301,12 +321,16 @@ static int iscsi_is_conn_dev(const struct device *dev)
* This can be called from a LLD or iscsi_transport. The connection
* is child of the session so cid must be unique for all connections
* on the session.
*
* Since we do not support MCS, cid will normally be zero. In some cases
* for software iscsi we could be trying to preallocate a connection struct
* in which case there could be two connection structs and cid would be
* non-zero.
**/
struct iscsi_cls_conn *
iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)
{
struct iscsi_transport *transport = session->transport;
struct Scsi_Host *shost = iscsi_session_to_shost(session);
struct iscsi_cls_conn *conn;
int err;

Expand All @@ -319,12 +343,14 @@ iscsi_create_conn(struct iscsi_cls_session *session, uint32_t cid)

INIT_LIST_HEAD(&conn->conn_list);
conn->transport = transport;
conn->cid = cid;

/* this is released in the dev's release function */
if (!get_device(&session->dev))
goto free_conn;

snprintf(conn->dev.bus_id, BUS_ID_SIZE, "connection%d:%u",
shost->host_no, cid);
session->sid, cid);
conn->dev.parent = &session->dev;
conn->dev.release = iscsi_conn_release;
err = device_register(&conn->dev);
Expand Down Expand Up @@ -607,7 +633,8 @@ int iscsi_recv_pdu(struct iscsi_cls_conn *conn, struct iscsi_hdr *hdr,
ev->type = ISCSI_KEVENT_RECV_PDU;
if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
ev->iferror = -ENOMEM;
ev->r.recv_req.conn_handle = iscsi_handle(conn);
ev->r.recv_req.cid = conn->cid;
ev->r.recv_req.sid = iscsi_conn_get_sid(conn);
pdu = (char*)ev + sizeof(*ev);
memcpy(pdu, hdr, sizeof(struct iscsi_hdr));
memcpy(pdu + sizeof(struct iscsi_hdr), data, data_size);
Expand Down Expand Up @@ -639,7 +666,8 @@ void iscsi_conn_error(struct iscsi_cls_conn *conn, enum iscsi_err error)
if (atomic_read(&conn->z_error->allocated) >= conn->z_error->hiwat)
ev->iferror = -ENOMEM;
ev->r.connerror.error = error;
ev->r.connerror.conn_handle = iscsi_handle(conn);
ev->r.connerror.cid = conn->cid;
ev->r.connerror.sid = iscsi_conn_get_sid(conn);

iscsi_unicast_skb(conn->z_error, skb);

Expand Down Expand Up @@ -689,7 +717,7 @@ iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
ISCSI_STATS_CUSTOM_MAX);
int err = 0;

conn = iscsi_conn_lookup(ev->u.get_stats.conn_handle);
conn = iscsi_conn_lookup(ev->u.get_stats.sid, ev->u.get_stats.cid);
if (!conn)
return -EEXIST;

Expand All @@ -713,8 +741,10 @@ iscsi_if_get_stats(struct iscsi_transport *transport, struct nlmsghdr *nlh)
evstat->type = nlh->nlmsg_type;
if (atomic_read(&conn->z_pdu->allocated) >= conn->z_pdu->hiwat)
evstat->iferror = -ENOMEM;
evstat->u.get_stats.conn_handle =
ev->u.get_stats.conn_handle;
evstat->u.get_stats.cid =
ev->u.get_stats.cid;
evstat->u.get_stats.sid =
ev->u.get_stats.sid;
stats = (struct iscsi_stats *)
((char*)evstat + sizeof(*evstat));
memset(stats, 0, sizeof(*stats));
Expand All @@ -740,16 +770,16 @@ iscsi_if_create_session(struct iscsi_internal *priv, struct iscsi_uevent *ev)
{
struct iscsi_transport *transport = priv->iscsi_transport;
struct iscsi_cls_session *session;
uint32_t sid;
uint32_t hostno;

session = transport->create_session(&priv->t,
ev->u.c_session.initial_cmdsn,
&sid);
&hostno);
if (!session)
return -ENOMEM;

ev->r.c_session_ret.session_handle = iscsi_handle(session);
ev->r.c_session_ret.sid = sid;
ev->r.c_session_ret.host_no = hostno;
ev->r.c_session_ret.sid = session->sid;
return 0;
}

Expand All @@ -760,13 +790,20 @@ iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
struct iscsi_cls_session *session;
unsigned long flags;

session = iscsi_session_lookup(ev->u.c_conn.session_handle);
if (!session)
session = iscsi_session_lookup(ev->u.c_conn.sid);
if (!session) {
printk(KERN_ERR "iscsi: invalid session %d\n",
ev->u.c_conn.sid);
return -EINVAL;
}

conn = transport->create_conn(session, ev->u.c_conn.cid);
if (!conn)
if (!conn) {
printk(KERN_ERR "iscsi: couldn't create a new "
"connection for session %d\n",
session->sid);
return -ENOMEM;
}

conn->z_pdu = mempool_zone_init(Z_MAX_PDU,
NLMSG_SPACE(sizeof(struct iscsi_uevent) +
Expand All @@ -788,7 +825,8 @@ iscsi_if_create_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev)
goto free_pdu_pool;
}

ev->r.handle = iscsi_handle(conn);
ev->r.c_conn_ret.sid = session->sid;
ev->r.c_conn_ret.cid = conn->cid;

spin_lock_irqsave(&connlock, flags);
list_add(&conn->conn_list, &connlist);
Expand All @@ -812,7 +850,7 @@ iscsi_if_destroy_conn(struct iscsi_transport *transport, struct iscsi_uevent *ev
struct iscsi_cls_conn *conn;
struct mempool_zone *z_error, *z_pdu;

conn = iscsi_conn_lookup(ev->u.d_conn.conn_handle);
conn = iscsi_conn_lookup(ev->u.d_conn.sid, ev->u.d_conn.cid);
if (!conn)
return -EINVAL;
spin_lock_irqsave(&connlock, flags);
Expand Down Expand Up @@ -855,7 +893,7 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
err = iscsi_if_create_session(priv, ev);
break;
case ISCSI_UEVENT_DESTROY_SESSION:
session = iscsi_session_lookup(ev->u.d_session.session_handle);
session = iscsi_session_lookup(ev->u.d_session.sid);
if (session)
transport->destroy_session(session);
else
Expand All @@ -868,8 +906,8 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
err = iscsi_if_destroy_conn(transport, ev);
break;
case ISCSI_UEVENT_BIND_CONN:
session = iscsi_session_lookup(ev->u.b_conn.session_handle);
conn = iscsi_conn_lookup(ev->u.b_conn.conn_handle);
session = iscsi_session_lookup(ev->u.b_conn.sid);
conn = iscsi_conn_lookup(ev->u.b_conn.sid, ev->u.b_conn.cid);

if (session && conn)
ev->r.retcode = transport->bind_conn(session, conn,
Expand All @@ -879,30 +917,30 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
err = -EINVAL;
break;
case ISCSI_UEVENT_SET_PARAM:
conn = iscsi_conn_lookup(ev->u.set_param.conn_handle);
conn = iscsi_conn_lookup(ev->u.set_param.sid, ev->u.set_param.cid);
if (conn)
ev->r.retcode = transport->set_param(conn,
ev->u.set_param.param, ev->u.set_param.value);
else
err = -EINVAL;
break;
case ISCSI_UEVENT_START_CONN:
conn = iscsi_conn_lookup(ev->u.start_conn.conn_handle);
conn = iscsi_conn_lookup(ev->u.start_conn.sid, ev->u.start_conn.cid);
if (conn)
ev->r.retcode = transport->start_conn(conn);
else
err = -EINVAL;

break;
case ISCSI_UEVENT_STOP_CONN:
conn = iscsi_conn_lookup(ev->u.stop_conn.conn_handle);
conn = iscsi_conn_lookup(ev->u.stop_conn.sid, ev->u.stop_conn.cid);
if (conn)
transport->stop_conn(conn, ev->u.stop_conn.flag);
else
err = -EINVAL;
break;
case ISCSI_UEVENT_SEND_PDU:
conn = iscsi_conn_lookup(ev->u.send_pdu.conn_handle);
conn = iscsi_conn_lookup(ev->u.send_pdu.sid, ev->u.send_pdu.cid);
if (conn)
ev->r.retcode = transport->send_pdu(conn,
(struct iscsi_hdr*)((char*)ev + sizeof(*ev)),
Expand All @@ -923,9 +961,11 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
return err;
}

/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
* processed by iscsi_if_recv_msg. Malformed skbs with wrong length are
* or invalid creds discarded silently. */
/*
* Get message from skb (based on rtnetlink_rcv_skb). Each message is
* processed by iscsi_if_recv_msg. Malformed skbs with wrong lengths or
* invalid creds are discarded silently.
*/
static void
iscsi_if_rx(struct sock *sk, int len)
{
Expand Down
37 changes: 23 additions & 14 deletions include/scsi/iscsi_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,59 +60,68 @@ struct iscsi_uevent {
uint32_t initial_cmdsn;
} c_session;
struct msg_destroy_session {
uint64_t session_handle;
uint32_t sid;
} d_session;
struct msg_create_conn {
uint64_t session_handle;
uint32_t cid;
uint32_t sid;
uint32_t cid;
} c_conn;
struct msg_bind_conn {
uint64_t session_handle;
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
uint32_t transport_fd;
uint32_t is_leading;
} b_conn;
struct msg_destroy_conn {
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
} d_conn;
struct msg_send_pdu {
uint32_t sid;
uint32_t cid;
uint32_t hdr_size;
uint32_t data_size;
uint64_t conn_handle;
} send_pdu;
struct msg_set_param {
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
uint32_t param; /* enum iscsi_param */
uint32_t value;
} set_param;
struct msg_start_conn {
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
} start_conn;
struct msg_stop_conn {
uint32_t sid;
uint32_t cid;
uint64_t conn_handle;
uint32_t flag;
} stop_conn;
struct msg_get_stats {
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
} get_stats;
} u;
union {
/* messages k -> u */
uint64_t handle;
int retcode;
struct msg_create_session_ret {
uint64_t session_handle;
uint32_t sid;
uint32_t host_no;
} c_session_ret;
struct msg_create_conn_ret {
uint32_t sid;
uint32_t cid;
} c_conn_ret;
struct msg_recv_req {
uint32_t sid;
uint32_t cid;
uint64_t recv_handle;
uint64_t conn_handle;
} recv_req;
struct msg_conn_error {
uint64_t conn_handle;
uint32_t sid;
uint32_t cid;
uint32_t error; /* enum iscsi_err */
} connerror;
} r;
Expand Down
Loading

0 comments on commit b5c7a12

Please sign in to comment.