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