Whamcloud - gitweb
LU-3963 lnet: convert cfs_atomic primitives
[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 (atomic_dec_and_test(&crpc->crp_trans->tas_remaining))
87                 wake_up(&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         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         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         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         atomic_set(&trans->tas_remaining, 0);
270         init_waitqueue_head(&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 (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         rc = wait_event_interruptible_timeout(trans->tas_waitq,
365                                               lstcon_rpc_trans_check(trans),
366                                               cfs_time_seconds(timeout));
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                 atomic_dec(&trans->tas_remaining);
590         }
591
592         LASSERT (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                 spin_lock(&console_session.ses_rpc_lock);
953                 if (!trans->tas_feats_updated) { /* recheck with lock */
954                         trans->tas_feats_updated = 1;
955                         trans->tas_features = reply->msg_ses_feats;
956                 }
957                 spin_unlock(&console_session.ses_rpc_lock);
958         }
959
960         if (reply->msg_ses_feats != trans->tas_features) {
961                 CNETERR("Framework features %x from %s is different with "
962                         "features on this transaction: %x\n",
963                          reply->msg_ses_feats, libcfs_nid2str(nd->nd_id.nid),
964                          trans->tas_features);
965                 status = mksn_rep->mksn_status = EPROTO;
966         }
967
968         if (status == 0) {
969                 /* session timeout on remote node */
970                 nd->nd_timeout = mksn_rep->mksn_timeout;
971         }
972
973         return status;
974 }
975
976 void
977 lstcon_rpc_stat_reply(lstcon_rpc_trans_t *trans, srpc_msg_t *msg,
978                       lstcon_node_t *nd, lstcon_trans_stat_t *stat)
979 {
980         srpc_rmsn_reply_t  *rmsn_rep;
981         srpc_debug_reply_t *dbg_rep;
982         srpc_batch_reply_t *bat_rep;
983         srpc_test_reply_t  *test_rep;
984         srpc_stat_reply_t  *stat_rep;
985         int                 rc = 0;
986
987         switch (trans->tas_opc) {
988         case LST_TRANS_SESNEW:
989                 rc = lstcon_sesnew_stat_reply(trans, nd, msg);
990                 if (rc == 0) {
991                         lstcon_sesop_stat_success(stat, 1);
992                         return;
993                 }
994
995                 lstcon_sesop_stat_failure(stat, 1);
996                 break;
997
998         case LST_TRANS_SESEND:
999                 rmsn_rep = &msg->msg_body.rmsn_reply;
1000                 /* ESRCH is not an error for end session */
1001                 if (rmsn_rep->rmsn_status == 0 ||
1002                     rmsn_rep->rmsn_status == ESRCH) {
1003                         lstcon_sesop_stat_success(stat, 1);
1004                         return;
1005                 }
1006
1007                 lstcon_sesop_stat_failure(stat, 1);
1008                 rc = rmsn_rep->rmsn_status;
1009                 break;
1010
1011         case LST_TRANS_SESQRY:
1012         case LST_TRANS_SESPING:
1013                 dbg_rep = &msg->msg_body.dbg_reply;
1014
1015                 if (dbg_rep->dbg_status == ESRCH) {
1016                         lstcon_sesqry_stat_unknown(stat, 1);
1017                         return;
1018                 }
1019
1020                 if (lstcon_session_match(dbg_rep->dbg_sid))
1021                         lstcon_sesqry_stat_active(stat, 1);
1022                 else
1023                         lstcon_sesqry_stat_busy(stat, 1);
1024                 return;
1025
1026         case LST_TRANS_TSBRUN:
1027         case LST_TRANS_TSBSTOP:
1028                 bat_rep = &msg->msg_body.bat_reply;
1029
1030                 if (bat_rep->bar_status == 0) {
1031                         lstcon_tsbop_stat_success(stat, 1);
1032                         return;
1033                 }
1034
1035                 if (bat_rep->bar_status == EPERM && 
1036                     trans->tas_opc == LST_TRANS_TSBSTOP) {
1037                         lstcon_tsbop_stat_success(stat, 1);
1038                         return;
1039                 }
1040
1041                 lstcon_tsbop_stat_failure(stat, 1);
1042                 rc = bat_rep->bar_status;
1043                 break;
1044
1045         case LST_TRANS_TSBCLIQRY:
1046         case LST_TRANS_TSBSRVQRY:
1047                 bat_rep = &msg->msg_body.bat_reply;
1048
1049                 if (bat_rep->bar_active != 0) 
1050                         lstcon_tsbqry_stat_run(stat, 1);
1051                 else
1052                         lstcon_tsbqry_stat_idle(stat, 1);
1053
1054                 if (bat_rep->bar_status == 0) 
1055                         return;
1056
1057                 lstcon_tsbqry_stat_failure(stat, 1);
1058                 rc = bat_rep->bar_status;
1059                 break;
1060
1061         case LST_TRANS_TSBCLIADD:
1062         case LST_TRANS_TSBSRVADD:
1063                 test_rep = &msg->msg_body.tes_reply;
1064
1065                 if (test_rep->tsr_status == 0) {
1066                         lstcon_tsbop_stat_success(stat, 1);
1067                         return;
1068                 }
1069
1070                 lstcon_tsbop_stat_failure(stat, 1);
1071                 rc = test_rep->tsr_status;
1072                 break;
1073
1074         case LST_TRANS_STATQRY:
1075                 stat_rep = &msg->msg_body.stat_reply;
1076
1077                 if (stat_rep->str_status == 0) {
1078                         lstcon_statqry_stat_success(stat, 1);
1079                         return;
1080                 }
1081
1082                 lstcon_statqry_stat_failure(stat, 1);
1083                 rc = stat_rep->str_status;
1084                 break;
1085
1086         default:
1087                 LBUG();
1088         }
1089
1090         if (stat->trs_fwk_errno == 0)
1091                 stat->trs_fwk_errno = rc;
1092
1093         return;
1094 }
1095
1096 int
1097 lstcon_rpc_trans_ndlist(cfs_list_t *ndlist,
1098                         cfs_list_t *translist, int transop,
1099                         void *arg, lstcon_rpc_cond_func_t condition,
1100                         lstcon_rpc_trans_t **transpp)
1101 {
1102         lstcon_rpc_trans_t *trans;
1103         lstcon_ndlink_t    *ndl;
1104         lstcon_node_t      *nd;
1105         lstcon_rpc_t       *rpc;
1106         unsigned            feats;
1107         int                 rc;
1108
1109         /* Creating session RPG for list of nodes */
1110
1111         rc = lstcon_rpc_trans_prep(translist, transop, &trans);
1112         if (rc != 0) {
1113                 CERROR("Can't create transaction %d: %d\n", transop, rc);
1114                 return rc;
1115         }
1116
1117         feats = trans->tas_features;
1118         cfs_list_for_each_entry_typed(ndl, ndlist, lstcon_ndlink_t, ndl_link) {
1119                 rc = condition == NULL ? 1 :
1120                      condition(transop, ndl->ndl_node, arg);
1121
1122                 if (rc == 0)
1123                         continue;
1124
1125                 if (rc < 0) {
1126                         CDEBUG(D_NET, "Condition error while creating RPC "
1127                                       " for transaction %d: %d\n", transop, rc);
1128                         break;
1129                 }
1130
1131                 nd = ndl->ndl_node;
1132
1133                 switch (transop) {
1134                 case LST_TRANS_SESNEW:
1135                 case LST_TRANS_SESEND:
1136                         rc = lstcon_sesrpc_prep(nd, transop, feats, &rpc);
1137                         break;
1138                 case LST_TRANS_SESQRY:
1139                 case LST_TRANS_SESPING:
1140                         rc = lstcon_dbgrpc_prep(nd, feats, &rpc);
1141                         break;
1142                 case LST_TRANS_TSBCLIADD:
1143                 case LST_TRANS_TSBSRVADD:
1144                         rc = lstcon_testrpc_prep(nd, transop, feats,
1145                                                  (lstcon_test_t *)arg, &rpc);
1146                         break;
1147                 case LST_TRANS_TSBRUN:
1148                 case LST_TRANS_TSBSTOP:
1149                 case LST_TRANS_TSBCLIQRY:
1150                 case LST_TRANS_TSBSRVQRY:
1151                         rc = lstcon_batrpc_prep(nd, transop, feats,
1152                                                 (lstcon_tsb_hdr_t *)arg, &rpc);
1153                         break;
1154                 case LST_TRANS_STATQRY:
1155                         rc = lstcon_statrpc_prep(nd, feats, &rpc);
1156                         break;
1157                 default:
1158                         rc = -EINVAL;
1159                         break;
1160                 }
1161
1162                 if (rc != 0) {
1163                         CERROR("Failed to create RPC for transaction %s: %d\n",
1164                                lstcon_rpc_trans_name(transop), rc);
1165                         break;
1166                 }
1167
1168                 lstcon_rpc_trans_addreq(trans, rpc);
1169         }
1170
1171         if (rc == 0) {
1172                 *transpp = trans;
1173                 return 0;
1174         }
1175
1176         lstcon_rpc_trans_destroy(trans);
1177
1178         return rc;
1179 }
1180
1181 void
1182 lstcon_rpc_pinger(void *arg)
1183 {
1184         stt_timer_t        *ptimer = (stt_timer_t *)arg;
1185         lstcon_rpc_trans_t *trans;
1186         lstcon_rpc_t       *crpc;
1187         srpc_msg_t         *rep;
1188         srpc_debug_reqst_t *drq;
1189         lstcon_ndlink_t    *ndl;
1190         lstcon_node_t      *nd;
1191         time_t              intv;
1192         int                 count = 0;
1193         int                 rc;
1194
1195         /* RPC pinger is a special case of transaction,
1196          * it's called by timer at 8 seconds interval.
1197          */
1198         mutex_lock(&console_session.ses_mutex);
1199
1200         if (console_session.ses_shutdown || console_session.ses_expired) {
1201                 mutex_unlock(&console_session.ses_mutex);
1202                 return;
1203         }
1204
1205         if (!console_session.ses_expired &&
1206             cfs_time_current_sec() - console_session.ses_laststamp >
1207             (time_t)console_session.ses_timeout)
1208                 console_session.ses_expired = 1;
1209
1210         trans = console_session.ses_ping;
1211
1212         LASSERT (trans != NULL);
1213
1214         cfs_list_for_each_entry_typed(ndl, &console_session.ses_ndl_list,
1215                                       lstcon_ndlink_t, ndl_link) {
1216                 nd = ndl->ndl_node;
1217
1218                 if (console_session.ses_expired) {
1219                         /* idle console, end session on all nodes */
1220                         if (nd->nd_state != LST_NODE_ACTIVE)
1221                                 continue;
1222
1223                         rc = lstcon_sesrpc_prep(nd, LST_TRANS_SESEND,
1224                                                 trans->tas_features, &crpc);
1225                         if (rc != 0) {
1226                                 CERROR("Out of memory\n");
1227                                 break;
1228                         }
1229
1230                         lstcon_rpc_trans_addreq(trans, crpc);
1231                         lstcon_rpc_post(crpc);
1232
1233                         continue;
1234                 }
1235
1236                 crpc = &nd->nd_ping;
1237
1238                 if (crpc->crp_rpc != NULL) {
1239                         LASSERT (crpc->crp_trans == trans);
1240                         LASSERT (!cfs_list_empty(&crpc->crp_link));
1241
1242                         spin_lock(&crpc->crp_rpc->crpc_lock);
1243
1244                         LASSERT(crpc->crp_posted);
1245
1246                         if (!crpc->crp_finished) {
1247                                 /* in flight */
1248                                 spin_unlock(&crpc->crp_rpc->crpc_lock);
1249                                 continue;
1250                         }
1251
1252                         spin_unlock(&crpc->crp_rpc->crpc_lock);
1253
1254                         lstcon_rpc_get_reply(crpc, &rep);
1255
1256                         cfs_list_del_init(&crpc->crp_link);
1257
1258                         lstcon_rpc_put(crpc);
1259                 }
1260
1261                 if (nd->nd_state != LST_NODE_ACTIVE)
1262                         continue;
1263
1264                 intv = cfs_duration_sec(cfs_time_sub(cfs_time_current(),
1265                                                      nd->nd_stamp));
1266                 if (intv < (time_t)nd->nd_timeout / 2)
1267                         continue;
1268
1269                 rc = lstcon_rpc_init(nd, SRPC_SERVICE_DEBUG,
1270                                      trans->tas_features, 0, 0, 1, crpc);
1271                 if (rc != 0) {
1272                         CERROR("Out of memory\n");
1273                         break;
1274                 }
1275
1276                 drq = &crpc->crp_rpc->crpc_reqstmsg.msg_body.dbg_reqst;
1277
1278                 drq->dbg_sid   = console_session.ses_id;
1279                 drq->dbg_flags = 0;
1280
1281                 lstcon_rpc_trans_addreq(trans, crpc);
1282                 lstcon_rpc_post(crpc);
1283
1284                 count ++;
1285         }
1286
1287         if (console_session.ses_expired) {
1288                 mutex_unlock(&console_session.ses_mutex);
1289                 return;
1290         }
1291
1292         CDEBUG(D_NET, "Ping %d nodes in session\n", count);
1293
1294         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1295         stt_add_timer(ptimer);
1296
1297         mutex_unlock(&console_session.ses_mutex);
1298 }
1299
1300 int
1301 lstcon_rpc_pinger_start(void)
1302 {
1303         stt_timer_t    *ptimer;
1304         int             rc;
1305
1306         LASSERT (cfs_list_empty(&console_session.ses_rpc_freelist));
1307         LASSERT (atomic_read(&console_session.ses_rpc_counter) == 0);
1308
1309         rc = lstcon_rpc_trans_prep(NULL, LST_TRANS_SESPING,
1310                                    &console_session.ses_ping);
1311         if (rc != 0) {
1312                 CERROR("Failed to create console pinger\n");
1313                 return rc;
1314         }
1315
1316         ptimer = &console_session.ses_ping_timer;
1317         ptimer->stt_expires = (cfs_time_t)(cfs_time_current_sec() + LST_PING_INTERVAL);
1318
1319         stt_add_timer(ptimer);
1320
1321         return 0;
1322 }
1323
1324 void
1325 lstcon_rpc_pinger_stop(void)
1326 {
1327         LASSERT (console_session.ses_shutdown);
1328
1329         stt_del_timer(&console_session.ses_ping_timer);
1330
1331         lstcon_rpc_trans_abort(console_session.ses_ping, -ESHUTDOWN);
1332         lstcon_rpc_trans_stat(console_session.ses_ping, lstcon_trans_stat());
1333         lstcon_rpc_trans_destroy(console_session.ses_ping);
1334
1335         memset(lstcon_trans_stat(), 0, sizeof(lstcon_trans_stat_t));
1336
1337         console_session.ses_ping = NULL;
1338 }
1339
1340 void
1341 lstcon_rpc_cleanup_wait(void)
1342 {
1343         lstcon_rpc_trans_t *trans;
1344         lstcon_rpc_t       *crpc;
1345         cfs_list_t         *pacer;
1346         cfs_list_t          zlist;
1347
1348         /* Called with hold of global mutex */
1349
1350         LASSERT (console_session.ses_shutdown);
1351
1352         while (!cfs_list_empty(&console_session.ses_trans_list)) { 
1353                 cfs_list_for_each(pacer, &console_session.ses_trans_list) {
1354                         trans = cfs_list_entry(pacer, lstcon_rpc_trans_t,
1355                                                tas_link);
1356
1357                         CDEBUG(D_NET, "Session closed, wakeup transaction %s\n",
1358                                lstcon_rpc_trans_name(trans->tas_opc));
1359
1360                         wake_up(&trans->tas_waitq);
1361                 }
1362
1363                 mutex_unlock(&console_session.ses_mutex);
1364
1365                 CWARN("Session is shutting down, "
1366                       "waiting for termination of transactions\n");
1367                 cfs_pause(cfs_time_seconds(1));
1368
1369                 mutex_lock(&console_session.ses_mutex);
1370         }
1371
1372         spin_lock(&console_session.ses_rpc_lock);
1373
1374         lst_wait_until((atomic_read(&console_session.ses_rpc_counter) == 0),
1375                        console_session.ses_rpc_lock,
1376                        "Network is not accessable or target is down, "
1377                        "waiting for %d console RPCs to being recycled\n",
1378                        atomic_read(&console_session.ses_rpc_counter));
1379
1380         cfs_list_add(&zlist, &console_session.ses_rpc_freelist);
1381         cfs_list_del_init(&console_session.ses_rpc_freelist);
1382
1383         spin_unlock(&console_session.ses_rpc_lock);
1384
1385         while (!cfs_list_empty(&zlist)) {
1386                 crpc = cfs_list_entry(zlist.next, lstcon_rpc_t, crp_link);
1387
1388                 cfs_list_del(&crpc->crp_link);
1389                 LIBCFS_FREE(crpc, sizeof(lstcon_rpc_t));
1390         }
1391 }
1392
1393 int
1394 lstcon_rpc_module_init(void)
1395 {
1396         CFS_INIT_LIST_HEAD(&console_session.ses_ping_timer.stt_list);
1397         console_session.ses_ping_timer.stt_func = lstcon_rpc_pinger;
1398         console_session.ses_ping_timer.stt_data = &console_session.ses_ping_timer;
1399
1400         console_session.ses_ping = NULL;
1401
1402         spin_lock_init(&console_session.ses_rpc_lock);
1403         atomic_set(&console_session.ses_rpc_counter, 0);
1404         CFS_INIT_LIST_HEAD(&console_session.ses_rpc_freelist);
1405
1406         return 0;
1407 }
1408
1409 void
1410 lstcon_rpc_module_fini(void)
1411 {
1412         LASSERT (cfs_list_empty(&console_session.ses_rpc_freelist));
1413         LASSERT (atomic_read(&console_session.ses_rpc_counter) == 0);
1414 }
1415
1416 #endif