linux/drivers/staging/lustre/lustre/obdclass/obd_config.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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2015, 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 * lustre/obdclass/obd_config.c
  37 *
  38 * Config API
  39 */
  40
  41#define DEBUG_SUBSYSTEM S_CLASS
  42#include "../include/obd_class.h"
  43#include <linux/string.h>
  44#include "../include/lustre_log.h"
  45#include "../include/lprocfs_status.h"
  46#include "../include/lustre_param.h"
  47
  48#include "llog_internal.h"
  49
  50static struct cfs_hash_ops uuid_hash_ops;
  51
  52/*********** string parsing utils *********/
  53
  54/* returns 0 if we find this key in the buffer, else 1 */
  55int class_find_param(char *buf, char *key, char **valp)
  56{
  57        char *ptr;
  58
  59        if (!buf)
  60                return 1;
  61
  62        ptr = strstr(buf, key);
  63        if (!ptr)
  64                return 1;
  65
  66        if (valp)
  67                *valp = ptr + strlen(key);
  68
  69        return 0;
  70}
  71EXPORT_SYMBOL(class_find_param);
  72
  73/* returns 0 if this is the first key in the buffer, else 1.
  74 * valp points to first char after key.
  75 */
  76static int class_match_param(char *buf, const char *key, char **valp)
  77{
  78        if (!buf)
  79                return 1;
  80
  81        if (memcmp(buf, key, strlen(key)) != 0)
  82                return 1;
  83
  84        if (valp)
  85                *valp = buf + strlen(key);
  86
  87        return 0;
  88}
  89
  90static int parse_nid(char *buf, void *value, int quiet)
  91{
  92        lnet_nid_t *nid = value;
  93
  94        *nid = libcfs_str2nid(buf);
  95        if (*nid != LNET_NID_ANY)
  96                return 0;
  97
  98        if (!quiet)
  99                LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
 100        return -EINVAL;
 101}
 102
 103static int parse_net(char *buf, void *value)
 104{
 105        __u32 *net = value;
 106
 107        *net = libcfs_str2net(buf);
 108        CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
 109        return 0;
 110}
 111
 112enum {
 113        CLASS_PARSE_NID = 1,
 114        CLASS_PARSE_NET,
 115};
 116
 117/* 0 is good nid,
 118 * 1 not found
 119 * < 0 error
 120 * endh is set to next separator
 121 */
 122static int class_parse_value(char *buf, int opc, void *value, char **endh,
 123                             int quiet)
 124{
 125        char *endp;
 126        char  tmp;
 127        int   rc = 0;
 128
 129        if (!buf)
 130                return 1;
 131        while (*buf == ',' || *buf == ':')
 132                buf++;
 133        if (*buf == ' ' || *buf == '/' || *buf == '\0')
 134                return 1;
 135
 136        /* nid separators or end of nids */
 137        endp = strpbrk(buf, ",: /");
 138        if (!endp)
 139                endp = buf + strlen(buf);
 140
 141        tmp = *endp;
 142        *endp = '\0';
 143        switch (opc) {
 144        default:
 145                LBUG();
 146        case CLASS_PARSE_NID:
 147                rc = parse_nid(buf, value, quiet);
 148                break;
 149        case CLASS_PARSE_NET:
 150                rc = parse_net(buf, value);
 151                break;
 152        }
 153        *endp = tmp;
 154        if (rc != 0)
 155                return rc;
 156        if (endh)
 157                *endh = endp;
 158        return 0;
 159}
 160
 161int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
 162{
 163        return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
 164}
 165EXPORT_SYMBOL(class_parse_nid);
 166
 167int class_parse_nid_quiet(char *buf, lnet_nid_t *nid, char **endh)
 168{
 169        return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
 170}
 171EXPORT_SYMBOL(class_parse_nid_quiet);
 172
 173/********************** class fns **********************/
 174
 175/**
 176 * Create a new obd device and set the type, name and uuid.  If successful,
 177 * the new device can be accessed by either name or uuid.
 178 */
 179static int class_attach(struct lustre_cfg *lcfg)
 180{
 181        struct obd_device *obd = NULL;
 182        char *typename, *name, *uuid;
 183        int rc, len;
 184
 185        if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
 186                CERROR("No type passed!\n");
 187                return -EINVAL;
 188        }
 189        typename = lustre_cfg_string(lcfg, 1);
 190
 191        if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
 192                CERROR("No name passed!\n");
 193                return -EINVAL;
 194        }
 195        name = lustre_cfg_string(lcfg, 0);
 196
 197        if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
 198                CERROR("No UUID passed!\n");
 199                return -EINVAL;
 200        }
 201        uuid = lustre_cfg_string(lcfg, 2);
 202
 203        CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
 204               MKSTR(typename), MKSTR(name), MKSTR(uuid));
 205
 206        obd = class_newdev(typename, name);
 207        if (IS_ERR(obd)) {
 208                /* Already exists or out of obds */
 209                rc = PTR_ERR(obd);
 210                obd = NULL;
 211                CERROR("Cannot create device %s of type %s : %d\n",
 212                       name, typename, rc);
 213                goto out;
 214        }
 215        LASSERTF(obd, "Cannot get obd device %s of type %s\n",
 216                 name, typename);
 217        LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
 218                 "obd %p obd_magic %08X != %08X\n",
 219                 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
 220        LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
 221                 "%p obd_name %s != %s\n", obd, obd->obd_name, name);
 222
 223        rwlock_init(&obd->obd_pool_lock);
 224        obd->obd_pool_limit = 0;
 225        obd->obd_pool_slv = 0;
 226
 227        INIT_LIST_HEAD(&obd->obd_exports);
 228        INIT_LIST_HEAD(&obd->obd_unlinked_exports);
 229        INIT_LIST_HEAD(&obd->obd_delayed_exports);
 230        spin_lock_init(&obd->obd_nid_lock);
 231        spin_lock_init(&obd->obd_dev_lock);
 232        mutex_init(&obd->obd_dev_mutex);
 233        spin_lock_init(&obd->obd_osfs_lock);
 234        /* obd->obd_osfs_age must be set to a value in the distant
 235         * past to guarantee a fresh statfs is fetched on mount.
 236         */
 237        obd->obd_osfs_age = cfs_time_shift_64(-1000);
 238
 239        /* XXX belongs in setup not attach  */
 240        init_rwsem(&obd->obd_observer_link_sem);
 241        /* recovery data */
 242        init_waitqueue_head(&obd->obd_evict_inprogress_waitq);
 243
 244        llog_group_init(&obd->obd_olg, FID_SEQ_LLOG);
 245
 246        obd->obd_conn_inprogress = 0;
 247
 248        len = strlen(uuid);
 249        if (len >= sizeof(obd->obd_uuid)) {
 250                CERROR("uuid must be < %d bytes long\n",
 251                       (int)sizeof(obd->obd_uuid));
 252                rc = -EINVAL;
 253                goto out;
 254        }
 255        memcpy(obd->obd_uuid.uuid, uuid, len);
 256
 257        /* do the attach */
 258        if (OBP(obd, attach)) {
 259                rc = OBP(obd, attach)(obd, sizeof(*lcfg), lcfg);
 260                if (rc) {
 261                        rc = -EINVAL;
 262                        goto out;
 263                }
 264        }
 265
 266        /* Detach drops this */
 267        spin_lock(&obd->obd_dev_lock);
 268        atomic_set(&obd->obd_refcount, 1);
 269        spin_unlock(&obd->obd_dev_lock);
 270        lu_ref_init(&obd->obd_reference);
 271        lu_ref_add(&obd->obd_reference, "attach", obd);
 272
 273        obd->obd_attached = 1;
 274        CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
 275               obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
 276        return 0;
 277 out:
 278        if (obd)
 279                class_release_dev(obd);
 280
 281        return rc;
 282}
 283
 284/** Create hashes, self-export, and call type-specific setup.
 285 * Setup is effectively the "start this obd" call.
 286 */
 287static int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
 288{
 289        int err = 0;
 290        struct obd_export *exp;
 291
 292        LASSERT(obd);
 293        LASSERTF(obd == class_num2obd(obd->obd_minor),
 294                 "obd %p != obd_devs[%d] %p\n",
 295                 obd, obd->obd_minor, class_num2obd(obd->obd_minor));
 296        LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
 297                 "obd %p obd_magic %08x != %08x\n",
 298                 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
 299
 300        /* have we attached a type to this device? */
 301        if (!obd->obd_attached) {
 302                CERROR("Device %d not attached\n", obd->obd_minor);
 303                return -ENODEV;
 304        }
 305
 306        if (obd->obd_set_up) {
 307                CERROR("Device %d already setup (type %s)\n",
 308                       obd->obd_minor, obd->obd_type->typ_name);
 309                return -EEXIST;
 310        }
 311
 312        /* is someone else setting us up right now? (attach inits spinlock) */
 313        spin_lock(&obd->obd_dev_lock);
 314        if (obd->obd_starting) {
 315                spin_unlock(&obd->obd_dev_lock);
 316                CERROR("Device %d setup in progress (type %s)\n",
 317                       obd->obd_minor, obd->obd_type->typ_name);
 318                return -EEXIST;
 319        }
 320        /* just leave this on forever.  I can't use obd_set_up here because
 321         * other fns check that status, and we're not actually set up yet.
 322         */
 323        obd->obd_starting = 1;
 324        obd->obd_uuid_hash = NULL;
 325        spin_unlock(&obd->obd_dev_lock);
 326
 327        /* create an uuid-export lustre hash */
 328        obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
 329                                             HASH_UUID_CUR_BITS,
 330                                             HASH_UUID_MAX_BITS,
 331                                             HASH_UUID_BKT_BITS, 0,
 332                                             CFS_HASH_MIN_THETA,
 333                                             CFS_HASH_MAX_THETA,
 334                                             &uuid_hash_ops, CFS_HASH_DEFAULT);
 335        if (!obd->obd_uuid_hash) {
 336                err = -ENOMEM;
 337                goto err_hash;
 338        }
 339
 340        exp = class_new_export(obd, &obd->obd_uuid);
 341        if (IS_ERR(exp)) {
 342                err = PTR_ERR(exp);
 343                goto err_hash;
 344        }
 345
 346        obd->obd_self_export = exp;
 347        class_export_put(exp);
 348
 349        err = obd_setup(obd, lcfg);
 350        if (err)
 351                goto err_exp;
 352
 353        obd->obd_set_up = 1;
 354
 355        spin_lock(&obd->obd_dev_lock);
 356        /* cleanup drops this */
 357        class_incref(obd, "setup", obd);
 358        spin_unlock(&obd->obd_dev_lock);
 359
 360        CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
 361               obd->obd_name, obd->obd_uuid.uuid);
 362
 363        return 0;
 364err_exp:
 365        if (obd->obd_self_export) {
 366                class_unlink_export(obd->obd_self_export);
 367                obd->obd_self_export = NULL;
 368        }
 369err_hash:
 370        if (obd->obd_uuid_hash) {
 371                cfs_hash_putref(obd->obd_uuid_hash);
 372                obd->obd_uuid_hash = NULL;
 373        }
 374        obd->obd_starting = 0;
 375        CERROR("setup %s failed (%d)\n", obd->obd_name, err);
 376        return err;
 377}
 378
 379/** We have finished using this obd and are ready to destroy it.
 380 * There can be no more references to this obd.
 381 */
 382static int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
 383{
 384        if (obd->obd_set_up) {
 385                CERROR("OBD device %d still set up\n", obd->obd_minor);
 386                return -EBUSY;
 387        }
 388
 389        spin_lock(&obd->obd_dev_lock);
 390        if (!obd->obd_attached) {
 391                spin_unlock(&obd->obd_dev_lock);
 392                CERROR("OBD device %d not attached\n", obd->obd_minor);
 393                return -ENODEV;
 394        }
 395        obd->obd_attached = 0;
 396        spin_unlock(&obd->obd_dev_lock);
 397
 398        CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
 399               obd->obd_name, obd->obd_uuid.uuid);
 400
 401        class_decref(obd, "attach", obd);
 402        return 0;
 403}
 404
 405/** Start shutting down the obd.  There may be in-progress ops when
 406 * this is called.  We tell them to start shutting down with a call
 407 * to class_disconnect_exports().
 408 */
 409static int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
 410{
 411        int err = 0;
 412        char *flag;
 413
 414        OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
 415
 416        if (!obd->obd_set_up) {
 417                CERROR("Device %d not setup\n", obd->obd_minor);
 418                return -ENODEV;
 419        }
 420
 421        spin_lock(&obd->obd_dev_lock);
 422        if (obd->obd_stopping) {
 423                spin_unlock(&obd->obd_dev_lock);
 424                CERROR("OBD %d already stopping\n", obd->obd_minor);
 425                return -ENODEV;
 426        }
 427        /* Leave this on forever */
 428        obd->obd_stopping = 1;
 429
 430        /* wait for already-arrived-connections to finish. */
 431        while (obd->obd_conn_inprogress > 0) {
 432                spin_unlock(&obd->obd_dev_lock);
 433
 434                cond_resched();
 435
 436                spin_lock(&obd->obd_dev_lock);
 437        }
 438        spin_unlock(&obd->obd_dev_lock);
 439
 440        if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
 441                for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
 442                        switch (*flag) {
 443                        case 'F':
 444                                obd->obd_force = 1;
 445                                break;
 446                        case 'A':
 447                                LCONSOLE_WARN("Failing over %s\n",
 448                                              obd->obd_name);
 449                                obd->obd_fail = 1;
 450                                obd->obd_no_transno = 1;
 451                                obd->obd_no_recov = 1;
 452                                if (OBP(obd, iocontrol)) {
 453                                        obd_iocontrol(OBD_IOC_SYNC,
 454                                                      obd->obd_self_export,
 455                                                      0, NULL, NULL);
 456                                }
 457                                break;
 458                        default:
 459                                CERROR("Unrecognised flag '%c'\n", *flag);
 460                        }
 461        }
 462
 463        LASSERT(obd->obd_self_export);
 464
 465        /* Precleanup, we must make sure all exports get destroyed. */
 466        err = obd_precleanup(obd, OBD_CLEANUP_EXPORTS);
 467        if (err)
 468                CERROR("Precleanup %s returned %d\n",
 469                       obd->obd_name, err);
 470
 471        /* destroy an uuid-export hash body */
 472        if (obd->obd_uuid_hash) {
 473                cfs_hash_putref(obd->obd_uuid_hash);
 474                obd->obd_uuid_hash = NULL;
 475        }
 476
 477        class_decref(obd, "setup", obd);
 478        obd->obd_set_up = 0;
 479
 480        return 0;
 481}
 482
 483struct obd_device *class_incref(struct obd_device *obd,
 484                                const char *scope, const void *source)
 485{
 486        lu_ref_add_atomic(&obd->obd_reference, scope, source);
 487        atomic_inc(&obd->obd_refcount);
 488        CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
 489               atomic_read(&obd->obd_refcount));
 490
 491        return obd;
 492}
 493EXPORT_SYMBOL(class_incref);
 494
 495void class_decref(struct obd_device *obd, const char *scope, const void *source)
 496{
 497        int err;
 498        int refs;
 499
 500        spin_lock(&obd->obd_dev_lock);
 501        atomic_dec(&obd->obd_refcount);
 502        refs = atomic_read(&obd->obd_refcount);
 503        spin_unlock(&obd->obd_dev_lock);
 504        lu_ref_del(&obd->obd_reference, scope, source);
 505
 506        CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
 507
 508        if ((refs == 1) && obd->obd_stopping) {
 509                /* All exports have been destroyed; there should
 510                 * be no more in-progress ops by this point.
 511                 */
 512
 513                spin_lock(&obd->obd_self_export->exp_lock);
 514                obd->obd_self_export->exp_flags |= exp_flags_from_obd(obd);
 515                spin_unlock(&obd->obd_self_export->exp_lock);
 516
 517                /* note that we'll recurse into class_decref again */
 518                class_unlink_export(obd->obd_self_export);
 519                return;
 520        }
 521
 522        if (refs == 0) {
 523                CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
 524                       obd->obd_name, obd->obd_uuid.uuid);
 525                LASSERT(!obd->obd_attached);
 526                if (obd->obd_stopping) {
 527                        /* If we're not stopping, we were never set up */
 528                        err = obd_cleanup(obd);
 529                        if (err)
 530                                CERROR("Cleanup %s returned %d\n",
 531                                       obd->obd_name, err);
 532                }
 533                if (OBP(obd, detach)) {
 534                        err = OBP(obd, detach)(obd);
 535                        if (err)
 536                                CERROR("Detach returned %d\n", err);
 537                }
 538                class_release_dev(obd);
 539        }
 540}
 541EXPORT_SYMBOL(class_decref);
 542
 543/** Add a failover nid location.
 544 * Client obd types contact server obd types using this nid list.
 545 */
 546static int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
 547{
 548        struct obd_import *imp;
 549        struct obd_uuid uuid;
 550        int rc;
 551
 552        if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
 553            LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
 554                CERROR("invalid conn_uuid\n");
 555                return -EINVAL;
 556        }
 557        if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
 558            strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
 559            strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
 560            strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
 561            strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
 562                CERROR("can't add connection on non-client dev\n");
 563                return -EINVAL;
 564        }
 565
 566        imp = obd->u.cli.cl_import;
 567        if (!imp) {
 568                CERROR("try to add conn on immature client dev\n");
 569                return -EINVAL;
 570        }
 571
 572        obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
 573        rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
 574
 575        return rc;
 576}
 577
 578/** Remove a failover nid location.
 579 */
 580static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
 581{
 582        struct obd_import *imp;
 583        struct obd_uuid uuid;
 584        int rc;
 585
 586        if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
 587            LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
 588                CERROR("invalid conn_uuid\n");
 589                return -EINVAL;
 590        }
 591        if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
 592            strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
 593                CERROR("can't del connection on non-client dev\n");
 594                return -EINVAL;
 595        }
 596
 597        imp = obd->u.cli.cl_import;
 598        if (!imp) {
 599                CERROR("try to del conn on immature client dev\n");
 600                return -EINVAL;
 601        }
 602
 603        obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
 604        rc = obd_del_conn(imp, &uuid);
 605
 606        return rc;
 607}
 608
 609LIST_HEAD(lustre_profile_list);
 610
 611struct lustre_profile *class_get_profile(const char *prof)
 612{
 613        struct lustre_profile *lprof;
 614
 615        list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
 616                if (!strcmp(lprof->lp_profile, prof)) {
 617                        return lprof;
 618                }
 619        }
 620        return NULL;
 621}
 622EXPORT_SYMBOL(class_get_profile);
 623
 624/** Create a named "profile".
 625 * This defines the mdc and osc names to use for a client.
 626 * This also is used to define the lov to be used by a mdt.
 627 */
 628static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
 629                             int mdclen, char *mdc)
 630{
 631        struct lustre_profile *lprof;
 632        int err = 0;
 633
 634        CDEBUG(D_CONFIG, "Add profile %s\n", prof);
 635
 636        lprof = kzalloc(sizeof(*lprof), GFP_NOFS);
 637        if (!lprof)
 638                return -ENOMEM;
 639        INIT_LIST_HEAD(&lprof->lp_list);
 640
 641        LASSERT(proflen == (strlen(prof) + 1));
 642        lprof->lp_profile = kmemdup(prof, proflen, GFP_NOFS);
 643        if (!lprof->lp_profile) {
 644                err = -ENOMEM;
 645                goto free_lprof;
 646        }
 647
 648        LASSERT(osclen == (strlen(osc) + 1));
 649        lprof->lp_dt = kmemdup(osc, osclen, GFP_NOFS);
 650        if (!lprof->lp_dt) {
 651                err = -ENOMEM;
 652                goto free_lp_profile;
 653        }
 654
 655        if (mdclen > 0) {
 656                LASSERT(mdclen == (strlen(mdc) + 1));
 657                lprof->lp_md = kmemdup(mdc, mdclen, GFP_NOFS);
 658                if (!lprof->lp_md) {
 659                        err = -ENOMEM;
 660                        goto free_lp_dt;
 661                }
 662        }
 663
 664        list_add(&lprof->lp_list, &lustre_profile_list);
 665        return err;
 666
 667free_lp_dt:
 668        kfree(lprof->lp_dt);
 669free_lp_profile:
 670        kfree(lprof->lp_profile);
 671free_lprof:
 672        kfree(lprof);
 673        return err;
 674}
 675
 676void class_del_profile(const char *prof)
 677{
 678        struct lustre_profile *lprof;
 679
 680        CDEBUG(D_CONFIG, "Del profile %s\n", prof);
 681
 682        lprof = class_get_profile(prof);
 683        if (lprof) {
 684                list_del(&lprof->lp_list);
 685                kfree(lprof->lp_profile);
 686                kfree(lprof->lp_dt);
 687                kfree(lprof->lp_md);
 688                kfree(lprof);
 689        }
 690}
 691EXPORT_SYMBOL(class_del_profile);
 692
 693/* COMPAT_146 */
 694void class_del_profiles(void)
 695{
 696        struct lustre_profile *lprof, *n;
 697
 698        list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
 699                list_del(&lprof->lp_list);
 700                kfree(lprof->lp_profile);
 701                kfree(lprof->lp_dt);
 702                kfree(lprof->lp_md);
 703                kfree(lprof);
 704        }
 705}
 706EXPORT_SYMBOL(class_del_profiles);
 707
 708static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
 709{
 710        if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
 711                at_min = val;
 712        else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
 713                at_max = val;
 714        else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
 715                at_extra = val;
 716        else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
 717                at_early_margin = val;
 718        else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
 719                at_history = val;
 720        else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
 721                strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
 722                        JOBSTATS_JOBID_VAR_MAX_LEN + 1);
 723        else
 724                return -EINVAL;
 725
 726        CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
 727        return 0;
 728}
 729
 730/* We can't call ll_process_config or lquota_process_config directly because
 731 * it lives in a module that must be loaded after this one.
 732 */
 733static int (*client_process_config)(struct lustre_cfg *lcfg);
 734static int (*quota_process_config)(struct lustre_cfg *lcfg);
 735
 736void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
 737{
 738        client_process_config = cpc;
 739}
 740EXPORT_SYMBOL(lustre_register_client_process_config);
 741
 742static int process_param2_config(struct lustre_cfg *lcfg)
 743{
 744        char *param = lustre_cfg_string(lcfg, 1);
 745        char *upcall = lustre_cfg_string(lcfg, 2);
 746        char *argv[] = {
 747                [0] = "/usr/sbin/lctl",
 748                [1] = "set_param",
 749                [2] = param,
 750                [3] = NULL
 751        };
 752        ktime_t start;
 753        ktime_t end;
 754        int             rc;
 755
 756        /* Add upcall processing here. Now only lctl is supported */
 757        if (strcmp(upcall, LCTL_UPCALL) != 0) {
 758                CERROR("Unsupported upcall %s\n", upcall);
 759                return -EINVAL;
 760        }
 761
 762        start = ktime_get();
 763        rc = call_usermodehelper(argv[0], argv, NULL, 1);
 764        end = ktime_get();
 765
 766        if (rc < 0) {
 767                CERROR(
 768                       "lctl: error invoking upcall %s %s %s: rc = %d; time %ldus\n",
 769                       argv[0], argv[1], argv[2], rc,
 770                       (long)ktime_us_delta(end, start));
 771        } else {
 772                CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
 773                       argv[0], argv[1], argv[2],
 774                       (long)ktime_us_delta(end, start));
 775                       rc = 0;
 776        }
 777
 778        return rc;
 779}
 780
 781/** Process configuration commands given in lustre_cfg form.
 782 * These may come from direct calls (e.g. class_manual_cleanup)
 783 * or processing the config llog, or ioctl from lctl.
 784 */
 785int class_process_config(struct lustre_cfg *lcfg)
 786{
 787        struct obd_device *obd;
 788        int err;
 789
 790        LASSERT(lcfg && !IS_ERR(lcfg));
 791        CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
 792
 793        /* Commands that don't need a device */
 794        switch (lcfg->lcfg_command) {
 795        case LCFG_ATTACH: {
 796                err = class_attach(lcfg);
 797                goto out;
 798        }
 799        case LCFG_ADD_UUID: {
 800                CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx (%s)\n",
 801                       lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid,
 802                       libcfs_nid2str(lcfg->lcfg_nid));
 803
 804                err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
 805                goto out;
 806        }
 807        case LCFG_DEL_UUID: {
 808                CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
 809                       (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
 810                       ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
 811
 812                err = class_del_uuid(lustre_cfg_string(lcfg, 1));
 813                goto out;
 814        }
 815        case LCFG_MOUNTOPT: {
 816                CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
 817                       lustre_cfg_string(lcfg, 1),
 818                       lustre_cfg_string(lcfg, 2),
 819                       lustre_cfg_string(lcfg, 3));
 820                /* set these mount options somewhere, so ll_fill_super
 821                 * can find them.
 822                 */
 823                err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
 824                                        lustre_cfg_string(lcfg, 1),
 825                                        LUSTRE_CFG_BUFLEN(lcfg, 2),
 826                                        lustre_cfg_string(lcfg, 2),
 827                                        LUSTRE_CFG_BUFLEN(lcfg, 3),
 828                                        lustre_cfg_string(lcfg, 3));
 829                goto out;
 830        }
 831        case LCFG_DEL_MOUNTOPT: {
 832                CDEBUG(D_IOCTL, "mountopt: profile %s\n",
 833                       lustre_cfg_string(lcfg, 1));
 834                class_del_profile(lustre_cfg_string(lcfg, 1));
 835                err = 0;
 836                goto out;
 837        }
 838        case LCFG_SET_TIMEOUT: {
 839                CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
 840                       obd_timeout, lcfg->lcfg_num);
 841                obd_timeout = max(lcfg->lcfg_num, 1U);
 842                obd_timeout_set = 1;
 843                err = 0;
 844                goto out;
 845        }
 846        case LCFG_SET_LDLM_TIMEOUT: {
 847                /* ldlm_timeout is not used on the client */
 848                err = 0;
 849                goto out;
 850        }
 851        case LCFG_SET_UPCALL: {
 852                LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
 853                /* COMPAT_146 Don't fail on old configs */
 854                err = 0;
 855                goto out;
 856        }
 857        case LCFG_MARKER: {
 858                struct cfg_marker *marker;
 859
 860                marker = lustre_cfg_buf(lcfg, 1);
 861                CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
 862                       marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
 863                err = 0;
 864                goto out;
 865        }
 866        case LCFG_PARAM: {
 867                char *tmp;
 868                /* llite has no obd */
 869                if ((class_match_param(lustre_cfg_string(lcfg, 1),
 870                                       PARAM_LLITE, NULL) == 0) &&
 871                    client_process_config) {
 872                        err = (*client_process_config)(lcfg);
 873                        goto out;
 874                } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
 875                                              PARAM_SYS, &tmp) == 0)) {
 876                        /* Global param settings */
 877                        err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
 878                        /*
 879                         * Client or server should not fail to mount if
 880                         * it hits an unknown configuration parameter.
 881                         */
 882                        if (err != 0)
 883                                CWARN("Ignoring unknown param %s\n", tmp);
 884
 885                        err = 0;
 886                        goto out;
 887                } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
 888                                              PARAM_QUOTA, &tmp) == 0) &&
 889                           quota_process_config) {
 890                        err = (*quota_process_config)(lcfg);
 891                        goto out;
 892                }
 893
 894                break;
 895        }
 896        case LCFG_SET_PARAM: {
 897                err = process_param2_config(lcfg);
 898                goto out;
 899        }
 900        }
 901        /* Commands that require a device */
 902        obd = class_name2obd(lustre_cfg_string(lcfg, 0));
 903        if (!obd) {
 904                if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
 905                        CERROR("this lcfg command requires a device name\n");
 906                else
 907                        CERROR("no device for: %s\n",
 908                               lustre_cfg_string(lcfg, 0));
 909
 910                err = -EINVAL;
 911                goto out;
 912        }
 913
 914        switch (lcfg->lcfg_command) {
 915        case LCFG_SETUP: {
 916                err = class_setup(obd, lcfg);
 917                goto out;
 918        }
 919        case LCFG_DETACH: {
 920                err = class_detach(obd, lcfg);
 921                err = 0;
 922                goto out;
 923        }
 924        case LCFG_CLEANUP: {
 925                err = class_cleanup(obd, lcfg);
 926                err = 0;
 927                goto out;
 928        }
 929        case LCFG_ADD_CONN: {
 930                err = class_add_conn(obd, lcfg);
 931                err = 0;
 932                goto out;
 933        }
 934        case LCFG_DEL_CONN: {
 935                err = class_del_conn(obd, lcfg);
 936                err = 0;
 937                goto out;
 938        }
 939        case LCFG_POOL_NEW: {
 940                err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
 941                err = 0;
 942                goto out;
 943        }
 944        case LCFG_POOL_ADD: {
 945                err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
 946                                   lustre_cfg_string(lcfg, 3));
 947                err = 0;
 948                goto out;
 949        }
 950        case LCFG_POOL_REM: {
 951                err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
 952                                   lustre_cfg_string(lcfg, 3));
 953                err = 0;
 954                goto out;
 955        }
 956        case LCFG_POOL_DEL: {
 957                err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
 958                err = 0;
 959                goto out;
 960        }
 961        default: {
 962                err = obd_process_config(obd, sizeof(*lcfg), lcfg);
 963                goto out;
 964
 965        }
 966        }
 967out:
 968        if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
 969                CWARN("Ignoring error %d on optional command %#x\n", err,
 970                      lcfg->lcfg_command);
 971                err = 0;
 972        }
 973        return err;
 974}
 975EXPORT_SYMBOL(class_process_config);
 976
 977int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
 978                             struct lustre_cfg *lcfg, void *data)
 979{
 980        struct lprocfs_vars *var;
 981        struct file fakefile;
 982        struct seq_file fake_seqfile;
 983        char *key, *sval;
 984        int i, keylen, vallen;
 985        int matched = 0, j = 0;
 986        int rc = 0;
 987        int skip = 0;
 988
 989        if (lcfg->lcfg_command != LCFG_PARAM) {
 990                CERROR("Unknown command: %d\n", lcfg->lcfg_command);
 991                return -EINVAL;
 992        }
 993
 994        /* fake a seq file so that var->fops->write can work... */
 995        fakefile.private_data = &fake_seqfile;
 996        fake_seqfile.private = data;
 997        /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
 998         * or   lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
 999         * or   lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1000         */
1001        for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1002                key = lustre_cfg_buf(lcfg, i);
1003                /* Strip off prefix */
1004                class_match_param(key, prefix, &key);
1005                sval = strchr(key, '=');
1006                if (!sval || (*(sval + 1) == 0)) {
1007                        CERROR("Can't parse param %s (missing '=')\n", key);
1008                        /* rc = -EINVAL;        continue parsing other params */
1009                        continue;
1010                }
1011                keylen = sval - key;
1012                sval++;
1013                vallen = strlen(sval);
1014                matched = 0;
1015                j = 0;
1016                /* Search proc entries */
1017                while (lvars[j].name) {
1018                        var = &lvars[j];
1019                        if (!class_match_param(key, var->name, NULL)
1020                            && keylen == strlen(var->name)) {
1021                                matched++;
1022                                rc = -EROFS;
1023                                if (var->fops && var->fops->write) {
1024                                        mm_segment_t oldfs;
1025
1026                                        oldfs = get_fs();
1027                                        set_fs(KERNEL_DS);
1028                                        rc = (var->fops->write)(&fakefile, sval,
1029                                                                vallen, NULL);
1030                                        set_fs(oldfs);
1031                                }
1032                                break;
1033                        }
1034                        j++;
1035                }
1036                if (!matched) {
1037                        /* If the prefix doesn't match, return error so we
1038                         * can pass it down the stack
1039                         */
1040                        if (strnchr(key, keylen, '.'))
1041                                return -ENOSYS;
1042                        CERROR("%s: unknown param %s\n",
1043                               (char *)lustre_cfg_string(lcfg, 0), key);
1044                        /* rc = -EINVAL;        continue parsing other params */
1045                        skip++;
1046                } else if (rc < 0) {
1047                        CERROR("writing proc entry %s err %d\n",
1048                               var->name, rc);
1049                        rc = 0;
1050                } else {
1051                        CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1052                               lustre_cfg_string(lcfg, 0),
1053                               (int)strlen(prefix) - 1, prefix,
1054                               (int)(sval - key - 1), key, sval);
1055                }
1056        }
1057
1058        if (rc > 0)
1059                rc = 0;
1060        if (!rc && skip)
1061                rc = skip;
1062        return rc;
1063}
1064EXPORT_SYMBOL(class_process_proc_param);
1065
1066extern int lustre_check_exclusion(struct super_block *sb, char *svname);
1067
1068/** Parse a configuration llog, doing various manipulations on them
1069 * for various reasons, (modifications for compatibility, skip obsolete
1070 * records, change uuids, etc), then class_process_config() resulting
1071 * net records.
1072 */
1073int class_config_llog_handler(const struct lu_env *env,
1074                              struct llog_handle *handle,
1075                              struct llog_rec_hdr *rec, void *data)
1076{
1077        struct config_llog_instance *clli = data;
1078        int cfg_len = rec->lrh_len;
1079        char *cfg_buf = (char *) (rec + 1);
1080        int rc = 0;
1081
1082        switch (rec->lrh_type) {
1083        case OBD_CFG_REC: {
1084                struct lustre_cfg *lcfg, *lcfg_new;
1085                struct lustre_cfg_bufs bufs;
1086                char *inst_name = NULL;
1087                int inst_len = 0;
1088                int inst = 0, swab = 0;
1089
1090                lcfg = (struct lustre_cfg *)cfg_buf;
1091                if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1092                        lustre_swab_lustre_cfg(lcfg);
1093                        swab = 1;
1094                }
1095
1096                rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1097                if (rc)
1098                        goto out;
1099
1100                /* Figure out config state info */
1101                if (lcfg->lcfg_command == LCFG_MARKER) {
1102                        struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1103
1104                        lustre_swab_cfg_marker(marker, swab,
1105                                               LUSTRE_CFG_BUFLEN(lcfg, 1));
1106                        CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1107                               clli->cfg_flags, marker->cm_flags);
1108                        if (marker->cm_flags & CM_START) {
1109                                /* all previous flags off */
1110                                clli->cfg_flags = CFG_F_MARKER;
1111                                if (marker->cm_flags & CM_SKIP) {
1112                                        clli->cfg_flags |= CFG_F_SKIP;
1113                                        CDEBUG(D_CONFIG, "SKIP #%d\n",
1114                                               marker->cm_step);
1115                                } else if ((marker->cm_flags & CM_EXCLUDE) ||
1116                                           (clli->cfg_sb &&
1117                                            lustre_check_exclusion(clli->cfg_sb,
1118                                                         marker->cm_tgtname))) {
1119                                        clli->cfg_flags |= CFG_F_EXCLUDE;
1120                                        CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1121                                               marker->cm_step);
1122                                }
1123                        } else if (marker->cm_flags & CM_END) {
1124                                clli->cfg_flags = 0;
1125                        }
1126                }
1127                /* A config command without a start marker before it is
1128                 * illegal (post 146)
1129                 */
1130                if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
1131                    !(clli->cfg_flags & CFG_F_MARKER) &&
1132                    (lcfg->lcfg_command != LCFG_MARKER)) {
1133                        CWARN("Config not inside markers, ignoring! (inst: %p, uuid: %s, flags: %#x)\n",
1134                              clli->cfg_instance,
1135                              clli->cfg_uuid.uuid, clli->cfg_flags);
1136                        clli->cfg_flags |= CFG_F_SKIP;
1137                }
1138                if (clli->cfg_flags & CFG_F_SKIP) {
1139                        CDEBUG(D_CONFIG, "skipping %#x\n",
1140                               clli->cfg_flags);
1141                        rc = 0;
1142                        /* No processing! */
1143                        break;
1144                }
1145
1146                /*
1147                 * For interoperability between 1.8 and 2.0,
1148                 * rename "mds" obd device type to "mdt".
1149                 */
1150                {
1151                        char *typename = lustre_cfg_string(lcfg, 1);
1152                        char *index = lustre_cfg_string(lcfg, 2);
1153
1154                        if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1155                             strcmp(typename, "mds") == 0)) {
1156                                CWARN("For 1.8 interoperability, rename obd type from mds to mdt\n");
1157                                typename[2] = 't';
1158                        }
1159                        if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1160                             strcmp(index, "type") == 0)) {
1161                                CDEBUG(D_INFO, "For 1.8 interoperability, set this index to '0'\n");
1162                                index[0] = '0';
1163                                index[1] = 0;
1164                        }
1165                }
1166
1167                if (clli->cfg_flags & CFG_F_EXCLUDE) {
1168                        CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1169                               lcfg->lcfg_command);
1170                        if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1171                                /* Add inactive instead */
1172                                lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1173                }
1174
1175                lustre_cfg_bufs_init(&bufs, lcfg);
1176
1177                if (clli && clli->cfg_instance &&
1178                    LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1179                        inst = 1;
1180                        inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1181                                   sizeof(clli->cfg_instance) * 2 + 4;
1182                        inst_name = kasprintf(GFP_NOFS, "%s-%p",
1183                                              lustre_cfg_string(lcfg, 0),
1184                                              clli->cfg_instance);
1185                        if (!inst_name) {
1186                                rc = -ENOMEM;
1187                                goto out;
1188                        }
1189                        lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1190                        CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1191                               lcfg->lcfg_command, inst_name);
1192                }
1193
1194                /* we override the llog's uuid for clients, to insure they
1195                 * are unique
1196                 */
1197                if (clli && clli->cfg_instance &&
1198                    lcfg->lcfg_command == LCFG_ATTACH) {
1199                        lustre_cfg_bufs_set_string(&bufs, 2,
1200                                                   clli->cfg_uuid.uuid);
1201                }
1202                /*
1203                 * sptlrpc config record, we expect 2 data segments:
1204                 *  [0]: fs_name/target_name,
1205                 *  [1]: rule string
1206                 * moving them to index [1] and [2], and insert MGC's
1207                 * obdname at index [0].
1208                 */
1209                if (clli && !clli->cfg_instance &&
1210                    lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1211                        lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1212                                            bufs.lcfg_buflen[1]);
1213                        lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1214                                            bufs.lcfg_buflen[0]);
1215                        lustre_cfg_bufs_set_string(&bufs, 0,
1216                                                   clli->cfg_obdname);
1217                }
1218
1219                lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1220
1221                lcfg_new->lcfg_num   = lcfg->lcfg_num;
1222                lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1223
1224                /* XXX Hack to try to remain binary compatible with
1225                 * pre-newconfig logs
1226                 */
1227                if (lcfg->lcfg_nal != 0 &&      /* pre-newconfig log? */
1228                    (lcfg->lcfg_nid >> 32) == 0) {
1229                        __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1230
1231                        lcfg_new->lcfg_nid =
1232                                LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1233                        CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1234                              lcfg->lcfg_nal, addr,
1235                              libcfs_nid2str(lcfg_new->lcfg_nid));
1236                } else {
1237                        lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1238                }
1239
1240                lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1241
1242                rc = class_process_config(lcfg_new);
1243                lustre_cfg_free(lcfg_new);
1244
1245                if (inst)
1246                        kfree(inst_name);
1247                break;
1248        }
1249        default:
1250                CERROR("Unknown llog record type %#x encountered\n",
1251                       rec->lrh_type);
1252                break;
1253        }
1254out:
1255        if (rc) {
1256                CERROR("%s: cfg command failed: rc = %d\n",
1257                       handle->lgh_ctxt->loc_obd->obd_name, rc);
1258                class_config_dump_handler(NULL, handle, rec, data);
1259        }
1260        return rc;
1261}
1262EXPORT_SYMBOL(class_config_llog_handler);
1263
1264int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1265                            char *name, struct config_llog_instance *cfg)
1266{
1267        struct llog_process_cat_data     cd = {0, 0};
1268        struct llog_handle              *llh;
1269        llog_cb_t                        callback;
1270        int                              rc;
1271
1272        CDEBUG(D_INFO, "looking up llog %s\n", name);
1273        rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1274        if (rc)
1275                return rc;
1276
1277        rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1278        if (rc)
1279                goto parse_out;
1280
1281        /* continue processing from where we last stopped to end-of-log */
1282        if (cfg) {
1283                cd.lpcd_first_idx = cfg->cfg_last_idx;
1284                callback = cfg->cfg_callback;
1285                LASSERT(callback);
1286        } else {
1287                callback = class_config_llog_handler;
1288        }
1289
1290        cd.lpcd_last_idx = 0;
1291
1292        rc = llog_process(env, llh, callback, cfg, &cd);
1293
1294        CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1295               cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1296        if (cfg)
1297                cfg->cfg_last_idx = cd.lpcd_last_idx;
1298
1299parse_out:
1300        llog_close(env, llh);
1301        return rc;
1302}
1303EXPORT_SYMBOL(class_config_parse_llog);
1304
1305/**
1306 * parse config record and output dump in supplied buffer.
1307 * This is separated from class_config_dump_handler() to use
1308 * for ioctl needs as well
1309 */
1310static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf,
1311                                  int size)
1312{
1313        struct lustre_cfg       *lcfg = (struct lustre_cfg *)(rec + 1);
1314        char                    *ptr = buf;
1315        char                    *end = buf + size;
1316        int                      rc = 0;
1317
1318        LASSERT(rec->lrh_type == OBD_CFG_REC);
1319        rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1320        if (rc < 0)
1321                return rc;
1322
1323        ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1324        if (lcfg->lcfg_flags)
1325                ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1326                                lcfg->lcfg_flags);
1327
1328        if (lcfg->lcfg_num)
1329                ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1330
1331        if (lcfg->lcfg_nid) {
1332                char nidstr[LNET_NIDSTR_SIZE];
1333
1334                libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1335                ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n     ",
1336                                nidstr, lcfg->lcfg_nid);
1337        }
1338
1339        if (lcfg->lcfg_command == LCFG_MARKER) {
1340                struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1341
1342                ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1343                                marker->cm_step, marker->cm_flags,
1344                                marker->cm_tgtname, marker->cm_comment);
1345        } else {
1346                int i;
1347
1348                for (i = 0; i <  lcfg->lcfg_bufcount; i++) {
1349                        ptr += snprintf(ptr, end-ptr, "%d:%s  ", i,
1350                                        lustre_cfg_string(lcfg, i));
1351                }
1352        }
1353        /* return consumed bytes */
1354        rc = ptr - buf;
1355        return rc;
1356}
1357
1358int class_config_dump_handler(const struct lu_env *env,
1359                              struct llog_handle *handle,
1360                              struct llog_rec_hdr *rec, void *data)
1361{
1362        char    *outstr;
1363        int      rc = 0;
1364
1365        outstr = kzalloc(256, GFP_NOFS);
1366        if (!outstr)
1367                return -ENOMEM;
1368
1369        if (rec->lrh_type == OBD_CFG_REC) {
1370                class_config_parse_rec(rec, outstr, 256);
1371                LCONSOLE(D_WARNING, "   %s\n", outstr);
1372        } else {
1373                LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1374                rc = -EINVAL;
1375        }
1376
1377        kfree(outstr);
1378        return rc;
1379}
1380
1381/** Call class_cleanup and class_detach.
1382 * "Manual" only in the sense that we're faking lcfg commands.
1383 */
1384int class_manual_cleanup(struct obd_device *obd)
1385{
1386        char                flags[3] = "";
1387        struct lustre_cfg      *lcfg;
1388        struct lustre_cfg_bufs  bufs;
1389        int                  rc;
1390
1391        if (!obd) {
1392                CERROR("empty cleanup\n");
1393                return -EALREADY;
1394        }
1395
1396        if (obd->obd_force)
1397                strcat(flags, "F");
1398        if (obd->obd_fail)
1399                strcat(flags, "A");
1400
1401        CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1402               obd->obd_name, flags);
1403
1404        lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1405        lustre_cfg_bufs_set_string(&bufs, 1, flags);
1406        lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1407        if (!lcfg)
1408                return -ENOMEM;
1409
1410        rc = class_process_config(lcfg);
1411        if (rc) {
1412                CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1413                goto out;
1414        }
1415
1416        /* the lcfg is almost the same for both ops */
1417        lcfg->lcfg_command = LCFG_DETACH;
1418        rc = class_process_config(lcfg);
1419        if (rc)
1420                CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1421out:
1422        lustre_cfg_free(lcfg);
1423        return rc;
1424}
1425EXPORT_SYMBOL(class_manual_cleanup);
1426
1427/*
1428 * uuid<->export lustre hash operations
1429 */
1430
1431static unsigned
1432uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
1433{
1434        return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
1435                                  sizeof(((struct obd_uuid *)key)->uuid), mask);
1436}
1437
1438static void *
1439uuid_key(struct hlist_node *hnode)
1440{
1441        struct obd_export *exp;
1442
1443        exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1444
1445        return &exp->exp_client_uuid;
1446}
1447
1448/*
1449 * NOTE: It is impossible to find an export that is in failed
1450 *       state with this function
1451 */
1452static int
1453uuid_keycmp(const void *key, struct hlist_node *hnode)
1454{
1455        struct obd_export *exp;
1456
1457        LASSERT(key);
1458        exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1459
1460        return obd_uuid_equals(key, &exp->exp_client_uuid) &&
1461               !exp->exp_failed;
1462}
1463
1464static void *
1465uuid_export_object(struct hlist_node *hnode)
1466{
1467        return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1468}
1469
1470static void
1471uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
1472{
1473        struct obd_export *exp;
1474
1475        exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1476        class_export_get(exp);
1477}
1478
1479static void
1480uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1481{
1482        struct obd_export *exp;
1483
1484        exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
1485        class_export_put(exp);
1486}
1487
1488static struct cfs_hash_ops uuid_hash_ops = {
1489        .hs_hash        = uuid_hash,
1490        .hs_key         = uuid_key,
1491        .hs_keycmp      = uuid_keycmp,
1492        .hs_object      = uuid_export_object,
1493        .hs_get         = uuid_export_get,
1494        .hs_put_locked  = uuid_export_put_locked,
1495};
1496