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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/obdclass/obd_config.c
36 #define DEBUG_SUBSYSTEM S_CLASS
38 #include <linux/kobject.h>
39 #include <linux/string.h>
41 #include <llog_swab.h>
42 #include <lprocfs_status.h>
43 #include <lustre_disk.h>
44 #include <uapi/linux/lustre/lustre_ioctl.h>
45 #include <lustre_log.h>
46 #include <uapi/linux/lustre/lustre_param.h>
47 #include <obd_class.h>
49 #include "llog_internal.h"
51 #ifdef HAVE_SERVER_SUPPORT
52 static struct cfs_hash_ops nid_stat_hash_ops;
53 static struct cfs_hash_ops gen_hash_ops;
54 #endif /* HAVE_SERVER_SUPPORT */
57 * uuid<->export lustre hash operations
60 * NOTE: It is impossible to find an export that is in failed
61 * state with this function
64 uuid_keycmp(struct rhashtable_compare_arg *arg, const void *obj)
66 const struct obd_uuid *uuid = arg->key;
67 const struct obd_export *exp = obj;
69 if (obd_uuid_equals(uuid, &exp->exp_client_uuid) &&
76 obd_export_exit(void *vexport, void *data)
78 struct obd_export *exp = vexport;
80 class_export_put(exp);
83 static const struct rhashtable_params uuid_hash_params = {
84 .key_len = sizeof(struct obd_uuid),
85 .key_offset = offsetof(struct obd_export, exp_client_uuid),
86 .head_offset = offsetof(struct obd_export, exp_uuid_hash),
87 .obj_cmpfn = uuid_keycmp,
88 .automatic_shrinking = true,
91 int obd_uuid_add(struct obd_device *obd, struct obd_export *export)
95 class_export_get(export);
97 rc = rhashtable_lookup_insert_fast(&obd->obd_uuid_hash,
98 &export->exp_uuid_hash,
101 class_export_put(export);
103 /* map obscure error codes to -ENOMEM */
113 EXPORT_SYMBOL(obd_uuid_add);
115 void obd_uuid_del(struct obd_device *obd, struct obd_export *export)
120 rc = rhashtable_remove_fast(&obd->obd_uuid_hash,
121 &export->exp_uuid_hash,
124 class_export_put(export);
127 EXPORT_SYMBOL(obd_uuid_del);
129 #ifdef HAVE_SERVER_SUPPORT
130 /* obd_uuid_lookup() is used only server side by target_handle_connect(),
131 * mdt_hsm_agent_send(), and obd_export_evict_by_uuid().
133 struct obd_export *obd_uuid_lookup(struct obd_device *obd,
134 struct obd_uuid *uuid)
136 struct obd_export *export = NULL;
139 export = rhashtable_lookup_fast(&obd->obd_uuid_hash, uuid,
141 if (export && !refcount_inc_not_zero(&export->exp_handle.h_ref))
147 EXPORT_SYMBOL(obd_uuid_lookup);
150 * nid<->export hash operations
152 static u32 nid_keyhash(const void *data, u32 key_len, u32 seed)
154 const struct obd_export *exp = data;
157 if (!exp->exp_connection)
160 key = &exp->exp_connection->c_peer.nid;
161 return jhash2(key, key_len / sizeof(u32), seed);
165 * NOTE: It is impossible to find an export that is in failed
166 * state with this function
169 nid_keycmp(struct rhashtable_compare_arg *arg, const void *obj)
171 const struct lnet_nid *nid = arg->key;
172 const struct obd_export *exp = obj;
174 if (nid_same(&exp->exp_connection->c_peer.nid, nid))
181 nid_export_exit(void *vexport, void *data)
183 struct obd_export *exp = vexport;
185 class_export_put(exp);
188 static const struct rhashtable_params nid_hash_params = {
189 .key_len = sizeof(struct lnet_nid),
190 .head_offset = offsetof(struct obd_export, exp_nid_hash),
191 .obj_hashfn = nid_keyhash,
192 .obj_cmpfn = nid_keycmp,
193 .automatic_shrinking = true,
196 int obd_nid_add(struct obd_device *obd, struct obd_export *exp)
200 if (exp == exp->exp_obd->obd_self_export || exp->exp_hashed)
203 class_export_get(exp);
204 rc = rhltable_insert_key(&obd->obd_nid_hash,
205 &exp->exp_connection->c_peer.nid,
209 class_export_put(exp);
210 /* map obscure error codes to -ENOMEM */
217 EXPORT_SYMBOL(obd_nid_add);
219 void obd_nid_del(struct obd_device *obd, struct obd_export *exp)
223 if (exp == exp->exp_obd->obd_self_export || !exp->exp_hashed)
226 rc = rhltable_remove(&obd->obd_nid_hash, &exp->exp_nid_hash,
229 class_export_put(exp);
233 EXPORT_SYMBOL(obd_nid_del);
235 int obd_nid_export_for_each(struct obd_device *obd, struct lnet_nid *nid,
236 int cb(struct obd_export *exp, void *data),
239 struct rhlist_head *exports, *tmp;
240 struct obd_export *exp;
244 exports = rhltable_lookup(&obd->obd_nid_hash, nid, nid_hash_params);
250 rhl_for_each_entry_rcu(exp, tmp, exports, exp_nid_hash) {
251 if (!exp->exp_failed && cb(exp, data))
259 EXPORT_SYMBOL(obd_nid_export_for_each);
260 #endif /* HAVE_SERVER_SUPPORT */
262 /*********** string parsing utils *********/
264 /* returns 0 if we find this key in the buffer, else 1 */
265 int class_find_param(char *buf, char *key, char **valp)
272 ptr = strstr(buf, key);
277 *valp = ptr + strlen(key);
281 EXPORT_SYMBOL(class_find_param);
284 * Check whether the proc parameter \a param is an old parameter or not from
285 * the array \a ptr which contains the mapping from old parameters to new ones.
286 * If it's an old one, then return the pointer to the cfg_interop_param struc-
287 * ture which contains both the old and new parameters.
289 * \param param proc parameter
290 * \param ptr an array which contains the mapping from
291 * old parameters to new ones
293 * \retval valid-pointer pointer to the cfg_interop_param structure
294 * which contains the old and new parameters
295 * \retval NULL \a param or \a ptr is NULL,
296 * or \a param is not an old parameter
298 struct cfg_interop_param *class_find_old_param(const char *param,
299 struct cfg_interop_param *ptr)
307 value = strchr(param, '=');
309 name_len = value - param;
311 name_len = strlen(param);
313 while (ptr->old_param) {
314 if (strncmp(param, ptr->old_param, name_len) == 0 &&
315 name_len == strlen(ptr->old_param))
322 EXPORT_SYMBOL(class_find_old_param);
325 * Finds a parameter in \a params and copies it to \a copy.
327 * Leading spaces are skipped. Next space or end of string is the
328 * parameter terminator with the exception that spaces inside single or double
329 * quotes get included into a parameter. The parameter is copied into \a copy
330 * which has to be allocated big enough by a caller, quotes are stripped in
331 * the copy and the copy is terminated by 0.
333 * On return \a params is set to next parameter or to NULL if last
334 * parameter is returned.
336 * \retval 0 if parameter is returned in \a copy
337 * \retval 1 otherwise
338 * \retval -EINVAL if unbalanced quota is found
340 int class_get_next_param(char **params, char *copy)
355 q1 = strpbrk(str, " '\"");
358 memcpy(copy, str, len);
365 memcpy(copy, str, len);
371 memcpy(copy, str, len);
374 /* search for the matching closing quote */
376 q2 = strchr(str, *q1);
378 CERROR("Unbalanced quota in parameters: \"%s\"\n",
383 memcpy(copy, str, len);
389 EXPORT_SYMBOL(class_get_next_param);
392 * returns 0 if this is the first key in the buffer, else 1.
393 * valp points to first char after key.
395 int class_match_param(char *buf, const char *key, char **valp)
400 if (memcmp(buf, key, strlen(key)) != 0)
404 *valp = buf + strlen(key);
408 EXPORT_SYMBOL(class_match_param);
410 static int parse_nid(char *buf, void *value, int quiet)
412 struct lnet_nid *nid = value;
414 if (libcfs_strnid(nid, buf) == 0)
418 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
422 static int parse_net(char *buf, void *value)
424 __u32 *net = (__u32 *)value;
426 *net = libcfs_str2net(buf);
427 CDEBUG(D_INFO, "Net %s\n", libcfs_net2str(*net));
440 * endh is set to next separator
442 static int class_parse_value(char *buf, int opc, void *value, char **endh,
452 while (*buf == ',' || *buf == ':')
454 if (*buf == ' ' || *buf == '/' || *buf == '\0')
457 /* NID separators or end of NIDs. Large NIDs can contain ':' so
458 * skip ahead to @ and then look for one of the delimiters.
460 endp = strchr(buf, '@');
464 endp = strpbrk(endp, ",: /");
466 endp = buf + strlen(buf);
473 case CLASS_PARSE_NID:
474 rc = parse_nid(buf, value, quiet);
476 case CLASS_PARSE_NET:
477 rc = parse_net(buf, value);
488 int class_parse_nid(char *buf, struct lnet_nid *nid, char **endh)
490 return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 0);
492 EXPORT_SYMBOL(class_parse_nid);
494 int class_parse_nid_quiet(char *buf, struct lnet_nid *nid, char **endh)
496 return class_parse_value(buf, CLASS_PARSE_NID, (void *)nid, endh, 1);
498 EXPORT_SYMBOL(class_parse_nid_quiet);
500 int class_parse_net(char *buf, __u32 *net, char **endh)
502 return class_parse_value(buf, CLASS_PARSE_NET, (void *)net, endh, 0);
506 * 1 param contains key and match
507 * 0 param contains key and not match
508 * -1 param does not contain key
510 int class_match_nid(char *buf, char *key, struct lnet_nid *nid)
515 while (class_find_param(buf, key, &buf) == 0) {
517 * please restrict to the NIDs pertaining to
520 while (class_parse_nid(buf, &tmp, &buf) == 0) {
521 if (nid_same(&tmp, nid))
528 EXPORT_SYMBOL(class_match_nid);
530 int class_match_net(char *buf, char *key, __u32 net)
535 while (class_find_param(buf, key, &buf) == 0) {
537 * please restrict to the NIDs pertaining to
538 * the specified networks
540 while (class_parse_net(buf, &tmp, &buf) == 0) {
548 EXPORT_SYMBOL(class_match_net);
550 char *lustre_cfg_string(struct lustre_cfg *lcfg, u32 index)
554 if (!lcfg->lcfg_buflens[index])
557 s = lustre_cfg_buf(lcfg, index);
562 * make sure it's NULL terminated, even if this kills a char
563 * of data. Try to use the padding first though.
565 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
566 size_t last = ALIGN(lcfg->lcfg_buflens[index], 8) - 1;
569 /* Use the smaller value */
570 if (last > lcfg->lcfg_buflens[index])
571 last = lcfg->lcfg_buflens[index];
576 CWARN("Truncated buf %d to '%s' (lost '%c'...)\n",
582 EXPORT_SYMBOL(lustre_cfg_string);
584 /********************** class fns **********************/
587 * Create a new OBD device and set the type, name and uuid. If successful,
588 * the new device can be accessed by either name or uuid.
590 int class_attach(struct lustre_cfg *lcfg)
592 struct obd_export *exp;
593 struct obd_device *obd = NULL;
594 char *typename, *name, *uuid;
599 if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
600 CERROR("No type passed!\n");
603 typename = lustre_cfg_string(lcfg, 1);
605 if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
606 CERROR("No name passed!\n");
609 name = lustre_cfg_string(lcfg, 0);
610 if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
611 CERROR("No UUID passed!\n");
615 uuid = lustre_cfg_string(lcfg, 2);
617 if (len >= sizeof(obd->obd_uuid)) {
618 CERROR("%s: uuid must be < %d bytes long\n",
619 name, (int)sizeof(obd->obd_uuid));
623 obd = class_newdev(typename, name, uuid);
624 if (IS_ERR(obd)) { /* Already exists or out of obds */
626 CERROR("Cannot create device %s of type %s : %d\n",
630 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
631 "obd %p obd_magic %08X != %08X\n",
632 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
633 LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0,
634 "%p obd_name %s != %s\n", obd, obd->obd_name, name);
636 exp = class_new_export_self(obd, &obd->obd_uuid);
643 obd->obd_self_export = exp;
644 list_del_init(&exp->exp_obd_chain_timed);
645 class_export_put(exp);
647 rc = class_register_device(obd);
649 class_decref(obd, "newdev", obd);
653 obd->obd_attached = 1;
654 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
655 obd->obd_minor, typename, kref_read(&obd->obd_refcount));
659 EXPORT_SYMBOL(class_attach);
662 * Create hashes, self-export, and call type-specific setup.
663 * Setup is effectively the "start this obd" call.
665 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
671 LASSERT(obd != NULL);
672 LASSERTF(obd == class_num2obd(obd->obd_minor),
673 "obd %p != obd_devs[%d] %p\n",
674 obd, obd->obd_minor, class_num2obd(obd->obd_minor));
675 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
676 "obd %p obd_magic %08x != %08x\n",
677 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
679 /* have we attached a type to this device? */
680 if (!obd->obd_attached) {
681 CERROR("Device %d not attached\n", obd->obd_minor);
685 if (obd->obd_set_up) {
686 CERROR("Device %d already setup (type %s)\n",
687 obd->obd_minor, obd->obd_type->typ_name);
691 /* is someone else setting us up right now? (attach inits spinlock) */
692 spin_lock(&obd->obd_dev_lock);
693 if (obd->obd_starting) {
694 spin_unlock(&obd->obd_dev_lock);
695 CERROR("Device %d setup in progress (type %s)\n",
696 obd->obd_minor, obd->obd_type->typ_name);
700 * just leave this on forever. I can't use obd_set_up here because
701 * other fns check that status, and we're not actually set up yet.
703 obd->obd_starting = 1;
704 obd->obd_nid_stats_hash = NULL;
705 obd->obd_gen_hash = NULL;
706 spin_unlock(&obd->obd_dev_lock);
708 /* create an uuid-export lustre hash */
709 err = rhashtable_init(&obd->obd_uuid_hash, &uuid_hash_params);
711 GOTO(err_starting, err);
713 #ifdef HAVE_SERVER_SUPPORT
714 /* create a nid-export lustre hash */
715 err = rhltable_init(&obd->obd_nid_hash, &nid_hash_params);
717 GOTO(err_uuid_hash, err = -ENOMEM);
719 /* create a nid-stats lustre hash */
720 obd->obd_nid_stats_hash = cfs_hash_create("NID_STATS",
721 HASH_NID_STATS_CUR_BITS,
722 HASH_NID_STATS_MAX_BITS,
723 HASH_NID_STATS_BKT_BITS, 0,
728 if (!obd->obd_nid_stats_hash)
729 GOTO(err_nid_hash, err = -ENOMEM);
731 /* create a client_generation-export lustre hash */
732 obd->obd_gen_hash = cfs_hash_create("UUID_HASH",
735 HASH_GEN_BKT_BITS, 0,
738 &gen_hash_ops, CFS_HASH_DEFAULT);
739 if (!obd->obd_gen_hash)
740 GOTO(err_nid_stats_hash, err = -ENOMEM);
741 #endif /* HAVE_SERVER_SUPPORT */
743 err = obd_setup(obd, lcfg);
745 #ifdef HAVE_SERVER_SUPPORT
746 GOTO(err_gen_hash, err);
748 GOTO(err_uuid_hash, err);
749 #endif /* ! HAVE_SERVER_SUPPORT */
753 spin_lock(&obd->obd_dev_lock);
754 /* cleanup drops this */
755 class_incref(obd, "setup", obd);
756 spin_unlock(&obd->obd_dev_lock);
758 CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
759 obd->obd_name, obd->obd_uuid.uuid);
763 #ifdef HAVE_SERVER_SUPPORT
765 if (obd->obd_gen_hash) {
766 cfs_hash_putref(obd->obd_gen_hash);
767 obd->obd_gen_hash = NULL;
770 if (obd->obd_nid_stats_hash) {
771 cfs_hash_putref(obd->obd_nid_stats_hash);
772 obd->obd_nid_stats_hash = NULL;
775 rhltable_destroy(&obd->obd_nid_hash);
776 #endif /* HAVE_SERVER_SUPPORT */
778 rhashtable_destroy(&obd->obd_uuid_hash);
780 obd->obd_starting = 0;
781 CERROR("setup %s failed (%d)\n", obd->obd_name, err);
784 EXPORT_SYMBOL(class_setup);
787 * We have finished using this OBD and are ready to destroy it.
788 * There can be no more references to this obd.
790 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
794 if (obd->obd_set_up) {
795 CERROR("OBD device %d still set up\n", obd->obd_minor);
799 spin_lock(&obd->obd_dev_lock);
800 if (!obd->obd_attached) {
801 spin_unlock(&obd->obd_dev_lock);
802 CERROR("OBD device %d not attached\n", obd->obd_minor);
805 obd->obd_attached = 0;
807 /* cleanup in progress. we don't like to find this device after now */
808 class_unregister_device(obd);
809 spin_unlock(&obd->obd_dev_lock);
811 CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
812 obd->obd_name, obd->obd_uuid.uuid);
814 class_decref(obd, "newdev", obd);
818 EXPORT_SYMBOL(class_detach);
821 * Start shutting down the OBD. There may be in-progess ops when
822 * this is called. We tell them to start shutting down with a call
823 * to class_disconnect_exports().
825 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
831 CFS_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
833 if (!obd->obd_set_up) {
834 CERROR("Device %d not setup\n", obd->obd_minor);
838 spin_lock(&obd->obd_dev_lock);
839 if (obd->obd_stopping) {
840 spin_unlock(&obd->obd_dev_lock);
841 CERROR("OBD %d already stopping\n", obd->obd_minor);
844 /* Leave this on forever */
845 obd->obd_stopping = 1;
846 spin_unlock(&obd->obd_dev_lock);
848 /* wait for already-arrived-connections to finish. */
849 while (obd->obd_conn_inprogress > 0)
853 if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
854 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
860 LCONSOLE(D_WARNING, "Failing over %s\n",
862 spin_lock(&obd->obd_dev_lock);
864 #ifdef HAVE_SERVER_SUPPORT
865 obd->obd_no_transno = 1;
867 obd->obd_no_recov = 1;
868 spin_unlock(&obd->obd_dev_lock);
869 if (obd->obd_type->typ_dt_ops->o_iocontrol) {
870 obd_iocontrol(OBD_IOC_SYNC,
871 obd->obd_self_export,
876 CERROR("Unrecognised flag '%c'\n", *flag);
880 LASSERT(obd->obd_self_export);
882 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d/%d\n",
883 obd->obd_name, obd->obd_num_exports,
884 kref_read(&obd->obd_refcount) - 2);
885 dump_exports(obd, 0, D_HA);
886 class_disconnect_exports(obd);
888 /* Precleanup, we must make sure all exports get destroyed. */
889 err = obd_precleanup(obd);
891 CERROR("Precleanup %s returned %d\n",
894 /* destroy an uuid-export hash body */
895 rhashtable_free_and_destroy(&obd->obd_uuid_hash, obd_export_exit,
897 #ifdef HAVE_SERVER_SUPPORT
898 /* destroy a nid-export hash body */
899 rhltable_free_and_destroy(&obd->obd_nid_hash, nid_export_exit, NULL);
901 /* destroy a nid-stats hash body */
902 if (obd->obd_nid_stats_hash) {
903 cfs_hash_putref(obd->obd_nid_stats_hash);
904 obd->obd_nid_stats_hash = NULL;
907 /* destroy a client_generation-export hash body */
908 if (obd->obd_gen_hash) {
909 cfs_hash_putref(obd->obd_gen_hash);
910 obd->obd_gen_hash = NULL;
912 #endif /* HAVE_SERVER_SUPPORT */
913 class_decref(obd, "setup", obd);
919 struct obd_device *class_incref(struct obd_device *obd,
923 lu_ref_add_atomic(&obd->obd_reference, scope, source);
924 kref_get(&obd->obd_refcount);
925 CDEBUG(D_INFO, "incref %s (%p) now %d - %s\n", obd->obd_name, obd,
926 kref_read(&obd->obd_refcount), scope);
930 EXPORT_SYMBOL(class_incref);
932 static void class_decref_free(struct kref *kref)
934 struct obd_device *obd;
935 struct obd_export *exp;
937 obd = container_of(kref, struct obd_device, obd_refcount);
938 LASSERT(!obd->obd_attached);
940 * All exports have been destroyed; there should
941 * be no more in-progress ops by this point.
943 exp = obd->obd_self_export;
946 exp->exp_flags |= exp_flags_from_obd(obd);
947 class_unlink_export(exp);
951 void class_decref(struct obd_device *obd, const char *scope, const void *source)
953 CDEBUG(D_INFO, "Decref %s (%p) now %d - %s\n", obd->obd_name, obd,
954 kref_read(&obd->obd_refcount), scope);
955 LASSERT(obd->obd_num_exports >= 0);
956 kref_put(&obd->obd_refcount, class_decref_free);
957 lu_ref_del(&obd->obd_reference, scope, source);
959 EXPORT_SYMBOL(class_decref);
962 * Add a failover NID location.
963 * Client OBD types contact server OBD types using this NID list.
965 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
967 struct obd_import *imp;
968 struct obd_uuid uuid;
973 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
974 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
975 CERROR("invalid conn_uuid\n");
978 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
979 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
980 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) &&
981 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) &&
982 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
983 CERROR("can't add connection on non-client dev\n");
987 imp = obd->u.cli.cl_import;
989 CERROR("try to add conn on immature client dev\n");
993 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
994 rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
998 EXPORT_SYMBOL(class_add_conn);
1000 /** Remove a failover NID location. */
1001 static int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
1003 struct obd_import *imp;
1004 struct obd_uuid uuid;
1009 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
1010 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
1011 CERROR("invalid conn_uuid\n");
1014 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
1015 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
1016 CERROR("can't del connection on non-client dev\n");
1020 imp = obd->u.cli.cl_import;
1022 CERROR("try to del conn on immature client dev\n");
1026 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
1027 rc = obd_del_conn(imp, &uuid);
1032 static LIST_HEAD(lustre_profile_list);
1033 static DEFINE_SPINLOCK(lustre_profile_list_lock);
1035 static struct lustre_profile *class_get_profile_nolock(const char *prof)
1037 struct lustre_profile *lprof;
1040 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
1041 if (strcmp(lprof->lp_profile, prof) == 0) {
1049 struct lustre_profile *class_get_profile(const char *prof)
1051 struct lustre_profile *lprof;
1054 spin_lock(&lustre_profile_list_lock);
1055 lprof = class_get_profile_nolock(prof);
1056 spin_unlock(&lustre_profile_list_lock);
1059 EXPORT_SYMBOL(class_get_profile);
1062 * Create a named "profile".
1063 * This defines the MDC and OSC names to use for a client.
1064 * This also is used to define the LOV to be used by a MDT.
1066 static int class_add_profile(int proflen, char *prof, int osclen, char *osc,
1067 int mdclen, char *mdc)
1069 struct lustre_profile *lprof;
1074 CDEBUG(D_CONFIG, "Add profile %s\n", prof);
1076 OBD_ALLOC(lprof, sizeof(*lprof));
1079 INIT_LIST_HEAD(&lprof->lp_list);
1081 LASSERT(proflen == (strlen(prof) + 1));
1082 OBD_ALLOC(lprof->lp_profile, proflen);
1083 if (!lprof->lp_profile)
1084 GOTO(out, err = -ENOMEM);
1085 memcpy(lprof->lp_profile, prof, proflen);
1087 LASSERT(osclen == (strlen(osc) + 1));
1088 OBD_ALLOC(lprof->lp_dt, osclen);
1090 GOTO(out, err = -ENOMEM);
1091 memcpy(lprof->lp_dt, osc, osclen);
1094 LASSERT(mdclen == (strlen(mdc) + 1));
1095 OBD_ALLOC(lprof->lp_md, mdclen);
1097 GOTO(out, err = -ENOMEM);
1098 memcpy(lprof->lp_md, mdc, mdclen);
1101 spin_lock(&lustre_profile_list_lock);
1103 lprof->lp_list_deleted = false;
1105 list_add(&lprof->lp_list, &lustre_profile_list);
1106 spin_unlock(&lustre_profile_list_lock);
1111 OBD_FREE(lprof->lp_md, mdclen);
1113 OBD_FREE(lprof->lp_dt, osclen);
1114 if (lprof->lp_profile)
1115 OBD_FREE(lprof->lp_profile, proflen);
1116 OBD_FREE(lprof, sizeof(*lprof));
1120 void class_del_profile(const char *prof)
1122 struct lustre_profile *lprof;
1126 CDEBUG(D_CONFIG, "Del profile %s\n", prof);
1128 spin_lock(&lustre_profile_list_lock);
1129 lprof = class_get_profile_nolock(prof);
1131 /* because get profile increments the ref counter */
1133 list_del(&lprof->lp_list);
1134 lprof->lp_list_deleted = true;
1135 spin_unlock(&lustre_profile_list_lock);
1137 class_put_profile(lprof);
1139 spin_unlock(&lustre_profile_list_lock);
1143 EXPORT_SYMBOL(class_del_profile);
1145 void class_put_profile(struct lustre_profile *lprof)
1147 spin_lock(&lustre_profile_list_lock);
1148 if ((--lprof->lp_refs) > 0) {
1149 LASSERT(lprof->lp_refs > 0);
1150 spin_unlock(&lustre_profile_list_lock);
1153 spin_unlock(&lustre_profile_list_lock);
1155 /* confirm not a negative number */
1156 LASSERT(lprof->lp_refs == 0);
1159 * At least one class_del_profile/profiles must be called
1160 * on the target profile or lustre_profile_list will corrupt
1162 LASSERT(lprof->lp_list_deleted);
1163 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
1164 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
1166 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
1167 OBD_FREE(lprof, sizeof(*lprof));
1169 EXPORT_SYMBOL(class_put_profile);
1172 void class_del_profiles(void)
1174 struct lustre_profile *lprof, *n;
1177 spin_lock(&lustre_profile_list_lock);
1178 list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
1179 list_del(&lprof->lp_list);
1180 lprof->lp_list_deleted = true;
1181 spin_unlock(&lustre_profile_list_lock);
1183 class_put_profile(lprof);
1185 spin_lock(&lustre_profile_list_lock);
1187 spin_unlock(&lustre_profile_list_lock);
1190 EXPORT_SYMBOL(class_del_profiles);
1193 * We can't call lquota_process_config directly because
1194 * it lives in a module that must be loaded after this one.
1196 #ifdef HAVE_SERVER_SUPPORT
1197 static int (*quota_process_config)(struct lustre_cfg *lcfg) = NULL;
1198 #endif /* HAVE_SERVER_SUPPORT */
1201 * Rename the proc parameter in \a cfg with a new name \a new_name.
1203 * \param cfg config structure which contains the proc parameter
1204 * \param new_name new name of the proc parameter
1206 * \retval valid-pointer pointer to the newly-allocated config structure
1207 * which contains the renamed proc parameter
1208 * \retval ERR_PTR(-EINVAL) if \a cfg or \a new_name is NULL, or \a cfg does
1209 * not contain a proc parameter
1210 * \retval ERR_PTR(-ENOMEM) if memory allocation failure occurs
1212 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
1213 const char *new_name)
1215 struct lustre_cfg_bufs *bufs = NULL;
1216 struct lustre_cfg *new_cfg = NULL;
1218 char *new_param = NULL;
1225 if (!cfg || !new_name)
1226 GOTO(out_nocfg, new_cfg = ERR_PTR(-EINVAL));
1228 param = lustre_cfg_string(cfg, 1);
1230 GOTO(out_nocfg, new_cfg = ERR_PTR(-EINVAL));
1232 value = strchr(param, '=');
1234 name_len = value - param;
1236 name_len = strlen(param);
1238 new_len = LUSTRE_CFG_BUFLEN(cfg, 1) + strlen(new_name) - name_len;
1240 OBD_ALLOC(new_param, new_len);
1242 GOTO(out_nocfg, new_cfg = ERR_PTR(-ENOMEM));
1244 strlcpy(new_param, new_name, new_len);
1246 strcat(new_param, value);
1248 OBD_ALLOC_PTR(bufs);
1250 GOTO(out_free_param, new_cfg = ERR_PTR(-ENOMEM));
1252 lustre_cfg_bufs_reset(bufs, NULL);
1253 lustre_cfg_bufs_init(bufs, cfg);
1254 lustre_cfg_bufs_set_string(bufs, 1, new_param);
1256 OBD_ALLOC(new_cfg, lustre_cfg_len(bufs->lcfg_bufcount,
1257 bufs->lcfg_buflen));
1259 GOTO(out_free_buf, new_cfg = ERR_PTR(-ENOMEM));
1261 lustre_cfg_init(new_cfg, cfg->lcfg_command, bufs);
1263 new_cfg->lcfg_num = cfg->lcfg_num;
1264 new_cfg->lcfg_flags = cfg->lcfg_flags;
1265 new_cfg->lcfg_nid = cfg->lcfg_nid;
1266 new_cfg->lcfg_nal = cfg->lcfg_nal;
1270 OBD_FREE(new_param, new_len);
1274 EXPORT_SYMBOL(lustre_cfg_rename);
1276 static ssize_t process_param2_config(struct lustre_cfg *lcfg)
1278 char *param = lustre_cfg_string(lcfg, 1);
1279 char *upcall = lustre_cfg_string(lcfg, 2);
1280 struct kobject *kobj = NULL;
1281 const char *subsys = param;
1282 char *newparam = NULL;
1284 [0] = "/usr/sbin/lctl",
1295 print_lustre_cfg(lcfg);
1297 len = strcspn(param, ".=");
1301 /* If we find '=' then its the top level sysfs directory */
1302 if (param[len] == '=')
1303 RETURN(class_set_global(param));
1305 subsys = kstrndup(param, len, GFP_KERNEL);
1309 kobj = kset_find_obj(lustre_kset, subsys);
1312 char *value = param;
1316 param = strsep(&value, "=");
1317 envp[0] = kasprintf(GFP_KERNEL, "PARAM=%s", param);
1318 envp[1] = kasprintf(GFP_KERNEL, "SETTING=%s", value);
1319 envp[2] = kasprintf(GFP_KERNEL, "TIME=%lld",
1320 ktime_get_real_seconds());
1323 rc = kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
1324 for (i = 0; i < ARRAY_SIZE(envp); i++)
1332 /* Add upcall processing here. Now only lctl is supported */
1333 if (strcmp(upcall, LCTL_UPCALL) != 0) {
1334 CERROR("Unsupported upcall %s\n", upcall);
1338 /* root_squash and nosquash_nids settings must be applied to
1339 * global subsystem (*.) so that it is taken into account by
1340 * both client and server sides. So do the equivalent of a
1341 * 's / mdt. / *. /'.
1343 if ((strstr(param, PARAM_NOSQUASHNIDS) ||
1344 strstr(param, PARAM_ROOTSQUASH)) &&
1345 (param[0] != '*' || param[1] != '.')) {
1346 newparam = kmalloc(strlen(param) + 1, GFP_NOFS);
1350 snprintf(newparam, strlen(param) + 1, "*%s", param + len);
1351 argv[2] = (char *)newparam;
1354 start = ktime_get();
1355 rc = call_usermodehelper(argv[0], argv, NULL, UMH_WAIT_PROC);
1359 CERROR("lctl: error invoking upcall %s %s %s: rc = %d; "
1360 "time %ldus\n", argv[0], argv[1], argv[2], rc,
1361 (long)ktime_us_delta(end, start));
1363 CDEBUG(D_HA, "lctl: invoked upcall %s %s %s, time %ldus\n",
1364 argv[0], argv[1], argv[2],
1365 (long)ktime_us_delta(end, start));
1373 #ifdef HAVE_SERVER_SUPPORT
1374 void lustre_register_quota_process_config(int (*qpc)(struct lustre_cfg *lcfg))
1376 quota_process_config = qpc;
1378 EXPORT_SYMBOL(lustre_register_quota_process_config);
1379 #endif /* HAVE_SERVER_SUPPORT */
1382 * Process configuration commands given in lustre_cfg form.
1383 * These may come from direct calls (e.g. class_manual_cleanup)
1384 * or processing the config llog, or ioctl from lctl.
1386 int class_process_config(struct lustre_cfg *lcfg)
1388 struct obd_device *obd;
1389 struct lnet_nid nid;
1392 LASSERT(lcfg && !IS_ERR(lcfg));
1393 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
1395 /* Commands that don't need a device */
1396 switch (lcfg->lcfg_command) {
1398 err = class_attach(lcfg);
1401 case LCFG_ADD_UUID: {
1403 "adding mapping from uuid %s to nid %#llx (%s)\n",
1404 lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid,
1405 libcfs_nid2str(lcfg->lcfg_nid));
1408 if (lcfg->lcfg_nid) {
1409 lnet_nid4_to_nid(lcfg->lcfg_nid, &nid);
1411 char *nidstr = lustre_cfg_string(lcfg, 2);
1414 err = libcfs_strnid(&nid, nidstr);
1419 err = class_add_uuid(lustre_cfg_string(lcfg, 1), &nid);
1422 case LCFG_DEL_UUID: {
1423 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
1424 (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) ==
1425 0) ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
1427 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
1430 case LCFG_MOUNTOPT: {
1431 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
1432 lustre_cfg_string(lcfg, 1),
1433 lustre_cfg_string(lcfg, 2),
1434 lustre_cfg_string(lcfg, 3));
1436 * set these mount options somewhere, so ll_fill_super
1439 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
1440 lustre_cfg_string(lcfg, 1),
1441 LUSTRE_CFG_BUFLEN(lcfg, 2),
1442 lustre_cfg_string(lcfg, 2),
1443 LUSTRE_CFG_BUFLEN(lcfg, 3),
1444 lustre_cfg_string(lcfg, 3));
1447 case LCFG_DEL_MOUNTOPT: {
1448 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
1449 lustre_cfg_string(lcfg, 1));
1450 class_del_profile(lustre_cfg_string(lcfg, 1));
1453 case LCFG_SET_TIMEOUT: {
1454 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
1455 obd_timeout, lcfg->lcfg_num);
1456 obd_timeout = max(lcfg->lcfg_num, 1U);
1457 ping_interval = max(obd_timeout / 4, 1U);
1458 obd_timeout_set = 1;
1461 case LCFG_SET_LDLM_TIMEOUT: {
1462 CDEBUG(D_IOCTL, "changing lustre ldlm_timeout from %d to %d\n",
1463 ldlm_timeout, lcfg->lcfg_num);
1464 ldlm_timeout = max(lcfg->lcfg_num, 1U);
1465 if (ldlm_timeout >= obd_timeout)
1466 ldlm_timeout = max(obd_timeout / 3, 1U);
1467 ldlm_timeout_set = 1;
1470 case LCFG_SET_UPCALL: {
1471 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
1472 /* COMPAT_146 Don't fail on old configs */
1476 struct cfg_marker *marker;
1478 marker = lustre_cfg_buf(lcfg, 1);
1479 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
1480 marker->cm_flags, marker->cm_tgtname,
1481 marker->cm_comment);
1487 /* llite has no OBD */
1488 if (class_match_param(lustre_cfg_string(lcfg, 1),
1489 PARAM_LLITE, NULL) == 0) {
1490 struct lustre_sb_info *lsi;
1495 * The instance name contains the sb:
1496 * lustre-client-aacfe000
1498 tmp = strrchr(lustre_cfg_string(lcfg, 0), '-');
1499 if (!tmp || !*(++tmp))
1500 GOTO(out, err = -EINVAL);
1502 if (sscanf(tmp, "%lx", &addr) != 1)
1503 GOTO(out, err = -EINVAL);
1505 lsi = s2lsi((struct super_block *)addr);
1506 /* This better be a real Lustre superblock! */
1507 LASSERT(lsi->lsi_lmd->lmd_magic == LMD_MAGIC);
1509 count = class_modify_config(lcfg, PARAM_LLITE,
1511 err = count < 0 ? count : 0;
1513 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1514 PARAM_SYS, &tmp) == 0)) {
1515 /* Global param settings */
1516 err = class_set_global(tmp);
1518 * Client or server should not fail to mount if
1519 * it hits an unknown configuration parameter.
1522 CWARN("Ignoring unknown param %s\n", tmp);
1525 #ifdef HAVE_SERVER_SUPPORT
1526 } else if ((class_match_param(lustre_cfg_string(lcfg, 1),
1527 PARAM_QUOTA, &tmp) == 0) &&
1528 quota_process_config) {
1529 err = (*quota_process_config)(lcfg);
1531 #endif /* HAVE_SERVER_SUPPORT */
1536 case LCFG_SET_PARAM: {
1537 err = process_param2_config(lcfg);
1541 /* Commands that require a device */
1542 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1544 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
1545 CERROR("this lcfg command requires a device name\n");
1547 CERROR("no device for: %s\n",
1548 lustre_cfg_string(lcfg, 0));
1550 GOTO(out, err = -EINVAL);
1552 switch(lcfg->lcfg_command) {
1554 err = class_setup(obd, lcfg);
1558 err = class_detach(obd, lcfg);
1561 case LCFG_CLEANUP: {
1562 err = class_cleanup(obd, lcfg);
1565 case LCFG_ADD_CONN: {
1566 err = class_add_conn(obd, lcfg);
1569 case LCFG_DEL_CONN: {
1570 err = class_del_conn(obd, lcfg);
1573 case LCFG_POOL_NEW: {
1574 err = obd_pool_new(obd, lustre_cfg_string(lcfg, 2));
1577 case LCFG_POOL_ADD: {
1578 err = obd_pool_add(obd, lustre_cfg_string(lcfg, 2),
1579 lustre_cfg_string(lcfg, 3));
1582 case LCFG_POOL_REM: {
1583 err = obd_pool_rem(obd, lustre_cfg_string(lcfg, 2),
1584 lustre_cfg_string(lcfg, 3));
1587 case LCFG_POOL_DEL: {
1588 err = obd_pool_del(obd, lustre_cfg_string(lcfg, 2));
1592 * Process config log ADD_MDC record twice to add MDC also to LOV
1595 * add 0:lustre-clilmv 1:lustre-MDT0000_UUID 2:0 3:1
1596 * 4:lustre-MDT0000-mdc_UUID
1598 case LCFG_ADD_MDC: {
1599 struct obd_device *lov_obd;
1602 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1606 /* make sure this is client LMV log entry */
1607 clilmv = strstr(lustre_cfg_string(lcfg, 0), "clilmv");
1612 * replace 'lmv' with 'lov' name to address LOV device and
1613 * process llog record to add MDC there.
1616 lov_obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1618 err = obd_process_config(lov_obd, sizeof(*lcfg), lcfg);
1621 CERROR("%s: Cannot find LOV by %s name, rc = %d\n",
1622 obd->obd_name, lustre_cfg_string(lcfg, 0), err);
1624 /* restore 'lmv' name */
1629 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
1635 if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
1636 CWARN("Ignoring error %d on optional command %#x\n", err,
1637 lcfg->lcfg_command);
1642 EXPORT_SYMBOL(class_process_config);
1644 ssize_t class_modify_config(struct lustre_cfg *lcfg, const char *prefix,
1645 struct kobject *kobj)
1647 const struct kobj_type *typ;
1651 if (lcfg->lcfg_command != LCFG_PARAM) {
1652 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1656 typ = get_ktype(kobj);
1657 if (!typ || !typ->default_groups)
1660 print_lustre_cfg(lcfg);
1663 * e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
1664 * or lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
1665 * or lctl conf_param lustre-OST0000.osc.max_dirty_mb=36
1667 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
1668 struct attribute *attr = NULL;
1673 key = lustre_cfg_buf(lcfg, i);
1674 /* Strip off prefix */
1675 if (class_match_param(key, prefix, &key))
1677 * If the prefix doesn't match, return error so we
1678 * can pass it down the stack
1682 value = strchr(key, '=');
1683 if (!value || *(value + 1) == 0) {
1684 CERROR("%s: can't parse param '%s' (missing '=')\n",
1685 lustre_cfg_string(lcfg, 0),
1686 lustre_cfg_string(lcfg, i));
1687 /* continue parsing other params */
1690 keylen = value - key;
1693 attr = get_attr_starts_with(typ, key, keylen);
1695 char *envp[4], *param, *path;
1697 path = kobject_get_path(kobj, GFP_KERNEL);
1701 /* convert sysfs path to uevent format */
1703 while ((param = strchr(param, '/')) != NULL)
1706 param = strstr(path, "fs.lustre.") + 10;
1708 envp[0] = kasprintf(GFP_KERNEL, "PARAM=%s.%.*s",
1709 param, (int) keylen, key);
1710 envp[1] = kasprintf(GFP_KERNEL, "SETTING=%s", value);
1711 envp[2] = kasprintf(GFP_KERNEL, "TIME=%lld",
1712 ktime_get_real_seconds());
1715 if (kobject_uevent_env(kobj, KOBJ_CHANGE, envp)) {
1716 CERROR("%s: failed to send uevent %s\n",
1717 kobject_name(kobj), key);
1720 for (i = 0; i < ARRAY_SIZE(envp); i++)
1724 count += lustre_attr_store(kobj, attr, value,
1730 EXPORT_SYMBOL(class_modify_config);
1733 * Supplemental functions for config logs, it allocates lustre_cfg
1734 * buffers plus initialized llog record header at the beginning.
1736 struct llog_cfg_rec *lustre_cfg_rec_new(int cmd, struct lustre_cfg_bufs *bufs)
1738 struct llog_cfg_rec *lcr;
1743 reclen = lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen);
1744 reclen = llog_data_len(reclen) + sizeof(struct llog_rec_hdr) +
1745 sizeof(struct llog_rec_tail);
1747 OBD_ALLOC(lcr, reclen);
1751 lustre_cfg_init(&lcr->lcr_cfg, cmd, bufs);
1753 lcr->lcr_hdr.lrh_len = reclen;
1754 lcr->lcr_hdr.lrh_type = OBD_CFG_REC;
1758 EXPORT_SYMBOL(lustre_cfg_rec_new);
1760 void lustre_cfg_rec_free(struct llog_cfg_rec *lcr)
1763 OBD_FREE(lcr, lcr->lcr_hdr.lrh_len);
1766 EXPORT_SYMBOL(lustre_cfg_rec_free);
1769 * Parse a configuration llog, doing various manipulations on them
1770 * for various reasons, (modifications for compatibility, skip obsolete
1771 * records, change uuids, etc), then class_process_config() resulting
1774 int class_config_llog_handler(const struct lu_env *env,
1775 struct llog_handle *handle,
1776 struct llog_rec_hdr *rec, void *data)
1778 struct config_llog_instance *cfg = data;
1779 int cfg_len = rec->lrh_len;
1780 char *cfg_buf = (char *) (rec + 1);
1784 /* class_config_dump_handler(handle, rec, data); */
1786 switch (rec->lrh_type) {
1788 struct lustre_cfg *lcfg, *lcfg_new;
1789 struct lustre_cfg_bufs bufs;
1790 char *inst_name = NULL;
1794 lcfg = (struct lustre_cfg *)cfg_buf;
1795 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
1796 lustre_swab_lustre_cfg(lcfg);
1800 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1804 /* Figure out config state info */
1805 if (lcfg->lcfg_command == LCFG_MARKER) {
1806 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1807 lustre_swab_cfg_marker(marker, swab,
1808 LUSTRE_CFG_BUFLEN(lcfg, 1));
1809 CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
1810 cfg->cfg_flags, marker->cm_flags);
1811 if (marker->cm_flags & CM_START) {
1812 /* all previous flags off */
1813 cfg->cfg_flags = CFG_F_MARKER;
1814 server_name2index(marker->cm_tgtname,
1815 &cfg->cfg_lwp_idx, NULL);
1816 if (marker->cm_flags & CM_SKIP) {
1817 cfg->cfg_flags |= CFG_F_SKIP;
1818 CDEBUG(D_CONFIG, "SKIP #%d\n",
1820 } else if ((marker->cm_flags & CM_EXCLUDE) ||
1822 lustre_check_exclusion(cfg->cfg_sb,
1823 marker->cm_tgtname))) {
1824 cfg->cfg_flags |= CFG_F_EXCLUDE;
1825 CDEBUG(D_CONFIG, "EXCLUDE %d\n",
1828 } else if (marker->cm_flags & CM_END) {
1833 * A config command without a start marker before it is
1836 if (!(cfg->cfg_flags & CFG_F_MARKER) &&
1837 (lcfg->lcfg_command != LCFG_MARKER)) {
1838 CWARN("Skip config outside markers, (inst: %016lx, uuid: %s, flags: %#x)\n",
1840 cfg->cfg_uuid.uuid, cfg->cfg_flags);
1841 cfg->cfg_flags |= CFG_F_SKIP;
1843 if (cfg->cfg_flags & CFG_F_SKIP) {
1844 CDEBUG(D_CONFIG, "skipping %#x\n",
1847 /* No processing! */
1852 * For interoperability between 1.8 and 2.0,
1853 * rename "mds" OBD device type to "mdt".
1856 char *typename = lustre_cfg_string(lcfg, 1);
1857 char *index = lustre_cfg_string(lcfg, 2);
1859 if ((lcfg->lcfg_command == LCFG_ATTACH && typename &&
1860 strcmp(typename, "mds") == 0)) {
1861 CWARN("For 1.8 interoperability, rename obd "
1862 "type from mds to mdt\n");
1865 if ((lcfg->lcfg_command == LCFG_SETUP && index &&
1866 strcmp(index, "type") == 0)) {
1867 CDEBUG(D_INFO, "For 1.8 interoperability, "
1868 "set this index to '0'\n");
1874 #ifdef HAVE_SERVER_SUPPORT
1875 /* newer MDS replaces LOV/OSC with LOD/OSP */
1876 if ((lcfg->lcfg_command == LCFG_ATTACH ||
1877 lcfg->lcfg_command == LCFG_SET_PARAM ||
1878 lcfg->lcfg_command == LCFG_PARAM) &&
1879 cfg->cfg_sb && IS_MDT(s2lsi(cfg->cfg_sb))) {
1880 char *typename = lustre_cfg_string(lcfg, 1);
1883 strcmp(typename, LUSTRE_LOV_NAME) == 0) {
1885 "For 2.x interoperability, rename obd "
1886 "type from lov to lod (%s)\n",
1887 s2lsi(cfg->cfg_sb)->lsi_svname);
1888 strcpy(typename, LUSTRE_LOD_NAME);
1891 strcmp(typename, LUSTRE_OSC_NAME) == 0) {
1893 "For 2.x interoperability, rename obd "
1894 "type from osc to osp (%s)\n",
1895 s2lsi(cfg->cfg_sb)->lsi_svname);
1896 strcpy(typename, LUSTRE_OSP_NAME);
1899 #endif /* HAVE_SERVER_SUPPORT */
1901 if (cfg->cfg_flags & CFG_F_EXCLUDE) {
1902 CDEBUG(D_CONFIG, "cmd: %x marked EXCLUDED\n",
1903 lcfg->lcfg_command);
1904 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD)
1905 /* Add inactive instead */
1906 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
1909 lustre_cfg_bufs_reset(&bufs, NULL);
1910 lustre_cfg_bufs_init(&bufs, lcfg);
1912 if (cfg->cfg_instance &&
1913 lcfg->lcfg_command != LCFG_SPTLRPC_CONF &&
1914 LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
1915 inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1916 LUSTRE_MAXINSTANCE + 4;
1917 OBD_ALLOC(inst_name, inst_len);
1919 GOTO(out, rc = -ENOMEM);
1920 snprintf(inst_name, inst_len, "%s-%016lx",
1921 lustre_cfg_string(lcfg, 0),
1923 lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1924 CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1925 lcfg->lcfg_command, inst_name);
1928 /* override llog UUID for clients, to insure they are unique */
1929 if (cfg->cfg_instance && lcfg->lcfg_command == LCFG_ATTACH)
1930 lustre_cfg_bufs_set_string(&bufs, 2,
1931 cfg->cfg_uuid.uuid);
1933 * sptlrpc config record, we expect 2 data segments:
1934 * [0]: fs_name/target_name,
1936 * moving them to index [1] and [2], and insert MGC's
1937 * obdname at index [0].
1939 if (cfg->cfg_instance &&
1940 lcfg->lcfg_command == LCFG_SPTLRPC_CONF) {
1941 /* After ASLR changes cfg_instance this needs fixing */
1942 /* "obd" is set in config_log_find_or_add() */
1943 struct obd_device *obd = (void *)cfg->cfg_instance;
1945 lustre_cfg_bufs_set(&bufs, 2, bufs.lcfg_buf[1],
1946 bufs.lcfg_buflen[1]);
1947 lustre_cfg_bufs_set(&bufs, 1, bufs.lcfg_buf[0],
1948 bufs.lcfg_buflen[0]);
1949 lustre_cfg_bufs_set_string(&bufs, 0,
1954 * Add net info to setup command
1955 * if given on command line.
1956 * So config log will be:
1960 * [3]: inactive-on-startup
1961 * [4]: restrictive net
1963 if (cfg && cfg->cfg_sb && s2lsi(cfg->cfg_sb) &&
1964 !IS_SERVER(s2lsi(cfg->cfg_sb))) {
1965 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1966 char *nidnet = lsi->lsi_lmd->lmd_nidnet;
1968 if (lcfg->lcfg_command == LCFG_SETUP &&
1969 lcfg->lcfg_bufcount != 2 && nidnet) {
1970 CDEBUG(D_CONFIG, "Adding net %s info to setup "
1971 "command for client %s\n", nidnet,
1972 lustre_cfg_string(lcfg, 0));
1973 lustre_cfg_bufs_set_string(&bufs, 4, nidnet);
1977 OBD_ALLOC(lcfg_new, lustre_cfg_len(bufs.lcfg_bufcount,
1980 GOTO(out_inst, rc = -ENOMEM);
1982 lustre_cfg_init(lcfg_new, lcfg->lcfg_command, &bufs);
1983 lcfg_new->lcfg_num = lcfg->lcfg_num;
1984 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1987 * XXX Hack to try to remain binary compatible with
1988 * pre-newconfig logs
1990 if (lcfg->lcfg_nal != 0 && /* pre-newconfig log? */
1991 (lcfg->lcfg_nid >> 32) == 0) {
1992 __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1994 lcfg_new->lcfg_nid =
1995 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1996 CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1997 lcfg->lcfg_nal, addr,
1998 libcfs_nid2str(lcfg_new->lcfg_nid));
2000 lcfg_new->lcfg_nid = lcfg->lcfg_nid;
2003 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
2005 rc = class_process_config(lcfg_new);
2006 OBD_FREE(lcfg_new, lustre_cfg_len(lcfg_new->lcfg_bufcount,
2007 lcfg_new->lcfg_buflens));
2010 OBD_FREE(inst_name, inst_len);
2014 CERROR("Unknown llog record type %#x encountered\n",
2020 CERROR("%s: cfg command failed: rc = %d\n",
2021 handle->lgh_ctxt->loc_obd->obd_name, rc);
2022 class_config_dump_handler(NULL, handle, rec, data);
2026 EXPORT_SYMBOL(class_config_llog_handler);
2028 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
2029 char *name, struct config_llog_instance *cfg)
2031 struct llog_process_cat_data cd = {
2032 .lpcd_first_idx = 0,
2033 .lpcd_read_mode = LLOG_READ_MODE_NORMAL,
2035 struct llog_handle *llh;
2040 CDEBUG(D_INFO, "looking up llog %s\n", name);
2041 rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
2045 rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
2047 GOTO(parse_out, rc);
2049 /* continue processing from where we last stopped to end-of-log */
2051 cd.lpcd_first_idx = cfg->cfg_last_idx;
2052 callback = cfg->cfg_callback;
2053 LASSERT(callback != NULL);
2055 callback = class_config_llog_handler;
2058 cd.lpcd_last_idx = 0;
2060 rc = llog_process(env, llh, callback, cfg, &cd);
2062 CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
2063 cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
2065 cfg->cfg_last_idx = cd.lpcd_last_idx;
2068 llog_close(env, llh);
2071 EXPORT_SYMBOL(class_config_parse_llog);
2074 * Get marker cfg_flag
2076 void llog_get_marker_cfg_flags(struct llog_rec_hdr *rec,
2077 unsigned int *cfg_flags)
2079 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
2080 struct cfg_marker *marker;
2082 if (lcfg->lcfg_command == LCFG_MARKER) {
2083 marker = lustre_cfg_buf(lcfg, 1);
2084 if (marker->cm_flags & CM_START) {
2085 *cfg_flags = CFG_F_MARKER;
2086 if (marker->cm_flags & CM_SKIP)
2087 *cfg_flags = CFG_F_SKIP;
2088 } else if (marker->cm_flags & CM_END) {
2091 CDEBUG(D_INFO, "index=%d, cm_flags=%#08x cfg_flags=%#08x\n",
2092 rec->lrh_index, marker->cm_flags, *cfg_flags);
2097 * Parse config record and output dump in supplied buffer.
2099 * This is separated from class_config_dump_handler() to use
2100 * for ioctl needs as well
2103 * - { index: 4, event: attach, device: lustrewt-clilov, type: lov,
2104 * UUID: lustrewt-clilov_UUID }
2106 int class_config_yaml_output(struct llog_rec_hdr *rec, char *buf, int size,
2107 unsigned int *cfg_flags, bool raw)
2109 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
2111 char *end = buf + size;
2113 struct lcfg_type_data *ldata;
2116 LASSERT(rec->lrh_type == OBD_CFG_REC);
2118 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION)) {
2119 lustre_swab_lustre_cfg(lcfg);
2123 rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2127 ldata = lcfg_cmd2data(lcfg->lcfg_command);
2131 llog_get_marker_cfg_flags(rec, cfg_flags);
2132 if ((lcfg->lcfg_command == LCFG_MARKER) && likely(!raw))
2134 /* entries outside marker are skipped */
2135 if (!(*cfg_flags & CFG_F_MARKER) && !raw)
2137 /* inside skipped marker */
2138 if ((*cfg_flags & CFG_F_SKIP) && !raw)
2141 /* form YAML entity */
2142 ptr += snprintf(ptr, end - ptr, "- { index: %u, event: %s",
2143 rec->lrh_index, ldata->ltd_name);
2147 if (lcfg->lcfg_flags) {
2148 ptr += snprintf(ptr, end - ptr, ", flags: %#08x",
2153 if (lcfg->lcfg_num) {
2154 ptr += snprintf(ptr, end - ptr, ", num: %#08x",
2159 if (lcfg->lcfg_nid) {
2160 char nidstr[LNET_NIDSTR_SIZE];
2162 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2163 ptr += snprintf(ptr, end - ptr, ", nid: %s(%#llx)",
2164 nidstr, lcfg->lcfg_nid);
2169 if (LUSTRE_CFG_BUFLEN(lcfg, 0) > 0) {
2170 ptr += snprintf(ptr, end - ptr, ", device: %s",
2171 lustre_cfg_string(lcfg, 0));
2176 if (lcfg->lcfg_command == LCFG_SET_PARAM) {
2178 * set_param -P parameters have param=val here, separate
2179 * them through pointer magic and print them out in
2182 char *cfg_str = lustre_cfg_string(lcfg, 1);
2183 char *tmp = strchr(cfg_str, '=');
2189 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[0]);
2190 len = tmp - cfg_str + 1;
2191 snprintf(ptr, len, "%s", cfg_str);
2194 ptr += snprintf(ptr, end - ptr, ", %s: ", ldata->ltd_bufs[1]);
2195 ptr += snprintf(ptr, end - ptr, "%s", tmp + 1);
2200 if (lcfg->lcfg_command == LCFG_MARKER) {
2201 struct cfg_marker *marker;
2203 marker = lustre_cfg_buf(lcfg, 1);
2204 ptr += snprintf(ptr, end - ptr, ", flags: %#04x",
2206 ptr += snprintf(ptr, end - ptr, ", version: %d.%d.%d.%d",
2207 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
2208 OBD_OCD_VERSION_MINOR(marker->cm_vers),
2209 OBD_OCD_VERSION_PATCH(marker->cm_vers),
2210 OBD_OCD_VERSION_FIX(marker->cm_vers));
2211 ptr += snprintf(ptr, end - ptr, ", createtime: %lld",
2212 marker->cm_createtime);
2213 ptr += snprintf(ptr, end - ptr, ", canceltime: %lld",
2214 marker->cm_canceltime);
2219 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
2220 if (LUSTRE_CFG_BUFLEN(lcfg, i) > 0) {
2221 ptr += snprintf(ptr, end - ptr, ", %s: %s",
2222 ldata->ltd_bufs[i - 1],
2223 lustre_cfg_string(lcfg, i));
2230 ptr += snprintf(ptr, end - ptr, " }\n");
2232 /* Return consumed bytes. If the buffer overflowed, zero last byte */
2243 * parse config record and output dump in supplied buffer.
2244 * This is separated from class_config_dump_handler() to use
2245 * for ioctl needs as well
2247 static int class_config_parse_rec(struct llog_rec_hdr *rec, char *buf, int size)
2249 struct lustre_cfg *lcfg = (struct lustre_cfg *)(rec + 1);
2251 char *end = buf + size;
2256 LASSERT(rec->lrh_type == OBD_CFG_REC);
2257 rc = lustre_cfg_sanity_check(lcfg, rec->lrh_len);
2261 ptr += snprintf(ptr, end-ptr, "cmd=%05x ", lcfg->lcfg_command);
2262 if (lcfg->lcfg_flags)
2263 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
2267 ptr += snprintf(ptr, end-ptr, "num=%#08x ", lcfg->lcfg_num);
2269 if (lcfg->lcfg_nid) {
2270 char nidstr[LNET_NIDSTR_SIZE];
2272 libcfs_nid2str_r(lcfg->lcfg_nid, nidstr, sizeof(nidstr));
2273 ptr += snprintf(ptr, end-ptr, "nid=%s(%#llx) ",
2274 nidstr, lcfg->lcfg_nid);
2277 if (lcfg->lcfg_command == LCFG_MARKER) {
2278 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
2280 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
2281 marker->cm_step, marker->cm_flags,
2282 marker->cm_tgtname, marker->cm_comment);
2286 for (i = 0; i < lcfg->lcfg_bufcount; i++) {
2287 ptr += snprintf(ptr, end-ptr, "%d:%s ", i,
2288 lustre_cfg_string(lcfg, i));
2291 ptr += snprintf(ptr, end - ptr, "\n");
2292 /* return consumed bytes */
2297 int class_config_dump_handler(const struct lu_env *env,
2298 struct llog_handle *handle,
2299 struct llog_rec_hdr *rec, void *data)
2306 OBD_ALLOC(outstr, 256);
2310 if (rec->lrh_type == OBD_CFG_REC) {
2311 class_config_parse_rec(rec, outstr, 256);
2312 LCONSOLE(D_WARNING, " %s\n", outstr);
2314 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
2318 OBD_FREE(outstr, 256);
2323 * Call class_cleanup and class_detach.
2324 * "Manual" only in the sense that we're faking lcfg commands.
2326 int class_manual_cleanup(struct obd_device *obd)
2329 struct lustre_cfg *lcfg;
2330 struct lustre_cfg_bufs bufs;
2336 CERROR("empty cleanup\n");
2341 strlcat(flags, "F", sizeof(flags));
2343 strlcat(flags, "A", sizeof(flags));
2345 CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
2346 obd->obd_name, flags);
2348 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
2349 lustre_cfg_bufs_set_string(&bufs, 1, flags);
2350 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
2353 lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
2355 rc = class_process_config(lcfg);
2357 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
2361 /* the lcfg is almost the same for both ops */
2362 lcfg->lcfg_command = LCFG_DETACH;
2363 rc = class_process_config(lcfg);
2365 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
2367 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
2370 EXPORT_SYMBOL(class_manual_cleanup);
2372 #ifdef HAVE_SERVER_SUPPORT
2374 * nid<->nidstats hash operations
2377 nidstats_hash(struct cfs_hash *hs, const void *key, const unsigned int bits)
2379 return cfs_hash_djb2_hash(key, sizeof(struct lnet_nid), bits);
2383 nidstats_key(struct hlist_node *hnode)
2385 struct nid_stat *ns;
2387 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2393 nidstats_keycmp(const void *key, struct hlist_node *hnode)
2395 return nid_same((struct lnet_nid *)nidstats_key(hnode),
2396 (struct lnet_nid *)key);
2400 nidstats_object(struct hlist_node *hnode)
2402 return hlist_entry(hnode, struct nid_stat, nid_hash);
2406 nidstats_get(struct cfs_hash *hs, struct hlist_node *hnode)
2408 struct nid_stat *ns;
2410 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2415 nidstats_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2417 struct nid_stat *ns;
2419 ns = hlist_entry(hnode, struct nid_stat, nid_hash);
2423 static struct cfs_hash_ops nid_stat_hash_ops = {
2424 .hs_hash = nidstats_hash,
2425 .hs_key = nidstats_key,
2426 .hs_keycmp = nidstats_keycmp,
2427 .hs_object = nidstats_object,
2428 .hs_get = nidstats_get,
2429 .hs_put_locked = nidstats_put_locked,
2433 * client_generation<->export hash operations
2437 gen_hash(struct cfs_hash *hs, const void *key, const unsigned int bits)
2439 /* XXX did hash needs ? */
2440 return cfs_hash_djb2_hash(key, sizeof(__u32), bits);
2444 gen_key(struct hlist_node *hnode)
2446 struct obd_export *exp;
2448 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2450 RETURN(&exp->exp_target_data.ted_lcd->lcd_generation);
2454 * NOTE: It is impossible to find an export that is in failed
2455 * state with this function
2458 gen_kepcmp(const void *key, struct hlist_node *hnode)
2460 struct obd_export *exp;
2463 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2465 RETURN(exp->exp_target_data.ted_lcd->lcd_generation == *(__u32 *)key &&
2470 gen_export_object(struct hlist_node *hnode)
2472 return hlist_entry(hnode, struct obd_export, exp_gen_hash);
2476 gen_export_get(struct cfs_hash *hs, struct hlist_node *hnode)
2478 struct obd_export *exp;
2480 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2481 class_export_get(exp);
2485 gen_export_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
2487 struct obd_export *exp;
2489 exp = hlist_entry(hnode, struct obd_export, exp_gen_hash);
2490 class_export_put(exp);
2493 static struct cfs_hash_ops gen_hash_ops = {
2494 .hs_hash = gen_hash,
2496 .hs_keycmp = gen_kepcmp,
2497 .hs_object = gen_export_object,
2498 .hs_get = gen_export_get,
2499 .hs_put_locked = gen_export_put_locked,
2502 #endif /* HAVE_SERVER_SUPPORT */