3
* (C) 2004 - 2005 FUJITA Tomonori <tomof@acm.org>
4
* (C) 2008 Arne Redlich <agr@powerkom-dd.de>
6
* This code is licenced under the GPL.
9
#include <linux/sched.h>
10
#include <linux/file.h>
11
#include <linux/kthread.h>
12
#include <asm/ioctls.h>
15
#include "iscsi_dbg.h"
18
enum daemon_state_bit {
23
void __nthread_wakeup(struct network_thread_info *info)
25
set_bit(D_DATA_READY, &info->flags);
26
wake_up_process(info->task);
29
void nthread_wakeup(struct iscsi_target *target)
31
struct network_thread_info *info = &target->nthread_info;
33
spin_lock_bh(&info->nthread_lock);
34
__nthread_wakeup(info);
35
spin_unlock_bh(&info->nthread_lock);
38
static inline void iscsi_conn_init_read(struct iscsi_conn *conn, void *data, size_t len)
40
len = (len + 3) & -4; // XXX ???
41
conn->read_iov[0].iov_base = data;
42
conn->read_iov[0].iov_len = len;
43
conn->read_msg.msg_iov = conn->read_iov;
44
conn->read_msg.msg_iovlen = 1;
45
conn->read_size = (len + 3) & -4;
48
static void iscsi_conn_read_ahs(struct iscsi_conn *conn, struct iscsi_cmnd *cmnd)
50
cmnd->pdu.ahs = kmalloc(cmnd->pdu.ahssize, __GFP_NOFAIL|GFP_KERNEL);
51
assert(cmnd->pdu.ahs);
52
iscsi_conn_init_read(conn, cmnd->pdu.ahs, cmnd->pdu.ahssize);
55
static struct iscsi_cmnd * iscsi_get_send_cmnd(struct iscsi_conn *conn)
57
struct iscsi_cmnd *cmnd = NULL;
59
spin_lock(&conn->list_lock);
60
if (!list_empty(&conn->write_list)) {
61
cmnd = list_entry(conn->write_list.next, struct iscsi_cmnd, list);
62
list_del_init(&cmnd->list);
64
spin_unlock(&conn->list_lock);
69
static int is_data_available(struct iscsi_conn *conn)
73
struct socket *sock = conn->sock;
77
res = sock->ops->ioctl(sock, SIOCINQ, (unsigned long) &avail);
79
return (res >= 0) ? avail : res;
82
static void forward_iov(struct msghdr *msg, int len)
84
while (msg->msg_iov->iov_len <= len) {
85
len -= msg->msg_iov->iov_len;
90
msg->msg_iov->iov_base = (char *) msg->msg_iov->iov_base + len;
91
msg->msg_iov->iov_len -= len;
94
static int do_recv(struct iscsi_conn *conn, int state)
98
struct iovec iov[ISCSI_CONN_IOV_MAX];
101
if (!test_bit(CONN_ACTIVE, &conn->state)) {
106
if (is_data_available(conn) <= 0) {
112
msg.msg_iovlen = min_t(size_t, conn->read_msg.msg_iovlen, ISCSI_CONN_IOV_MAX);
113
for (i = 0, len = 0; i < msg.msg_iovlen; i++) {
114
iov[i] = conn->read_msg.msg_iov[i];
115
len += iov[i].iov_len;
120
res = sock_recvmsg(conn->sock, &msg, len, MSG_DONTWAIT | MSG_NOSIGNAL);
129
eprintk("%d\n", res);
134
conn->read_size -= res;
136
forward_iov(&conn->read_msg, res);
138
conn->read_state = state;
142
dprintk(D_IOD, "%d\n", res);
148
RX_INIT_BHS, /* Must be zero. */
168
static void rx_ddigest(struct iscsi_conn *conn, int state)
170
struct iscsi_cmnd *cmnd = conn->read_cmnd;
171
int res = digest_rx_data(cmnd);
174
conn->read_state = state;
179
static void rx_hdigest(struct iscsi_conn *conn, int state)
181
struct iscsi_cmnd *cmnd = conn->read_cmnd;
182
int res = digest_rx_header(cmnd);
185
conn->read_state = state;
190
static struct iscsi_cmnd *create_cmnd(struct iscsi_conn *conn)
192
struct iscsi_cmnd *cmnd;
194
cmnd = cmnd_alloc(conn, 1);
195
iscsi_conn_init_read(cmnd->conn, &cmnd->pdu.bhs, sizeof(cmnd->pdu.bhs));
196
conn->read_state = RX_BHS;
201
static int recv(struct iscsi_conn *conn)
203
struct iscsi_cmnd *cmnd = conn->read_cmnd;
204
int hdigest, ddigest, res = 1;
206
if (!test_bit(CONN_ACTIVE, &conn->state))
209
hdigest = conn->hdigest_type & DIGEST_NONE ? 0 : 1;
210
ddigest = conn->ddigest_type & DIGEST_NONE ? 0 : 1;
213
switch (conn->read_state) {
216
cmnd = conn->read_cmnd = create_cmnd(conn);
218
res = do_recv(conn, RX_INIT_AHS);
219
if (res <= 0 || conn->read_state != RX_INIT_AHS)
222
iscsi_cmnd_get_length(&cmnd->pdu);
223
if (cmnd->pdu.ahssize) {
224
iscsi_conn_read_ahs(conn, cmnd);
225
conn->read_state = RX_AHS;
227
conn->read_state = hdigest ? RX_INIT_HDIGEST : RX_INIT_DATA;
229
if (conn->read_state != RX_AHS)
232
res = do_recv(conn, hdigest ? RX_INIT_HDIGEST : RX_INIT_DATA);
233
if (res <= 0 || conn->read_state != RX_INIT_HDIGEST)
235
case RX_INIT_HDIGEST:
236
iscsi_conn_init_read(conn, &cmnd->hdigest, sizeof(u32));
237
conn->read_state = RX_HDIGEST;
239
res = do_recv(conn, RX_CHECK_HDIGEST);
240
if (res <= 0 || conn->read_state != RX_CHECK_HDIGEST)
242
case RX_CHECK_HDIGEST:
243
rx_hdigest(conn, RX_INIT_DATA);
244
if (conn->read_state != RX_INIT_DATA)
248
conn->read_state = cmnd->pdu.datasize ? RX_DATA : RX_END;
249
if (conn->read_state != RX_DATA)
252
res = do_recv(conn, ddigest ? RX_INIT_DDIGEST : RX_END);
253
if (res <= 0 || conn->read_state != RX_INIT_DDIGEST)
255
case RX_INIT_DDIGEST:
256
iscsi_conn_init_read(conn, &cmnd->ddigest, sizeof(u32));
257
conn->read_state = RX_DDIGEST;
259
res = do_recv(conn, RX_CHECK_DDIGEST);
260
if (res <= 0 || conn->read_state != RX_CHECK_DDIGEST)
262
case RX_CHECK_DDIGEST:
263
rx_ddigest(conn, RX_END);
266
eprintk("%d %d %x\n", res, conn->read_state, cmnd_opcode(cmnd));
273
if (conn->read_state != RX_END)
276
if (conn->read_size) {
277
eprintk("%d %x %d\n", res, cmnd_opcode(cmnd), conn->read_size);
282
if (conn->read_size) {
283
eprintk("%x %d\n", cmnd_opcode(cmnd), conn->read_size);
284
conn->read_state = RX_DATA;
288
conn->read_cmnd = NULL;
289
conn->read_state = RX_INIT_BHS;
295
* @locking: grabs the target's nthread_lock to protect it from races with
298
static void set_conn_wspace_wait(struct iscsi_conn *conn)
300
struct network_thread_info *info = &conn->session->target->nthread_info;
301
struct sock *sk = conn->sock->sk;
303
spin_lock_bh(&info->nthread_lock);
305
if (sk_stream_wspace(sk) < sk_stream_min_wspace(sk))
306
set_bit(CONN_WSPACE_WAIT, &conn->state);
308
spin_unlock_bh(&info->nthread_lock);
311
/* This is taken from the Ardis code. */
312
static int write_data(struct iscsi_conn *conn)
317
ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
320
int saved_size, size, sendsize;
325
saved_size = size = conn->write_size;
326
iop = conn->write_iop;
335
for (count = 0; vec->iov_len; count++, vec++)
339
res = vfs_writev(file, (struct iovec __user *) iop, count, &off);
341
dprintk(D_DATA, "%#Lx:%u: %d(%ld)\n",
342
(unsigned long long) conn->session->sid, conn->cid,
343
res, (long) iop->iov_len);
344
if (unlikely(res <= 0)) {
345
if (res == -EAGAIN || res == -EINTR) {
346
conn->write_iop = iop;
354
while (iop->iov_len <= rest && rest) {
355
rest -= iop->iov_len;
358
iop->iov_base += rest;
359
iop->iov_len -= rest;
362
conn->write_iop = NULL;
369
if (!(tio = conn->write_tcmnd)) {
370
eprintk("%s\n", "warning data missing!");
373
offset = conn->write_offset;
374
idx = offset >> PAGE_CACHE_SHIFT;
375
offset &= ~PAGE_CACHE_MASK;
378
sendpage = sock->ops->sendpage ? : sock_no_sendpage;
379
flags = MSG_DONTWAIT;
382
sendsize = PAGE_CACHE_SIZE - offset;
383
if (size <= sendsize) {
384
res = sendpage(sock, tio->pvec[idx], offset, size, flags);
385
dprintk(D_DATA, "%s %#Lx:%u: %d(%lu,%u,%u)\n",
386
sock->ops->sendpage ? "sendpage" : "writepage",
387
(unsigned long long ) conn->session->sid, conn->cid,
388
res, tio->pvec[idx]->index, offset, size);
389
if (unlikely(res <= 0)) {
390
if (res == -EAGAIN || res == -EINTR) {
396
conn->write_tcmnd = NULL;
397
conn->write_size = 0;
405
res = sendpage(sock, tio->pvec[idx], offset,sendsize, flags | MSG_MORE);
406
dprintk(D_DATA, "%s %#Lx:%u: %d(%lu,%u,%u)\n",
407
sock->ops->sendpage ? "sendpage" : "writepage",
408
(unsigned long long ) conn->session->sid, conn->cid,
409
res, tio->pvec[idx]->index, offset, sendsize);
410
if (unlikely(res <= 0)) {
411
if (res == -EAGAIN || res == -EINTR) {
416
if (res == sendsize) {
424
conn->write_offset = (idx << PAGE_CACHE_SHIFT) + offset;
426
conn->write_size = size;
427
if (res == -EAGAIN) {
428
set_conn_wspace_wait(conn);
429
if (saved_size == size)
433
return saved_size - size;
436
eprintk("error %d at %#Lx:%u\n", res,
437
(unsigned long long) conn->session->sid, conn->cid);
441
static void exit_tx(struct iscsi_conn *conn, int res)
451
eprintk("%d %d %d\n", conn->write_size, conn->write_state, res);
457
static int tx_ddigest(struct iscsi_cmnd *cmnd, int state)
459
int res, rest = cmnd->conn->write_size;
460
struct msghdr msg = {.msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT};
463
iov.iov_base = (char *) (&cmnd->ddigest) + (sizeof(u32) - rest);
466
res = kernel_sendmsg(cmnd->conn->sock, &msg, &iov, 1, rest);
469
cmnd->conn->write_size -= res;
470
if (!cmnd->conn->write_size)
471
cmnd->conn->write_state = state;
473
exit_tx(cmnd->conn, res);
478
static void init_tx_hdigest(struct iscsi_cmnd *cmnd)
480
struct iscsi_conn *conn = cmnd->conn;
483
if (conn->hdigest_type & DIGEST_NONE)
486
digest_tx_header(cmnd);
488
for (iop = conn->write_iop; iop->iov_len; iop++)
490
iop->iov_base = &(cmnd->hdigest);
491
iop->iov_len = sizeof(u32);
492
conn->write_size += sizeof(u32);
500
TX_INIT, /* Must be zero. */
507
static int do_send(struct iscsi_conn *conn, int state)
511
res = write_data(conn);
514
if (!conn->write_size)
515
conn->write_state = state;
522
static int send(struct iscsi_conn *conn)
524
struct iscsi_cmnd *cmnd = conn->write_cmnd;
525
int ddigest, res = 0;
527
ddigest = conn->ddigest_type != DIGEST_NONE ? 1 : 0;
530
switch (conn->write_state) {
533
cmnd = conn->write_cmnd = iscsi_get_send_cmnd(conn);
537
init_tx_hdigest(cmnd);
538
conn->write_state = TX_BHS_DATA;
540
res = do_send(conn, ddigest && cmnd->pdu.datasize ? TX_INIT_DDIGEST : TX_END);
541
if (res <= 0 || conn->write_state != TX_INIT_DDIGEST)
543
case TX_INIT_DDIGEST:
544
digest_tx_data(cmnd);
545
assert(!cmnd->conn->write_size);
546
cmnd->conn->write_size += sizeof(u32);
547
conn->write_state = TX_DDIGEST;
549
res = tx_ddigest(cmnd, TX_END);
552
eprintk("%d %d %x\n", res, conn->write_state, cmnd_opcode(cmnd));
559
if (conn->write_state != TX_END)
562
if (conn->write_size) {
563
eprintk("%d %x %u\n", res, cmnd_opcode(cmnd), conn->write_size);
564
assert(!conn->write_size);
567
cmnd_release(cmnd, 0);
568
conn->write_cmnd = NULL;
569
conn->write_state = TX_INIT;
574
static void conn_nop_timeout(unsigned long data)
576
struct iscsi_conn *conn = (struct iscsi_conn *)data;
578
if (test_bit(CONN_ACTIVE, &conn->state))
579
set_bit(CONN_NEED_NOP_IN, &conn->state);
581
dprintk(D_THREAD, "conn %llu:%hu, NOP timer %p\n", conn->session->sid,
582
conn->cid, &conn->nop_timer);
584
nthread_wakeup(conn->session->target);
587
static void conn_reset_nop_timer(struct iscsi_conn *conn)
589
struct iscsi_target *target = conn->session->target;
591
if (target->trgt_param.nop_interval)
592
mod_timer(&conn->nop_timer,
593
jiffies + HZ * target->trgt_param.nop_interval);
596
static void conn_start_nop_timer(struct iscsi_conn *conn)
598
struct iscsi_target *target = conn->session->target;
600
if (!target->trgt_param.nop_interval || timer_pending(&conn->nop_timer))
603
conn->nop_timer.data = (unsigned long)conn;
604
conn->nop_timer.function = conn_nop_timeout;
606
dprintk(D_THREAD, "conn %llu:%hu, NOP timer %p\n", conn->session->sid,
607
conn->cid, &conn->nop_timer);
609
mod_timer(&conn->nop_timer,
610
jiffies + HZ * target->trgt_param.nop_interval);
613
static void process_io(struct iscsi_conn *conn)
615
struct iscsi_target *target = conn->session->target;
620
if (is_data_available(conn) > 0 || res > 0) {
621
conn_reset_nop_timer(conn);
625
if (!test_bit(CONN_ACTIVE, &conn->state)) {
630
if (test_bit(CONN_WSPACE_WAIT, &conn->state))
635
if (!list_empty(&conn->write_list) || conn->write_cmnd) {
636
conn_reset_nop_timer(conn);
642
nthread_wakeup(target);
643
else if (test_and_clear_bit(CONN_NEED_NOP_IN, &conn->state)) {
645
nthread_wakeup(target);
647
conn_start_nop_timer(conn);
652
static void close_conn(struct iscsi_conn *conn)
654
struct iscsi_session *session = conn->session;
655
struct iscsi_target *target = conn->session->target;
656
struct iscsi_cmnd *cmnd;
658
if (target->trgt_param.nop_interval)
659
del_timer_sync(&conn->nop_timer);
661
conn->sock->ops->shutdown(conn->sock, 2);
663
write_lock_bh(&conn->sock->sk->sk_callback_lock);
664
conn->sock->sk->sk_state_change = target->nthread_info.old_state_change;
665
conn->sock->sk->sk_data_ready = target->nthread_info.old_data_ready;
666
conn->sock->sk->sk_write_space = target->nthread_info.old_write_space;
667
write_unlock_bh(&conn->sock->sk->sk_callback_lock);
673
while (atomic_read(&conn->nr_busy_cmnds))
676
while (!list_empty(&conn->pdu_list)) {
677
cmnd = list_entry(conn->pdu_list.next, struct iscsi_cmnd, conn_list);
679
list_del_init(&cmnd->list);
680
cmnd_release(cmnd, 1);
683
if (atomic_read(&conn->nr_cmnds)) {
684
eprintk("%u\n", atomic_read(&conn->nr_cmnds));
685
list_for_each_entry(cmnd, &conn->pdu_list, conn_list)
686
eprintk("%x %x\n", cmnd_opcode(cmnd), cmnd_itt(cmnd));
690
event_send(target->tid, session->sid, conn->cid, E_CONN_CLOSE, 0);
693
if (list_empty(&session->conn_list)) {
695
complete(session->done);
697
session_del(target, session->sid);
701
static int istd(void *arg)
703
struct iscsi_target *target = arg;
704
struct network_thread_info *info = &target->nthread_info;
705
struct iscsi_conn *conn, *tmp;
707
__set_current_state(TASK_RUNNING);
709
spin_lock_bh(&info->nthread_lock);
710
__set_current_state(TASK_INTERRUPTIBLE);
712
if (!test_bit(D_DATA_READY, &info->flags)) {
713
spin_unlock_bh(&info->nthread_lock);
715
spin_lock_bh(&info->nthread_lock);
717
__set_current_state(TASK_RUNNING);
718
clear_bit(D_DATA_READY, &info->flags);
719
spin_unlock_bh(&info->nthread_lock);
721
target_lock(target, 0);
722
list_for_each_entry_safe(conn, tmp, &info->active_conns, poll_list) {
723
if (test_bit(CONN_ACTIVE, &conn->state))
728
target_unlock(target);
730
} while (!kthread_should_stop());
735
int nthread_init(struct iscsi_target *target)
737
struct network_thread_info *info = &target->nthread_info;
742
info->old_state_change = NULL;
743
info->old_data_ready = NULL;
744
info->old_write_space = NULL;
746
INIT_LIST_HEAD(&info->active_conns);
748
spin_lock_init(&info->nthread_lock);
753
int nthread_start(struct iscsi_target *target)
756
struct network_thread_info *info = &target->nthread_info;
757
struct task_struct *task;
760
eprintk("Target (%u) already runs\n", target->tid);
764
task = kthread_run(istd, target, "istd%d", target->tid);
774
int nthread_stop(struct iscsi_target *target)
777
struct network_thread_info *info = &target->nthread_info;
782
err = kthread_stop(info->task);
784
if (err < 0 && err != -EINTR)