Whamcloud - gitweb
LU-812 kernel: remove smp_lock.h
[fs/lustre-release.git] / lnet / selftest / conrpc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/selftest/conctl.c
37  *
38  * Console framework rpcs
39  *
40  * Author: Liang Zhen <liang@whamcloud.com>
41  */
42
43 #ifdef __KERNEL__
44
45 #include <libcfs/libcfs.h>
46 #include <lnet/lib-lnet.h>
47 #include "timer.h"
48 #include "conrpc.h"
49 #include "console.h"
50
51 void lstcon_rpc_stat_reply(lstcon_rpc_trans_t *, srpc_msg_t *,
52                            lstcon_node_t *, lstcon_trans_stat_t *);
53
54 static void
55 lstcon_rpc_done(srpc_client_rpc_t *rpc)
56 {
57         lstcon_rpc_t *crpc = (lstcon_rpc_t *)rpc->crpc_priv;
58
59         LASSERT (crpc != NULL && rpc == crpc->crp_rpc);
60         LASSERT (crpc->crp_posted && !crpc->crp_finished);
61
62         cfs_spin_lock(&rpc->crpc_lock);
63
64         if (crpc->crp_trans == NULL) {
65                 /* Orphan RPC is not in any transaction, 
66                  * I'm just a poor body and nobody loves me */
67                 cfs_spin_unlock(&rpc->crpc_lock);
68
69                 /* release it */
70                 lstcon_rpc_put(crpc);
71                 return;
72         }
73
74         /* not an orphan RPC */
75         crpc->crp_finished = 1;
76
77         if (crpc->crp_stamp == 0) {
78                 /* not aborted */
79                 LASSERT (crpc->crp_status == 0);
80
81                 crpc->crp_stamp  = cfs_time_current();
82                 crpc->crp_status = rpc->crpc_status;
83         }
84
85         /* wakeup (transaction)thread if I'm the last RPC in the transaction */
86         if (cfs_atomic_dec_and_test(&crpc->crp_trans->tas_remaining))
87                 cfs_waitq_signal(&crpc->crp_trans->tas_waitq);
88
89         cfs_spin_unlock(&rpc->crpc_lock);
90 }
91
92 int
93 lstcon_rpc_init(lstcon_node_t *nd, int service, unsigned feats,
94                 int bulk_npg, int bulk_len, int embedded, lstcon_rpc_t *crpc)
95 {
96         crpc->crp_rpc = sfw_create_rpc(nd->nd_id, service,
97                                        feats, bulk_npg, bulk_len,
98                                        lstcon_rpc_done, (void *)crpc);
99         if (crpc->crp_rpc == NULL)
100                 return -ENOMEM;
101
102         crpc->crp_trans    = NULL;
103         crpc->crp_node     = nd;
104         crpc->crp_posted   = 0;
105         crpc->crp_finished = 0;
106         crpc->crp_unpacked = 0;
107         crpc->crp_status   = 0;
108         crpc->crp_stamp    = 0;
109         crpc->crp_embedded = embedded;
110         CFS_INIT_LIST_HEAD(&crpc->crp_link);
111
112         cfs_atomic_inc(&console_session.ses_rpc_counter);
113
114         return 0;
115 }
116
117 int
118 lstcon_rpc_prep(lstcon_node_t *nd, int service, unsigned feats,
119                 int bulk_npg, int bulk_len, lstcon_rpc_t **crpcpp)
120 {
121         lstcon_rpc_t  *crpc = NULL;
122         int            rc;
123
124         cfs_spin_lock(&console_session.ses_rpc_lock);
125
126         if (!cfs_list_empty(&console_session.ses_rpc_freelist)) {
127                 crpc = cfs_list_entry(console_session.ses_rpc_freelist.next,
128                                       lstcon_rpc_t, crp_link);
129                 cfs_list_del_init(&crpc->crp_link);
130         }
131
132         cfs_spin_unlock(&console_session.ses_rpc_lock);
133
134         if (crpc == NULL) {
135                 LIBCFS_ALLOC(crpc, sizeof(*crpc));
136                 if (crpc == NULL)
137                         return -ENOMEM;
138         }
139
140         rc = lstcon_rpc_init(nd, service, feats,
141                              bulk_npg, bulk_len, 0, crpc);
142         if (rc == 0) {
143                 *crpcpp = crpc;
144                 return 0;
145         }
146
147         LIBCFS_FREE(crpc, sizeof(*crpc));
148
149         return rc;
150 }
151
152 void
153 lstcon_rpc_put(lstcon_rpc_t *crpc)
154 {
155         srpc_bulk_t *bulk = &crpc->crp_rpc->crpc_bulk;
156         int          i;
157
158         LASSERT (cfs_list_empty(&crpc->crp_link));
159
160         for (i = 0; i < bulk->bk_niov; i++) {
161                 if (bulk->bk_iovs[i].kiov_page == NULL)
162                         continue;
163
164                 cfs_free_page(bulk->bk_iovs[i].kiov_page);
165         }
166
167         srpc_client_rpc_decref(crpc->crp_rpc);
168
169         if (crpc->crp_embedded) {
170                 /* embedded RPC, don't recycle it */
171                 memset(crpc, 0, sizeof(*crpc));
172                 crpc->crp_embedded = 1;
173
174         } else {
175                 cfs_spin_lock(&console_session.ses_rpc_lock);
176
177                 cfs_list_add(&crpc->crp_link,
178                              &console_session.ses_rpc_freelist);
179
180                 cfs_spin_unlock(&console_session.ses_rpc_lock);
181         }
182
183         /* RPC is not alive now */
184         cfs_atomic_dec(&console_session.ses_rpc_counter);
185 }
186
187 void
188 lstcon_rpc_post(lstcon_rpc_t *crpc)
189 {
190         lstcon_rpc_trans_t *trans = crpc->crp_trans;
191
192         LASSERT (trans != NULL);
193
194         cfs_atomic_inc(&trans->tas_remaining);
195         crpc->crp_posted = 1;
196
197         sfw_post_rpc(crpc->crp_rpc);
198 }
199
200 static char *
201 lstcon_rpc_trans_name(int transop)
202 {
203         if (transop == LST_TRANS_SESNEW)
204                 return "SESNEW";
205
206         if (transop == LST_TRANS_SESEND)
207                 return "SESEND";
208
209         if (transop == LST_TRANS_SESQRY)
210                 return "SESQRY";
211
212         if (transop == LST_TRANS_SESPING)
213                 return "SESPING";
214
215         if (transop == LST_TRANS_TSBCLIADD)
216                 return "TSBCLIADD";
217
218         if (transop == LST_TRANS_TSBSRVADD)
219                 return "TSBSRVADD";
220
221         if (transop == LST_TRANS_TSBRUN)
222                 return "TSBRUN";
223
224         if (transop == LST_TRANS_TSBSTOP)
225                 return "TSBSTOP";
226
227         if (transop == LST_TRANS_TSBCLIQRY)
228                 return "TSBCLIQRY";
229
230         if (transop == LST_TRANS_TSBSRVQRY)
231                 return "TSBSRVQRY";
232
233         if (transop == LST_TRANS_STATQRY)
234                 return "STATQRY";
235
236         return "Unknown";
237 }
238
239 int
240 lstcon_rpc_trans_prep(cfs_list_t *translist,
241                       int transop, lstcon_rpc_trans_t **transpp)
242 {
243         lstcon_rpc_trans_t *trans;
244
245         if (translist != NULL) {
246                 cfs_list_for_each_entry_typed(trans, translist,
247                                               lstcon_rpc_trans_t, tas_link) {
248                         /* Can't enqueue two private transaction on
249                          * the same object */
250                         if ((trans->tas_opc & transop) == LST_TRANS_PRIVATE)
251                                 return -EPERM;
252                 }
253         }
254
255         /* create a trans group */
256         LIBCFS_ALLOC(trans, sizeof(*trans));
257         if (trans == NULL)
258                 return -ENOMEM;
259
260         trans->tas_opc = transop;
261
262         if (translist == NULL)
263                 CFS_INIT_LIST_HEAD(&trans->tas_olink);
264         else
265                 cfs_list_add_tail(&trans->tas_olink, translist);
266
267         cfs_list_add_tail(&trans->tas_link, &console_session.ses_trans_list);
268
269         CFS_INIT_LIST_HEAD(&trans->tas_rpcs_list);
270         cfs_atomic_set(&trans->tas_remaining, 0);
271         cfs_waitq_init(&trans->tas_waitq);
272
273         cfs_spin_lock(&console_session.ses_rpc_lock);
274         trans->tas_features = console_session.ses_features;
275         cfs_spin_unlock(&console_session.ses_rpc_lock);
276
277         *transpp = trans;
278         return 0;
279 }
280
281 void
282 lstcon_rpc_trans_addreq(lstcon_rpc_trans_t *trans, lstcon_rpc_t *crpc)
283 {
284         cfs_list_add_tail(&crpc->crp_link, &trans->tas_rpcs_list);
285         crpc->crp_trans = trans;
286 }
287
288 void
289 lstcon_rpc_trans_abort(lstcon_rpc_trans_t *trans, int error)
290 {
291         srpc_client_rpc_t *rpc;
292         lstcon_rpc_t      *crpc;
293         lstcon_node_t     *nd;
294
295         cfs_list_for_each_entry_typed (crpc, &trans->tas_rpcs_list,
296                                        lstcon_rpc_t, crp_link) {
297                 rpc = crpc->crp_rpc;
298
299                 cfs_spin_lock(&rpc->crpc_lock);
300
301                 if (!crpc->crp_posted || /* not posted */
302                     crpc->crp_stamp != 0) { /* rpc done or aborted already */
303                         if (crpc->crp_stamp == 0) {
304                                 crpc->crp_stamp = cfs_time_current();
305                                 crpc->crp_status = -EINTR;
306                         }
307                         cfs_spin_unlock(&rpc->crpc_lock);
308                         continue;
309                 }
310
311                 crpc->crp_stamp  = cfs_time_current();
312                 crpc->crp_status = error;
313
314                 cfs_spin_unlock(&rpc->crpc_lock);
315
316                 sfw_abort_rpc(rpc);
317
318                 if  (error != ETIMEDOUT)
319                         continue;
320
321                 nd = crpc->crp_node;
322                 if (cfs_time_after(nd->nd_stamp, crpc->crp_stamp))
323                         continue;
324
325                 nd->nd_stamp = crpc->crp_stamp;
326                 nd->nd_state = LST_NODE_DOWN;
327         }
328 }
329
330 static int
331 lstcon_rpc_trans_check(lstcon_rpc_trans_t *trans)
332 {
333         if (console_session.ses_shutdown &&
334             !cfs_list_empty(&trans->tas_olink)) /* Not an end session RPC */
335                 return 1;
336
337         return (cfs_atomic_read(&trans->tas_remaining) == 0) ? 1: 0;
338 }
339
340 int
341 lstcon_rpc_trans_postwait(lstcon_rpc_trans_t *trans, int timeout)
342 {
343         lstcon_rpc_t  *crpc;
344         int            rc;
345
346         if (cfs_list_empty(&trans->tas_rpcs_list))
347                 return 0;
348
349         if (timeout < LST_TRANS_MIN_TIMEOUT)
350                 timeout = LST_TRANS_MIN_TIMEOUT;
351
352         CDEBUG(D_NET, "Transaction %s started\n",
353                lstcon_rpc_trans_name(trans->tas_opc));
354
355         /* post all requests */
356         cfs_list_for_each_entry_typed (crpc, &trans->tas_rpcs_list,
357                                        lstcon_rpc_t, crp_link) {
358                 LASSERT (!crpc->crp_posted);
359
360                 lstcon_rpc_post(crpc);
361         }
362
363         cfs_mutex_unlock(&console_session.ses_mutex);
364
365         cfs_waitq_wait_event_interruptible_timeout(trans->tas_waitq,
366                                               lstcon_rpc_trans_check(trans),
367                                               cfs_time_seconds(timeout), rc);
368
369         rc = (rc > 0)? 0: ((rc < 0)? -EINTR: -ETIMEDOUT);
370
371         cfs_mutex_lock(&console_session.ses_mutex);
372
373         if (console_session.ses_shutdown)
374                 rc = -ESHUTDOWN;
375
376         if (rc != 0 || atomic_read(&trans->tas_remaining) != 0) {
377                 /* treat short timeout as canceled */
378                 if (rc == -ETIMEDOUT && timeout < LST_TRANS_MIN_TIMEOUT * 2)
379                         rc = -EINTR;
380
381                 lstcon_rpc_trans_abort(trans, rc);
382         }
383
384         CDEBUG(D_NET, "Transaction %s stopped: %d\n",
385                lstcon_rpc_trans_name(trans->tas_opc), rc);
386
387         lstcon_rpc_trans_stat(trans, lstcon_trans_stat());
388
389         return rc;
390 }
391
392 int
393 lstcon_rpc_get_reply(lstcon_rpc_t *crpc, srpc_msg_t **msgpp)
394 {
395         lstcon_node_t        *nd  = crpc->crp_node;
396         srpc_client_rpc_t    *rpc = crpc->crp_rpc;
397         srpc_generic_reply_t *rep;
398
399         LASSERT (nd != NULL && rpc != NULL);
400         LASSERT (crpc->crp_stamp != 0);
401
402         if (crpc->crp_status != 0) {
403                 *msgpp = NULL;
404                 return crpc->crp_status;
405         }
406
407         *msgpp = &rpc->crpc_replymsg;
408         if (!crpc->crp_unpacked) {
409                 sfw_unpack_message(*msgpp);
410                 crpc->crp_unpacked = 1;
411         }
412
413         if (cfs_time_after(nd->nd_stamp, crpc->crp_stamp))
414                 return 0;
415
416         nd->nd_stamp = crpc->crp_stamp;
417         rep = &(*msgpp)->msg_body.reply;
418
419         if (rep->sid.ses_nid == LNET_NID_ANY)
420                 nd->nd_state = LST_NODE_UNKNOWN;
421         else if (lstcon_session_match(rep->sid))
422                 nd->nd_state = LST_NODE_ACTIVE;
423         else
424                 nd->nd_state = LST_NODE_BUSY;
425
426         return 0;
427 }
428
429 void
430 lstcon_rpc_trans_stat(lstcon_rpc_trans_t *trans, lstcon_trans_stat_t *stat)
431 {
432         lstcon_rpc_t      *crpc;
433         srpc_msg_t        *rep;
434         int                error;
435
436         LASSERT (stat != NULL);
437
438         memset(stat, 0, sizeof(*stat));
439
440         cfs_list_for_each_entry_typed(crpc, &trans->tas_rpcs_list,
441                                       lstcon_rpc_t, crp_link) {
442                 lstcon_rpc_stat_total(stat, 1);
443
444                 LASSERT (crpc->crp_stamp != 0);
445
446                 error = lstcon_rpc_get_reply(crpc, &rep);
447                 if (error != 0) {
448                         lstcon_rpc_stat_failure(stat, 1);
449                         if (stat->trs_rpc_errno == 0)
450                                 stat->trs_rpc_errno = -error;
451
452                         continue;
453                 }
454
455                 lstcon_rpc_stat_success(stat, 1);
456
457                 lstcon_rpc_stat_reply(trans, rep, crpc->crp_node, stat);
458         }
459
460         if (trans->tas_opc == LST_TRANS_SESNEW && stat->trs_fwk_errno == 0) {
461                 stat->trs_fwk_errno =
462                       lstcon_session_feats_check(trans->tas_features);
463         }
464
465         CDEBUG(D_NET, "transaction %s : success %d, failure %d, total %d, "
466                       "RPC error(%d), Framework error(%d)\n",
467                lstcon_rpc_trans_name(trans->tas_opc),
468                lstcon_rpc_stat_success(stat, 0),
469                lstcon_rpc_stat_failure(stat, 0),
470                lstcon_rpc_stat_total(stat, 0),
471                stat->trs_rpc_errno, stat->trs_fwk_errno);
472
473         return;
474 }
475
476 int
477 lstcon_rpc_trans_interpreter(lstcon_rpc_trans_t *trans,
478                              cfs_list_t *head_up,
479                              lstcon_rpc_readent_func_t readent)
480 {
481         cfs_list_t            tmp;
482         cfs_list_t           *next;
483         lstcon_rpc_ent_t     *ent;
484         srpc_generic_reply_t *rep;
485         lstcon_rpc_t         *crpc;
486         srpc_msg_t           *msg;
487         lstcon_node_t        *nd;
488         cfs_duration_t        dur;
489         struct timeval        tv;
490         int                   error;
491
492         LASSERT (head_up != NULL);
493
494         next = head_up;
495
496         cfs_list_for_each_entry_typed(crpc, &trans->tas_rpcs_list,
497                                       lstcon_rpc_t, crp_link) {
498                 if (cfs_copy_from_user(&tmp, next,
499                                        sizeof(cfs_list_t)))
500                         return -EFAULT;
501
502                 if (tmp.next == head_up)
503                         return 0;
504
505                 next = tmp.next;
506
507                 ent = cfs_list_entry(next, lstcon_rpc_ent_t, rpe_link);
508
509                 LASSERT (crpc->crp_stamp != 0);
510
511                 error = lstcon_rpc_get_reply(crpc, &msg);
512
513                 nd = crpc->crp_node;
514
515                 dur = (cfs_duration_t)cfs_time_sub(crpc->crp_stamp,
516                       (cfs_time_t)console_session.ses_id.ses_stamp);
517                 cfs_duration_usec(dur, &tv);
518
519                 if (cfs_copy_to_user(&ent->rpe_peer,
520                                      &nd->nd_id, sizeof(lnet_process_id_t)) ||
521                     cfs_copy_to_user(&ent->rpe_stamp, &tv, sizeof(tv)) ||
522                     cfs_copy_to_user(&ent->rpe_state,
523                                      &nd->nd_state, sizeof(nd->nd_state)) ||
524                     cfs_copy_to_user(&ent->rpe_rpc_errno, &error,
525                                      sizeof(error)))
526                         return -EFAULT;
527
528                 if (error != 0)
529                         continue;
530
531                 /* RPC is done */
532                 rep = (srpc_generic_reply_t *)&msg->msg_body.reply;
533
534                 if (cfs_copy_to_user(&ent->rpe_sid,
535                                      &rep->sid, sizeof(lst_sid_t)) ||
536                     cfs_copy_to_user(&ent->rpe_fwk_errno,
537                                      &rep->status, sizeof(rep->status)))
538                         return -EFAULT;
539
540                 if (readent == NULL)
541                         continue;
542
543                 if ((error = readent(trans->tas_opc, msg, ent)) != 0)
544                         return error;
545         }
546
547         return 0;
548 }
549
550 void
551 lstcon_rpc_trans_destroy(lstcon_rpc_trans_t *trans)
552 {
553         srpc_client_rpc_t *rpc;
554         lstcon_rpc_t      *crpc;
555         lstcon_rpc_t      *tmp;
556         int                count = 0;
557
558         cfs_list_for_each_entry_safe_typed(crpc, tmp,
559                                            &trans->tas_rpcs_list,
560                                            lstcon_rpc_t, crp_link) {
561                 rpc = crpc->crp_rpc;
562
563                 cfs_spin_lock(&rpc->crpc_lock);
564
565                 /* free it if not posted or finished already */
566                 if (!crpc->crp_posted || crpc->crp_finished) {
567                         cfs_spin_unlock(&rpc->crpc_lock);
568
569                         cfs_list_del_init(&crpc->crp_link);
570                         lstcon_rpc_put(crpc);
571
572                         continue;
573                 }
574
575                 /* rpcs can be still not callbacked (even LNetMDUnlink is called)
576                  * because huge timeout for inaccessible network, don't make
577                  * user wait for them, just abandon them, they will be recycled
578                  * in callback */
579
580                 LASSERT (crpc->crp_status != 0);
581
582                 crpc->crp_node  = NULL;
583                 crpc->crp_trans = NULL;
584                 cfs_list_del_init(&crpc->crp_link);
585                 count ++;
586
587                 cfs_spin_unlock(&rpc->crpc_lock);
588
589                 cfs_atomic_dec(&trans->tas_remaining);
590         }
591
592         LASSERT (cfs_atomic_read(&trans->tas_remaining) == 0);
593
594         cfs_list_del(&trans->tas_link);
595         if (!cfs_list_empty(&trans->tas_olink))
596                 cfs_list_del(&trans->tas_olink);
597
598         CDEBUG(D_NET, "Transaction %s destroyed with %d pending RPCs\n",
599                lstcon_rpc_trans_name(trans->tas_opc), count);
600
601         LIBCFS_FREE(trans, sizeof(*trans));
602
603         return;
604 }
605
606 int
607 lstcon_sesrpc_prep(lstcon_node_t *nd, int transop,
608                    unsigned feats, lstcon_rpc_t **crpc)
609 {
610         srpc_mksn_reqst_t *msrq;
611         srpc_rmsn_reqst_t *rsrq;
612         int                rc;
613
614         switch (transop) {
615         case LST_TRANS_SESNEW:
616                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_MAKE_SESSION,
617                                      feats, 0, 0, crpc);
618                 if (rc != 0)
619                         return rc;
620
621                 msrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.mksn_reqst;
622                 msrq->mksn_sid     = console_session.ses_id;
623                 msrq->mksn_force   = console_session.ses_force;
624                 strncpy(msrq->mksn_name, console_session.ses_name,
625                         strlen(console_session.ses_name));
626                 break;
627
628         case LST_TRANS_SESEND:
629                 rc = lstcon_rpc_prep(nd, SRPC_SERVICE_REMOVE_SESSION,
630                                      feats, 0, 0, crpc);
631                 if (rc != 0)
632                         return rc;
633
634                 rsrq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.rmsn_reqst;
635                 rsrq->rmsn_sid = console_session.ses_id;
636                 break;
637
638         default:
639                 LBUG();
640         }
641
642         return 0;
643 }
644
645 int
646 lstcon_dbgrpc_prep(lstcon_node_t *nd, unsigned feats, lstcon_rpc_t **crpc)
647 {
648         srpc_debug_reqst_t *drq;
649         int                 rc;
650
651         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_DEBUG, feats, 0, 0, crpc);
652         if (rc != 0)
653                 return rc;
654
655         drq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
656
657         drq->dbg_sid   = console_session.ses_id;
658         drq->dbg_flags = 0;
659
660         return rc;
661 }
662
663 int
664 lstcon_batrpc_prep(lstcon_node_t *nd, int transop, unsigned feats,
665                    lstcon_tsb_hdr_t *tsb, lstcon_rpc_t **crpc)
666 {
667         lstcon_batch_t     *batch;
668         srpc_batch_reqst_t *brq;
669         int                 rc;
670
671         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_BATCH, feats, 0, 0, crpc);
672         if (rc != 0)
673                 return rc;
674
675         brq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.bat_reqst;
676
677         brq->bar_sid     = console_session.ses_id;
678         brq->bar_bid     = tsb->tsb_id;
679         brq->bar_testidx = tsb->tsb_index;
680         brq->bar_opc     = transop == LST_TRANS_TSBRUN ? SRPC_BATCH_OPC_RUN :
681                            (transop == LST_TRANS_TSBSTOP ? SRPC_BATCH_OPC_STOP:
682                             SRPC_BATCH_OPC_QUERY);
683
684         if (transop != LST_TRANS_TSBRUN &&
685             transop != LST_TRANS_TSBSTOP)
686                 return 0;
687
688         LASSERT (tsb->tsb_index == 0);
689
690         batch = (lstcon_batch_t *)tsb;
691         brq->bar_arg = batch->bat_arg;
692
693         return 0;
694 }
695
696 int
697 lstcon_statrpc_prep(lstcon_node_t *nd, unsigned feats, lstcon_rpc_t **crpc)
698 {
699         srpc_stat_reqst_t *srq;
700         int                rc;
701
702         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_QUERY_STAT, feats, 0, 0, crpc);
703         if (rc != 0)
704                 return rc;
705
706         srq = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.stat_reqst;
707
708         srq->str_sid  = console_session.ses_id;
709         srq->str_type = 0; /* XXX remove it */
710
711         return 0;
712 }
713
714 lnet_process_id_packed_t *
715 lstcon_next_id(int idx, int nkiov, lnet_kiov_t *kiov)
716 {
717         lnet_process_id_packed_t *pid;
718         int                       i;
719
720         i = idx / SFW_ID_PER_PAGE;
721         
722         LASSERT (i < nkiov);
723
724         pid = (lnet_process_id_packed_t *)cfs_page_address(kiov[i].kiov_page);
725
726         return &pid[idx % SFW_ID_PER_PAGE];
727 }
728
729 int
730 lstcon_dstnodes_prep(lstcon_group_t *grp, int idx,
731                      int dist, int span, int nkiov, lnet_kiov_t *kiov)
732 {
733         lnet_process_id_packed_t *pid;
734         lstcon_ndlink_t          *ndl;
735         lstcon_node_t            *nd;
736         int                       start;
737         int                       end;
738         int                       i = 0;
739
740         LASSERT (dist >= 1);
741         LASSERT (span >= 1);
742         LASSERT (grp->grp_nnode >= 1);
743
744         if (span > grp->grp_nnode)
745                 return -EINVAL;
746
747         start = ((idx / dist) * span) % grp->grp_nnode;
748         end   = ((idx / dist) * span + span - 1) % grp->grp_nnode;
749
750         cfs_list_for_each_entry_typed(ndl, &grp->grp_ndl_list,
751                                       lstcon_ndlink_t, ndl_link) {
752                 nd = ndl->ndl_node;
753                 if (i < start) {
754                         i ++;
755                         continue;
756                 }
757
758                 if (i > (end >= start ? end: grp->grp_nnode))
759                         break;
760
761                 pid = lstcon_next_id((i - start), nkiov, kiov);
762                 pid->nid = nd->nd_id.nid;
763                 pid->pid = nd->nd_id.pid;
764                 i++;
765         }
766
767         if (start <= end) /* done */
768                 return 0;
769
770         cfs_list_for_each_entry_typed(ndl, &grp->grp_ndl_list,
771                                       lstcon_ndlink_t, ndl_link) {
772                 if (i > grp->grp_nnode + end)
773                         break;
774
775                 nd = ndl->ndl_node;
776                 pid = lstcon_next_id((i - start), nkiov, kiov);
777                 pid->nid = nd->nd_id.nid;
778                 pid->pid = nd->nd_id.pid;
779                 i++;
780         }
781
782         return 0;
783 }
784
785 int
786 lstcon_pingrpc_prep(lst_test_ping_param_t *param, srpc_test_reqst_t *req)
787 {
788         test_ping_req_t *prq = &req->tsr_u.ping;
789
790         prq->png_size   = param->png_size;
791         prq->png_flags  = param->png_flags;
792         /* TODO dest */
793         return 0;
794 }
795
796 int
797 lstcon_bulkrpc_v0_prep(lst_test_bulk_param_t *param, srpc_test_reqst_t *req)
798 {
799         test_bulk_req_t *brq = &req->tsr_u.bulk_v0;
800
801         brq->blk_opc    = param->blk_opc;
802         brq->blk_npg    = (param->blk_size + CFS_PAGE_SIZE - 1) / CFS_PAGE_SIZE;
803         brq->blk_flags  = param->blk_flags;
804
805         return 0;
806 }
807
808 int
809 lstcon_bulkrpc_v1_prep(lst_test_bulk_param_t *param, srpc_test_reqst_t *req)
810 {
811         test_bulk_req_v1_t *brq = &req->tsr_u.bulk_v1;
812
813         brq->blk_opc    = param->blk_opc;
814         brq->blk_flags  = param->blk_flags;
815         brq->blk_len    = param->blk_size;
816         brq->blk_offset = 0; /* reserved */
817
818         return 0;
819 }
820
821 int
822 lstcon_testrpc_prep(lstcon_node_t *nd, int transop, unsigned feats,
823                     lstcon_test_t *test, lstcon_rpc_t **crpc)
824 {
825         lstcon_group_t    *sgrp = test->tes_src_grp;
826         lstcon_group_t    *dgrp = test->tes_dst_grp;
827         srpc_test_reqst_t *trq;
828         srpc_bulk_t       *bulk;
829         int                i;
830         int                npg = 0;
831         int                nob = 0;
832         int                rc  = 0;
833
834         if (transop == LST_TRANS_TSBCLIADD) {
835                 npg = sfw_id_pages(test->tes_span);
836                 nob = (feats & LST_FEAT_BULK_LEN) == 0 ?
837                       npg * CFS_PAGE_SIZE :
838                       sizeof(lnet_process_id_packed_t) * test->tes_span;
839         }
840
841         rc = lstcon_rpc_prep(nd, SRPC_SERVICE_TEST, feats, npg, nob, crpc);
842         if (rc != 0)
843                 return rc;
844
845         trq  = &(*crpc)->crp_rpc->crpc_reqstmsg.msg_body.tes_reqst;
846
847         if (transop == LST_TRANS_TSBSRVADD) {
848                 int ndist = (sgrp->grp_nnode + test->tes_dist - 1) / test->tes_dist;
849                 int nspan = (dgrp->grp_nnode + test->tes_span - 1) / test->tes_span;
850                 int nmax = (ndist + nspan - 1) / nspan;
851
852                 trq->tsr_ndest = 0;
853                 trq->tsr_loop  = nmax * test->tes_dist * test->tes_concur;
854
855         } else {
856                 bulk = &(*crpc)->crp_rpc->crpc_bulk;
857
858                 for (i = 0; i < npg; i++) {
859                         int     len;
860
861                         LASSERT(nob > 0);
862
863                         len = (feats & LST_FEAT_BULK_LEN) == 0 ?
864                               CFS_PAGE_SIZE : min_t(int, nob, CFS_PAGE_SIZE);
865                         nob -= len;
866
867                         bulk->bk_iovs[i].kiov_offset = 0;
868                         bulk->bk_iovs[i].kiov_len    = len;
869                         bulk->bk_iovs[i].kiov_page   =
870                                 cfs_alloc_page(CFS_ALLOC_STD);
871
872                         if (bulk->bk_iovs[i].kiov_page == NULL) {
873                                 lstcon_rpc_put(*crpc);
874                                 return -ENOMEM;
875                         }
876                 }
877
878                 bulk->bk_sink = 0;
879
880                 LASSERT (transop == LST_TRANS_TSBCLIADD);
881
882                 rc = lstcon_dstnodes_prep(test->tes_dst_grp,
883                                           test->tes_cliidx++,
884                                           test->tes_dist,
885                                           test->tes_span,
886                                           npg, &bulk->bk_iovs[0]);
887                 if (rc != 0) {
888                         lstcon_rpc_put(*crpc);
889                         return rc;
890                 }
891
892                 trq->tsr_ndest = test->tes_span;
893                 trq->tsr_loop  = test->tes_loop;
894         } 
895
896         trq->tsr_sid        = console_session.ses_id;
897         trq->tsr_bid        = test->tes_hdr.tsb_id;
898         trq->tsr_concur     = test->tes_concur;
899         trq->tsr_is_client  = (transop == LST_TRANS_TSBCLIADD) ? 1 : 0;
900         trq->tsr_stop_onerr = !!test->tes_stop_onerr;
901
902         switch (test->tes_type) {
903         case LST_TEST_PING:
904                 trq->tsr_service = SRPC_SERVICE_PING;
905                 rc = lstcon_pingrpc_prep((lst_test_ping_param_t *)
906                                          &test->tes_param[0], trq);
907                 break;
908
909         case LST_TEST_BULK:
910                 trq->tsr_service = SRPC_SERVICE_BRW;
911                 if ((feats & LST_FEAT_BULK_LEN) == 0) {
912                         rc = lstcon_bulkrpc_v0_prep((lst_test_bulk_param_t *)
913                                                     &test->tes_param[0], trq);
914                 } else {
915                         rc = lstcon_bulkrpc_v1_prep((lst_test_bulk_param_t *)
916                                                     &test->tes_param[0], trq);
917                 }
918
919                 break;
920         default:
921                 LBUG();
922                 break;
923         }
924
925         return rc;
926 }
927
928 int
929 lstcon_sesnew_stat_reply(lstcon_rpc_trans_t *trans,
930                          lstcon_node_t *nd, srpc_msg_t *reply)
931 {
932         srpc_mksn_reply_t *mksn_rep = &reply->msg_body.mksn_reply;
933         int                status   = mksn_rep->mksn_status;
934
935         if (status == 0 &&
936             (reply->msg_ses_feats & ~LST_FEATS_MASK) != 0) {
937                 mksn_rep->mksn_status = EPROTO;
938                 status = EPROTO;
939         }
940
941         if (status == EPROTO) {
942                 CNETERR("session protocol error from %s: %u\n",
943                         libcfs_nid2str(nd->nd_id.nid),
944                         reply->msg_ses_feats);
945         }
946
947         if (status != 0)
948                 return status;
949
950         if (!trans->tas_feats_updated) {
951                 trans->tas_feats_updated = 1;
952                 trans->tas_features = reply->msg_ses_feats;
953         }
954
955         if (reply->msg_ses_feats != trans->tas_features) {
956                 CNETERR("Framework features %x from %s is different with "
957                         "features on this transaction: %x\n",
958                          reply->msg_ses_feats, libcfs_nid2str(nd->nd_id.nid),
959                          trans->tas_features);
960                 status = mksn_rep->mksn_status = EPROTO;
961         }
962
963         if (status == 0) {
964                 /* session timeout on remote node */
965                 nd->nd_timeout = mksn_rep->mksn_timeout;
966         }
967
968         return status;
969 }
970
971 void
972 lstcon_rpc_stat_reply(lstcon_rpc_trans_t *trans, srpc_msg_t *msg,
973                       lstcon_node_t *nd, lstcon_trans_stat_t *stat)
974 {
975         srpc_rmsn_reply_t  *rmsn_rep;
976         srpc_debug_reply_t *dbg_rep;
977         srpc_batch_reply_t *bat_rep;
978         srpc_test_reply_t  *test_rep;
979         srpc_stat_reply_t  *stat_rep;
980         int                 rc = 0;
981
982         switch (trans->tas_opc) {
983         case LST_TRANS_SESNEW:
984                 rc = lstcon_sesnew_stat_reply(trans, nd, msg);
985                 if (rc == 0) {
986                         lstcon_sesop_stat_success(stat, 1);
987                         return;
988                 }
989
990                 lstcon_sesop_stat_failure(stat, 1);
991                 break;
992
993         case LST_TRANS_SESEND:
994                 rmsn_rep = &msg->msg_body.rmsn_reply;
995                 /* ESRCH is not an error for end session */
996                 if (rmsn_rep->rmsn_status == 0 ||
997                     rmsn_rep->rmsn_status == ESRCH) {
998                         lstcon_sesop_stat_success(stat, 1);
999                         return;
1000                 }
1001
1002                 lstcon_sesop_stat_failure(stat, 1);
1003                 rc = rmsn_rep->rmsn_status;
1004                 break;
1005
1006         case LST_TRANS_SESQRY:
1007         case LST_TRANS_SESPING:
1008                 dbg_rep = &msg->msg_body.dbg_reply;
1009
1010                 if (dbg_rep->dbg_status == ESRCH) {
1011                         lstcon_sesqry_stat_unknown(stat, 1);
1012                         return;
1013                 }
1014
1015                 if (lstcon_session_match(dbg_rep->dbg_sid))
1016                         lstcon_sesqry_stat_active(stat, 1);
1017                 else
1018                         lstcon_sesqry_stat_busy(stat, 1);
1019                 return;
1020
1021         case LST_TRANS_TSBRUN:
1022         case LST_TRANS_TSBSTOP:
1023                 bat_rep = &msg->msg_body.bat_reply;
1024
1025                 if (bat_rep->bar_status == 0) {
1026                         lstcon_tsbop_stat_success(stat, 1);
1027                         return;
1028                 }
1029
1030                 if (bat_rep->bar_status == EPERM && 
1031                     trans->tas_opc == LST_TRANS_TSBSTOP) {
1032                         lstcon_tsbop_stat_success(stat, 1);
1033                         return;
1034                 }
1035
1036                 lstcon_tsbop_stat_failure(stat, 1);
1037                 rc = bat_rep->bar_status;
1038                 break;
1039
1040         case LST_TRANS_TSBCLIQRY:
1041         case LST_TRANS_TSBSRVQRY:
1042                 bat_rep = &msg->msg_body.bat_reply;
1043
1044                 if (bat_rep->bar_active != 0) 
1045                         lstcon_tsbqry_stat_run(stat, 1);
1046                 else
1047                         lstcon_tsbqry_stat_idle(stat, 1);
1048
1049                 if (bat_rep->bar_status == 0) 
1050                         return;
1051
1052                 lstcon_tsbqry_stat_failure(stat, 1);
1053                 rc = bat_rep->bar_status;
1054                 break;
1055
1056         case LST_TRANS_TSBCLIADD:
1057         case LST_TRANS_TSBSRVADD:
1058                 test_rep = &msg->msg_body.tes_reply;
1059
1060                 if (test_rep->tsr_status == 0) {
1061                         lstcon_tsbop_stat_success(stat, 1);
1062                         return;
1063                 }
1064
1065                 lstcon_tsbop_stat_failure(stat, 1);
1066                 rc = test_rep->tsr_status;
1067                 break;
1068
1069         case LST_TRANS_STATQRY:
1070                 stat_rep = &msg->msg_body.stat_reply;
1071
1072                 if (stat_rep->str_status == 0) {
1073                         lstcon_statqry_stat_success(stat, 1);
1074                         return;
1075                 }
1076
1077                 lstcon_statqry_stat_failure(stat, 1);
1078                 rc = stat_rep->str_status;
1079                 break;
1080
1081         default:
1082                 LBUG();
1083         }
1084
1085         if (stat->trs_fwk_errno == 0)
1086                 stat->trs_fwk_errno = rc;
1087
1088         return;
1089 }
1090
1091 int
1092 lstcon_rpc_trans_ndlist(cfs_list_t *ndlist,
1093                         cfs_list_t *translist, int transop,
1094                         void *arg, lstcon_rpc_cond_func_t condition,
1095                         lstcon_rpc_trans_t **transpp)
1096 {
1097         lstcon_rpc_trans_t *trans;
1098         lstcon_ndlink_t    *ndl;
1099         lstcon_node_t      *nd;
1100         lstcon_rpc_t       *rpc;
1101         unsigned            feats;
1102         int                 rc;
1103
1104         /* Creating session RPG for list of nodes */
1105
1106         rc = lstcon_rpc_trans_prep(translist, transop, &trans);
1107         if (rc != 0) {
1108                 CERROR("Can't create transaction %d: %d\n", transop, rc);
1109                 return rc;
1110         }
1111
1112         feats = trans->tas_features;
1113         cfs_list_for_each_entry_typed(ndl, ndlist, lstcon_ndlink_t, ndl_link) {
1114                 rc = condition == NULL ? 1 :
1115                      condition(transop, ndl->ndl_node, arg);
1116
1117                 if (rc == 0)
1118                         continue;
1119
1120                 if (rc < 0) {
1121                         CDEBUG(D_NET, "Condition error while creating RPC "
1122                                       " for transaction %d: %d\n", transop, rc);
1123                         break;
1124                 }
1125
1126                 nd = ndl->ndl_node;
1127
1128                 switch (transop) {
1129                 case LST_TRANS_SESNEW:
1130                 case LST_TRANS_SESEND:
1131                         rc = lstcon_sesrpc_prep(nd, transop, feats, &rpc);
1132                         break;
1133                 case LST_TRANS_SESQRY:
1134                 case LST_TRANS_SESPING:
1135                         rc = lstcon_dbgrpc_prep(nd, feats, &rpc);
1136                         break;
1137                 case LST_TRANS_TSBCLIADD:
1138                 case LST_TRANS_TSBSRVADD:
1139                         rc = lstcon_testrpc_prep(nd, transop, feats,
1140                                                  (lstcon_test_t *)arg, &rpc);
1141                         break;
1142                 case LST_TRANS_TSBRUN:
1143                 case LST_TRANS_TSBSTOP:
1144                 case LST_TRANS_TSBCLIQRY:
1145                 case LST_TRANS_TSBSRVQRY:
1146                         rc = lstcon_batrpc_prep(nd, transop, feats,
1147                                                 (lstcon_tsb_hdr_t *)arg, &rpc);
1148                         break;
1149                 case LST_TRANS_STATQRY:
1150                         rc = lstcon_statrpc_prep(nd, feats, &rpc);
1151                         break;
1152                 default:
1153                         rc = -EINVAL;
1154                         break;
1155                 }
1156
1157                 if (rc != 0) {
1158                         CERROR("Failed to create RPC for transaction %s: %d\n",
1159                                lstcon_rpc_trans_name(transop), rc);
1160                         break;
1161                 }
1162
1163                 lstcon_rpc_trans_addreq(trans, rpc);
1164         }
1165
1166         if (rc == 0) {
1167                 *transpp = trans;
1168                 return 0;
1169         }
1170
1171         lstcon_rpc_trans_destroy(trans);
1172
1173         return rc;
1174 }
1175
1176 void
1177 lstcon_rpc_pinger(void *arg)
1178 {
1179         stt_timer_t        *ptimer = (stt_timer_t *)arg;
1180         lstcon_rpc_trans_t *trans;
1181         lstcon_rpc_t       *crpc;
1182         srpc_msg_t         *rep;
1183         srpc_debug_reqst_t *drq;
1184         lstcon_ndlink_t    *ndl;
1185         lstcon_node_t      *nd;
1186         time_t              intv;
1187         int                 count = 0;
1188         int                 rc;
1189
1190         /* RPC pinger is a special case of transaction,
1191          * it's called by timer at 8 seconds interval.
1192          */
1193         cfs_mutex_lock(&console_session.ses_mutex);
1194
1195         if (console_session.ses_shutdown || console_session.ses_expired) {
1196                 cfs_mutex_unlock(&console_session.ses_mutex);
1197                 return;
1198         }
1199
1200         if (!console_session.ses_expired &&
1201             cfs_time_current_sec() - console_session.ses_laststamp >
1202             (time_t)console_session.ses_timeout)
1203                 console_session.ses_expired = 1;
1204
1205         trans = console_session.ses_ping;
1206
1207         LASSERT (trans != NULL);
1208
1209         cfs_list_for_each_entry_typed(ndl, &console_session.ses_ndl_list,
1210                                       lstcon_ndlink_t, ndl_link) {
1211                 nd = ndl->ndl_node;
1212
1213                 if (console_session.ses_expired) {
1214                         /* idle console, end session on all nodes */
1215                         if (nd->nd_state != LST_NODE_ACTIVE)
1216                                 continue;
1217
1218                         rc = lstcon_sesrpc_prep(nd, LST_TRANS_SESEND,
1219                                                 trans->tas_features, &crpc);
1220                         if (rc != 0) {
1221                                 CERROR("Out of memory\n");
1222                                 break;
1223                         }
1224
1225                         lstcon_rpc_trans_addreq(trans, crpc);
1226                         lstcon_rpc_post(crpc);
1227
1228                         continue;
1229                 }
1230
1231                 crpc = &nd->nd_ping;
1232
1233                 if (crpc->crp_rpc != NULL) {
1234                         LASSERT (crpc->crp_trans == trans);
1235                         LASSERT (!cfs_list_empty(&crpc->crp_link));
1236
1237                         cfs_spin_lock(&crpc->crp_rpc->crpc_lock);
1238
1239                         LASSERT (crpc->crp_posted);
1240
1241                         if (!crpc->crp_finished) {
1242                                 /* in flight */
1243                                 cfs_spin_unlock(&crpc->crp_rpc->crpc_lock);
1244                                 continue;
1245                         }
1246
1247                         cfs_spin_unlock(&crpc->crp_rpc->crpc_lock);
1248
1249                         lstcon_rpc_get_reply(crpc, &rep);
1250
1251                         cfs_list_del_init(&crpc->crp_link);
1252
1253                         lstcon_rpc_put(crpc);
1254                 }
1255
1256                 if (nd->nd_state != LST_NODE_ACTIVE)
1257                         continue;
1258
1259                 intv = cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1260                                                      nd->nd_stamp));
1261                 if (intv < (time_t)nd->nd_timeout / 2)
1262                         continue;
1263
1264                 rc = lstcon_rpc_init(nd, SRPC_SERVICE_DEBUG,
1265                                      trans->tas_features, 0, 0, 1, crpc);
1266                 if (rc != 0) {
1267                         CERROR("Out of memory\n");
1268                         break;
1269                 }
1270
1271                 drq = &crpc->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
1272
1273                 drq->dbg_sid   = console_session.ses_id;
1274                 drq->dbg_flags = 0;
1275
1276                 lstcon_rpc_trans_addreq(trans, crpc);
1277                 lstcon_rpc_post(crpc);
1278
1279                 count ++;
1280         }
1281
1282         if (console_session.ses_expired) {
1283                 cfs_mutex_unlock(&console_session.ses_mutex);
1284                 return;
1285         }
1286
1287         CDEBUG(D_NET, "Ping %d nodes in session\n", count);
1288
1289         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1290         stt_add_timer(ptimer);
1291
1292         cfs_mutex_unlock(&console_session.ses_mutex);
1293 }
1294
1295 int
1296 lstcon_rpc_pinger_start(void)
1297 {
1298         stt_timer_t    *ptimer;
1299         int             rc;
1300
1301         LASSERT (cfs_list_empty(&console_session.ses_rpc_freelist));
1302         LASSERT (cfs_atomic_read(&console_session.ses_rpc_counter) == 0);
1303
1304         rc = lstcon_rpc_trans_prep(NULL, LST_TRANS_SESPING,
1305                                    &console_session.ses_ping);
1306         if (rc != 0) {
1307                 CERROR("Failed to create console pinger\n");
1308                 return rc;
1309         }
1310
1311         ptimer = &console_session.ses_ping_timer;
1312         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1313
1314         stt_add_timer(ptimer);
1315
1316         return 0;
1317 }
1318
1319 void
1320 lstcon_rpc_pinger_stop(void)
1321 {
1322         LASSERT (console_session.ses_shutdown);
1323
1324         stt_del_timer(&console_session.ses_ping_timer);
1325
1326         lstcon_rpc_trans_abort(console_session.ses_ping, -ESHUTDOWN);
1327         lstcon_rpc_trans_stat(console_session.ses_ping, lstcon_trans_stat());
1328         lstcon_rpc_trans_destroy(console_session.ses_ping);
1329
1330         memset(lstcon_trans_stat(), 0, sizeof(lstcon_trans_stat_t));
1331
1332         console_session.ses_ping = NULL;
1333 }
1334
1335 void
1336 lstcon_rpc_cleanup_wait(void)
1337 {
1338         lstcon_rpc_trans_t *trans;
1339         lstcon_rpc_t       *crpc;
1340         cfs_list_t         *pacer;
1341         cfs_list_t          zlist;
1342
1343         /* Called with hold of global mutex */
1344
1345         LASSERT (console_session.ses_shutdown);
1346
1347         while (!cfs_list_empty(&console_session.ses_trans_list)) { 
1348                 cfs_list_for_each(pacer, &console_session.ses_trans_list) {
1349                         trans = cfs_list_entry(pacer, lstcon_rpc_trans_t,
1350                                                tas_link);
1351
1352                         CDEBUG(D_NET, "Session closed, wakeup transaction %s\n",
1353                                lstcon_rpc_trans_name(trans->tas_opc));
1354
1355                         cfs_waitq_signal(&trans->tas_waitq);
1356                 }
1357
1358                 cfs_mutex_unlock(&console_session.ses_mutex);
1359
1360                 CWARN("Session is shutting down, "
1361                       "waiting for termination of transactions\n");
1362                 cfs_pause(cfs_time_seconds(1));
1363
1364                 cfs_mutex_lock(&console_session.ses_mutex);
1365         }
1366
1367         cfs_spin_lock(&console_session.ses_rpc_lock);
1368
1369         lst_wait_until((cfs_atomic_read(&console_session.ses_rpc_counter) == 0),
1370                        console_session.ses_rpc_lock,
1371                        "Network is not accessable or target is down, "
1372                        "waiting for %d console RPCs to being recycled\n",
1373                        cfs_atomic_read(&console_session.ses_rpc_counter));
1374
1375         cfs_list_add(&zlist, &console_session.ses_rpc_freelist);
1376         cfs_list_del_init(&console_session.ses_rpc_freelist);
1377
1378         cfs_spin_unlock(&console_session.ses_rpc_lock);
1379
1380         while (!cfs_list_empty(&zlist)) {
1381                 crpc = cfs_list_entry(zlist.next, lstcon_rpc_t, crp_link);
1382
1383                 cfs_list_del(&crpc->crp_link);
1384                 LIBCFS_FREE(crpc, sizeof(lstcon_rpc_t));
1385         }
1386 }
1387
1388 int
1389 lstcon_rpc_module_init(void)
1390 {
1391         CFS_INIT_LIST_HEAD(&console_session.ses_ping_timer.stt_list);
1392         console_session.ses_ping_timer.stt_func = lstcon_rpc_pinger;
1393         console_session.ses_ping_timer.stt_data = &console_session.ses_ping_timer;
1394
1395         console_session.ses_ping = NULL;
1396
1397         cfs_spin_lock_init(&console_session.ses_rpc_lock);
1398         cfs_atomic_set(&console_session.ses_rpc_counter, 0);
1399         CFS_INIT_LIST_HEAD(&console_session.ses_rpc_freelist);
1400
1401         return 0;
1402 }
1403
1404 void
1405 lstcon_rpc_module_fini(void)
1406 {
1407         LASSERT (cfs_list_empty(&console_session.ses_rpc_freelist));
1408         LASSERT (cfs_atomic_read(&console_session.ses_rpc_counter) == 0);
1409 }
1410
1411 #endif