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