Whamcloud - gitweb
LU-2074 build: fix 'copy into fixed size buffer' errors
[fs/lustre-release.git] / lnet / selftest / console.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) 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  * Infrastructure of LST console
39  *
40  * Author: Liang Zhen <liangzhen@clusterfs.com>
41  */
42
43 #ifdef __KERNEL__
44
45 #include <libcfs/libcfs.h>
46 #include <lnet/lib-lnet.h>
47 #include "console.h"
48 #include "conrpc.h"
49
50 #define LST_NODE_STATE_COUNTER(nd, p)                   \
51 do {                                                    \
52         if ((nd)->nd_state == LST_NODE_ACTIVE)          \
53                 (p)->nle_nactive ++;                    \
54         else if ((nd)->nd_state == LST_NODE_BUSY)       \
55                 (p)->nle_nbusy ++;                      \
56         else if ((nd)->nd_state == LST_NODE_DOWN)       \
57                 (p)->nle_ndown ++;                      \
58         else                                            \
59                 (p)->nle_nunknown ++;                   \
60         (p)->nle_nnode ++;                              \
61 } while (0)
62
63 lstcon_session_t        console_session;
64
65 void
66 lstcon_node_get(lstcon_node_t *nd)
67 {
68         LASSERT (nd->nd_ref >= 1);
69
70         nd->nd_ref++;
71 }
72
73 static int
74 lstcon_node_find(lnet_process_id_t id, lstcon_node_t **ndpp, int create)
75 {
76         lstcon_ndlink_t *ndl;
77         unsigned int     idx = LNET_NIDADDR(id.nid) % LST_GLOBAL_HASHSIZE;
78
79         LASSERT (id.nid != LNET_NID_ANY);
80
81         cfs_list_for_each_entry_typed(ndl, &console_session.ses_ndl_hash[idx],
82                                       lstcon_ndlink_t, ndl_hlink) {
83                 if (ndl->ndl_node->nd_id.nid != id.nid ||
84                     ndl->ndl_node->nd_id.pid != id.pid)
85                         continue;
86
87                 lstcon_node_get(ndl->ndl_node);
88                 *ndpp = ndl->ndl_node;
89                 return 0;
90         }
91         
92         if (!create)
93                 return -ENOENT;
94
95         LIBCFS_ALLOC(*ndpp, sizeof(lstcon_node_t) + sizeof(lstcon_ndlink_t));
96         if (*ndpp == NULL)
97                 return -ENOMEM;
98
99         ndl = (lstcon_ndlink_t *)(*ndpp + 1);
100
101         ndl->ndl_node = *ndpp;
102
103         ndl->ndl_node->nd_ref   = 1;
104         ndl->ndl_node->nd_id    = id;
105         ndl->ndl_node->nd_stamp = cfs_time_current();
106         ndl->ndl_node->nd_state = LST_NODE_UNKNOWN;
107         ndl->ndl_node->nd_timeout = 0;
108         memset(&ndl->ndl_node->nd_ping, 0, sizeof(lstcon_rpc_t));
109
110         /* queued in global hash & list, no refcount is taken by
111          * global hash & list, if caller release his refcount,
112          * node will be released */
113         cfs_list_add_tail(&ndl->ndl_hlink, &console_session.ses_ndl_hash[idx]);
114         cfs_list_add_tail(&ndl->ndl_link, &console_session.ses_ndl_list);
115
116         return 0;
117 }
118
119 void
120 lstcon_node_put(lstcon_node_t *nd)
121 {
122         lstcon_ndlink_t  *ndl;
123
124         LASSERT (nd->nd_ref > 0);
125
126         if (--nd->nd_ref > 0)
127                 return;
128
129         ndl = (lstcon_ndlink_t *)(nd + 1);
130
131         LASSERT (!cfs_list_empty(&ndl->ndl_link));
132         LASSERT (!cfs_list_empty(&ndl->ndl_hlink));
133
134         /* remove from session */
135         cfs_list_del(&ndl->ndl_link);
136         cfs_list_del(&ndl->ndl_hlink);
137
138         LIBCFS_FREE(nd, sizeof(lstcon_node_t) + sizeof(lstcon_ndlink_t));
139 }
140
141 static int
142 lstcon_ndlink_find(cfs_list_t *hash,
143                    lnet_process_id_t id, lstcon_ndlink_t **ndlpp, int create)
144 {
145         unsigned int     idx = LNET_NIDADDR(id.nid) % LST_NODE_HASHSIZE;
146         lstcon_ndlink_t *ndl;
147         lstcon_node_t   *nd;
148         int              rc;
149
150         if (id.nid == LNET_NID_ANY)
151                 return -EINVAL;
152
153         /* search in hash */
154         cfs_list_for_each_entry_typed(ndl, &hash[idx],
155                                       lstcon_ndlink_t, ndl_hlink) {
156                 if (ndl->ndl_node->nd_id.nid != id.nid ||
157                     ndl->ndl_node->nd_id.pid != id.pid)
158                         continue;
159
160                 *ndlpp = ndl;
161                 return 0;
162         }
163
164         if (create == 0)
165                 return -ENOENT;
166
167         /* find or create in session hash */
168         rc = lstcon_node_find(id, &nd, (create == 1) ? 1 : 0);
169         if (rc != 0)
170                 return rc;
171
172         LIBCFS_ALLOC(ndl, sizeof(lstcon_ndlink_t));
173         if (ndl == NULL) {
174                 lstcon_node_put(nd);
175                 return -ENOMEM;
176         }
177         
178         *ndlpp = ndl;
179
180         ndl->ndl_node = nd;
181         CFS_INIT_LIST_HEAD(&ndl->ndl_link);
182         cfs_list_add_tail(&ndl->ndl_hlink, &hash[idx]);
183
184         return  0;
185 }
186
187 static void
188 lstcon_ndlink_release(lstcon_ndlink_t *ndl)
189 {
190         LASSERT (cfs_list_empty(&ndl->ndl_link));
191         LASSERT (!cfs_list_empty(&ndl->ndl_hlink));
192
193         cfs_list_del(&ndl->ndl_hlink); /* delete from hash */
194         lstcon_node_put(ndl->ndl_node);
195
196         LIBCFS_FREE(ndl, sizeof(*ndl));
197 }
198
199 static int
200 lstcon_group_alloc(char *name, lstcon_group_t **grpp)
201 {
202         lstcon_group_t *grp;
203         int             i;
204
205         LIBCFS_ALLOC(grp, offsetof(lstcon_group_t,
206                                    grp_ndl_hash[LST_NODE_HASHSIZE]));
207         if (grp == NULL)
208                 return -ENOMEM;
209
210         memset(grp, 0, offsetof(lstcon_group_t,
211                                 grp_ndl_hash[LST_NODE_HASHSIZE]));
212
213         grp->grp_ref = 1;
214         if (name != NULL) {
215                 if (strlen(name) > sizeof(grp->grp_name)-1) {
216                         LIBCFS_FREE(grp, offsetof(lstcon_group_t,
217                                           grp_ndl_hash[LST_NODE_HASHSIZE]));
218                         return -E2BIG;
219                 }
220                 strncpy(grp->grp_name, name, sizeof(grp->grp_name));
221         }
222
223         CFS_INIT_LIST_HEAD(&grp->grp_link);
224         CFS_INIT_LIST_HEAD(&grp->grp_ndl_list);
225         CFS_INIT_LIST_HEAD(&grp->grp_trans_list);
226
227         for (i = 0; i < LST_NODE_HASHSIZE; i++)
228                 CFS_INIT_LIST_HEAD(&grp->grp_ndl_hash[i]);
229
230         *grpp = grp;
231
232         return 0;
233 }
234
235 static void
236 lstcon_group_addref(lstcon_group_t *grp)
237 {
238         grp->grp_ref ++;
239 }
240
241 static void lstcon_group_ndlink_release(lstcon_group_t *, lstcon_ndlink_t *);
242
243 static void
244 lstcon_group_drain(lstcon_group_t *grp, int keep)
245 {
246         lstcon_ndlink_t *ndl;
247         lstcon_ndlink_t *tmp;
248
249         cfs_list_for_each_entry_safe_typed(ndl, tmp, &grp->grp_ndl_list,
250                                            lstcon_ndlink_t, ndl_link) {
251                 if ((ndl->ndl_node->nd_state & keep) == 0)
252                         lstcon_group_ndlink_release(grp, ndl);
253         }
254 }
255
256 static void
257 lstcon_group_decref(lstcon_group_t *grp)
258 {
259         int     i;
260
261         if (--grp->grp_ref > 0)
262                 return;
263
264         if (!cfs_list_empty(&grp->grp_link))
265                 cfs_list_del(&grp->grp_link);
266
267         lstcon_group_drain(grp, 0);
268
269         for (i = 0; i < LST_NODE_HASHSIZE; i++) {
270                 LASSERT (cfs_list_empty(&grp->grp_ndl_hash[i]));
271         }
272
273         LIBCFS_FREE(grp, offsetof(lstcon_group_t,
274                                   grp_ndl_hash[LST_NODE_HASHSIZE]));
275 }
276
277 static int
278 lstcon_group_find(const char *name, lstcon_group_t **grpp)
279 {
280         lstcon_group_t   *grp;
281
282         cfs_list_for_each_entry_typed(grp, &console_session.ses_grp_list,
283                                       lstcon_group_t, grp_link) {
284                 if (strncmp(grp->grp_name, name, LST_NAME_SIZE) != 0)
285                         continue;
286
287                 lstcon_group_addref(grp);  /* +1 ref for caller */
288                 *grpp = grp;
289                 return 0;
290         }
291
292         return -ENOENT;
293 }
294
295 static void
296 lstcon_group_put(lstcon_group_t *grp)
297 {
298         lstcon_group_decref(grp);
299 }
300
301 static int
302 lstcon_group_ndlink_find(lstcon_group_t *grp, lnet_process_id_t id,
303                          lstcon_ndlink_t **ndlpp, int create)
304 {
305         int     rc;
306
307         rc = lstcon_ndlink_find(&grp->grp_ndl_hash[0], id, ndlpp, create);
308         if (rc != 0)
309                 return rc;
310
311         if (!cfs_list_empty(&(*ndlpp)->ndl_link))
312                 return 0;
313
314         cfs_list_add_tail(&(*ndlpp)->ndl_link, &grp->grp_ndl_list);
315         grp->grp_nnode ++;
316
317         return 0;
318 }
319
320 static void
321 lstcon_group_ndlink_release(lstcon_group_t *grp, lstcon_ndlink_t *ndl)
322 {
323         cfs_list_del_init(&ndl->ndl_link);
324         lstcon_ndlink_release(ndl);
325         grp->grp_nnode --;
326 }
327
328 static void
329 lstcon_group_ndlink_move(lstcon_group_t *old,
330                          lstcon_group_t *new, lstcon_ndlink_t *ndl)
331 {
332         unsigned int idx = LNET_NIDADDR(ndl->ndl_node->nd_id.nid) %
333                            LST_NODE_HASHSIZE;
334
335         cfs_list_del(&ndl->ndl_hlink);
336         cfs_list_del(&ndl->ndl_link);
337         old->grp_nnode --;
338
339         cfs_list_add_tail(&ndl->ndl_hlink, &new->grp_ndl_hash[idx]);
340         cfs_list_add_tail(&ndl->ndl_link, &new->grp_ndl_list);
341         new->grp_nnode ++;
342
343         return;
344 }
345
346 static void
347 lstcon_group_move(lstcon_group_t *old, lstcon_group_t *new)
348 {
349         lstcon_ndlink_t *ndl;
350
351         while (!cfs_list_empty(&old->grp_ndl_list)) {
352                 ndl = cfs_list_entry(old->grp_ndl_list.next,
353                                      lstcon_ndlink_t, ndl_link);
354                 lstcon_group_ndlink_move(old, new, ndl);
355         }
356 }
357
358 int
359 lstcon_sesrpc_condition(int transop, lstcon_node_t *nd, void *arg)
360 {
361         lstcon_group_t *grp = (lstcon_group_t *)arg;
362
363         switch (transop) {
364         case LST_TRANS_SESNEW:
365                 if (nd->nd_state == LST_NODE_ACTIVE)
366                         return 0;
367                 break;
368
369         case LST_TRANS_SESEND:
370                 if (nd->nd_state != LST_NODE_ACTIVE)
371                         return 0;
372
373                 if (grp != NULL && nd->nd_ref > 1)
374                         return 0;
375                 break;
376
377         case LST_TRANS_SESQRY:
378                 break;
379
380         default:
381                 LBUG();
382         }
383
384         return 1;
385 }
386
387 int
388 lstcon_sesrpc_readent(int transop, srpc_msg_t *msg,
389                       lstcon_rpc_ent_t *ent_up)
390 {
391         srpc_debug_reply_t *rep;
392
393         switch (transop) {
394         case LST_TRANS_SESNEW:
395         case LST_TRANS_SESEND:
396                 return 0;
397
398         case LST_TRANS_SESQRY:
399                 rep = &msg->msg_body.dbg_reply;
400
401                 if (copy_to_user(&ent_up->rpe_priv[0],
402                                      &rep->dbg_timeout, sizeof(int)) ||
403                     copy_to_user(&ent_up->rpe_payload[0],
404                                      &rep->dbg_name, LST_NAME_SIZE))
405                         return -EFAULT;
406
407                 return 0;
408
409         default:
410                 LBUG();
411         }
412
413         return 0;
414 }
415
416 static int
417 lstcon_group_nodes_add(lstcon_group_t *grp,
418                        int count, lnet_process_id_t *ids_up,
419                        unsigned *featp, cfs_list_t *result_up)
420 {
421         lstcon_rpc_trans_t      *trans;
422         lstcon_ndlink_t         *ndl;
423         lstcon_group_t          *tmp;
424         lnet_process_id_t        id;
425         int                      i;
426         int                      rc;
427
428         rc = lstcon_group_alloc(NULL, &tmp);
429         if (rc != 0) {
430                 CERROR("Out of memory\n");
431                 return -ENOMEM;
432         }
433
434         for (i = 0 ; i < count; i++) {
435                 if (copy_from_user(&id, &ids_up[i], sizeof(id))) {
436                         rc = -EFAULT;
437                         break;
438                 }
439
440                 /* skip if it's in this group already */
441                 rc = lstcon_group_ndlink_find(grp, id, &ndl, 0);
442                 if (rc == 0)
443                         continue;
444
445                 /* add to tmp group */
446                 rc = lstcon_group_ndlink_find(tmp, id, &ndl, 1);
447                 if (rc != 0) {
448                         CERROR("Can't create ndlink, out of memory\n");
449                         break;
450                 }
451         }
452
453         if (rc != 0) {
454                 lstcon_group_put(tmp);
455                 return rc;
456         }
457
458         rc = lstcon_rpc_trans_ndlist(&tmp->grp_ndl_list,
459                                      &tmp->grp_trans_list, LST_TRANS_SESNEW,
460                                      tmp, lstcon_sesrpc_condition, &trans);
461         if (rc != 0) {
462                 CERROR("Can't create transaction: %d\n", rc);
463                 lstcon_group_put(tmp);
464                 return rc;
465         }
466
467         /* post all RPCs */
468         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
469
470         rc = lstcon_rpc_trans_interpreter(trans, result_up,
471                                           lstcon_sesrpc_readent);
472         *featp = trans->tas_features;
473
474         /* destroy all RPGs */
475         lstcon_rpc_trans_destroy(trans);
476
477         lstcon_group_move(tmp, grp);
478         lstcon_group_put(tmp);
479
480         return rc;
481 }
482
483 static int
484 lstcon_group_nodes_remove(lstcon_group_t *grp,
485                           int count, lnet_process_id_t *ids_up,
486                           cfs_list_t *result_up)
487 {
488         lstcon_rpc_trans_t     *trans;
489         lstcon_ndlink_t        *ndl;
490         lstcon_group_t         *tmp;
491         lnet_process_id_t       id;
492         int                     rc;
493         int                     i;
494
495         /* End session and remove node from the group */
496
497         rc = lstcon_group_alloc(NULL, &tmp);
498         if (rc != 0) {
499                 CERROR("Out of memory\n");
500                 return -ENOMEM;
501         }
502
503         for (i = 0; i < count; i++) {
504                 if (copy_from_user(&id, &ids_up[i], sizeof(id))) {
505                         rc = -EFAULT;
506                         goto error;
507                 }
508                 
509                 /* move node to tmp group */
510                 if (lstcon_group_ndlink_find(grp, id, &ndl, 0) == 0)
511                         lstcon_group_ndlink_move(grp, tmp, ndl);
512         }
513
514         rc = lstcon_rpc_trans_ndlist(&tmp->grp_ndl_list,
515                                      &tmp->grp_trans_list, LST_TRANS_SESEND,
516                                      tmp, lstcon_sesrpc_condition, &trans);
517         if (rc != 0) {
518                 CERROR("Can't create transaction: %d\n", rc);
519                 goto error;
520         }
521
522         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
523
524         rc = lstcon_rpc_trans_interpreter(trans, result_up, NULL);
525
526         lstcon_rpc_trans_destroy(trans);
527         /* release nodes anyway, because we can't rollback status */
528         lstcon_group_put(tmp);
529
530         return rc;
531 error:
532         lstcon_group_move(tmp, grp);
533         lstcon_group_put(tmp);
534
535         return rc;
536 }
537
538 int
539 lstcon_group_add(char *name)
540 {
541         lstcon_group_t *grp;
542         int             rc;
543
544         rc = (lstcon_group_find(name, &grp) == 0)? -EEXIST: 0;
545         if (rc != 0) {
546                 /* find a group with same name */
547                 lstcon_group_put(grp);
548                 return rc;
549         }
550
551         rc = lstcon_group_alloc(name, &grp);
552         if (rc != 0) {
553                 CERROR("Can't allocate descriptor for group %s\n", name);
554                 return -ENOMEM;
555         }
556
557         cfs_list_add_tail(&grp->grp_link, &console_session.ses_grp_list);
558
559         return rc;
560 }
561
562 int
563 lstcon_nodes_add(char *name, int count, lnet_process_id_t *ids_up,
564                  unsigned *featp, cfs_list_t *result_up)
565 {
566         lstcon_group_t         *grp;
567         int                     rc;
568
569         LASSERT (count > 0);
570         LASSERT (ids_up != NULL);
571
572         rc = lstcon_group_find(name, &grp);
573         if (rc != 0) {
574                 CDEBUG(D_NET, "Can't find group %s\n", name);
575                 return rc;
576         }
577
578         if (grp->grp_ref > 2) {
579                 /* referred by other threads or test */
580                 CDEBUG(D_NET, "Group %s is busy\n", name);
581                 lstcon_group_put(grp);
582
583                 return -EBUSY;
584         }
585
586         rc = lstcon_group_nodes_add(grp, count, ids_up, featp, result_up);
587
588         lstcon_group_put(grp);
589
590         return rc;
591 }
592
593 int
594 lstcon_group_del(char *name)
595 {
596         lstcon_rpc_trans_t *trans;
597         lstcon_group_t     *grp;
598         int                 rc;
599
600         rc = lstcon_group_find(name, &grp);
601         if (rc != 0) {
602                 CDEBUG(D_NET, "Can't find group: %s\n", name);
603                 return rc;
604         }
605
606         if (grp->grp_ref > 2) {
607                 /* referred by others threads or test */
608                 CDEBUG(D_NET, "Group %s is busy\n", name);
609                 lstcon_group_put(grp);
610                 return -EBUSY;
611         }
612
613         rc = lstcon_rpc_trans_ndlist(&grp->grp_ndl_list,
614                                      &grp->grp_trans_list, LST_TRANS_SESEND,
615                                      grp, lstcon_sesrpc_condition, &trans);
616         if (rc != 0) {
617                 CERROR("Can't create transaction: %d\n", rc);
618                 lstcon_group_put(grp);
619                 return rc;
620         }
621
622         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
623
624         lstcon_rpc_trans_destroy(trans);
625
626         lstcon_group_put(grp);
627         /* -ref for session, it's destroyed,
628          * status can't be rolled back, destroy group anway */
629         lstcon_group_put(grp);
630
631         return rc;
632 }
633
634 int
635 lstcon_group_clean(char *name, int args)
636 {
637         lstcon_group_t *grp = NULL;
638         int             rc;
639
640         rc = lstcon_group_find(name, &grp);
641         if (rc != 0) {
642                 CDEBUG(D_NET, "Can't find group %s\n", name);
643                 return rc;
644         }
645
646         if (grp->grp_ref > 2) {
647                 /* referred by test */
648                 CDEBUG(D_NET, "Group %s is busy\n", name);
649                 lstcon_group_put(grp);
650                 return -EBUSY;
651         }
652
653         args = (LST_NODE_ACTIVE | LST_NODE_BUSY |
654                 LST_NODE_DOWN | LST_NODE_UNKNOWN) & ~args;
655
656         lstcon_group_drain(grp, args);
657
658         lstcon_group_put(grp);
659         /* release empty group */
660         if (cfs_list_empty(&grp->grp_ndl_list))
661                 lstcon_group_put(grp);
662
663         return 0;
664 }
665
666 int
667 lstcon_nodes_remove(char *name, int count,
668                     lnet_process_id_t *ids_up, cfs_list_t *result_up)
669 {
670         lstcon_group_t *grp = NULL;
671         int             rc;
672
673         rc = lstcon_group_find(name, &grp);
674         if (rc != 0) {
675                 CDEBUG(D_NET, "Can't find group: %s\n", name);
676                 return rc;
677         }
678
679         if (grp->grp_ref > 2) {
680                 /* referred by test */
681                 CDEBUG(D_NET, "Group %s is busy\n", name);
682                 lstcon_group_put(grp);
683                 return -EBUSY;
684         }
685
686         rc = lstcon_group_nodes_remove(grp, count, ids_up, result_up);
687
688         lstcon_group_put(grp);
689         /* release empty group */
690         if (cfs_list_empty(&grp->grp_ndl_list))
691                 lstcon_group_put(grp);
692
693         return rc;
694 }
695
696 int
697 lstcon_group_refresh(char *name, cfs_list_t *result_up)
698 {
699         lstcon_rpc_trans_t      *trans;
700         lstcon_group_t          *grp;
701         int                      rc;
702
703         rc = lstcon_group_find(name, &grp);
704         if (rc != 0) {
705                 CDEBUG(D_NET, "Can't find group: %s\n", name);
706                 return rc;
707         }
708
709         if (grp->grp_ref > 2) {
710                 /* referred by test */
711                 CDEBUG(D_NET, "Group %s is busy\n", name);
712                 lstcon_group_put(grp);
713                 return -EBUSY;
714         }
715
716         /* re-invite all inactive nodes int the group */
717         rc = lstcon_rpc_trans_ndlist(&grp->grp_ndl_list,
718                                      &grp->grp_trans_list, LST_TRANS_SESNEW,
719                                      grp, lstcon_sesrpc_condition, &trans);
720         if (rc != 0) {
721                 /* local error, return */
722                 CDEBUG(D_NET, "Can't create transaction: %d\n", rc);
723                 lstcon_group_put(grp);
724                 return rc;
725         }
726
727         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
728
729         rc = lstcon_rpc_trans_interpreter(trans, result_up, NULL);
730
731         lstcon_rpc_trans_destroy(trans);
732         /* -ref for me */
733         lstcon_group_put(grp);
734
735         return rc;
736 }
737
738 int
739 lstcon_group_list(int index, int len, char *name_up)
740 {
741         lstcon_group_t *grp;
742
743         LASSERT (index >= 0);
744         LASSERT (name_up != NULL);
745
746         cfs_list_for_each_entry_typed(grp, &console_session.ses_grp_list,
747                                       lstcon_group_t, grp_link) {
748                 if (index-- == 0) {
749                         return copy_to_user(name_up, grp->grp_name, len) ?
750                                -EFAULT : 0;
751                 }
752         }
753
754         return -ENOENT;
755 }
756
757 static int
758 lstcon_nodes_getent(cfs_list_t *head, int *index_p,
759                     int *count_p, lstcon_node_ent_t *dents_up)
760 {
761         lstcon_ndlink_t  *ndl;
762         lstcon_node_t    *nd;
763         int               count = 0;
764         int               index = 0;
765
766         LASSERT (index_p != NULL && count_p != NULL);
767         LASSERT (dents_up != NULL);
768         LASSERT (*index_p >= 0);
769         LASSERT (*count_p > 0);
770
771         cfs_list_for_each_entry_typed(ndl, head, lstcon_ndlink_t, ndl_link) {
772                 if (index++ < *index_p)
773                         continue;
774
775                 if (count >= *count_p)
776                         break;
777
778                 nd = ndl->ndl_node;
779                 if (copy_to_user(&dents_up[count].nde_id,
780                                      &nd->nd_id, sizeof(nd->nd_id)) ||
781                     copy_to_user(&dents_up[count].nde_state,
782                                      &nd->nd_state, sizeof(nd->nd_state)))
783                         return -EFAULT;
784
785                 count ++;
786         }
787
788         if (index <= *index_p)
789                 return -ENOENT;
790
791         *count_p = count;
792         *index_p = index;
793
794         return 0;
795 }
796
797 int
798 lstcon_group_info(char *name, lstcon_ndlist_ent_t *gents_p,
799                   int *index_p, int *count_p, lstcon_node_ent_t *dents_up)
800 {
801         lstcon_ndlist_ent_t *gentp;
802         lstcon_group_t      *grp;
803         lstcon_ndlink_t     *ndl;
804         int                  rc;
805
806         rc = lstcon_group_find(name, &grp);
807         if (rc != 0) {
808                 CDEBUG(D_NET, "Can't find group %s\n", name);
809                 return rc;
810         }
811
812         if (dents_up != 0) {
813                 /* verbose query */
814                 rc = lstcon_nodes_getent(&grp->grp_ndl_list,
815                                          index_p, count_p, dents_up);
816                 lstcon_group_put(grp);
817
818                 return rc;
819         }
820
821         /* non-verbose query */
822         LIBCFS_ALLOC(gentp, sizeof(lstcon_ndlist_ent_t));
823         if (gentp == NULL) {
824                 CERROR("Can't allocate ndlist_ent\n");
825                 lstcon_group_put(grp);
826
827                 return -ENOMEM;
828         }
829
830         memset(gentp, 0, sizeof(lstcon_ndlist_ent_t));
831
832         cfs_list_for_each_entry_typed(ndl, &grp->grp_ndl_list,
833                                       lstcon_ndlink_t, ndl_link)
834                 LST_NODE_STATE_COUNTER(ndl->ndl_node, gentp);
835
836         rc = copy_to_user(gents_p, gentp,
837                               sizeof(lstcon_ndlist_ent_t)) ? -EFAULT: 0;
838
839         LIBCFS_FREE(gentp, sizeof(lstcon_ndlist_ent_t));
840
841         lstcon_group_put(grp);
842
843         return 0;
844 }
845
846 static int
847 lstcon_batch_find(const char *name, lstcon_batch_t **batpp)
848 {
849         lstcon_batch_t   *bat;
850
851         cfs_list_for_each_entry_typed(bat, &console_session.ses_bat_list,
852                                       lstcon_batch_t, bat_link) {
853                 if (strncmp(bat->bat_name, name, LST_NAME_SIZE) == 0) {
854                         *batpp = bat;
855                         return 0;
856                 }
857         }
858
859         return -ENOENT;
860 }
861
862 int
863 lstcon_batch_add(char *name)
864 {
865         lstcon_batch_t   *bat;
866         int               i;
867         int               rc;
868
869         rc = (lstcon_batch_find(name, &bat) == 0)? -EEXIST: 0;
870         if (rc != 0) {
871                 CDEBUG(D_NET, "Batch %s already exists\n", name);
872                 return rc;
873         }
874
875         LIBCFS_ALLOC(bat, sizeof(lstcon_batch_t));
876         if (bat == NULL) {
877                 CERROR("Can't allocate descriptor for batch %s\n", name);
878                 return -ENOMEM;
879         }
880
881         LIBCFS_ALLOC(bat->bat_cli_hash,
882                      sizeof(cfs_list_t) * LST_NODE_HASHSIZE);
883         if (bat->bat_cli_hash == NULL) {
884                 CERROR("Can't allocate hash for batch %s\n", name);
885                 LIBCFS_FREE(bat, sizeof(lstcon_batch_t));
886
887                 return -ENOMEM;
888         }
889
890         LIBCFS_ALLOC(bat->bat_srv_hash,
891                      sizeof(cfs_list_t) * LST_NODE_HASHSIZE);
892         if (bat->bat_srv_hash == NULL) {
893                 CERROR("Can't allocate hash for batch %s\n", name);
894                 LIBCFS_FREE(bat->bat_cli_hash, LST_NODE_HASHSIZE);
895                 LIBCFS_FREE(bat, sizeof(lstcon_batch_t));
896
897                 return -ENOMEM;
898         }
899
900         if (strlen(name) > sizeof(bat->bat_name)-1) {
901                 LIBCFS_FREE(bat->bat_srv_hash, LST_NODE_HASHSIZE);
902                 LIBCFS_FREE(bat->bat_cli_hash, LST_NODE_HASHSIZE);
903                 LIBCFS_FREE(bat, sizeof(lstcon_batch_t));
904                 return -E2BIG;
905         }
906         strncpy(bat->bat_name, name, sizeof(bat->bat_name));
907         bat->bat_hdr.tsb_index = 0;
908         bat->bat_hdr.tsb_id.bat_id = ++console_session.ses_id_cookie;
909
910         bat->bat_ntest = 0;
911         bat->bat_state = LST_BATCH_IDLE;
912
913         CFS_INIT_LIST_HEAD(&bat->bat_cli_list);
914         CFS_INIT_LIST_HEAD(&bat->bat_srv_list);
915         CFS_INIT_LIST_HEAD(&bat->bat_test_list);
916         CFS_INIT_LIST_HEAD(&bat->bat_trans_list);
917
918         for (i = 0; i < LST_NODE_HASHSIZE; i++) {
919                 CFS_INIT_LIST_HEAD(&bat->bat_cli_hash[i]);
920                 CFS_INIT_LIST_HEAD(&bat->bat_srv_hash[i]);
921         }
922
923         cfs_list_add_tail(&bat->bat_link, &console_session.ses_bat_list);
924
925         return rc;
926 }
927
928 int
929 lstcon_batch_list(int index, int len, char *name_up)
930 {
931         lstcon_batch_t    *bat;
932
933         LASSERT (name_up != NULL);
934         LASSERT (index >= 0);
935
936         cfs_list_for_each_entry_typed(bat, &console_session.ses_bat_list,
937                                       lstcon_batch_t, bat_link) {
938                 if (index-- == 0) {
939                         return copy_to_user(name_up, bat->bat_name, len) ?
940                                -EFAULT: 0;
941                 }
942         }
943
944         return -ENOENT;
945 }
946
947 int
948 lstcon_batch_info(char *name, lstcon_test_batch_ent_t *ent_up, int server,
949                   int testidx, int *index_p, int *ndent_p,
950                   lstcon_node_ent_t *dents_up)
951 {
952         lstcon_test_batch_ent_t *entp;
953         cfs_list_t              *clilst;
954         cfs_list_t              *srvlst;
955         lstcon_test_t           *test = NULL;
956         lstcon_batch_t          *bat;
957         lstcon_ndlink_t         *ndl;
958         int                      rc;
959
960         rc = lstcon_batch_find(name, &bat);
961         if (rc != 0) {
962                 CDEBUG(D_NET, "Can't find batch %s\n", name);
963                 return -ENOENT;
964         }
965
966         if (testidx > 0) {
967                 /* query test, test index start from 1 */
968                 cfs_list_for_each_entry_typed(test, &bat->bat_test_list,
969                                               lstcon_test_t, tes_link) {
970                         if (testidx-- == 1)
971                                 break;
972                 }
973
974                 if (testidx > 0) {
975                         CDEBUG(D_NET, "Can't find specified test in batch\n");
976                         return -ENOENT;
977                 }
978         }
979
980         clilst = (test == NULL) ? &bat->bat_cli_list :
981                                   &test->tes_src_grp->grp_ndl_list;
982         srvlst = (test == NULL) ? &bat->bat_srv_list :
983                                   &test->tes_dst_grp->grp_ndl_list;
984
985         if (dents_up != NULL) {
986                 rc = lstcon_nodes_getent((server ? srvlst: clilst),
987                                          index_p, ndent_p, dents_up);
988                 return rc;
989         }
990
991         /* non-verbose query */
992         LIBCFS_ALLOC(entp, sizeof(lstcon_test_batch_ent_t));
993         if (entp == NULL)
994                 return -ENOMEM;
995
996         memset(entp, 0, sizeof(lstcon_test_batch_ent_t));
997
998         if (test == NULL) {
999                 entp->u.tbe_batch.bae_ntest = bat->bat_ntest;
1000                 entp->u.tbe_batch.bae_state = bat->bat_state;
1001
1002         } else {
1003
1004                 entp->u.tbe_test.tse_type   = test->tes_type;
1005                 entp->u.tbe_test.tse_loop   = test->tes_loop;
1006                 entp->u.tbe_test.tse_concur = test->tes_concur;
1007         }
1008
1009         cfs_list_for_each_entry_typed(ndl, clilst, lstcon_ndlink_t, ndl_link)
1010                 LST_NODE_STATE_COUNTER(ndl->ndl_node, &entp->tbe_cli_nle);
1011
1012         cfs_list_for_each_entry_typed(ndl, srvlst, lstcon_ndlink_t, ndl_link)
1013                 LST_NODE_STATE_COUNTER(ndl->ndl_node, &entp->tbe_srv_nle);
1014
1015         rc = copy_to_user(ent_up, entp,
1016                               sizeof(lstcon_test_batch_ent_t)) ? -EFAULT : 0;
1017
1018         LIBCFS_FREE(entp, sizeof(lstcon_test_batch_ent_t));
1019
1020         return rc;
1021 }
1022
1023 int
1024 lstcon_batrpc_condition(int transop, lstcon_node_t *nd, void *arg)
1025 {
1026         switch (transop) {
1027         case LST_TRANS_TSBRUN:
1028                 if (nd->nd_state != LST_NODE_ACTIVE)
1029                         return -ENETDOWN;
1030                 break;
1031
1032         case LST_TRANS_TSBSTOP:
1033                 if (nd->nd_state != LST_NODE_ACTIVE)
1034                         return 0;
1035                 break;
1036
1037         case LST_TRANS_TSBCLIQRY:
1038         case LST_TRANS_TSBSRVQRY:
1039                 break;
1040         }
1041
1042         return 1;
1043 }
1044
1045 static int
1046 lstcon_batch_op(lstcon_batch_t *bat, int transop,
1047                 cfs_list_t *result_up)
1048 {
1049         lstcon_rpc_trans_t *trans;
1050         int                 rc;
1051
1052         rc = lstcon_rpc_trans_ndlist(&bat->bat_cli_list,
1053                                      &bat->bat_trans_list, transop,
1054                                      bat, lstcon_batrpc_condition, &trans);
1055         if (rc != 0) {
1056                 CERROR("Can't create transaction: %d\n", rc);
1057                 return rc;
1058         }
1059
1060         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
1061
1062         rc = lstcon_rpc_trans_interpreter(trans, result_up, NULL);
1063
1064         lstcon_rpc_trans_destroy(trans);
1065
1066         return rc;
1067 }
1068
1069 int
1070 lstcon_batch_run(char *name, int timeout, cfs_list_t *result_up)
1071 {
1072         lstcon_batch_t *bat;
1073         int             rc;
1074
1075         if (lstcon_batch_find(name, &bat) != 0) {
1076                 CDEBUG(D_NET, "Can't find batch %s\n", name);
1077                 return -ENOENT;
1078         }
1079
1080         bat->bat_arg = timeout;
1081
1082         rc = lstcon_batch_op(bat, LST_TRANS_TSBRUN, result_up);
1083
1084         /* mark batch as running if it's started in any node */
1085         if (lstcon_tsbop_stat_success(lstcon_trans_stat(), 0) != 0)
1086                 bat->bat_state = LST_BATCH_RUNNING;
1087
1088         return rc;
1089 }
1090
1091 int
1092 lstcon_batch_stop(char *name, int force, cfs_list_t *result_up)
1093 {
1094         lstcon_batch_t *bat;
1095         int             rc;
1096
1097         if (lstcon_batch_find(name, &bat) != 0) {
1098                 CDEBUG(D_NET, "Can't find batch %s\n", name);
1099                 return -ENOENT;
1100         }
1101
1102         bat->bat_arg = force;
1103
1104         rc = lstcon_batch_op(bat, LST_TRANS_TSBSTOP, result_up);
1105         
1106         /* mark batch as stopped if all RPCs finished */
1107         if (lstcon_tsbop_stat_failure(lstcon_trans_stat(), 0) == 0)
1108                 bat->bat_state = LST_BATCH_IDLE;
1109
1110         return rc;
1111 }
1112
1113 static void
1114 lstcon_batch_destroy(lstcon_batch_t *bat)
1115 {
1116         lstcon_ndlink_t    *ndl;
1117         lstcon_test_t      *test;
1118         int                 i;
1119
1120         cfs_list_del(&bat->bat_link);
1121
1122         while (!cfs_list_empty(&bat->bat_test_list)) {
1123                 test = cfs_list_entry(bat->bat_test_list.next,
1124                                       lstcon_test_t, tes_link);
1125                 LASSERT (cfs_list_empty(&test->tes_trans_list));
1126
1127                 cfs_list_del(&test->tes_link);
1128
1129                 lstcon_group_put(test->tes_src_grp);
1130                 lstcon_group_put(test->tes_dst_grp);
1131
1132                 LIBCFS_FREE(test, offsetof(lstcon_test_t,
1133                                            tes_param[test->tes_paramlen]));
1134         }
1135
1136         LASSERT (cfs_list_empty(&bat->bat_trans_list));
1137
1138         while (!cfs_list_empty(&bat->bat_cli_list)) {
1139                 ndl = cfs_list_entry(bat->bat_cli_list.next,
1140                                      lstcon_ndlink_t, ndl_link);
1141                 cfs_list_del_init(&ndl->ndl_link);
1142
1143                 lstcon_ndlink_release(ndl);
1144         }
1145
1146         while (!cfs_list_empty(&bat->bat_srv_list)) {
1147                 ndl = cfs_list_entry(bat->bat_srv_list.next,
1148                                      lstcon_ndlink_t, ndl_link);
1149                 cfs_list_del_init(&ndl->ndl_link);
1150
1151                 lstcon_ndlink_release(ndl);
1152         }
1153
1154         for (i = 0; i < LST_NODE_HASHSIZE; i++) {
1155                 LASSERT (cfs_list_empty(&bat->bat_cli_hash[i]));
1156                 LASSERT (cfs_list_empty(&bat->bat_srv_hash[i]));
1157         }
1158
1159         LIBCFS_FREE(bat->bat_cli_hash,
1160                     sizeof(cfs_list_t) * LST_NODE_HASHSIZE);
1161         LIBCFS_FREE(bat->bat_srv_hash,
1162                     sizeof(cfs_list_t) * LST_NODE_HASHSIZE);
1163         LIBCFS_FREE(bat, sizeof(lstcon_batch_t));
1164 }
1165
1166 int
1167 lstcon_testrpc_condition(int transop, lstcon_node_t *nd, void *arg)
1168 {
1169         lstcon_test_t    *test;
1170         lstcon_batch_t   *batch;
1171         lstcon_ndlink_t  *ndl;
1172         cfs_list_t       *hash;
1173         cfs_list_t       *head;
1174
1175         test = (lstcon_test_t *)arg;
1176         LASSERT (test != NULL);
1177
1178         batch = test->tes_batch;
1179         LASSERT (batch != NULL);
1180
1181         if (test->tes_oneside &&
1182             transop == LST_TRANS_TSBSRVADD)
1183                 return 0;
1184
1185         if (nd->nd_state != LST_NODE_ACTIVE)
1186                 return -ENETDOWN;
1187
1188         if (transop == LST_TRANS_TSBCLIADD) {
1189                 hash = batch->bat_cli_hash;
1190                 head = &batch->bat_cli_list;
1191         
1192         } else {
1193                 LASSERT (transop == LST_TRANS_TSBSRVADD);
1194
1195                 hash = batch->bat_srv_hash;
1196                 head = &batch->bat_srv_list;
1197         }
1198
1199         LASSERT (nd->nd_id.nid != LNET_NID_ANY);
1200
1201         if (lstcon_ndlink_find(hash, nd->nd_id, &ndl, 1) != 0)
1202                 return -ENOMEM;
1203
1204         if (cfs_list_empty(&ndl->ndl_link))
1205                 cfs_list_add_tail(&ndl->ndl_link, head);
1206
1207         return 1;
1208 }
1209
1210 static int
1211 lstcon_test_nodes_add(lstcon_test_t *test, cfs_list_t *result_up)
1212 {
1213         lstcon_rpc_trans_t     *trans;
1214         lstcon_group_t         *grp;
1215         int                     transop;
1216         int                     rc;
1217
1218         LASSERT (test->tes_src_grp != NULL);
1219         LASSERT (test->tes_dst_grp != NULL);
1220
1221         transop = LST_TRANS_TSBSRVADD;
1222         grp  = test->tes_dst_grp;
1223 again:
1224         rc = lstcon_rpc_trans_ndlist(&grp->grp_ndl_list,
1225                                      &test->tes_trans_list, transop,
1226                                      test, lstcon_testrpc_condition, &trans);
1227         if (rc != 0) {
1228                 CERROR("Can't create transaction: %d\n", rc);
1229                 return rc;
1230         }
1231
1232         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
1233
1234         if (lstcon_trans_stat()->trs_rpc_errno != 0 ||
1235             lstcon_trans_stat()->trs_fwk_errno != 0) {
1236                 lstcon_rpc_trans_interpreter(trans, result_up, NULL);
1237
1238                 lstcon_rpc_trans_destroy(trans);
1239                 /* return if any error */
1240                 CDEBUG(D_NET, "Failed to add test %s, "
1241                               "RPC error %d, framework error %d\n",
1242                        transop == LST_TRANS_TSBCLIADD ? "client" : "server",
1243                        lstcon_trans_stat()->trs_rpc_errno,
1244                        lstcon_trans_stat()->trs_fwk_errno);
1245
1246                 return rc;
1247         }
1248
1249         lstcon_rpc_trans_destroy(trans);
1250
1251         if (transop == LST_TRANS_TSBCLIADD)
1252                 return rc;
1253
1254         transop = LST_TRANS_TSBCLIADD;
1255         grp = test->tes_src_grp;
1256         test->tes_cliidx = 0;
1257
1258         /* requests to test clients */
1259         goto again;
1260 }
1261
1262 static int
1263 lstcon_verify_batch(const char *name, lstcon_batch_t **batch)
1264 {
1265         int rc;
1266
1267         rc = lstcon_batch_find(name, batch);
1268         if (rc != 0) {
1269                 CDEBUG(D_NET, "Can't find batch %s\n", name);
1270                 return rc;
1271         }
1272
1273         if ((*batch)->bat_state != LST_BATCH_IDLE) {
1274                 CDEBUG(D_NET, "Can't change running batch %s\n", name);
1275                 return -EINVAL;
1276         }
1277
1278         return 0;
1279 }
1280
1281 static int
1282 lstcon_verify_group(const char *name, lstcon_group_t **grp)
1283 {
1284         int                     rc;
1285         lstcon_ndlink_t         *ndl;
1286
1287         rc = lstcon_group_find(name, grp);
1288         if (rc != 0) {
1289                 CDEBUG(D_NET, "can't find group %s\n", name);
1290                 return rc;
1291         }
1292
1293         cfs_list_for_each_entry_typed(ndl, &(*grp)->grp_ndl_list,
1294                                       lstcon_ndlink_t, ndl_link) {
1295                 if (ndl->ndl_node->nd_state == LST_NODE_ACTIVE) {
1296                         return 0;
1297                 }
1298         }
1299
1300         CDEBUG(D_NET, "Group %s has no ACTIVE nodes\n", name);
1301
1302         return -EINVAL;
1303 }
1304
1305 int
1306 lstcon_test_add(char *batch_name, int type, int loop,
1307                 int concur, int dist, int span,
1308                 char *src_name, char *dst_name,
1309                 void *param, int paramlen, int *retp,
1310                 cfs_list_t *result_up)
1311 {
1312         lstcon_test_t    *test   = NULL;
1313         int              rc;
1314         lstcon_group_t   *src_grp = NULL;
1315         lstcon_group_t   *dst_grp = NULL;
1316         lstcon_batch_t   *batch = NULL;
1317
1318         /*
1319          * verify that a batch of the given name exists, and the groups
1320          * that will be part of the batch exist and have at least one
1321          * active node
1322          */
1323         rc = lstcon_verify_batch(batch_name, &batch);
1324         if (rc != 0)
1325                 goto out;
1326
1327         rc = lstcon_verify_group(src_name, &src_grp);
1328         if (rc != 0)
1329                 goto out;
1330
1331         rc = lstcon_verify_group(dst_name, &dst_grp);
1332         if (rc != 0)
1333                 goto out;
1334
1335         if (dst_grp->grp_userland)
1336                 *retp = 1;
1337
1338         LIBCFS_ALLOC(test, offsetof(lstcon_test_t, tes_param[paramlen]));
1339         if (!test) {
1340                 CERROR("Can't allocate test descriptor\n");
1341                 rc = -ENOMEM;
1342
1343                 goto out;
1344         }
1345
1346         memset(test, 0, offsetof(lstcon_test_t, tes_param[paramlen]));
1347         test->tes_hdr.tsb_id    = batch->bat_hdr.tsb_id;
1348         test->tes_batch         = batch;
1349         test->tes_type          = type;
1350         test->tes_oneside       = 0; /* TODO */
1351         test->tes_loop          = loop;
1352         test->tes_concur        = concur;
1353         test->tes_stop_onerr    = 1; /* TODO */
1354         test->tes_span          = span;
1355         test->tes_dist          = dist;
1356         test->tes_cliidx        = 0; /* just used for creating RPC */
1357         test->tes_src_grp       = src_grp;
1358         test->tes_dst_grp       = dst_grp;
1359         CFS_INIT_LIST_HEAD(&test->tes_trans_list);
1360
1361         if (param != NULL) {
1362                 test->tes_paramlen = paramlen;
1363                 memcpy(&test->tes_param[0], param, paramlen);
1364         }
1365
1366         rc = lstcon_test_nodes_add(test, result_up);
1367
1368         if (rc != 0)
1369                 goto out;
1370
1371         if (lstcon_trans_stat()->trs_rpc_errno != 0 ||
1372             lstcon_trans_stat()->trs_fwk_errno != 0)
1373                 CDEBUG(D_NET, "Failed to add test %d to batch %s\n", type,
1374                        batch_name);
1375
1376         /* add to test list anyway, so user can check what's going on */
1377         cfs_list_add_tail(&test->tes_link, &batch->bat_test_list);
1378
1379         batch->bat_ntest++;
1380         test->tes_hdr.tsb_index = batch->bat_ntest;
1381
1382         /*  hold groups so nobody can change them */
1383         return rc;
1384 out:
1385         if (test != NULL)
1386                 LIBCFS_FREE(test, offsetof(lstcon_test_t, tes_param[paramlen]));
1387
1388         if (dst_grp != NULL)
1389                 lstcon_group_put(dst_grp);
1390
1391         if (src_grp != NULL)
1392                 lstcon_group_put(src_grp);
1393
1394         return rc;
1395 }
1396
1397 int
1398 lstcon_test_find(lstcon_batch_t *batch, int idx, lstcon_test_t **testpp)
1399 {
1400         lstcon_test_t *test;
1401
1402         cfs_list_for_each_entry_typed(test, &batch->bat_test_list,
1403                                       lstcon_test_t, tes_link) {
1404                 if (idx == test->tes_hdr.tsb_index) {
1405                         *testpp = test;
1406                         return 0;
1407                 }
1408         }
1409
1410         return -ENOENT;
1411 }
1412
1413 int
1414 lstcon_tsbrpc_readent(int transop, srpc_msg_t *msg,
1415                       lstcon_rpc_ent_t *ent_up)
1416 {
1417         srpc_batch_reply_t *rep = &msg->msg_body.bat_reply;
1418
1419         LASSERT (transop == LST_TRANS_TSBCLIQRY ||
1420                  transop == LST_TRANS_TSBSRVQRY);
1421
1422         /* positive errno, framework error code */
1423         if (copy_to_user(&ent_up->rpe_priv[0],
1424                              &rep->bar_active, sizeof(rep->bar_active)))
1425                 return -EFAULT;
1426
1427         return 0;
1428 }
1429
1430 int
1431 lstcon_test_batch_query(char *name, int testidx, int client,
1432                         int timeout, cfs_list_t *result_up)
1433 {
1434         lstcon_rpc_trans_t *trans;
1435         cfs_list_t         *translist;
1436         cfs_list_t         *ndlist;
1437         lstcon_tsb_hdr_t   *hdr;
1438         lstcon_batch_t     *batch;
1439         lstcon_test_t      *test = NULL;
1440         int                 transop;
1441         int                 rc;
1442
1443         rc = lstcon_batch_find(name, &batch);
1444         if (rc != 0) {
1445                 CDEBUG(D_NET, "Can't find batch: %s\n", name);
1446                 return rc;
1447         }
1448
1449         if (testidx == 0) {
1450                 translist = &batch->bat_trans_list;
1451                 ndlist    = &batch->bat_cli_list;
1452                 hdr       = &batch->bat_hdr;
1453
1454         } else {
1455                 /* query specified test only */
1456                 rc = lstcon_test_find(batch, testidx, &test);
1457                 if (rc != 0) {
1458                         CDEBUG(D_NET, "Can't find test: %d\n", testidx);
1459                         return rc;
1460                 }
1461         
1462                 translist = &test->tes_trans_list;
1463                 ndlist    = &test->tes_src_grp->grp_ndl_list;
1464                 hdr       = &test->tes_hdr;
1465         } 
1466
1467         transop = client ? LST_TRANS_TSBCLIQRY : LST_TRANS_TSBSRVQRY;
1468
1469         rc = lstcon_rpc_trans_ndlist(ndlist, translist, transop, hdr,
1470                                      lstcon_batrpc_condition, &trans);
1471         if (rc != 0) {
1472                 CERROR("Can't create transaction: %d\n", rc);
1473                 return rc;
1474         }
1475
1476         lstcon_rpc_trans_postwait(trans, timeout);
1477
1478         if (testidx == 0 && /* query a batch, not a test */
1479             lstcon_rpc_stat_failure(lstcon_trans_stat(), 0) == 0 &&
1480             lstcon_tsbqry_stat_run(lstcon_trans_stat(), 0) == 0) {
1481                 /* all RPCs finished, and no active test */
1482                 batch->bat_state = LST_BATCH_IDLE;
1483         }
1484
1485         rc = lstcon_rpc_trans_interpreter(trans, result_up,
1486                                           lstcon_tsbrpc_readent);
1487         lstcon_rpc_trans_destroy(trans);
1488
1489         return rc;
1490 }
1491
1492 int
1493 lstcon_statrpc_readent(int transop, srpc_msg_t *msg,
1494                        lstcon_rpc_ent_t *ent_up)
1495 {
1496         srpc_stat_reply_t *rep = &msg->msg_body.stat_reply;
1497         sfw_counters_t    *sfwk_stat;
1498         srpc_counters_t   *srpc_stat;
1499         lnet_counters_t   *lnet_stat;
1500         
1501         if (rep->str_status != 0)
1502                 return 0;
1503
1504         sfwk_stat = (sfw_counters_t *)&ent_up->rpe_payload[0];
1505         srpc_stat = (srpc_counters_t *)((char *)sfwk_stat + sizeof(*sfwk_stat));
1506         lnet_stat = (lnet_counters_t *)((char *)srpc_stat + sizeof(*srpc_stat));
1507
1508         if (copy_to_user(sfwk_stat, &rep->str_fw, sizeof(*sfwk_stat)) ||
1509             copy_to_user(srpc_stat, &rep->str_rpc, sizeof(*srpc_stat)) ||
1510             copy_to_user(lnet_stat, &rep->str_lnet, sizeof(*lnet_stat)))
1511                 return -EFAULT;
1512
1513         return 0;
1514 }
1515
1516 int
1517 lstcon_ndlist_stat(cfs_list_t *ndlist,
1518                    int timeout, cfs_list_t *result_up)
1519 {
1520         cfs_list_t          head;
1521         lstcon_rpc_trans_t *trans;
1522         int                 rc;
1523
1524         CFS_INIT_LIST_HEAD(&head);
1525
1526         rc = lstcon_rpc_trans_ndlist(ndlist, &head,
1527                                      LST_TRANS_STATQRY, NULL, NULL, &trans);
1528         if (rc != 0) {
1529                 CERROR("Can't create transaction: %d\n", rc);
1530                 return rc;
1531         }
1532
1533         lstcon_rpc_trans_postwait(trans, LST_VALIDATE_TIMEOUT(timeout));
1534
1535         rc = lstcon_rpc_trans_interpreter(trans, result_up,
1536                                           lstcon_statrpc_readent);
1537         lstcon_rpc_trans_destroy(trans);
1538
1539         return rc;
1540 }
1541
1542 int
1543 lstcon_group_stat(char *grp_name, int timeout, cfs_list_t *result_up)
1544 {
1545         lstcon_group_t     *grp;
1546         int                 rc;
1547
1548         rc = lstcon_group_find(grp_name, &grp);
1549         if (rc != 0) {
1550                 CDEBUG(D_NET, "Can't find group %s\n", grp_name);
1551                 return rc;
1552         }
1553
1554         rc = lstcon_ndlist_stat(&grp->grp_ndl_list, timeout, result_up);
1555
1556         lstcon_group_put(grp);
1557
1558         return rc;
1559 }
1560
1561 int
1562 lstcon_nodes_stat(int count, lnet_process_id_t *ids_up,
1563                   int timeout, cfs_list_t *result_up)
1564 {
1565         lstcon_ndlink_t         *ndl;
1566         lstcon_group_t          *tmp;
1567         lnet_process_id_t        id;
1568         int                      i;
1569         int                      rc;
1570
1571         rc = lstcon_group_alloc(NULL, &tmp);
1572         if (rc != 0) {
1573                 CERROR("Out of memory\n");
1574                 return -ENOMEM;
1575         }
1576
1577         for (i = 0 ; i < count; i++) {
1578                 if (copy_from_user(&id, &ids_up[i], sizeof(id))) {
1579                         rc = -EFAULT;
1580                         break;
1581                 }
1582
1583                 /* add to tmp group */
1584                 rc = lstcon_group_ndlink_find(tmp, id, &ndl, 2);
1585                 if (rc != 0) {
1586                         CDEBUG((rc == -ENOMEM) ? D_ERROR : D_NET,
1587                                "Failed to find or create %s: %d\n",
1588                                libcfs_id2str(id), rc);
1589                         break;
1590                 }
1591         }
1592
1593         if (rc != 0) {
1594                 lstcon_group_put(tmp);
1595                 return rc;
1596         }
1597
1598         rc = lstcon_ndlist_stat(&tmp->grp_ndl_list, timeout, result_up);
1599
1600         lstcon_group_put(tmp);
1601
1602         return rc;
1603 }
1604
1605 int
1606 lstcon_debug_ndlist(cfs_list_t *ndlist,
1607                     cfs_list_t *translist,
1608                     int timeout, cfs_list_t *result_up)
1609 {
1610         lstcon_rpc_trans_t *trans;
1611         int                 rc;
1612
1613         rc = lstcon_rpc_trans_ndlist(ndlist, translist, LST_TRANS_SESQRY,
1614                                      NULL, lstcon_sesrpc_condition, &trans);
1615         if (rc != 0) {
1616                 CERROR("Can't create transaction: %d\n", rc);
1617                 return rc;
1618         }
1619
1620         lstcon_rpc_trans_postwait(trans, LST_VALIDATE_TIMEOUT(timeout));
1621
1622         rc = lstcon_rpc_trans_interpreter(trans, result_up,
1623                                           lstcon_sesrpc_readent);
1624         lstcon_rpc_trans_destroy(trans);
1625
1626         return rc;
1627 }
1628
1629 int
1630 lstcon_session_debug(int timeout, cfs_list_t *result_up)
1631 {
1632         return lstcon_debug_ndlist(&console_session.ses_ndl_list,
1633                                    NULL, timeout, result_up);
1634 }
1635
1636 int
1637 lstcon_batch_debug(int timeout, char *name,
1638                    int client, cfs_list_t *result_up)
1639 {
1640         lstcon_batch_t *bat;
1641         int             rc;
1642
1643         rc = lstcon_batch_find(name, &bat);
1644         if (rc != 0)
1645                 return -ENOENT;
1646
1647         rc = lstcon_debug_ndlist(client ? &bat->bat_cli_list :
1648                                           &bat->bat_srv_list,
1649                                  NULL, timeout, result_up);
1650
1651         return rc;
1652 }
1653
1654 int
1655 lstcon_group_debug(int timeout, char *name,
1656                    cfs_list_t *result_up)
1657 {
1658         lstcon_group_t *grp;
1659         int             rc;
1660
1661         rc = lstcon_group_find(name, &grp);
1662         if (rc != 0)
1663                 return -ENOENT;
1664
1665         rc = lstcon_debug_ndlist(&grp->grp_ndl_list, NULL,
1666                                  timeout, result_up);
1667         lstcon_group_put(grp);
1668
1669         return rc;
1670 }
1671
1672 int
1673 lstcon_nodes_debug(int timeout,
1674                    int count, lnet_process_id_t *ids_up, 
1675                    cfs_list_t *result_up)
1676 {
1677         lnet_process_id_t  id;
1678         lstcon_ndlink_t   *ndl;
1679         lstcon_group_t    *grp;
1680         int                i;
1681         int                rc;
1682
1683         rc = lstcon_group_alloc(NULL, &grp);
1684         if (rc != 0) {
1685                 CDEBUG(D_NET, "Out of memory\n");
1686                 return rc;
1687         }
1688
1689         for (i = 0; i < count; i++) {
1690                 if (copy_from_user(&id, &ids_up[i], sizeof(id))) {
1691                         rc = -EFAULT;
1692                         break;
1693                 }
1694
1695                 /* node is added to tmp group */
1696                 rc = lstcon_group_ndlink_find(grp, id, &ndl, 1);
1697                 if (rc != 0) {
1698                         CERROR("Can't create node link\n");
1699                         break;
1700                 }
1701         }
1702
1703         if (rc != 0) {
1704                 lstcon_group_put(grp);
1705                 return rc;
1706         }
1707
1708         rc = lstcon_debug_ndlist(&grp->grp_ndl_list, NULL,
1709                                  timeout, result_up);
1710
1711         lstcon_group_put(grp);
1712
1713         return rc;
1714 }
1715
1716 int
1717 lstcon_session_match(lst_sid_t sid)
1718 {
1719         return (console_session.ses_id.ses_nid   == sid.ses_nid &&
1720                 console_session.ses_id.ses_stamp == sid.ses_stamp) ?  1: 0;
1721 }
1722
1723 static void
1724 lstcon_new_session_id(lst_sid_t *sid)
1725 {
1726         lnet_process_id_t      id;
1727
1728         LASSERT (console_session.ses_state == LST_SESSION_NONE);
1729
1730         LNetGetId(1, &id);
1731         sid->ses_nid   = id.nid;
1732         sid->ses_stamp = cfs_time_current();
1733 }
1734
1735 extern srpc_service_t lstcon_acceptor_service;
1736
1737 int
1738 lstcon_session_new(char *name, int key, unsigned feats,
1739                    int timeout, int force, lst_sid_t *sid_up)
1740 {
1741         int     rc = 0;
1742         int     i;
1743
1744         if (console_session.ses_state != LST_SESSION_NONE) {
1745                 /* session exists */
1746                 if (!force) {
1747                         CNETERR("Session %s already exists\n",
1748                                 console_session.ses_name);
1749                         return -EEXIST;
1750                 }
1751
1752                 rc = lstcon_session_end();
1753
1754                 /* lstcon_session_end() only return local error */
1755                 if  (rc != 0)
1756                         return rc;
1757         }
1758
1759         if ((feats & ~LST_FEATS_MASK) != 0) {
1760                 CNETERR("Unknown session features %x\n",
1761                         (feats & ~LST_FEATS_MASK));
1762                 return -EINVAL;
1763         }
1764
1765         for (i = 0; i < LST_GLOBAL_HASHSIZE; i++)
1766                 LASSERT(cfs_list_empty(&console_session.ses_ndl_hash[i]));
1767
1768         lstcon_new_session_id(&console_session.ses_id);
1769
1770         console_session.ses_key     = key;
1771         console_session.ses_state   = LST_SESSION_ACTIVE;
1772         console_session.ses_force   = !!force;
1773         console_session.ses_features = feats;
1774         console_session.ses_feats_updated = 0;
1775         console_session.ses_timeout = (timeout <= 0) ?
1776                                       LST_CONSOLE_TIMEOUT : timeout;
1777
1778         if (strlen(name) > sizeof(console_session.ses_name)-1)
1779                 return -E2BIG;
1780         strncpy(console_session.ses_name, name,
1781                 sizeof(console_session.ses_name));
1782
1783         rc = lstcon_batch_add(LST_DEFAULT_BATCH);
1784         if (rc != 0)
1785                 return rc;
1786
1787         rc = lstcon_rpc_pinger_start();
1788         if (rc != 0) {
1789                 lstcon_batch_t *bat = NULL;
1790
1791                 lstcon_batch_find(LST_DEFAULT_BATCH, &bat);
1792                 lstcon_batch_destroy(bat);
1793
1794                 return rc;
1795         }
1796
1797         if (copy_to_user(sid_up, &console_session.ses_id,
1798                              sizeof(lst_sid_t)) == 0)
1799                 return rc;
1800
1801         lstcon_session_end();
1802
1803         return -EFAULT;
1804 }
1805
1806 int
1807 lstcon_session_info(lst_sid_t *sid_up, int *key_up, unsigned *featp,
1808                     lstcon_ndlist_ent_t *ndinfo_up, char *name_up, int len)
1809 {
1810         lstcon_ndlist_ent_t *entp;
1811         lstcon_ndlink_t     *ndl;
1812         int                  rc = 0;
1813
1814         if (console_session.ses_state != LST_SESSION_ACTIVE)
1815                 return -ESRCH;
1816
1817         LIBCFS_ALLOC(entp, sizeof(*entp));
1818         if (entp == NULL)
1819                 return -ENOMEM;
1820
1821         memset(entp, 0, sizeof(*entp));
1822
1823         cfs_list_for_each_entry_typed(ndl, &console_session.ses_ndl_list,
1824                                       lstcon_ndlink_t, ndl_link)
1825                 LST_NODE_STATE_COUNTER(ndl->ndl_node, entp);
1826
1827         if (copy_to_user(sid_up, &console_session.ses_id,
1828                              sizeof(lst_sid_t)) ||
1829             copy_to_user(key_up, &console_session.ses_key,
1830                              sizeof(*key_up)) ||
1831             copy_to_user(featp, &console_session.ses_features,
1832                              sizeof(*featp)) ||
1833             copy_to_user(ndinfo_up, entp, sizeof(*entp)) ||
1834             copy_to_user(name_up, console_session.ses_name, len))
1835                 rc = -EFAULT;
1836
1837         LIBCFS_FREE(entp, sizeof(*entp));
1838
1839         return rc;
1840 }
1841
1842 int
1843 lstcon_session_end()
1844 {
1845         lstcon_rpc_trans_t *trans;
1846         lstcon_group_t     *grp;
1847         lstcon_batch_t     *bat;
1848         int                 rc = 0;
1849
1850         LASSERT (console_session.ses_state == LST_SESSION_ACTIVE);
1851
1852         rc = lstcon_rpc_trans_ndlist(&console_session.ses_ndl_list, 
1853                                      NULL, LST_TRANS_SESEND, NULL,
1854                                      lstcon_sesrpc_condition, &trans);
1855         if (rc != 0) {
1856                 CERROR("Can't create transaction: %d\n", rc);
1857                 return rc;
1858         }
1859
1860         console_session.ses_shutdown = 1;
1861
1862         lstcon_rpc_pinger_stop();
1863
1864         lstcon_rpc_trans_postwait(trans, LST_TRANS_TIMEOUT);
1865
1866         lstcon_rpc_trans_destroy(trans);
1867         /* User can do nothing even rpc failed, so go on */
1868
1869         /* waiting for orphan rpcs to die */
1870         lstcon_rpc_cleanup_wait();
1871
1872         console_session.ses_id    = LST_INVALID_SID;
1873         console_session.ses_state = LST_SESSION_NONE;
1874         console_session.ses_key   = 0;
1875         console_session.ses_force = 0;
1876         console_session.ses_feats_updated = 0;
1877
1878         /* destroy all batches */
1879         while (!cfs_list_empty(&console_session.ses_bat_list)) {
1880                 bat = cfs_list_entry(console_session.ses_bat_list.next,
1881                                      lstcon_batch_t, bat_link);
1882
1883                 lstcon_batch_destroy(bat);
1884         }
1885
1886         /* destroy all groups */
1887         while (!cfs_list_empty(&console_session.ses_grp_list)) {
1888                 grp = cfs_list_entry(console_session.ses_grp_list.next,
1889                                      lstcon_group_t, grp_link);
1890                 LASSERT (grp->grp_ref == 1);
1891
1892                 lstcon_group_put(grp);
1893         }
1894
1895         /* all nodes should be released */
1896         LASSERT (cfs_list_empty(&console_session.ses_ndl_list));
1897
1898         console_session.ses_shutdown = 0;
1899         console_session.ses_expired  = 0;
1900
1901         return rc;
1902 }
1903
1904 int
1905 lstcon_session_feats_check(unsigned feats)
1906 {
1907         int rc = 0;
1908
1909         if ((feats & ~LST_FEATS_MASK) != 0) {
1910                 CERROR("Can't support these features: %x\n",
1911                        (feats & ~LST_FEATS_MASK));
1912                 return -EPROTO;
1913         }
1914
1915         spin_lock(&console_session.ses_rpc_lock);
1916
1917         if (!console_session.ses_feats_updated) {
1918                 console_session.ses_feats_updated = 1;
1919                 console_session.ses_features = feats;
1920         }
1921
1922         if (console_session.ses_features != feats)
1923                 rc = -EPROTO;
1924
1925         spin_unlock(&console_session.ses_rpc_lock);
1926
1927         if (rc != 0) {
1928                 CERROR("remote features %x do not match with "
1929                        "session features %x of console\n",
1930                        feats, console_session.ses_features);
1931         }
1932
1933         return rc;
1934 }
1935
1936 static int
1937 lstcon_acceptor_handle (srpc_server_rpc_t *rpc)
1938 {
1939         srpc_msg_t        *rep  = &rpc->srpc_replymsg;
1940         srpc_msg_t        *req  = &rpc->srpc_reqstbuf->buf_msg;
1941         srpc_join_reqst_t *jreq = &req->msg_body.join_reqst;
1942         srpc_join_reply_t *jrep = &rep->msg_body.join_reply;
1943         lstcon_group_t    *grp  = NULL;
1944         lstcon_ndlink_t   *ndl;
1945         int                rc   = 0;
1946
1947         sfw_unpack_message(req);
1948
1949         mutex_lock(&console_session.ses_mutex);
1950
1951         jrep->join_sid = console_session.ses_id;
1952
1953         if (console_session.ses_id.ses_nid == LNET_NID_ANY) {
1954                 jrep->join_status = ESRCH;
1955                 goto out;
1956         }
1957
1958         if (lstcon_session_feats_check(req->msg_ses_feats) != 0) {
1959                 jrep->join_status = EPROTO;
1960                 goto out;
1961         }
1962
1963         if (jreq->join_sid.ses_nid != LNET_NID_ANY &&
1964              !lstcon_session_match(jreq->join_sid)) {
1965                 jrep->join_status = EBUSY;
1966                 goto out;
1967         }
1968
1969         if (lstcon_group_find(jreq->join_group, &grp) != 0) {
1970                 rc = lstcon_group_alloc(jreq->join_group, &grp);
1971                 if (rc != 0) {
1972                         CERROR("Out of memory\n");
1973                         goto out;
1974                 }
1975
1976                 cfs_list_add_tail(&grp->grp_link,
1977                                   &console_session.ses_grp_list);
1978                 lstcon_group_addref(grp);
1979         }
1980
1981         if (grp->grp_ref > 2) {
1982                 /* Group in using */
1983                 jrep->join_status = EBUSY;
1984                 goto out;
1985         }
1986
1987         rc = lstcon_group_ndlink_find(grp, rpc->srpc_peer, &ndl, 0);
1988         if (rc == 0) {
1989                 jrep->join_status = EEXIST;
1990                 goto out;
1991         }
1992
1993         rc = lstcon_group_ndlink_find(grp, rpc->srpc_peer, &ndl, 1);
1994         if (rc != 0) {
1995                 CERROR("Out of memory\n");
1996                 goto out;
1997         }
1998
1999         ndl->ndl_node->nd_state   = LST_NODE_ACTIVE;
2000         ndl->ndl_node->nd_timeout = console_session.ses_timeout;
2001
2002         if (grp->grp_userland == 0)
2003                 grp->grp_userland = 1;
2004
2005         strcpy(jrep->join_session, console_session.ses_name);
2006         jrep->join_timeout = console_session.ses_timeout;
2007         jrep->join_status  = 0;
2008
2009 out:
2010         rep->msg_ses_feats = console_session.ses_features;
2011         if (grp != NULL)
2012                 lstcon_group_put(grp);
2013
2014         mutex_unlock(&console_session.ses_mutex);
2015
2016         return rc;
2017 }
2018
2019 srpc_service_t lstcon_acceptor_service;
2020 void lstcon_init_acceptor_service(void)
2021 {
2022         /* initialize selftest console acceptor service table */
2023         lstcon_acceptor_service.sv_name    = "join session";
2024         lstcon_acceptor_service.sv_handler = lstcon_acceptor_handle;
2025         lstcon_acceptor_service.sv_id      = SRPC_SERVICE_JOIN;
2026         lstcon_acceptor_service.sv_wi_total = SFW_FRWK_WI_MAX;
2027 }
2028
2029 extern int lstcon_ioctl_entry(unsigned int cmd, struct libcfs_ioctl_data *data);
2030
2031 DECLARE_IOCTL_HANDLER(lstcon_ioctl_handler, lstcon_ioctl_entry);
2032
2033 /* initialize console */
2034 int
2035 lstcon_console_init(void)
2036 {
2037         int     i;
2038         int     rc;
2039
2040         memset(&console_session, 0, sizeof(lstcon_session_t));
2041
2042         console_session.ses_id              = LST_INVALID_SID;
2043         console_session.ses_state           = LST_SESSION_NONE;
2044         console_session.ses_timeout         = 0;
2045         console_session.ses_force           = 0;
2046         console_session.ses_expired         = 0;
2047         console_session.ses_feats_updated   = 0;
2048         console_session.ses_features        = LST_FEATS_MASK;
2049         console_session.ses_laststamp       = cfs_time_current_sec();
2050
2051         mutex_init(&console_session.ses_mutex);
2052
2053         CFS_INIT_LIST_HEAD(&console_session.ses_ndl_list);
2054         CFS_INIT_LIST_HEAD(&console_session.ses_grp_list);
2055         CFS_INIT_LIST_HEAD(&console_session.ses_bat_list);
2056         CFS_INIT_LIST_HEAD(&console_session.ses_trans_list);
2057
2058         LIBCFS_ALLOC(console_session.ses_ndl_hash,
2059                      sizeof(cfs_list_t) * LST_GLOBAL_HASHSIZE);
2060         if (console_session.ses_ndl_hash == NULL)
2061                 return -ENOMEM;
2062
2063         for (i = 0; i < LST_GLOBAL_HASHSIZE; i++)
2064                 CFS_INIT_LIST_HEAD(&console_session.ses_ndl_hash[i]);
2065
2066
2067         /* initialize acceptor service table */
2068         lstcon_init_acceptor_service();
2069
2070         rc = srpc_add_service(&lstcon_acceptor_service);
2071         LASSERT (rc != -EBUSY);
2072         if (rc != 0) {
2073                 LIBCFS_FREE(console_session.ses_ndl_hash,
2074                             sizeof(cfs_list_t) * LST_GLOBAL_HASHSIZE);
2075                 return rc;
2076         }
2077
2078         rc = srpc_service_add_buffers(&lstcon_acceptor_service,
2079                                       lstcon_acceptor_service.sv_wi_total);
2080         if (rc != 0) {
2081                 rc = -ENOMEM;
2082                 goto out;
2083         }
2084
2085         rc = libcfs_register_ioctl(&lstcon_ioctl_handler);
2086
2087         if (rc == 0) {
2088                 lstcon_rpc_module_init();
2089                 return 0;
2090         }
2091
2092 out:
2093         srpc_shutdown_service(&lstcon_acceptor_service);
2094         srpc_remove_service(&lstcon_acceptor_service);
2095
2096         LIBCFS_FREE(console_session.ses_ndl_hash,
2097                     sizeof(cfs_list_t) * LST_GLOBAL_HASHSIZE);
2098
2099         srpc_wait_service_shutdown(&lstcon_acceptor_service);
2100
2101         return rc;
2102 }
2103
2104 int
2105 lstcon_console_fini(void)
2106 {
2107         int     i;
2108
2109         libcfs_deregister_ioctl(&lstcon_ioctl_handler);
2110
2111         mutex_lock(&console_session.ses_mutex);
2112
2113         srpc_shutdown_service(&lstcon_acceptor_service);
2114         srpc_remove_service(&lstcon_acceptor_service);
2115
2116         if (console_session.ses_state != LST_SESSION_NONE)
2117                 lstcon_session_end();
2118
2119         lstcon_rpc_module_fini();
2120
2121         mutex_unlock(&console_session.ses_mutex);
2122
2123         LASSERT (cfs_list_empty(&console_session.ses_ndl_list));
2124         LASSERT (cfs_list_empty(&console_session.ses_grp_list));
2125         LASSERT (cfs_list_empty(&console_session.ses_bat_list));
2126         LASSERT (cfs_list_empty(&console_session.ses_trans_list));
2127
2128         for (i = 0; i < LST_NODE_HASHSIZE; i++) {
2129                 LASSERT (cfs_list_empty(&console_session.ses_ndl_hash[i]));
2130         }
2131
2132         LIBCFS_FREE(console_session.ses_ndl_hash,
2133                     sizeof(cfs_list_t) * LST_GLOBAL_HASHSIZE);
2134
2135         srpc_wait_service_shutdown(&lstcon_acceptor_service);
2136
2137         return 0;
2138 }
2139
2140 #endif