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