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