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