iscsi: fix leak of task for delayed management request

Fix the following crash of tgtd which occured when removing a target
with --force option:

  conn_close(90) already closed 0x2058598 1
  iscsi_target_destroy(416) bug still have sessions 91

This crash happens if a task remains after the following calls of
conn_close() function for every connection on the target.

  list_for_each_entry_safe(session, stmp, &target->sessions_list, slist) {
          list_for_each_entry_safe(conn, ctmp, &session->conn_list, clist) {
                  conn_close(conn);
          }
  }

The leaked task turned out to be a task for handling ABORT_TASK
management request which was delayed due to a flying io.  The delayed
management request is attached to cmd->mreq of the io command and is
completed by iscsi_tm_done() function.  However, this function fails
with the following error and leaks the task if associated connection
(task->conn) is already closed:

 tgt_event_modify(245) Cannot find event 25
 iscsi_event_modify(560) tgt_event_modify failed

This fixes the issue by freeing task in iscsi_tm_done() if state of
the connection is STATE_CLOSE.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
iscsi: fix segfault at iscsi_scsi_cmd_done()

When enabling ping-nop of iscsi by setting valid values to
nop_interval and nop_count parameters, tgtd crashed with the following
segmentation fault at iscsi_scsi_cmd_done() function:

 Program terminated with signal 11, Segmentation fault.
 #0  __list_add (nid=<value optimized out>, result=<value optimized out>,
     scmd=0x980f20) at ./list.h:67
 67              prev->next = new;
 (gdb) bt
 #0  __list_add (nid=<value optimized out>, result=<value optimized out>,
     scmd=0x980f20) at ./list.h:67
 #1  list_add_tail (nid=<value optimized out>, result=<value optimized out>,
     scmd=0x980f20) at ./list.h:77
 #2  iscsi_scsi_cmd_done (nid=<value optimized out>,
     result=<value optimized out>, scmd=0x980f20) at iscsi/iscsid.c:1270
 #3  0x000000000042b69d in bs_sig_request_done (fd=<value optimized out>,
     events=<value optimized out>, data=<value optimized out>) at bs.c:212
 #4  0x0000000000414231 in event_loop () at tgtd.c:432
 #5  0x000000000041490a in main (argc=<value optimized out>,
     argv=<value optimized out>) at tgtd.c:620

This segfault is caused by forcible destruction of tcp connection
using iscsi_tcp_release() within iscsi_tcp_nop_work_handler().

This patch fixes it by closing connection cleanly there with
conn_close() function.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@lab.ntt.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Fix segfault if device_type set to pt but bstype not sg or bsg

If bstype not set to sg or bsg, it will default to rdwr, and we will
call target_cmd_perform instead of target_cmd_perform_passthrough when
accessing the device, which crashes tgtd. There's an existing check for
setting bstype without setting dev_type, but also add a check the other
way, setting dev_type without setting bstype to a valid value for pt.

Add an error message to the existing check, as well.

Signed-off-by: Andy Grover <agrover@redhat.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
tgtd: add helper functions for checking iostate of iscsi connections

This patch adds helper functions for checking iostate of iscsi
connections from outside of iscsid.c. These functions will be used in
iscsi_tcp.c in the future commits.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
tgtd: decompose iscsi_[rt]x_handler()

Basic strategy of parallelizing sending/receiving iSCSI PDUs is like
this: offload do_recv(), do_send() and crc checking to worker threads
and do other part in the main event loop thread.

To follow the above strategy, this patch decomposes iscsi_rx_handler()
and iscsi_tx_handler() into some parts. Some of them can be called in
worker threads and others are not. In the future commit,
multi-threaded TCP event handler will be implemented based on
them. This patch re-implements single threaded TCP event handler
(past iscsi_tcp_event_handler(), renamed iscsi_tcp_st_event_handler())
based on these decomposed parts. iscsi_tcp_st_event_handler() is
also required because under some workload the multi-threaded version
degrades performance because of frequent communication between
threads.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
tgtd: add a new option "-T" for specifying a number of threads which send/recv iSCSI PDUs

Currently, nr_iothreads, specified with "-t" option, is used as a
number of worker threads per logical unit conventionally in some
backing store drivers.

This patch adds a new option "-T" to tgtd for specified a number of
threads which send/recv iSCSI PDUs. Because using the "-t" option for
this purpose would be introduce inconvenience of performance tuning.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
tgtd: implement a deferred event modification mechanism

In the next commit, worker threads for sending/receiving iSCSI PDUs
will be implemented. When the feature is used, the worker threads read
and write fds created by accept(2). For avoiding conflicts between the
main event loop thread and the worker threads, no event should be
notified to the main event via epoll_wait(2) when the worker threads
are running.

For this purpose, this patch implements a deferred event modification
mechanism. Two new members are added to iscsi_tcp_connection:
used_in_worker_threads and restore_events. The first one is used for
indicating that the connection is used by worker threads. The second
one is used for storing an event mask which should be set to the
fd. When control of the fd is passed back to the main event, the
stored event mask is set via tgt_event_modify().

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
tgtd: offload iSCSI PDU send/recv to worker threads

Current tgtd sends and receives iSCSI PDUs in its main event
loop. This design can cause bottleneck when many iSCSI clients connect
to single tgtd process. For example, we need multiple tgtd processes
for utilizing fast network like 10 GbE because typical single
processor core isn't fast enough for processing a bunch of requests.

This patch lets tgtd offload send/recv iSCSI PDUs and check digests to
worker threads when a parameter of "-T" option is larger than 1. The
offloading is done by a newly added event handler,
iscsi_tcp_mt_event_handler(). When "-T" isn't passed or value 1 is
passed, single threaded version (iscsi_tcp_st_event_handler()) is
used. The single threaded version is provided because in some cases
the multi-threaded version degrades performance (e.g. a number of
initiators are not so large).

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>
tgtd: let conn_close() transfer state of a connection

Current tgtd seems to have a possibility that it has a connection
whose state isn't STATE_CLOSE even after the connection is passed to
conn_close(). Such a connection is confusing because
iscsi_scsi_cmd_done() cannot detect the closing of the connection.

This patch lets conn_close() transfer state of a connection to
STATE_CLOSE for avoiding this purpose.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi@lab.ntt.co.jp>