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