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