4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/obd_config.c
37 #define DEBUG_SUBSYSTEM S_CLASS
39 #include <linux/string.h>
41 #include <llog_swab.h>
42 #include <lprocfs_status.h>
43 #include <lustre_disk.h>
44 #include <lustre_ioctl.h>
45 #include <lustre_log.h>
46 #include <lustre_param.h>
47 #include <obd_class.h>
49 #include "llog_internal.h"
51 static struct cfs_hash_ops uuid_hash_ops;
52 static struct cfs_hash_ops nid_hash_ops;
53 static struct cfs_hash_ops nid_stat_hash_ops;
54 static struct cfs_hash_ops gen_hash_ops;
56 /*********** string parsing utils *********/
58 /* returns 0 if we find this key in the buffer, else 1 */
59 int class_find_param(char *buf, char *key, char **valp)
66 if ((ptr = strstr(buf, key)) == NULL)
70 *valp = ptr + strlen(key);
74 EXPORT_SYMBOL(class_find_param);
77 * Check whether the proc parameter \a param is an old parameter or not from
78 * the array \a ptr which contains the mapping from old parameters to new ones.
79 * If it's an old one, then return the pointer to the cfg_interop_param struc-
80 * ture which contains both the old and new parameters.
82 * \param param proc parameter
83 * \param ptr an array which contains the mapping from
84 * old parameters to new ones
86 * \retval valid-pointer pointer to the cfg_interop_param structure
87 * which contains the old and new parameters
88 * \retval NULL \a param or \a ptr is NULL,
89 * or \a param is not an old parameter
91 struct cfg_interop_param *class_find_old_param(const char *param,
92 struct cfg_interop_param *ptr)
97 if (param == NULL || ptr == NULL)
100 value = strchr(param, '=');
102 name_len = strlen(param);
104 name_len = value - param;
106 while (ptr->old_param != NULL) {
107 if (strncmp(param, ptr->old_param, name_len) == 0 &&
108 name_len == strlen(ptr->old_param))
115 EXPORT_SYMBOL(class_find_old_param);
118 * Finds a parameter in \a params and copies it to \a copy.
120 * Leading spaces are skipped. Next space or end of string is the
121 * parameter terminator with the exception that spaces inside single or double
122 * quotes get included into a parameter. The parameter is copied into \a copy
123 * which has to be allocated big enough by a caller, quotes are stripped in
124 * the copy and the copy is terminated by 0.
126 * On return \a params is set to next parameter or to NULL if last
127 * parameter is returned.
129 * \retval 0 if parameter is returned in \a copy
130 * \retval 1 otherwise
131 * \retval -EINVAL if unbalanced quota is found
133 int class_get_next_param(char **params, char *copy)
148 q1 = strpbrk(str, " '\"");
151 memcpy(copy, str, len);
158 memcpy(copy, str, len);
164 memcpy(copy, str, len);
167 /* search for the matching closing quote */
169 q2 = strchr(str, *q1);
171 CERROR("Unbalanced quota in parameters: \"%s\"\n",
176 memcpy(copy, str, len);
182 EXPORT_SYMBOL(class_get_next_param);
184 /* returns 0 if this is the first key in the buffer, else 1.
185 valp points to first char after key. */
186 int class_match_param(char *buf, const char *key, char **valp)
191 if (memcmp(buf, key, strlen(key)) != 0)
195 *valp = buf + strlen(key);
199 EXPORT_SYMBOL(class_match_param);
201 static int parse_nid(char *buf, void *value, int quiet)
203 lnet_nid_t *nid = (lnet_nid_t *)value;
205 *nid = libcfs_str2nid(buf);
206 if (*nid != LNET_NID_ANY)
210 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
214 static int parse_net(char *buf, void *value)
216 __u32 *net = (__u32 *)value;
218 *net = libcfs_str2net(buf);
219 CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
231 endh is set to next separator */
232 static int class_parse_value(char *buf, int opc, void *value, char **endh,
241 while (*buf == ',' || *buf == ':')
243 if (*buf == ' ' || *buf == '/' || *buf == '\0')
246 /* nid separators or end of nids */
247 endp = strpbrk(buf, ",: /");
249 endp = buf + strlen(buf);
256 case CLASS_PARSE_NID:
257 rc = parse_nid(buf, value, quiet);
259 case CLASS_PARSE_NET:
260 rc = parse_net(buf, value);
271 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
273 return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
275 EXPORT_SYMBOL(class_parse_nid);
277 int class_parse_nid_quiet(char *buf, lnet_nid_t *nid, char **endh)
279 return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
281 EXPORT_SYMBOL(class_parse_nid_quiet);
283 int class_parse_net(char *buf, __u32 *net, char **endh)
285 return class_parse_value(buf, CLASS_PARSE_NET, (void *)net, endh, 0);
288 /* 1 param contains key and match
289 * 0 param contains key and not match
290 * -1 param does not contain key
292 int class_match_nid(char *buf, char *key, lnet_nid_t nid)
297 while (class_find_param(buf, key, &buf) == 0) {
298 /* please restrict to the nids pertaining to
299 * the specified nids */
300 while (class_parse_nid(buf, &tmp, &buf) == 0) {
309 int class_match_net(char *buf, char *key, __u32 net)
314 while (class_find_param(buf, key, &buf) == 0) {
315 /* please restrict to the nids pertaining to
316 * the specified networks */
317 while (class_parse_net(buf, &tmp, &buf) == 0) {
326 /********************** class fns **********************/
329 * Create a new obd device and set the type, name and uuid. If successful,
330 * the new device can be accessed by either name or uuid.
332 int class_attach(struct lustre_cfg *lcfg)
334 struct obd_device *obd = NULL;
335 char *typename, *name, *uuid;
339 if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
340 CERROR("No type passed!\n");
343 typename = lustre_cfg_string(lcfg, 1);
345 if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
346 CERROR("No name passed!\n");
349 name = lustre_cfg_string(lcfg, 0);
351 if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
352 CERROR("No UUID passed!\n");
355 uuid = lustre_cfg_string(lcfg, 2);
357 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
358 MKSTR(typename), MKSTR(name), MKSTR(uuid));
360 obd = class_newdev(typename, name);
362 /* Already exists or out of obds */
365 CERROR("Cannot create device %s of type %s : %d\n",
369 LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
371 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
372 "obd %p obd_magic %08X != %08X\n",
373 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
374 LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
375 "%p obd_name %s != %s\n", obd, obd->obd_name, name);
377 rwlock_init(&obd->obd_pool_lock);
378 obd->obd_pool_limit = 0;
379 obd->obd_pool_slv = 0;
381 INIT_LIST_HEAD(&obd->obd_exports);
382 INIT_LIST_HEAD(&obd->obd_unlinked_exports);
383 INIT_LIST_HEAD(&obd->obd_delayed_exports);
384 INIT_LIST_HEAD(&obd->obd_exports_timed);
385 INIT_LIST_HEAD(&obd->obd_nid_stats);
386 spin_lock_init(&obd->obd_nid_lock);
387 spin_lock_init(&obd->obd_dev_lock);
388 mutex_init(&obd->obd_dev_mutex);
389 spin_lock_init(&obd->obd_osfs_lock);
390 /* obd->obd_osfs_age must be set to a value in the distant
391 * past to guarantee a fresh statfs is fetched on mount. */
392 obd->obd_osfs_age = cfs_time_shift_64(-1000);
394 /* XXX belongs in setup not attach */
395 init_rwsem(&obd->obd_observer_link_sem);
397 cfs_init_timer(&obd->obd_recovery_timer);
398 spin_lock_init(&obd->obd_recovery_task_lock);
399 init_waitqueue_head(&obd->obd_next_transno_waitq);
400 init_waitqueue_head(&obd->obd_evict_inprogress_waitq);
401 INIT_LIST_HEAD(&obd->obd_req_replay_queue);
402 INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
403 INIT_LIST_HEAD(&obd->obd_final_req_queue);
404 INIT_LIST_HEAD(&obd->obd_evict_list);
405 INIT_LIST_HEAD(&obd->obd_lwp_list);
407 llog_group_init(&obd->obd_olg);
409 obd->obd_conn_inprogress = 0;
412 if (len >= sizeof(obd->obd_uuid)) {
413 CERROR("uuid must be < %d bytes long\n",
414 (int)sizeof(obd->obd_uuid));
415 GOTO(out, rc = -EINVAL);
417 memcpy(obd->obd_uuid.uuid, uuid, len);
419 /* Detach drops this */
420 spin_lock(&obd->obd_dev_lock);
421 atomic_set(&obd->obd_refcount, 1);
422 spin_unlock(&obd->obd_dev_lock);
423 lu_ref_init(&obd->obd_reference);
424 lu_ref_add(&obd->obd_reference, "attach", obd);
426 obd->obd_attached = 1;
427 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
428 obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
432 class_release_dev(obd);
436 EXPORT_SYMBOL(class_attach);
438 /** Create hashes, self-export, and call type-specific setup.
439 * Setup is effectively the "start this obd" call.
441 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
444 struct obd_export *exp;
447 LASSERT(obd != NULL);
448 LASSERTF(obd == class_num2obd(obd->obd_minor),
449 "obd %p != obd_devs[%d] %p\n",
450 obd, obd->obd_minor, class_num2obd(obd->obd_minor));
451 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
452 "obd %p obd_magic %08x != %08x\n",
453 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
455 /* have we attached a type to this device? */
456 if (!obd->obd_attached) {
457 CERROR("Device %d not attached\n", obd->obd_minor);
461 if (obd->obd_set_up) {
462 CERROR("Device %d already setup (type %s)\n",
463 obd->obd_minor, obd->obd_type->typ_name);
467 /* is someone else setting us up right now? (attach inits spinlock) */
468 spin_lock(&obd->obd_dev_lock);
469 if (obd->obd_starting) {
470 spin_unlock(&obd->obd_dev_lock);
471 CERROR("Device %d setup in progress (type %s)\n",
472 obd->obd_minor, obd->obd_type->typ_name);
475 /* just leave this on forever. I can't use obd_set_up here because
476 other fns check that status, and we're not actually set up yet. */
477 obd->obd_starting = 1;
478 obd->obd_uuid_hash = NULL;
479 obd->obd_nid_hash = NULL;
480 obd->obd_nid_stats_hash = NULL;
481 obd->obd_gen_hash = NULL;
482 spin_unlock(&obd->obd_dev_lock);
484 /* create an uuid-export lustre hash */
485 obd->obd_uuid_hash = cfs_hash_create("UUID_HASH",
488 HASH_UUID_BKT_BITS, 0,
491 &uuid_hash_ops, CFS_HASH_DEFAULT);
492 if (!obd->obd_uuid_hash)
493 GOTO(err_hash, err = -ENOMEM);
495 /* create a nid-export lustre hash */
496 obd->obd_nid_hash = cfs_hash_create("NID_HASH",
499 HASH_NID_BKT_BITS, 0,
502 &nid_hash_ops, CFS_HASH_DEFAULT);
503 if (!obd->obd_nid_hash)
504 GOTO(err_hash, err = -ENOMEM);
506 /* create a nid-stats lustre hash */
507 obd->obd_nid_stats_hash = cfs_hash_create("NID_STATS",
508 HASH_NID_STATS_CUR_BITS,
509 HASH_NID_STATS_MAX_BITS,
510 HASH_NID_STATS_BKT_BITS, 0,
513 &nid_stat_hash_ops, CFS_HASH_DEFAULT);
514 if (!obd->obd_nid_stats_hash)
515 GOTO(err_hash, err = -ENOMEM);
517 /* create a client_generation-export lustre hash */
518 obd->obd_gen_hash = cfs_hash_create("UUID_HASH",
521 HASH_GEN_BKT_BITS, 0,
524 &gen_hash_ops, CFS_HASH_DEFAULT);
525 if (!obd->obd_gen_hash)
526 GOTO(err_hash, err = -ENOMEM);
528 exp = class_new_export(obd, &obd->obd_uuid);
530 GOTO(err_hash, err = PTR_ERR(exp));
532 obd->obd_self_export = exp;
533 list_del_init(&exp->exp_obd_chain_timed);
534 class_export_put(exp);
536 err = obd_setup(obd, lcfg);
542 spin_lock(&obd->obd_dev_lock);
543 /* cleanup drops this */
544 class_incref(obd, "setup", obd);
545 spin_unlock(&obd->obd_dev_lock);
547 CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
548 obd->obd_name, obd->obd_uuid.uuid);
552 if (obd->obd_self_export) {
553 class_unlink_export(obd->obd_self_export);
554 obd->obd_self_export = NULL;
557 if (obd->obd_uuid_hash) {
558 cfs_hash_putref(obd->obd_uuid_hash);
559 obd->obd_uuid_hash = NULL;
561 if (obd->obd_nid_hash) {
562 cfs_hash_putref(obd->obd_nid_hash);
563 obd->obd_nid_hash = NULL;
565 if (obd->obd_nid_stats_hash) {
566 cfs_hash_putref(obd->obd_nid_stats_hash);
567 obd->obd_nid_stats_hash = NULL;
569 if (obd->obd_gen_hash) {
570 cfs_hash_putref(obd->obd_gen_hash);
571 obd->obd_gen_hash = NULL;
573 obd->obd_starting = 0;
574 CERROR("setup %s failed (%d)\n", obd->obd_name, err);
577 EXPORT_SYMBOL(class_setup);
579 /** We have finished using this obd and are ready to destroy it.
580 * There can be no more references to this obd.
582 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
586 if (obd->obd_set_up) {
587 CERROR("OBD device %d still set up\n", obd->obd_minor);
591 spin_lock(&obd->obd_dev_lock);
592 if (!obd->obd_attached) {
593 spin_unlock(&obd->obd_dev_lock);
594 CERROR("OBD device %d not attached\n", obd->obd_minor);
597 obd->obd_attached = 0;
598 spin_unlock(&obd->obd_dev_lock);
600 CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
601 obd->obd_name, obd->obd_uuid.uuid);
603 class_decref(obd, "attach", obd);
606 EXPORT_SYMBOL(class_detach);
608 /** Start shutting down the obd. There may be in-progess ops when
609 * this is called. We tell them to start shutting down with a call
610 * to class_disconnect_exports().
612 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
618 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
620 if (!obd->obd_set_up) {
621 CERROR("Device %d not setup\n", obd->obd_minor);
625 spin_lock(&obd->obd_dev_lock);
626 if (obd->obd_stopping) {
627 spin_unlock(&obd->obd_dev_lock);
628 CERROR("OBD %d already stopping\n", obd->obd_minor);
631 /* Leave this on forever */
632 obd->obd_stopping = 1;
633 spin_unlock(&obd->obd_dev_lock);
635 /* wait for already-arrived-connections to finish. */
636 while (obd->obd_conn_inprogress > 0)
640 if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
641 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
647 LCONSOLE_WARN("Failing over %s\n",
650 obd->obd_no_transno = 1;
651 obd->obd_no_recov = 1;
652 if (OBP(obd, iocontrol)) {
653 obd_iocontrol(OBD_IOC_SYNC,
654 obd->obd_self_export,
659 CERROR("Unrecognised flag '%c'\n", *flag);
663 LASSERT(obd->obd_self_export);
665 /* The three references that should be remaining are the
666 * obd_self_export and the attach and setup references. */
667 if (atomic_read(&obd->obd_refcount) > 3) {
668 /* refcounf - 3 might be the number of real exports
669 (excluding self export). But class_incref is called
670 by other things as well, so don't count on it. */
671 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
672 obd->obd_name, atomic_read(&obd->obd_refcount) - 3);
673 dump_exports(obd, 0, D_HA);
674 class_disconnect_exports(obd);
677 /* Precleanup, we must make sure all exports get destroyed. */
678 err = obd_precleanup(obd);
680 CERROR("Precleanup %s returned %d\n",
683 /* destroy an uuid-export hash body */
684 if (obd->obd_uuid_hash) {
685 cfs_hash_putref(obd->obd_uuid_hash);
686 obd->obd_uuid_hash = NULL;
689 /* destroy a nid-export hash body */
690 if (obd->obd_nid_hash) {
691 cfs_hash_putref(obd->obd_nid_hash);
692 obd->obd_nid_hash = NULL;
695 /* destroy a nid-stats hash body */
696 if (obd->obd_nid_stats_hash) {
697 cfs_hash_putref(obd->obd_nid_stats_hash);
698 obd->obd_nid_stats_hash = NULL;
701 /* destroy a client_generation-export hash body */
702 if (obd->obd_gen_hash) {
703 cfs_hash_putref(obd->obd_gen_hash);
704 obd->obd_gen_hash = NULL;
707 class_decref(obd, "setup", obd);
713 struct obd_device *class_incref(struct obd_device *obd,
714 const char *scope, const void *source)
716 lu_ref_add_atomic(&obd->obd_reference, scope, source);
717 atomic_inc(&obd->obd_refcount);
718 CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
719 atomic_read(&obd->obd_refcount));
723 EXPORT_SYMBOL(class_incref);
725 void class_decref(struct obd_device *obd, const char *scope, const void *source)
730 spin_lock(&obd->obd_dev_lock);
731 atomic_dec(&obd->obd_refcount);
732 refs = atomic_read(&obd->obd_refcount);
733 spin_unlock(&obd->obd_dev_lock);
734 lu_ref_del(&obd->obd_reference, scope, source);
736 CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
738 if ((refs == 1) && obd->obd_stopping) {
739 /* All exports have been destroyed; there should
740 be no more in-progress ops by this point.*/
742 spin_lock(&obd->obd_self_export->exp_lock);
743 obd->obd_self_export->exp_flags |= exp_flags_from_obd(obd);
744 spin_unlock(&obd->obd_self_export->exp_lock);
746 /* note that we'll recurse into class_decref again */
747 class_unlink_export(obd->obd_self_export);
752 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
753 obd->obd_name, obd->obd_uuid.uuid);
754 LASSERT(!obd->obd_attached);
755 if (obd->obd_stopping) {
756 /* If we're not stopping, we were never set up */
757 err = obd_cleanup(obd);
759 CERROR("Cleanup %s returned %d\n",
763 class_release_dev(obd);
766 EXPORT_SYMBOL(class_decref);
768 /** Add a failover nid location.
769 * Client obd types contact server obd types using this nid list.
771 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
773 struct obd_import *imp;
774 struct obd_uuid uuid;
778 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
779 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
780 CERROR("invalid conn_uuid\n");
783 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
784 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
785 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
786 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
787 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
788 CERROR("can't add connection on non-client dev\n");
792 imp = obd->u.cli.cl_import;
794 CERROR("try to add conn on immature client dev\n");
798 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
799 rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
804 /** Remove a failover nid location.
806 static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
808 struct obd_import *imp;
809 struct obd_uuid uuid;
813 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
814 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
815 CERROR("invalid conn_uuid\n");
818 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
819 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
820 CERROR("can't del connection on non-client dev\n");
824 imp = obd->u.cli.cl_import;
826 CERROR("try to del conn on immature client dev\n");
830 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
831 rc = obd_del_conn(imp, &uuid);
836 static struct list_head lustre_profile_list =
837 LIST_HEAD_INIT(lustre_profile_list);
838 static DEFINE_SPINLOCK(lustre_profile_list_lock);
840 struct lustre_profile *class_get_profile(const char * prof)
842 struct lustre_profile *lprof;
845 spin_lock(&lustre_profile_list_lock);
846 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
847 if (!strcmp(lprof->lp_profile, prof)) {
849 spin_unlock(&lustre_profile_list_lock);
853 spin_unlock(&lustre_profile_list_lock);
856 EXPORT_SYMBOL(class_get_profile);
858 /** Create a named "profile".
859 * This defines the mdc and osc names to use for a client.
860 * This also is used to define the lov to be used by a mdt.
862 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
863 int mdclen, char *mdc)
865 struct lustre_profile *lprof;
869 CDEBUG(D_CONFIG, "Add profile %s\n", prof);
871 OBD_ALLOC(lprof, sizeof(*lprof));
874 INIT_LIST_HEAD(&lprof->lp_list);
876 LASSERT(proflen == (strlen(prof) + 1));
877 OBD_ALLOC(lprof->lp_profile, proflen);
878 if (lprof->lp_profile == NULL)
879 GOTO(out, err = -ENOMEM);
880 memcpy(lprof->lp_profile, prof, proflen);
882 LASSERT(osclen == (strlen(osc) + 1));
883 OBD_ALLOC(lprof->lp_dt, osclen);
884 if (lprof->lp_dt == NULL)
885 GOTO(out, err = -ENOMEM);
886 memcpy(lprof->lp_dt, osc, osclen);
889 LASSERT(mdclen == (strlen(mdc) + 1));
890 OBD_ALLOC(lprof->lp_md, mdclen);
891 if (lprof->lp_md == NULL)
892 GOTO(out, err = -ENOMEM);
893 memcpy(lprof->lp_md, mdc, mdclen);
896 spin_lock(&lustre_profile_list_lock);
898 lprof->lp_list_deleted = false;
900 list_add(&lprof->lp_list, &lustre_profile_list);
901 spin_unlock(&lustre_profile_list_lock);
906 OBD_FREE(lprof->lp_md, mdclen);
908 OBD_FREE(lprof->lp_dt, osclen);
909 if (lprof->lp_profile)
910 OBD_FREE(lprof->lp_profile, proflen);
911 OBD_FREE(lprof, sizeof(*lprof));
915 void class_del_profile(const char *prof)
917 struct lustre_profile *lprof;
920 CDEBUG(D_CONFIG, "Del profile %s\n", prof);
922 lprof = class_get_profile(prof);
924 spin_lock(&lustre_profile_list_lock);
925 /* because get profile increments the ref counter */
927 list_del(&lprof->lp_list);
928 lprof->lp_list_deleted = true;
929 spin_unlock(&lustre_profile_list_lock);
931 class_put_profile(lprof);
935 EXPORT_SYMBOL(class_del_profile);
937 void class_put_profile(struct lustre_profile *lprof)
939 spin_lock(&lustre_profile_list_lock);
940 if ((--lprof->lp_refs) > 0) {
941 LASSERT(lprof->lp_refs > 0);
942 spin_unlock(&lustre_profile_list_lock);
945 spin_unlock(&lustre_profile_list_lock);
947 /* confirm not a negative number */
948 LASSERT(lprof->lp_refs == 0);
950 /* At least one class_del_profile/profiles must be called
951 * on the target profile or lustre_profile_list will corrupt */
952 LASSERT(lprof->lp_list_deleted);
953 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
954 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
955 if (lprof->lp_md != NULL)
956 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
957 OBD_FREE(lprof, sizeof(*lprof));
959 EXPORT_SYMBOL(class_put_profile);
962 void class_del_profiles(void)
964 struct lustre_profile *lprof, *n;
967 spin_lock(&lustre_profile_list_lock);
968 list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
969 list_del(&lprof->lp_list);
970 lprof->lp_list_deleted = true;
971 spin_unlock(&lustre_profile_list_lock);
973 class_put_profile(lprof);
975 spin_lock(&lustre_profile_list_lock);
977 spin_unlock(&lustre_profile_list_lock);
980 EXPORT_SYMBOL(class_del_profiles);
982 static int class_set_global(char *ptr, int val, struct lustre_cfg *lcfg)
985 if (class_match_param(ptr, PARAM_AT_MIN, NULL) == 0)
987 else if (class_match_param(ptr, PARAM_AT_MAX, NULL) == 0)
989 else if (class_match_param(ptr, PARAM_AT_EXTRA, NULL) == 0)
991 else if (class_match_param(ptr, PARAM_AT_EARLY_MARGIN, NULL) == 0)
992 at_early_margin = val;
993 else if (class_match_param(ptr, PARAM_AT_HISTORY, NULL) == 0)
995 else if (class_match_param(ptr, PARAM_JOBID_VAR, NULL) == 0)
996 strlcpy(obd_jobid_var, lustre_cfg_string(lcfg, 2),
997 JOBSTATS_JOBID_VAR_MAX_LEN + 1);
1001 CDEBUG(D_IOCTL, "global %s = %d\n", ptr, val);
1006 /* We can't call ll_process_config or lquota_process_config directly because
1007 * it lives in a module that must be loaded after this one. */
1008 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
1009 static int (*quota_process_config)(struct lustre_cfg *lcfg) = NULL;
1011 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
1013 client_process_config = cpc;
1015 EXPORT_SYMBOL(lustre_register_client_process_config);
1018 * Rename the proc parameter in \a cfg with a new name \a new_name.
1020 * \param cfg config structure which contains the proc parameter
1021 * \param new_name new name of the proc parameter
1023 * \retval valid-pointer pointer to the newly-allocated config structure
1024 * which contains the renamed proc parameter
1025 * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
1026 * not contain a proc parameter
1027 * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
1029 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
1030 const char *new_name)
1032 struct lustre_cfg_bufs *bufs = NULL;
1033 struct lustre_cfg *new_cfg = NULL;
1035 char *new_param = NULL;
1041 if (cfg == NULL || new_name == NULL)
1042 RETURN(ERR_PTR(-EINVAL));
1044 param = lustre_cfg_string(cfg, 1);
1046 RETURN(ERR_PTR(-EINVAL));
1048 value = strchr(param, '=');
1050 name_len = strlen(param);
1052 name_len = value - param;
1054 new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
1056 OBD_ALLOC(new_param, new_len);
1057 if (new_param == NULL)
1058 RETURN(ERR_PTR(-ENOMEM));
1060 strcpy(new_param, new_name);
1062 strcat(new_param, value);
1064 OBD_ALLOC_PTR(bufs);
1066 OBD_FREE(new_param, new_len);
1067 RETURN(ERR_PTR(-ENOMEM));
1070 lustre_cfg_bufs_reset(bufs, NULL);
1071 lustre_cfg_bufs_init(bufs, cfg);
1072 lustre_cfg_bufs_set_string(bufs, 1, new_param);
1074 new_cfg = lustre_cfg_new(cfg->lcfg_command, bufs);
1075 OBD_FREE(new_param, new_len);
1077 if (new_cfg == NULL)
1078 RETURN(ERR_PTR(-ENOMEM));
1080 new_cfg->lcfg_num = cfg->lcfg_num;
1081 new_cfg->lcfg_flags = cfg->lcfg_flags;
1082 new_cfg->lcfg_nid = cfg->lcfg_nid;
1083 new_cfg->lcfg_nal = cfg->lcfg_nal;
1087 EXPORT_SYMBOL(lustre_cfg_rename);
1089 static int process_param2_config(struct lustre_cfg *lcfg)
1091 char *param = lustre_cfg_string(lcfg, 1);
1092 char *upcall = lustre_cfg_string(lcfg, 2);
1094 [0] = "/usr/sbin/lctl",
1099 struct timeval start;
1104 /* Add upcall processing here. Now only lctl is supported */
1105 if (strcmp(upcall, LCTL_UPCALL) != 0) {
1106 CERROR("Unsupported upcall %s\n", upcall);
1110 do_gettimeofday(&start);
1111 rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1112 do_gettimeofday(&end);
1115 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1116 "time %ldus\n", argv[0], argv[1], argv[2], rc,
1117 cfs_timeval_sub(&end, &start, NULL));
1119 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1120 argv[0], argv[1], argv[2],
1121 cfs_timeval_sub(&end, &start, NULL));
1128 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1130 quota_process_config = qpc;
1132 EXPORT_SYMBOL(lustre_register_quota_process_config);
1134 /** Process configuration commands given in lustre_cfg form.
1135 * These may come from direct calls (e.g. class_manual_cleanup)
1136 * or processing the config llog, or ioctl from lctl.
1138 int class_process_config(struct lustre_cfg *lcfg)
1140 struct obd_device *obd;
1143 LASSERT(lcfg && !IS_ERR(lcfg));
1144 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1146 /* Commands that don't need a device */
1147 switch(lcfg->lcfg_command) {
1149 err = class_attach(lcfg);
1152 case LCFG_ADD_UUID: {
1153 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid %#llx"
1154 " (%s)\n", lustre_cfg_string(lcfg, 1),
1155 lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
1157 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
1160 case LCFG_DEL_UUID: {
1161 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1162 (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
1163 ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1165 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1168 case LCFG_MOUNTOPT: {
1169 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1170 lustre_cfg_string(lcfg, 1),
1171 lustre_cfg_string(lcfg, 2),
1172 lustre_cfg_string(lcfg, 3));
1173 /* set these mount options somewhere, so ll_fill_super
1175 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1176 lustre_cfg_string(lcfg, 1),
1177 LUSTRE_CFG_BUFLEN(lcfg, 2),
1178 lustre_cfg_string(lcfg, 2),
1179 LUSTRE_CFG_BUFLEN(lcfg, 3),
1180 lustre_cfg_string(lcfg, 3));
1183 case LCFG_DEL_MOUNTOPT: {
1184 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1185 lustre_cfg_string(lcfg, 1));
1186 class_del_profile(lustre_cfg_string(lcfg, 1));
1189 case LCFG_SET_TIMEOUT: {
1190 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1191 obd_timeout, lcfg->lcfg_num);
1192 obd_timeout = max(lcfg->lcfg_num, 1U);
1193 obd_timeout_set = 1;
1196 case LCFG_SET_LDLM_TIMEOUT: {
1197 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1198 ldlm_timeout, lcfg->lcfg_num);
1199 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1200 if (ldlm_timeout >= obd_timeout)
1201 ldlm_timeout = max(obd_timeout / 3, 1U);
1202 ldlm_timeout_set = 1;
1205 case LCFG_SET_UPCALL: {
1206 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1207 /* COMPAT_146 Don't fail on old configs */
1211 struct cfg_marker *marker;
1212 marker = lustre_cfg_buf(lcfg, 1);
1213 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1214 marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
1219 /* llite has no obd */
1220 if ((class_match_param(lustre_cfg_string(lcfg, 1),
1221 PARAM_LLITE, NULL) == 0) &&
1222 client_process_config) {
1223 err = (*client_process_config)(lcfg);
1225 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1226 PARAM_SYS, &tmp) == 0)) {
1227 /* Global param settings */
1228 err = class_set_global(tmp, lcfg->lcfg_num, lcfg);
1230 * Client or server should not fail to mount if
1231 * it hits an unknown configuration parameter.
1234 CWARN("Ignoring unknown param %s\n", tmp);
1237 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1238 PARAM_QUOTA, &tmp) == 0) &&
1239 quota_process_config) {
1240 err = (*quota_process_config)(lcfg);
1246 case LCFG_SET_PARAM: {
1247 err = process_param2_config(lcfg);
1251 /* Commands that require a device */
1252 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1254 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1255 CERROR("this lcfg command requires a device name\n");
1257 CERROR("no device for: %s\n",
1258 lustre_cfg_string(lcfg, 0));
1260 GOTO(out, err = -EINVAL);
1263 switch(lcfg->lcfg_command) {
1265 err = class_setup(obd, lcfg);
1269 err = class_detach(obd, lcfg);
1272 case LCFG_CLEANUP: {
1273 err = class_cleanup(obd, lcfg);
1276 case LCFG_ADD_CONN: {
1277 err = class_add_conn(obd, lcfg);
1280 case LCFG_DEL_CONN: {
1281 err = class_del_conn(obd, lcfg);
1284 case LCFG_POOL_NEW: {
1285 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1288 case LCFG_POOL_ADD: {
1289 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1290 lustre_cfg_string(lcfg, 3));
1293 case LCFG_POOL_REM: {
1294 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1295 lustre_cfg_string(lcfg, 3));
1298 case LCFG_POOL_DEL: {
1299 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1303 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1309 if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1310 CWARN("Ignoring error %d on optional command %#x\n", err,
1311 lcfg->lcfg_command);
1316 EXPORT_SYMBOL(class_process_config);
1318 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
1319 struct lustre_cfg *lcfg, void *data)
1321 struct lprocfs_vars *var;
1322 struct file fakefile;
1323 struct seq_file fake_seqfile;
1325 int i, keylen, vallen;
1326 int matched = 0, j = 0;
1331 if (lcfg->lcfg_command != LCFG_PARAM) {
1332 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1336 /* fake a seq file so that var->fops->write can work... */
1337 fakefile.private_data = &fake_seqfile;
1338 fake_seqfile.private = data;
1339 /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1340 or lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1341 or lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
1342 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1343 key = lustre_cfg_buf(lcfg, i);
1344 /* Strip off prefix */
1345 if (class_match_param(key, prefix, &key))
1346 /* If the prefix doesn't match, return error so we
1347 * can pass it down the stack */
1349 sval = strchr(key, '=');
1350 if (!sval || (*(sval + 1) == 0)) {
1351 CERROR("Can't parse param %s (missing '=')\n", key);
1352 /* rc = -EINVAL; continue parsing other params */
1355 keylen = sval - key;
1357 vallen = strlen(sval);
1360 /* Search proc entries */
1361 while (lvars[j].name) {
1363 if (class_match_param(key, var->name, NULL) == 0 &&
1364 keylen == strlen(var->name)) {
1368 if (var->fops && var->fops->write) {
1372 rc = (var->fops->write)(&fakefile, sval,
1381 /* It was upgraded from old MDT/OST device,
1382 * ignore the obsolete "sec_level" parameter. */
1383 if (strncmp("sec_level", key, keylen) == 0)
1386 CERROR("%.*s: %s unknown param %s\n",
1387 (int)strlen(prefix) - 1, prefix,
1388 (char *)lustre_cfg_string(lcfg, 0), key);
1389 /* rc = -EINVAL; continue parsing other params */
1391 } else if (rc < 0) {
1392 CERROR("%s: error writing proc entry '%s': rc = %d\n",
1393 prefix, var->name, rc);
1396 CDEBUG(D_CONFIG, "%s.%.*s: Set parameter %.*s=%s\n",
1397 lustre_cfg_string(lcfg, 0),
1398 (int)strlen(prefix) - 1, prefix,
1399 (int)(sval - key - 1), key, sval);
1409 EXPORT_SYMBOL(class_process_proc_param);
1412 * Supplemental functions for config logs, it allocates lustre_cfg
1413 * buffers plus initialized llog record header at the beginning.
1415 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1417 struct llog_cfg_rec *lcr;
1422 reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1423 reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1424 sizeof(struct llog_rec_tail);
1426 OBD_ALLOC(lcr, reclen);
1430 lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1432 lcr->lcr_hdr.lrh_len = reclen;
1433 lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1437 EXPORT_SYMBOL(lustre_cfg_rec_new);
1439 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1442 OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1445 EXPORT_SYMBOL(lustre_cfg_rec_free);
1447 /** Parse a configuration llog, doing various manipulations on them
1448 * for various reasons, (modifications for compatibility, skip obsolete
1449 * records, change uuids, etc), then class_process_config() resulting
1452 int class_config_llog_handler(const struct lu_env *env,
1453 struct llog_handle *handle,
1454 struct llog_rec_hdr *rec, void *data)
1456 struct config_llog_instance *cfg = data;
1457 int cfg_len = rec->lrh_len;
1458 char *cfg_buf = (char *) (rec + 1);
1462 /* class_config_dump_handler(handle, rec, data); */
1464 switch (rec->lrh_type) {
1466 struct lustre_cfg *lcfg, *lcfg_new;
1467 struct lustre_cfg_bufs bufs;
1468 char *inst_name = NULL;
1470 int inst = 0, swab = 0;
1472 lcfg = (struct lustre_cfg *)cfg_buf;
1473 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1474 lustre_swab_lustre_cfg(lcfg);
1478 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1482 /* Figure out config state info */
1483 if (lcfg->lcfg_command == LCFG_MARKER) {
1484 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1485 lustre_swab_cfg_marker(marker, swab,
1486 LUSTRE_CFG_BUFLEN(lcfg, 1));
1487 CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1488 cfg->cfg_flags, marker->cm_flags);
1489 if (marker->cm_flags & CM_START) {
1490 /* all previous flags off */
1491 cfg->cfg_flags = CFG_F_MARKER;
1492 server_name2index(marker->cm_tgtname,
1493 &cfg->cfg_lwp_idx, NULL);
1494 if (marker->cm_flags & CM_SKIP) {
1495 cfg->cfg_flags |= CFG_F_SKIP;
1496 CDEBUG(D_CONFIG, "SKIP #%d\n",
1498 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1500 lustre_check_exclusion(cfg->cfg_sb,
1501 marker->cm_tgtname))) {
1502 cfg->cfg_flags |= CFG_F_EXCLUDE;
1503 CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1506 } else if (marker->cm_flags & CM_END) {
1510 /* A config command without a start marker before it is
1511 illegal (post 146) */
1512 if (!(cfg->cfg_flags & CFG_F_COMPAT146) &&
1513 !(cfg->cfg_flags & CFG_F_MARKER) &&
1514 (lcfg->lcfg_command != LCFG_MARKER)) {
1515 CWARN("Config not inside markers, ignoring! "
1516 "(inst: %p, uuid: %s, flags: %#x)\n",
1518 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1519 cfg->cfg_flags |= CFG_F_SKIP;
1521 if (cfg->cfg_flags & CFG_F_SKIP) {
1522 CDEBUG(D_CONFIG, "skipping %#x\n",
1525 /* No processing! */
1530 * For interoperability between 1.8 and 2.0,
1531 * rename "mds" obd device type to "mdt".
1534 char *typename = lustre_cfg_string(lcfg, 1);
1535 char *index = lustre_cfg_string(lcfg, 2);
1537 if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1538 strcmp(typename, "mds") == 0)) {
1539 CWARN("For 1.8 interoperability, rename obd "
1540 "type from mds to mdt\n");
1543 if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1544 strcmp(index, "type") == 0)) {
1545 CDEBUG(D_INFO, "For 1.8 interoperability, "
1546 "set this index to '0'\n");
1552 #ifdef HAVE_SERVER_SUPPORT
1553 /* newer MDS replaces LOV/OSC with LOD/OSP */
1555 char *typename = lustre_cfg_string(lcfg, 1);
1557 if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1558 strcmp(typename, LUSTRE_LOV_NAME) == 0) &&
1559 cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1561 "For 2.x interoperability, rename obd "
1562 "type from lov to lod (%s)\n",
1563 s2lsi(cfg->cfg_sb)->lsi_svname);
1564 strcpy(typename, LUSTRE_LOD_NAME);
1566 if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1567 strcmp(typename, LUSTRE_OSC_NAME) == 0) &&
1568 cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1570 "For 2.x interoperability, rename obd "
1571 "type from osc to osp (%s)\n",
1572 s2lsi(cfg->cfg_sb)->lsi_svname);
1573 strcpy(typename, LUSTRE_OSP_NAME);
1576 #endif /* HAVE_SERVER_SUPPORT */
1578 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1579 CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1580 lcfg->lcfg_command);
1581 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1582 /* Add inactive instead */
1583 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1586 lustre_cfg_bufs_reset(&bufs, NULL);
1587 lustre_cfg_bufs_init(&bufs, lcfg);
1589 if (cfg->cfg_instance &&
1590 LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1592 inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1593 sizeof(cfg->cfg_instance) * 2 + 4;
1594 OBD_ALLOC(inst_name, inst_len);
1595 if (inst_name == NULL)
1596 GOTO(out, rc = -ENOMEM);
1597 snprintf(inst_name, inst_len, "%s-%p",
1598 lustre_cfg_string(lcfg, 0),
1600 lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1601 CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1602 lcfg->lcfg_command, inst_name);
1605 /* we override the llog's uuid for clients, to insure they
1607 if (cfg->cfg_instance != NULL &&
1608 lcfg->lcfg_command == LCFG_ATTACH) {
1609 lustre_cfg_bufs_set_string(&bufs, 2,
1610 cfg->cfg_uuid.uuid);
1613 * sptlrpc config record, we expect 2 data segments:
1614 * [0]: fs_name/target_name,
1616 * moving them to index [1] and [2], and insert MGC's
1617 * obdname at index [0].
1619 if (cfg->cfg_instance == NULL &&
1620 lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1621 lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1622 bufs.lcfg_buflen[1]);
1623 lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1624 bufs.lcfg_buflen[0]);
1625 lustre_cfg_bufs_set_string(&bufs, 0,
1629 /* Add net info to setup command
1630 * if given on command line.
1631 * So config log will be:
1635 * [3]: inactive-on-startup
1636 * [4]: restrictive net
1638 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1639 !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1640 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1641 char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1643 if (lcfg->lcfg_command == LCFG_SETUP &&
1644 lcfg->lcfg_bufcount != 2 && nidnet) {
1645 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1646 "command for client %s\n", nidnet,
1647 lustre_cfg_string(lcfg, 0));
1648 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1652 /* Skip add_conn command if uuid is
1653 * not on restricted net */
1654 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1655 !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1656 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1657 char *uuid_str = lustre_cfg_string(lcfg, 1);
1659 if (lcfg->lcfg_command == LCFG_ADD_CONN &&
1660 lsi->lsi_lmd->lmd_nidnet &&
1661 LNET_NIDNET(libcfs_str2nid(uuid_str)) !=
1662 libcfs_str2net(lsi->lsi_lmd->lmd_nidnet)) {
1663 CDEBUG(D_CONFIG, "skipping add_conn for %s\n",
1666 /* No processing! */
1671 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1672 if (lcfg_new == NULL)
1673 GOTO(out, rc = -ENOMEM);
1675 lcfg_new->lcfg_num = lcfg->lcfg_num;
1676 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1678 /* XXX Hack to try to remain binary compatible with
1679 * pre-newconfig logs */
1680 if (lcfg->lcfg_nal != 0 && /* pre-newconfig log? */
1681 (lcfg->lcfg_nid >> 32) == 0) {
1682 __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1684 lcfg_new->lcfg_nid =
1685 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1686 CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1687 lcfg->lcfg_nal, addr,
1688 libcfs_nid2str(lcfg_new->lcfg_nid));
1690 lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1693 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1695 rc = class_process_config(lcfg_new);
1696 lustre_cfg_free(lcfg_new);
1699 OBD_FREE(inst_name, inst_len);
1703 CERROR("Unknown llog record type %#x encountered\n",
1709 CERROR("%s: cfg command failed: rc = %d\n",
1710 handle->lgh_ctxt->loc_obd->obd_name, rc);
1711 class_config_dump_handler(NULL, handle, rec, data);
1715 EXPORT_SYMBOL(class_config_llog_handler);
1717 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
1718 char *name, struct config_llog_instance *cfg)
1720 struct llog_process_cat_data cd = {
1721 .lpcd_first_idx = 0,
1723 struct llog_handle *llh;
1728 CDEBUG(D_INFO, "looking up llog %s\n", name);
1729 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1733 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1735 GOTO(parse_out, rc);
1737 /* continue processing from where we last stopped to end-of-log */
1739 cd.lpcd_first_idx = cfg->cfg_last_idx;
1740 callback = cfg->cfg_callback;
1741 LASSERT(callback != NULL);
1743 callback = class_config_llog_handler;
1746 cd.lpcd_last_idx = 0;
1748 rc = llog_process(env, llh, callback, cfg, &cd);
1750 CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1751 cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1753 cfg->cfg_last_idx = cd.lpcd_last_idx;
1756 llog_close(env, llh);
1759 EXPORT_SYMBOL(class_config_parse_llog);
1761 static struct lcfg_type_data {
1765 } lcfg_data_table[] = {
1766 { LCFG_ATTACH, "attach", { "type", "UUID", "3", "4" } },
1767 { LCFG_DETACH, "detach", { "1", "2", "3", "4" } },
1768 { LCFG_SETUP, "setup", { "UUID", "node", "options", "failout" } },
1769 { LCFG_CLEANUP, "cleanup", { "1", "2", "3", "4" } },
1770 { LCFG_ADD_UUID, "add_uuid", { "node", "2", "3", "4" } },
1771 { LCFG_DEL_UUID, "del_uuid", { "1", "2", "3", "4" } },
1772 { LCFG_MOUNTOPT, "new_profile", { "name", "lov", "lmv", "4" } },
1773 { LCFG_DEL_MOUNTOPT, "del_mountopt", { "1", "2", "3", "4" } , },
1774 { LCFG_SET_TIMEOUT, "set_timeout", { "parameter", "2", "3", "4" } },
1775 { LCFG_SET_UPCALL, "set_upcall", { "1", "2", "3", "4" } },
1776 { LCFG_ADD_CONN, "add_conn", { "node", "2", "3", "4" } },
1777 { LCFG_DEL_CONN, "del_conn", { "1", "2", "3", "4" } },
1778 { LCFG_LOV_ADD_OBD, "add_osc", { "ost", "index", "gen", "UUID" } },
1779 { LCFG_LOV_DEL_OBD, "del_osc", { "1", "2", "3", "4" } },
1780 { LCFG_PARAM, "set_param", { "parameter", "value", "3", "4" } },
1781 { LCFG_MARKER, "marker", { "1", "2", "3", "4" } },
1782 { LCFG_LOG_START, "log_start", { "1", "2", "3", "4" } },
1783 { LCFG_LOG_END, "log_end", { "1", "2", "3", "4" } },
1784 { LCFG_LOV_ADD_INA, "add_osc_inactive", { "1", "2", "3", "4" } },
1785 { LCFG_ADD_MDC, "add_mdc", { "mdt", "index", "gen", "UUID" } },
1786 { LCFG_DEL_MDC, "del_mdc", { "1", "2", "3", "4" } },
1787 { LCFG_SPTLRPC_CONF, "security", { "parameter", "2", "3", "4" } },
1788 { LCFG_POOL_NEW, "new_pool", { "fsname", "pool", "3", "4" } },
1789 { LCFG_POOL_ADD, "add_pool", { "fsname", "pool", "ost", "4" } },
1790 { LCFG_POOL_REM, "remove_pool", { "fsname", "pool", "ost", "4" } },
1791 { LCFG_POOL_DEL, "del_pool", { "fsname", "pool", "3", "4" } },
1792 { LCFG_SET_LDLM_TIMEOUT, "set_ldlm_timeout",
1793 { "parameter", "2", "3", "4" } },
1794 { 0, NULL, { NULL, NULL, NULL, NULL } }
1797 static struct lcfg_type_data *lcfg_cmd2data(__u32 cmd)
1801 while (lcfg_data_table[i].ltd_type != 0) {
1802 if (lcfg_data_table[i].ltd_type == cmd)
1803 return &lcfg_data_table[i];
1810 * parse config record and output dump in supplied buffer.
1811 * This is separated from class_config_dump_handler() to use
1812 * for ioctl needs as well
1815 * - { event: attach, device: lustrewt-clilov, type: lov, UUID:
1816 * lustrewt-clilov_UUID }
1818 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size)
1820 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1822 char *end = buf + size;
1824 struct lcfg_type_data *ldata;
1826 LASSERT(rec->lrh_type == OBD_CFG_REC);
1827 rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1831 ldata = lcfg_cmd2data(lcfg->lcfg_command);
1835 if (lcfg->lcfg_command == LCFG_MARKER)
1838 /* form YAML entity */
1839 ptr += snprintf(ptr, end - ptr, "- { event: %s", ldata->ltd_name);
1841 if (lcfg->lcfg_flags)
1842 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
1845 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
1847 if (lcfg->lcfg_nid) {
1848 char nidstr[LNET_NIDSTR_SIZE];
1850 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1851 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
1852 nidstr, lcfg->lcfg_nid);
1855 if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0)
1856 ptr += snprintf(ptr, end - ptr, ", device: %s",
1857 lustre_cfg_string(lcfg, 0));
1859 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1860 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0)
1861 ptr += snprintf(ptr, end - ptr, ", %s: %s",
1862 ldata->ltd_bufs[i - 1],
1863 lustre_cfg_string(lcfg, i));
1866 ptr += snprintf(ptr, end - ptr, " }\n");
1867 /* return consumed bytes */
1873 * parse config record and output dump in supplied buffer.
1874 * This is separated from class_config_dump_handler() to use
1875 * for ioctl needs as well
1877 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
1879 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
1881 char *end = buf + size;
1886 LASSERT(rec->lrh_type == OBD_CFG_REC);
1887 rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
1891 ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
1892 if (lcfg->lcfg_flags)
1893 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1897 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
1899 if (lcfg->lcfg_nid) {
1900 char nidstr[LNET_NIDSTR_SIZE];
1902 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
1903 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx)\n ",
1904 nidstr, lcfg->lcfg_nid);
1907 if (lcfg->lcfg_command == LCFG_MARKER) {
1908 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1910 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1911 marker->cm_step, marker->cm_flags,
1912 marker->cm_tgtname, marker->cm_comment);
1916 for (i = 0; i < lcfg->lcfg_bufcount; i++) {
1917 ptr += snprintf(ptr, end-ptr, "%d:%s ", i,
1918 lustre_cfg_string(lcfg, i));
1921 ptr += snprintf(ptr, end - ptr, "\n");
1922 /* return consumed bytes */
1927 int class_config_dump_handler(const struct lu_env *env,
1928 struct llog_handle *handle,
1929 struct llog_rec_hdr *rec, void *data)
1936 OBD_ALLOC(outstr, 256);
1940 if (rec->lrh_type == OBD_CFG_REC) {
1941 class_config_parse_rec(rec, outstr, 256);
1942 LCONSOLE(D_WARNING, " %s\n", outstr);
1944 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1948 OBD_FREE(outstr, 256);
1952 /** Call class_cleanup and class_detach.
1953 * "Manual" only in the sense that we're faking lcfg commands.
1955 int class_manual_cleanup(struct obd_device *obd)
1958 struct lustre_cfg *lcfg;
1959 struct lustre_cfg_bufs bufs;
1964 CERROR("empty cleanup\n");
1973 CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1974 obd->obd_name, flags);
1976 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1977 lustre_cfg_bufs_set_string(&bufs, 1, flags);
1978 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1982 rc = class_process_config(lcfg);
1984 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1988 /* the lcfg is almost the same for both ops */
1989 lcfg->lcfg_command = LCFG_DETACH;
1990 rc = class_process_config(lcfg);
1992 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1994 lustre_cfg_free(lcfg);
1997 EXPORT_SYMBOL(class_manual_cleanup);
2000 * uuid<->export lustre hash operations
2004 uuid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2006 return cfs_hash_djb2_hash(((struct obd_uuid *)key)->uuid,
2007 sizeof(((struct obd_uuid *)key)->uuid), mask);
2011 uuid_key(struct hlist_node *hnode)
2013 struct obd_export *exp;
2015 exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2017 return &exp->exp_client_uuid;
2021 * NOTE: It is impossible to find an export that is in failed
2022 * state with this function
2025 uuid_keycmp(const void *key, struct hlist_node *hnode)
2027 struct obd_export *exp;
2030 exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2032 return obd_uuid_equals(key, &exp->exp_client_uuid) &&
2037 uuid_export_object(struct hlist_node *hnode)
2039 return hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2043 uuid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2045 struct obd_export *exp;
2047 exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2048 class_export_get(exp);
2052 uuid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2054 struct obd_export *exp;
2056 exp = hlist_entry(hnode, struct obd_export, exp_uuid_hash);
2057 class_export_put(exp);
2060 static struct cfs_hash_ops uuid_hash_ops = {
2061 .hs_hash = uuid_hash,
2063 .hs_keycmp = uuid_keycmp,
2064 .hs_object = uuid_export_object,
2065 .hs_get = uuid_export_get,
2066 .hs_put_locked = uuid_export_put_locked,
2071 * nid<->export hash operations
2075 nid_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2077 return cfs_hash_djb2_hash(key, sizeof(lnet_nid_t), mask);
2081 nid_key(struct hlist_node *hnode)
2083 struct obd_export *exp;
2085 exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2087 RETURN(&exp->exp_connection->c_peer.nid);
2091 * NOTE: It is impossible to find an export that is in failed
2092 * state with this function
2095 nid_kepcmp(const void *key, struct hlist_node *hnode)
2097 struct obd_export *exp;
2100 exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2102 RETURN(exp->exp_connection->c_peer.nid == *(lnet_nid_t *)key &&
2107 nid_export_object(struct hlist_node *hnode)
2109 return hlist_entry(hnode, struct obd_export, exp_nid_hash);
2113 nid_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2115 struct obd_export *exp;
2117 exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2118 class_export_get(exp);
2122 nid_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2124 struct obd_export *exp;
2126 exp = hlist_entry(hnode, struct obd_export, exp_nid_hash);
2127 class_export_put(exp);
2130 static struct cfs_hash_ops nid_hash_ops = {
2131 .hs_hash = nid_hash,
2133 .hs_keycmp = nid_kepcmp,
2134 .hs_object = nid_export_object,
2135 .hs_get = nid_export_get,
2136 .hs_put_locked = nid_export_put_locked,
2141 * nid<->nidstats hash operations
2145 nidstats_key(struct hlist_node *hnode)
2147 struct nid_stat *ns;
2149 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2155 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2157 return *(lnet_nid_t *)nidstats_key(hnode) == *(lnet_nid_t *)key;
2161 nidstats_object(struct hlist_node *hnode)
2163 return hlist_entry(hnode, struct nid_stat, nid_hash);
2167 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2169 struct nid_stat *ns;
2171 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2176 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2178 struct nid_stat *ns;
2180 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2184 static struct cfs_hash_ops nid_stat_hash_ops = {
2185 .hs_hash = nid_hash,
2186 .hs_key = nidstats_key,
2187 .hs_keycmp = nidstats_keycmp,
2188 .hs_object = nidstats_object,
2189 .hs_get = nidstats_get,
2190 .hs_put_locked = nidstats_put_locked,
2195 * client_generation<->export hash operations
2199 gen_hash(struct cfs_hash *hs, const void *key, unsigned mask)
2201 return cfs_hash_djb2_hash(key, sizeof(__u32), mask);
2205 gen_key(struct hlist_node *hnode)
2207 struct obd_export *exp;
2209 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2211 RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2215 * NOTE: It is impossible to find an export that is in failed
2216 * state with this function
2219 gen_kepcmp(const void *key, struct hlist_node *hnode)
2221 struct obd_export *exp;
2224 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2226 RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2231 gen_export_object(struct hlist_node *hnode)
2233 return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2237 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2239 struct obd_export *exp;
2241 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2242 class_export_get(exp);
2246 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2248 struct obd_export *exp;
2250 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2251 class_export_put(exp);
2254 static struct cfs_hash_ops gen_hash_ops = {
2255 .hs_hash = gen_hash,
2257 .hs_keycmp = gen_kepcmp,
2258 .hs_object = gen_export_object,
2259 .hs_get = gen_export_get,
2260 .hs_put_locked = gen_export_put_locked,