1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/obd_config.c
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <obd_class.h>
44 #include <linux/string.h>
46 #include <liblustre.h>
47 #include <obd_class.h>
50 #include <lustre_log.h>
51 #include <lprocfs_status.h>
52 #include <libcfs/list.h>
53 #include <lustre_param.h>
54 #include <class_hash.h>
56 extern struct lustre_hash_operations uuid_hash_operations;
57 extern struct lustre_hash_operations nid_hash_operations;
59 /*********** string parsing utils *********/
61 /* returns 0 if we find this key in the buffer, else 1 */
62 int class_find_param(char *buf, char *key, char **valp)
69 if ((ptr = strstr(buf, key)) == NULL)
73 *valp = ptr + strlen(key);
78 /* returns 0 if this is the first key in the buffer, else 1.
79 valp points to first char after key. */
80 int class_match_param(char *buf, char *key, char **valp)
85 if (memcmp(buf, key, strlen(key)) != 0)
89 *valp = buf + strlen(key);
97 endh is set to next separator */
98 int class_parse_nid(char *buf, lnet_nid_t *nid, char **endh)
104 while (*buf == ',' || *buf == ':')
106 if (*buf == ' ' || *buf == '/' || *buf == '\0')
109 /* nid separators or end of nids */
110 endp = strpbrk(buf, ",: /");
112 endp = buf + strlen(buf);
116 *nid = libcfs_str2nid(buf);
117 if (*nid == LNET_NID_ANY) {
118 LCONSOLE_ERROR_MSG(0x159, "Can't parse NID '%s'\n", buf);
126 CDEBUG(D_INFO, "Nid %s\n", libcfs_nid2str(*nid));
130 EXPORT_SYMBOL(class_find_param);
131 EXPORT_SYMBOL(class_match_param);
132 EXPORT_SYMBOL(class_parse_nid);
134 /********************** class fns **********************/
137 * Create a new device and set the type, name and uuid. If successful, the new
138 * device can be accessed by either name or uuid.
140 int class_attach(struct lustre_cfg *lcfg)
142 struct obd_device *obd = NULL;
143 char *typename, *name, *uuid;
147 if (!LUSTRE_CFG_BUFLEN(lcfg, 1)) {
148 CERROR("No type passed!\n");
151 typename = lustre_cfg_string(lcfg, 1);
153 if (!LUSTRE_CFG_BUFLEN(lcfg, 0)) {
154 CERROR("No name passed!\n");
157 name = lustre_cfg_string(lcfg, 0);
159 if (!LUSTRE_CFG_BUFLEN(lcfg, 2)) {
160 CERROR("No UUID passed!\n");
163 uuid = lustre_cfg_string(lcfg, 2);
165 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
166 MKSTR(typename), MKSTR(name), MKSTR(uuid));
168 obd = class_newdev(typename, name);
170 /* Already exists or out of obds */
173 CERROR("Cannot create device %s of type %s : %d\n",
177 LASSERTF(obd != NULL, "Cannot get obd device %s of type %s\n",
179 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
180 "obd %p obd_magic %08X != %08X\n",
181 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
182 LASSERTF(strncmp(obd->obd_name, name, strlen(name)) == 0, "%p obd_name %s != %s\n",
183 obd, obd->obd_name, name);
185 rwlock_init(&obd->obd_pool_lock);
186 obd->obd_pool_limit = 0;
187 obd->obd_pool_slv = 0;
189 CFS_INIT_LIST_HEAD(&obd->obd_exports);
190 CFS_INIT_LIST_HEAD(&obd->obd_exports_timed);
191 CFS_INIT_LIST_HEAD(&obd->obd_nid_stats);
192 spin_lock_init(&obd->obd_nid_lock);
193 spin_lock_init(&obd->obd_dev_lock);
194 sema_init(&obd->obd_dev_sem, 1);
195 spin_lock_init(&obd->obd_osfs_lock);
196 /* obd->obd_osfs_age must be set to a value in the distant
197 * past to guarantee a fresh statfs is fetched on mount. */
198 obd->obd_osfs_age = cfs_time_shift_64(-1000);
200 /* XXX belongs in setup not attach */
202 cfs_init_timer(&obd->obd_recovery_timer);
203 spin_lock_init(&obd->obd_processing_task_lock);
204 cfs_waitq_init(&obd->obd_next_transno_waitq);
205 cfs_waitq_init(&obd->obd_evict_inprogress_waitq);
206 CFS_INIT_LIST_HEAD(&obd->obd_req_replay_queue);
207 CFS_INIT_LIST_HEAD(&obd->obd_lock_replay_queue);
208 CFS_INIT_LIST_HEAD(&obd->obd_final_req_queue);
210 llog_group_init(&obd->obd_olg, OBD_LLOG_GROUP);
212 spin_lock_init(&obd->obd_uncommitted_replies_lock);
213 CFS_INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
216 if (len >= sizeof(obd->obd_uuid)) {
217 CERROR("uuid must be < %d bytes long\n",
218 (int)sizeof(obd->obd_uuid));
219 GOTO(out, rc = -EINVAL);
221 memcpy(obd->obd_uuid.uuid, uuid, len);
224 if (OBP(obd, attach)) {
225 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
227 GOTO(out, rc = -EINVAL);
230 /* Detach drops this */
231 spin_lock(&obd->obd_dev_lock);
232 atomic_set(&obd->obd_refcount, 1);
233 spin_unlock(&obd->obd_dev_lock);
235 obd->obd_attached = 1;
236 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s with refcount %d\n",
237 obd->obd_minor, typename, atomic_read(&obd->obd_refcount));
241 class_release_dev(obd);
246 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
249 struct obd_export *exp;
252 LASSERT(obd != NULL);
253 LASSERTF(obd == class_num2obd(obd->obd_minor), "obd %p != obd_devs[%d] %p\n",
254 obd, obd->obd_minor, class_num2obd(obd->obd_minor));
255 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n",
256 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
258 /* have we attached a type to this device? */
259 if (!obd->obd_attached) {
260 CERROR("Device %d not attached\n", obd->obd_minor);
264 if (obd->obd_set_up) {
265 CERROR("Device %d already setup (type %s)\n",
266 obd->obd_minor, obd->obd_type->typ_name);
270 /* is someone else setting us up right now? (attach inits spinlock) */
271 spin_lock(&obd->obd_dev_lock);
272 if (obd->obd_starting) {
273 spin_unlock(&obd->obd_dev_lock);
274 CERROR("Device %d setup in progress (type %s)\n",
275 obd->obd_minor, obd->obd_type->typ_name);
278 /* just leave this on forever. I can't use obd_set_up here because
279 other fns check that status, and we're not actually set up yet. */
280 obd->obd_starting = 1;
281 spin_unlock(&obd->obd_dev_lock);
283 /* create an uuid-export hash body */
284 err = lustre_hash_init(&obd->obd_uuid_hash_body, "UUID_HASH",
285 128, &uuid_hash_operations);
289 /* create a nid-export hash body */
290 err = lustre_hash_init(&obd->obd_nid_hash_body, "NID_HASH",
291 128, &nid_hash_operations);
295 /* create a nid-stats hash body */
296 err = lustre_hash_init(&obd->obd_nid_stats_hash_body, "NID_STATS",
297 128, &nid_stat_hash_operations);
301 exp = class_new_export(obd, &obd->obd_uuid);
303 RETURN(PTR_ERR(exp));
305 obd->obd_self_export = exp;
306 list_del_init(&exp->exp_obd_chain_timed);
307 class_export_put(exp);
309 err = obd_setup(obd, lcfg);
315 spin_lock(&obd->obd_dev_lock);
316 /* cleanup drops this */
318 spin_unlock(&obd->obd_dev_lock);
320 CDEBUG(D_IOCTL, "finished setup of obd %s (uuid %s)\n",
321 obd->obd_name, obd->obd_uuid.uuid);
326 class_unlink_export(obd->obd_self_export);
327 obd->obd_self_export = NULL;
329 lustre_hash_exit(&obd->obd_uuid_hash_body);
330 lustre_hash_exit(&obd->obd_nid_hash_body);
331 lustre_hash_exit(&obd->obd_nid_stats_hash_body);
332 obd->obd_starting = 0;
333 CERROR("setup %s failed (%d)\n", obd->obd_name, err);
337 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
341 if (obd->obd_set_up) {
342 CERROR("OBD device %d still set up\n", obd->obd_minor);
346 spin_lock(&obd->obd_dev_lock);
347 if (!obd->obd_attached) {
348 spin_unlock(&obd->obd_dev_lock);
349 CERROR("OBD device %d not attached\n", obd->obd_minor);
352 obd->obd_attached = 0;
353 spin_unlock(&obd->obd_dev_lock);
355 CDEBUG(D_IOCTL, "detach on obd %s (uuid %s)\n",
356 obd->obd_name, obd->obd_uuid.uuid);
360 /* not strictly necessary, but cleans up eagerly */
361 obd_zombie_impexp_cull();
366 static void dump_exports(struct obd_device *obd)
368 struct obd_export *exp, *n;
370 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
371 struct ptlrpc_reply_state *rs;
372 struct ptlrpc_reply_state *first_reply = NULL;
375 list_for_each_entry (rs, &exp->exp_outstanding_replies,
382 CDEBUG(D_IOCTL, "%s: %p %s %s %d %d %d: %p %s\n",
383 obd->obd_name, exp, exp->exp_client_uuid.uuid,
384 obd_export_nid2str(exp),
385 atomic_read(&exp->exp_refcount),
386 exp->exp_failed, nreplies, first_reply,
387 nreplies > 3 ? "..." : "");
391 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
397 OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
399 if (!obd->obd_set_up) {
400 CERROR("Device %d not setup\n", obd->obd_minor);
404 spin_lock(&obd->obd_dev_lock);
405 if (obd->obd_stopping) {
406 spin_unlock(&obd->obd_dev_lock);
407 CERROR("OBD %d already stopping\n", obd->obd_minor);
410 /* Leave this on forever */
411 obd->obd_stopping = 1;
412 spin_unlock(&obd->obd_dev_lock);
414 if (lcfg->lcfg_bufcount >= 2 && LUSTRE_CFG_BUFLEN(lcfg, 1) > 0) {
415 for (flag = lustre_cfg_string(lcfg, 1); *flag != 0; flag++)
421 LCONSOLE_WARN("Failing over %s\n",
424 obd->obd_no_transno = 1;
425 obd->obd_no_recov = 1;
426 /* Set the obd readonly if we can */
427 if (OBP(obd, iocontrol))
428 obd_iocontrol(OBD_IOC_SET_READONLY,
429 obd->obd_self_export,
433 CERROR("unrecognised flag '%c'\n",
438 /* The three references that should be remaining are the
439 * obd_self_export and the attach and setup references. */
440 if (atomic_read(&obd->obd_refcount) > 3) {
441 #if 0 /* We should never fail to cleanup with mountconf */
442 if (!(obd->obd_fail || obd->obd_force)) {
443 CERROR("OBD %s is still busy with %d references\n"
444 "You should stop active file system users,"
445 " or use the --force option to cleanup.\n",
446 obd->obd_name, atomic_read(&obd->obd_refcount));
448 /* Allow a failed cleanup to try again. */
449 obd->obd_stopping = 0;
452 /* refcounf - 3 might be the number of real exports
453 (excluding self export). But class_incref is called
454 by other things as well, so don't count on it. */
455 CDEBUG(D_IOCTL, "%s: forcing exports to disconnect: %d\n",
456 obd->obd_name, atomic_read(&obd->obd_refcount) - 3);
458 class_disconnect_exports(obd);
460 LASSERT(obd->obd_self_export);
462 /* destroy an uuid-export hash body */
463 lustre_hash_exit(&obd->obd_uuid_hash_body);
465 /* destroy a nid-export hash body */
466 lustre_hash_exit(&obd->obd_nid_hash_body);
468 /* destroy a nid-stats hash body */
469 lustre_hash_exit(&obd->obd_nid_stats_hash_body);
471 /* Precleanup, we must make sure all exports get destroyed. */
472 err = obd_precleanup(obd, OBD_CLEANUP_EXPORTS);
474 CERROR("Precleanup %s returned %d\n",
482 struct obd_device *class_incref(struct obd_device *obd)
484 atomic_inc(&obd->obd_refcount);
485 CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
486 atomic_read(&obd->obd_refcount));
491 void class_decref(struct obd_device *obd)
496 spin_lock(&obd->obd_dev_lock);
497 atomic_dec(&obd->obd_refcount);
498 refs = atomic_read(&obd->obd_refcount);
499 spin_unlock(&obd->obd_dev_lock);
501 CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
503 if ((refs == 1) && obd->obd_stopping) {
504 /* All exports have been destroyed; there should
505 be no more in-progress ops by this point.*/
507 spin_lock(&obd->obd_self_export->exp_lock);
508 obd->obd_self_export->exp_flags |=
509 (obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
510 (obd->obd_force ? OBD_OPT_FORCE : 0);
511 spin_unlock(&obd->obd_self_export->exp_lock);
513 /* note that we'll recurse into class_decref again */
514 class_unlink_export(obd->obd_self_export);
519 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
520 obd->obd_name, obd->obd_uuid.uuid);
521 LASSERT(!obd->obd_attached);
522 if (obd->obd_stopping) {
523 /* If we're not stopping, we were never set up */
524 err = obd_cleanup(obd);
526 CERROR("Cleanup %s returned %d\n",
529 if (OBP(obd, detach)) {
530 err = OBP(obd,detach)(obd);
532 CERROR("Detach returned %d\n", err);
534 class_release_dev(obd);
538 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
540 struct obd_import *imp;
541 struct obd_uuid uuid;
545 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
546 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
547 CERROR("invalid conn_uuid\n");
550 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
551 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
552 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
553 CERROR("can't add connection on non-client dev\n");
557 imp = obd->u.cli.cl_import;
559 CERROR("try to add conn on immature client dev\n");
563 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
564 rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
569 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
571 struct obd_import *imp;
572 struct obd_uuid uuid;
576 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
577 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
578 CERROR("invalid conn_uuid\n");
581 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
582 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
583 CERROR("can't del connection on non-client dev\n");
587 imp = obd->u.cli.cl_import;
589 CERROR("try to del conn on immature client dev\n");
593 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
594 rc = obd_del_conn(imp, &uuid);
599 CFS_LIST_HEAD(lustre_profile_list);
601 struct lustre_profile *class_get_profile(const char * prof)
603 struct lustre_profile *lprof;
606 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
607 if (!strcmp(lprof->lp_profile, prof)) {
614 int class_add_profile(int proflen, char *prof, int osclen, char *osc,
615 int mdclen, char *mdc)
617 struct lustre_profile *lprof;
621 CDEBUG(D_CONFIG, "Add profile %s\n", prof);
623 OBD_ALLOC(lprof, sizeof(*lprof));
626 CFS_INIT_LIST_HEAD(&lprof->lp_list);
628 LASSERT(proflen == (strlen(prof) + 1));
629 OBD_ALLOC(lprof->lp_profile, proflen);
630 if (lprof->lp_profile == NULL)
631 GOTO(out, err = -ENOMEM);
632 memcpy(lprof->lp_profile, prof, proflen);
634 LASSERT(osclen == (strlen(osc) + 1));
635 OBD_ALLOC(lprof->lp_dt, osclen);
636 if (lprof->lp_dt == NULL)
637 GOTO(out, err = -ENOMEM);
638 memcpy(lprof->lp_dt, osc, osclen);
641 LASSERT(mdclen == (strlen(mdc) + 1));
642 OBD_ALLOC(lprof->lp_md, mdclen);
643 if (lprof->lp_md == NULL)
644 GOTO(out, err = -ENOMEM);
645 memcpy(lprof->lp_md, mdc, mdclen);
648 list_add(&lprof->lp_list, &lustre_profile_list);
653 OBD_FREE(lprof->lp_md, mdclen);
655 OBD_FREE(lprof->lp_dt, osclen);
656 if (lprof->lp_profile)
657 OBD_FREE(lprof->lp_profile, proflen);
658 OBD_FREE(lprof, sizeof(*lprof));
662 void class_del_profile(const char *prof)
664 struct lustre_profile *lprof;
667 CDEBUG(D_CONFIG, "Del profile %s\n", prof);
669 lprof = class_get_profile(prof);
671 list_del(&lprof->lp_list);
672 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
673 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
675 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
676 OBD_FREE(lprof, sizeof *lprof);
682 void class_del_profiles(void)
684 struct lustre_profile *lprof, *n;
687 list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
688 list_del(&lprof->lp_list);
689 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
690 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
692 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
693 OBD_FREE(lprof, sizeof *lprof);
698 /* We can't call ll_process_config directly because it lives in a module that
699 must be loaded after this one. */
700 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
702 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
704 client_process_config = cpc;
706 EXPORT_SYMBOL(lustre_register_client_process_config);
708 int class_process_config(struct lustre_cfg *lcfg)
710 struct obd_device *obd;
713 LASSERT(lcfg && !IS_ERR(lcfg));
714 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
716 /* Commands that don't need a device */
717 switch(lcfg->lcfg_command) {
719 err = class_attach(lcfg);
722 case LCFG_ADD_UUID: {
723 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
724 " (%s)\n", lustre_cfg_string(lcfg, 1),
725 lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
727 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
730 case LCFG_DEL_UUID: {
731 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
732 (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
733 ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
735 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
738 case LCFG_MOUNTOPT: {
739 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
740 lustre_cfg_string(lcfg, 1),
741 lustre_cfg_string(lcfg, 2),
742 lustre_cfg_string(lcfg, 3));
743 /* set these mount options somewhere, so ll_fill_super
745 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
746 lustre_cfg_string(lcfg, 1),
747 LUSTRE_CFG_BUFLEN(lcfg, 2),
748 lustre_cfg_string(lcfg, 2),
749 LUSTRE_CFG_BUFLEN(lcfg, 3),
750 lustre_cfg_string(lcfg, 3));
753 case LCFG_DEL_MOUNTOPT: {
754 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
755 lustre_cfg_string(lcfg, 1));
756 class_del_profile(lustre_cfg_string(lcfg, 1));
759 case LCFG_SET_TIMEOUT: {
760 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
761 obd_timeout, lcfg->lcfg_num);
762 obd_timeout = max(lcfg->lcfg_num, 1U);
765 case LCFG_SET_UPCALL: {
766 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
767 /* COMPAT_146 Don't fail on old configs */
771 struct cfg_marker *marker;
772 marker = lustre_cfg_buf(lcfg, 1);
773 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
774 marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
778 /* llite has no obd */
779 if ((class_match_param(lustre_cfg_string(lcfg, 1),
780 PARAM_LLITE, 0) == 0) &&
781 client_process_config) {
782 err = (*client_process_config)(lcfg);
790 /* Commands that require a device */
791 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
793 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
794 CERROR("this lcfg command requires a device name\n");
796 CERROR("no device for: %s\n",
797 lustre_cfg_string(lcfg, 0));
799 GOTO(out, err = -EINVAL);
802 switch(lcfg->lcfg_command) {
804 err = class_setup(obd, lcfg);
808 err = class_detach(obd, lcfg);
812 err = class_cleanup(obd, lcfg);
815 case LCFG_ADD_CONN: {
816 err = class_add_conn(obd, lcfg);
819 case LCFG_DEL_CONN: {
820 err = class_del_conn(obd, lcfg);
824 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
830 if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
831 CWARN("Ignoring error %d on optional command %#x\n", err,
838 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
839 struct lustre_cfg *lcfg, void *data)
842 struct lprocfs_vars *var;
844 int i, keylen, vallen;
845 int matched = 0, j = 0;
849 if (lcfg->lcfg_command != LCFG_PARAM) {
850 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
854 /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
855 or lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
856 or lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
857 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
858 key = lustre_cfg_buf(lcfg, i);
859 /* Strip off prefix */
860 class_match_param(key, prefix, &key);
861 sval = strchr(key, '=');
862 if (!sval || (*(sval + 1) == 0)) {
863 CERROR("Can't parse param %s\n", key);
864 /* rc = -EINVAL; continue parsing other params */
869 vallen = strlen(sval);
872 /* Search proc entries */
873 while (lvars[j].name) {
875 if (class_match_param(key, (char *)var->name, 0) == 0 &&
876 keylen == strlen(var->name)) {
879 if (var->write_fptr) {
883 rc = (var->write_fptr)(NULL, sval,
888 CERROR("writing proc entry %s err %d\n",
895 CERROR("%s: unknown param %s\n",
896 (char *)lustre_cfg_string(lcfg, 0), key);
897 /* rc = -EINVAL; continue parsing other params */
899 LCONSOLE_INFO("%s.%.*s: set parameter %.*s=%s\n",
900 (char *)lustre_cfg_string(lcfg, 0),
901 (int)strlen(prefix) - 1, prefix,
902 (int)(sval - key - 1), key, sval);
910 CDEBUG(D_CONFIG, "liblustre can't process params.\n");
911 /* Don't throw config error */
916 int class_config_dump_handler(struct llog_handle * handle,
917 struct llog_rec_hdr *rec, void *data);
920 extern int lustre_check_exclusion(struct super_block *sb, char *svname);
922 #define lustre_check_exclusion(a,b) 0
925 static int class_config_llog_handler(struct llog_handle * handle,
926 struct llog_rec_hdr *rec, void *data)
928 struct config_llog_instance *clli = data;
929 int cfg_len = rec->lrh_len;
930 char *cfg_buf = (char*) (rec + 1);
934 //class_config_dump_handler(handle, rec, data);
936 switch (rec->lrh_type) {
938 struct lustre_cfg *lcfg, *lcfg_new;
939 struct lustre_cfg_bufs bufs;
940 char *inst_name = NULL;
944 lcfg = (struct lustre_cfg *)cfg_buf;
945 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION))
946 lustre_swab_lustre_cfg(lcfg);
948 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
952 /* Figure out config state info */
953 if (lcfg->lcfg_command == LCFG_MARKER) {
954 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
955 CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
956 clli->cfg_flags, marker->cm_flags);
957 if (marker->cm_flags & CM_START) {
958 /* all previous flags off */
959 clli->cfg_flags = CFG_F_MARKER;
960 if (marker->cm_flags & CM_SKIP) {
961 clli->cfg_flags |= CFG_F_SKIP;
962 CDEBUG(D_CONFIG, "SKIP #%d\n",
964 } else if ((marker->cm_flags & CM_EXCLUDE) ||
965 lustre_check_exclusion(clli->cfg_sb,
966 marker->cm_tgtname)) {
967 clli->cfg_flags |= CFG_F_EXCLUDE;
968 CDEBUG(D_CONFIG, "EXCLUDE %d\n",
971 } else if (marker->cm_flags & CM_END) {
975 /* A config command without a start marker before it is
976 illegal (post 146) */
977 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
978 !(clli->cfg_flags & CFG_F_MARKER) &&
979 (lcfg->lcfg_command != LCFG_MARKER)) {
980 CWARN("Config not inside markers, ignoring! "
981 "(inst: %s, uuid: %s, flags: %#x)\n",
982 clli->cfg_instance ? clli->cfg_instance : "<null>",
983 clli->cfg_uuid.uuid, clli->cfg_flags);
984 clli->cfg_flags |= CFG_F_SKIP;
986 if (clli->cfg_flags & CFG_F_SKIP) {
987 CDEBUG(D_CONFIG, "skipping %#x\n",
994 if ((clli->cfg_flags & CFG_F_EXCLUDE) &&
995 (lcfg->lcfg_command == LCFG_LOV_ADD_OBD))
996 /* Add inactive instead */
997 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
999 lustre_cfg_bufs_init(&bufs, lcfg);
1001 if (clli && clli->cfg_instance &&
1002 LUSTRE_CFG_BUFLEN(lcfg, 0) > 0){
1004 inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1005 strlen(clli->cfg_instance) + 1;
1006 OBD_ALLOC(inst_name, inst_len);
1007 if (inst_name == NULL)
1008 GOTO(out, rc = -ENOMEM);
1009 sprintf(inst_name, "%s-%s",
1010 lustre_cfg_string(lcfg, 0),
1011 clli->cfg_instance);
1012 lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1013 CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1014 lcfg->lcfg_command, inst_name);
1017 /* we override the llog's uuid for clients, to insure they
1019 if (clli && clli->cfg_instance &&
1020 lcfg->lcfg_command == LCFG_ATTACH) {
1021 lustre_cfg_bufs_set_string(&bufs, 2,
1022 clli->cfg_uuid.uuid);
1025 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1027 lcfg_new->lcfg_num = lcfg->lcfg_num;
1028 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1030 /* XXX Hack to try to remain binary compatible with
1031 * pre-newconfig logs */
1032 if (lcfg->lcfg_nal != 0 && /* pre-newconfig log? */
1033 (lcfg->lcfg_nid >> 32) == 0) {
1034 __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1036 lcfg_new->lcfg_nid =
1037 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1038 CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1039 lcfg->lcfg_nal, addr,
1040 libcfs_nid2str(lcfg_new->lcfg_nid));
1042 lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1045 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1047 rc = class_process_config(lcfg_new);
1048 lustre_cfg_free(lcfg_new);
1051 OBD_FREE(inst_name, inst_len);
1055 CERROR("Unknown llog record type %#x encountered\n",
1061 CERROR("Err %d on cfg command:\n", rc);
1062 class_config_dump_handler(handle, rec, data);
1067 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
1068 struct config_llog_instance *cfg)
1070 struct llog_process_cat_data cd = {0, 0};
1071 struct llog_handle *llh;
1075 CDEBUG(D_INFO, "looking up llog %s\n", name);
1076 rc = llog_create(ctxt, &llh, NULL, name);
1080 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1082 GOTO(parse_out, rc);
1084 /* continue processing from where we last stopped to end-of-log */
1086 cd.first_idx = cfg->cfg_last_idx;
1089 rc = llog_process(llh, class_config_llog_handler, cfg, &cd);
1091 CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1092 cd.first_idx + 1, cd.last_idx, rc);
1095 cfg->cfg_last_idx = cd.last_idx;
1098 rc2 = llog_close(llh);
1105 int class_config_dump_handler(struct llog_handle * handle,
1106 struct llog_rec_hdr *rec, void *data)
1108 int cfg_len = rec->lrh_len;
1109 char *cfg_buf = (char*) (rec + 1);
1110 char *outstr, *ptr, *end;
1114 OBD_ALLOC(outstr, 256);
1120 if (rec->lrh_type == OBD_CFG_REC) {
1121 struct lustre_cfg *lcfg;
1124 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1127 lcfg = (struct lustre_cfg *)cfg_buf;
1129 ptr += snprintf(ptr, end-ptr, "cmd=%05x ",
1130 lcfg->lcfg_command);
1131 if (lcfg->lcfg_flags) {
1132 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1135 if (lcfg->lcfg_num) {
1136 ptr += snprintf(ptr, end-ptr, "num=%#08x ",
1139 if (lcfg->lcfg_nid) {
1140 ptr += snprintf(ptr, end-ptr, "nid=%s("LPX64")\n ",
1141 libcfs_nid2str(lcfg->lcfg_nid),
1144 if (lcfg->lcfg_command == LCFG_MARKER) {
1145 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1146 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1147 marker->cm_step, marker->cm_flags,
1148 marker->cm_tgtname, marker->cm_comment);
1150 for (i = 0; i < lcfg->lcfg_bufcount; i++) {
1151 ptr += snprintf(ptr, end-ptr, "%d:%s ", i,
1152 lustre_cfg_string(lcfg, i));
1155 LCONSOLE(D_WARNING, " %s\n", outstr);
1157 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1161 OBD_FREE(outstr, 256);
1165 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
1166 struct config_llog_instance *cfg)
1168 struct llog_handle *llh;
1172 LCONSOLE_INFO("Dumping config log %s\n", name);
1174 rc = llog_create(ctxt, &llh, NULL, name);
1178 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1180 GOTO(parse_out, rc);
1182 rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
1184 rc2 = llog_close(llh);
1188 LCONSOLE_INFO("End config log %s\n", name);
1193 /* Cleanup and detach */
1194 int class_manual_cleanup(struct obd_device *obd)
1196 struct lustre_cfg *lcfg;
1197 struct lustre_cfg_bufs bufs;
1203 CERROR("empty cleanup\n");
1212 CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1213 obd->obd_name, flags);
1215 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1216 lustre_cfg_bufs_set_string(&bufs, 1, flags);
1217 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1219 rc = class_process_config(lcfg);
1221 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1225 /* the lcfg is almost the same for both ops */
1226 lcfg->lcfg_command = LCFG_DETACH;
1227 rc = class_process_config(lcfg);
1229 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1231 lustre_cfg_free(lcfg);