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