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