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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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",
481 struct obd_device *class_incref(struct obd_device *obd)
483 atomic_inc(&obd->obd_refcount);
484 CDEBUG(D_INFO, "incref %s (%p) now %d\n", obd->obd_name, obd,
485 atomic_read(&obd->obd_refcount));
490 void class_decref(struct obd_device *obd)
495 spin_lock(&obd->obd_dev_lock);
496 atomic_dec(&obd->obd_refcount);
497 refs = atomic_read(&obd->obd_refcount);
498 spin_unlock(&obd->obd_dev_lock);
500 CDEBUG(D_INFO, "Decref %s (%p) now %d\n", obd->obd_name, obd, refs);
502 if ((refs == 1) && obd->obd_stopping) {
503 /* All exports have been destroyed; there should
504 be no more in-progress ops by this point.*/
506 spin_lock(&obd->obd_self_export->exp_lock);
507 obd->obd_self_export->exp_flags |=
508 (obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
509 (obd->obd_force ? OBD_OPT_FORCE : 0);
510 spin_unlock(&obd->obd_self_export->exp_lock);
512 /* note that we'll recurse into class_decref again */
513 class_unlink_export(obd->obd_self_export);
518 CDEBUG(D_CONFIG, "finishing cleanup of obd %s (%s)\n",
519 obd->obd_name, obd->obd_uuid.uuid);
520 LASSERT(!obd->obd_attached);
521 if (obd->obd_stopping) {
522 /* If we're not stopping, we were never set up */
523 err = obd_cleanup(obd);
525 CERROR("Cleanup %s returned %d\n",
528 if (OBP(obd, detach)) {
529 err = OBP(obd,detach)(obd);
531 CERROR("Detach returned %d\n", err);
533 class_release_dev(obd);
537 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
539 struct obd_import *imp;
540 struct obd_uuid uuid;
544 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
545 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
546 CERROR("invalid conn_uuid\n");
549 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
550 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
551 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
552 CERROR("can't add connection on non-client dev\n");
556 imp = obd->u.cli.cl_import;
558 CERROR("try to add conn on immature client dev\n");
562 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
563 rc = obd_add_conn(imp, &uuid, lcfg->lcfg_num);
568 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
570 struct obd_import *imp;
571 struct obd_uuid uuid;
575 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1 ||
576 LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(struct obd_uuid)) {
577 CERROR("invalid conn_uuid\n");
580 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
581 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME)) {
582 CERROR("can't del connection on non-client dev\n");
586 imp = obd->u.cli.cl_import;
588 CERROR("try to del conn on immature client dev\n");
592 obd_str2uuid(&uuid, lustre_cfg_string(lcfg, 1));
593 rc = obd_del_conn(imp, &uuid);
598 CFS_LIST_HEAD(lustre_profile_list);
600 struct lustre_profile *class_get_profile(const char * prof)
602 struct lustre_profile *lprof;
605 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
606 if (!strcmp(lprof->lp_profile, prof)) {
613 int class_add_profile(int proflen, char *prof, int osclen, char *osc,
614 int mdclen, char *mdc)
616 struct lustre_profile *lprof;
620 CDEBUG(D_CONFIG, "Add profile %s\n", prof);
622 OBD_ALLOC(lprof, sizeof(*lprof));
625 CFS_INIT_LIST_HEAD(&lprof->lp_list);
627 LASSERT(proflen == (strlen(prof) + 1));
628 OBD_ALLOC(lprof->lp_profile, proflen);
629 if (lprof->lp_profile == NULL)
630 GOTO(out, err = -ENOMEM);
631 memcpy(lprof->lp_profile, prof, proflen);
633 LASSERT(osclen == (strlen(osc) + 1));
634 OBD_ALLOC(lprof->lp_dt, osclen);
635 if (lprof->lp_dt == NULL)
636 GOTO(out, err = -ENOMEM);
637 memcpy(lprof->lp_dt, osc, osclen);
640 LASSERT(mdclen == (strlen(mdc) + 1));
641 OBD_ALLOC(lprof->lp_md, mdclen);
642 if (lprof->lp_md == NULL)
643 GOTO(out, err = -ENOMEM);
644 memcpy(lprof->lp_md, mdc, mdclen);
647 list_add(&lprof->lp_list, &lustre_profile_list);
652 OBD_FREE(lprof->lp_md, mdclen);
654 OBD_FREE(lprof->lp_dt, osclen);
655 if (lprof->lp_profile)
656 OBD_FREE(lprof->lp_profile, proflen);
657 OBD_FREE(lprof, sizeof(*lprof));
661 void class_del_profile(const char *prof)
663 struct lustre_profile *lprof;
666 CDEBUG(D_CONFIG, "Del profile %s\n", prof);
668 lprof = class_get_profile(prof);
670 list_del(&lprof->lp_list);
671 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
672 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
674 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
675 OBD_FREE(lprof, sizeof *lprof);
681 void class_del_profiles(void)
683 struct lustre_profile *lprof, *n;
686 list_for_each_entry_safe(lprof, n, &lustre_profile_list, lp_list) {
687 list_del(&lprof->lp_list);
688 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
689 OBD_FREE(lprof->lp_dt, strlen(lprof->lp_dt) + 1);
691 OBD_FREE(lprof->lp_md, strlen(lprof->lp_md) + 1);
692 OBD_FREE(lprof, sizeof *lprof);
697 /* We can't call ll_process_config directly because it lives in a module that
698 must be loaded after this one. */
699 static int (*client_process_config)(struct lustre_cfg *lcfg) = NULL;
701 void lustre_register_client_process_config(int (*cpc)(struct lustre_cfg *lcfg))
703 client_process_config = cpc;
705 EXPORT_SYMBOL(lustre_register_client_process_config);
707 int class_process_config(struct lustre_cfg *lcfg)
709 struct obd_device *obd;
712 LASSERT(lcfg && !IS_ERR(lcfg));
713 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
715 /* Commands that don't need a device */
716 switch(lcfg->lcfg_command) {
718 err = class_attach(lcfg);
721 case LCFG_ADD_UUID: {
722 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
723 " (%s)\n", lustre_cfg_string(lcfg, 1),
724 lcfg->lcfg_nid, libcfs_nid2str(lcfg->lcfg_nid));
726 err = class_add_uuid(lustre_cfg_string(lcfg, 1), lcfg->lcfg_nid);
729 case LCFG_DEL_UUID: {
730 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
731 (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) == 0)
732 ? "<all uuids>" : lustre_cfg_string(lcfg, 1));
734 err = class_del_uuid(lustre_cfg_string(lcfg, 1));
737 case LCFG_MOUNTOPT: {
738 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
739 lustre_cfg_string(lcfg, 1),
740 lustre_cfg_string(lcfg, 2),
741 lustre_cfg_string(lcfg, 3));
742 /* set these mount options somewhere, so ll_fill_super
744 err = class_add_profile(LUSTRE_CFG_BUFLEN(lcfg, 1),
745 lustre_cfg_string(lcfg, 1),
746 LUSTRE_CFG_BUFLEN(lcfg, 2),
747 lustre_cfg_string(lcfg, 2),
748 LUSTRE_CFG_BUFLEN(lcfg, 3),
749 lustre_cfg_string(lcfg, 3));
752 case LCFG_DEL_MOUNTOPT: {
753 CDEBUG(D_IOCTL, "mountopt: profile %s\n",
754 lustre_cfg_string(lcfg, 1));
755 class_del_profile(lustre_cfg_string(lcfg, 1));
758 case LCFG_SET_TIMEOUT: {
759 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
760 obd_timeout, lcfg->lcfg_num);
761 obd_timeout = max(lcfg->lcfg_num, 1U);
764 case LCFG_SET_UPCALL: {
765 LCONSOLE_ERROR_MSG(0x15a, "recovery upcall is deprecated\n");
766 /* COMPAT_146 Don't fail on old configs */
770 struct cfg_marker *marker;
771 marker = lustre_cfg_buf(lcfg, 1);
772 CDEBUG(D_IOCTL, "marker %d (%#x) %.16s %s\n", marker->cm_step,
773 marker->cm_flags, marker->cm_tgtname, marker->cm_comment);
777 /* llite has no obd */
778 if ((class_match_param(lustre_cfg_string(lcfg, 1),
779 PARAM_LLITE, 0) == 0) &&
780 client_process_config) {
781 err = (*client_process_config)(lcfg);
789 /* Commands that require a device */
790 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
792 if (!LUSTRE_CFG_BUFLEN(lcfg, 0))
793 CERROR("this lcfg command requires a device name\n");
795 CERROR("no device for: %s\n",
796 lustre_cfg_string(lcfg, 0));
798 GOTO(out, err = -EINVAL);
801 switch(lcfg->lcfg_command) {
803 err = class_setup(obd, lcfg);
807 err = class_detach(obd, lcfg);
811 err = class_cleanup(obd, lcfg);
814 case LCFG_ADD_CONN: {
815 err = class_add_conn(obd, lcfg);
818 case LCFG_DEL_CONN: {
819 err = class_del_conn(obd, lcfg);
823 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
829 if ((err < 0) && !(lcfg->lcfg_command & LCFG_REQUIRED)) {
830 CWARN("Ignoring error %d on optional command %#x\n", err,
837 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
838 struct lustre_cfg *lcfg, void *data)
841 struct lprocfs_vars *var;
843 int i, keylen, vallen;
844 int matched = 0, j = 0;
848 if (lcfg->lcfg_command != LCFG_PARAM) {
849 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
853 /* e.g. tunefs.lustre --param mdt.group_upcall=foo /r/tmp/lustre-mdt
854 or lctl conf_param lustre-MDT0000.mdt.group_upcall=bar
855 or lctl conf_param lustre-OST0000.osc.max_dirty_mb=36 */
856 for (i = 1; i < lcfg->lcfg_bufcount; i++) {
857 key = lustre_cfg_buf(lcfg, i);
858 /* Strip off prefix */
859 class_match_param(key, prefix, &key);
860 sval = strchr(key, '=');
861 if (!sval || (*(sval + 1) == 0)) {
862 CERROR("Can't parse param %s\n", key);
863 /* rc = -EINVAL; continue parsing other params */
868 vallen = strlen(sval);
871 /* Search proc entries */
872 while (lvars[j].name) {
874 if (class_match_param(key, (char *)var->name, 0) == 0 &&
875 keylen == strlen(var->name)) {
878 if (var->write_fptr) {
882 rc = (var->write_fptr)(NULL, sval,
887 CERROR("writing proc entry %s err %d\n",
894 CERROR("%s: unknown param %s\n",
895 (char *)lustre_cfg_string(lcfg, 0), key);
896 /* rc = -EINVAL; continue parsing other params */
898 LCONSOLE_INFO("%s.%.*s: set parameter %.*s=%s\n",
899 (char *)lustre_cfg_string(lcfg, 0),
900 (int)strlen(prefix) - 1, prefix,
901 (int)(sval - key - 1), key, sval);
909 CDEBUG(D_CONFIG, "liblustre can't process params.\n");
910 /* Don't throw config error */
915 int class_config_dump_handler(struct llog_handle * handle,
916 struct llog_rec_hdr *rec, void *data);
919 extern int lustre_check_exclusion(struct super_block *sb, char *svname);
921 #define lustre_check_exclusion(a,b) 0
924 static int class_config_llog_handler(struct llog_handle * handle,
925 struct llog_rec_hdr *rec, void *data)
927 struct config_llog_instance *clli = data;
928 int cfg_len = rec->lrh_len;
929 char *cfg_buf = (char*) (rec + 1);
933 //class_config_dump_handler(handle, rec, data);
935 switch (rec->lrh_type) {
937 struct lustre_cfg *lcfg, *lcfg_new;
938 struct lustre_cfg_bufs bufs;
939 char *inst_name = NULL;
943 lcfg = (struct lustre_cfg *)cfg_buf;
944 if (lcfg->lcfg_version == __swab32(LUSTRE_CFG_VERSION))
945 lustre_swab_lustre_cfg(lcfg);
947 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
951 /* Figure out config state info */
952 if (lcfg->lcfg_command == LCFG_MARKER) {
953 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
954 CDEBUG(D_CONFIG, "Marker, inst_flg=%#x mark_flg=%#x\n",
955 clli->cfg_flags, marker->cm_flags);
956 if (marker->cm_flags & CM_START) {
957 /* all previous flags off */
958 clli->cfg_flags = CFG_F_MARKER;
959 if (marker->cm_flags & CM_SKIP) {
960 clli->cfg_flags |= CFG_F_SKIP;
961 CDEBUG(D_CONFIG, "SKIP #%d\n",
963 } else if ((marker->cm_flags & CM_EXCLUDE) ||
964 lustre_check_exclusion(clli->cfg_sb,
965 marker->cm_tgtname)) {
966 clli->cfg_flags |= CFG_F_EXCLUDE;
967 CDEBUG(D_CONFIG, "EXCLUDE %d\n",
970 } else if (marker->cm_flags & CM_END) {
974 /* A config command without a start marker before it is
975 illegal (post 146) */
976 if (!(clli->cfg_flags & CFG_F_COMPAT146) &&
977 !(clli->cfg_flags & CFG_F_MARKER) &&
978 (lcfg->lcfg_command != LCFG_MARKER)) {
979 CWARN("Config not inside markers, ignoring! "
980 "(inst: %s, uuid: %s, flags: %#x)\n",
981 clli->cfg_instance ? clli->cfg_instance : "<null>",
982 clli->cfg_uuid.uuid, clli->cfg_flags);
983 clli->cfg_flags |= CFG_F_SKIP;
985 if (clli->cfg_flags & CFG_F_SKIP) {
986 CDEBUG(D_CONFIG, "skipping %#x\n",
993 if ((clli->cfg_flags & CFG_F_EXCLUDE) &&
994 (lcfg->lcfg_command == LCFG_LOV_ADD_OBD))
995 /* Add inactive instead */
996 lcfg->lcfg_command = LCFG_LOV_ADD_INA;
998 lustre_cfg_bufs_init(&bufs, lcfg);
1000 if (clli && clli->cfg_instance &&
1001 LUSTRE_CFG_BUFLEN(lcfg, 0) > 0){
1003 inst_len = LUSTRE_CFG_BUFLEN(lcfg, 0) +
1004 strlen(clli->cfg_instance) + 1;
1005 OBD_ALLOC(inst_name, inst_len);
1006 if (inst_name == NULL)
1007 GOTO(out, rc = -ENOMEM);
1008 sprintf(inst_name, "%s-%s",
1009 lustre_cfg_string(lcfg, 0),
1010 clli->cfg_instance);
1011 lustre_cfg_bufs_set_string(&bufs, 0, inst_name);
1012 CDEBUG(D_CONFIG, "cmd %x, instance name: %s\n",
1013 lcfg->lcfg_command, inst_name);
1016 /* we override the llog's uuid for clients, to insure they
1018 if (clli && clli->cfg_instance &&
1019 lcfg->lcfg_command == LCFG_ATTACH) {
1020 lustre_cfg_bufs_set_string(&bufs, 2,
1021 clli->cfg_uuid.uuid);
1024 lcfg_new = lustre_cfg_new(lcfg->lcfg_command, &bufs);
1026 lcfg_new->lcfg_num = lcfg->lcfg_num;
1027 lcfg_new->lcfg_flags = lcfg->lcfg_flags;
1029 /* XXX Hack to try to remain binary compatible with
1030 * pre-newconfig logs */
1031 if (lcfg->lcfg_nal != 0 && /* pre-newconfig log? */
1032 (lcfg->lcfg_nid >> 32) == 0) {
1033 __u32 addr = (__u32)(lcfg->lcfg_nid & 0xffffffff);
1035 lcfg_new->lcfg_nid =
1036 LNET_MKNID(LNET_MKNET(lcfg->lcfg_nal, 0), addr);
1037 CWARN("Converted pre-newconfig NAL %d NID %x to %s\n",
1038 lcfg->lcfg_nal, addr,
1039 libcfs_nid2str(lcfg_new->lcfg_nid));
1041 lcfg_new->lcfg_nid = lcfg->lcfg_nid;
1044 lcfg_new->lcfg_nal = 0; /* illegal value for obsolete field */
1046 rc = class_process_config(lcfg_new);
1047 lustre_cfg_free(lcfg_new);
1050 OBD_FREE(inst_name, inst_len);
1054 CERROR("Unknown llog record type %#x encountered\n",
1060 CERROR("Err %d on cfg command:\n", rc);
1061 class_config_dump_handler(handle, rec, data);
1066 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
1067 struct config_llog_instance *cfg)
1069 struct llog_process_cat_data cd = {0, 0};
1070 struct llog_handle *llh;
1074 CDEBUG(D_INFO, "looking up llog %s\n", name);
1075 rc = llog_create(ctxt, &llh, NULL, name);
1079 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1081 GOTO(parse_out, rc);
1083 /* continue processing from where we last stopped to end-of-log */
1085 cd.lpcd_first_idx = cfg->cfg_last_idx;
1086 cd.lpcd_last_idx = 0;
1088 rc = llog_process(llh, class_config_llog_handler, cfg, &cd);
1090 CDEBUG(D_CONFIG, "Processed log %s gen %d-%d (rc=%d)\n", name,
1091 cd.lpcd_first_idx + 1, cd.lpcd_last_idx, rc);
1094 cfg->cfg_last_idx = cd.lpcd_last_idx;
1097 rc2 = llog_close(llh);
1104 int class_config_dump_handler(struct llog_handle * handle,
1105 struct llog_rec_hdr *rec, void *data)
1107 int cfg_len = rec->lrh_len;
1108 char *cfg_buf = (char*) (rec + 1);
1109 char *outstr, *ptr, *end;
1113 OBD_ALLOC(outstr, 256);
1119 if (rec->lrh_type == OBD_CFG_REC) {
1120 struct lustre_cfg *lcfg;
1123 rc = lustre_cfg_sanity_check(cfg_buf, cfg_len);
1126 lcfg = (struct lustre_cfg *)cfg_buf;
1128 ptr += snprintf(ptr, end-ptr, "cmd=%05x ",
1129 lcfg->lcfg_command);
1130 if (lcfg->lcfg_flags) {
1131 ptr += snprintf(ptr, end-ptr, "flags=%#08x ",
1134 if (lcfg->lcfg_num) {
1135 ptr += snprintf(ptr, end-ptr, "num=%#08x ",
1138 if (lcfg->lcfg_nid) {
1139 ptr += snprintf(ptr, end-ptr, "nid=%s("LPX64")\n ",
1140 libcfs_nid2str(lcfg->lcfg_nid),
1143 if (lcfg->lcfg_command == LCFG_MARKER) {
1144 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
1145 ptr += snprintf(ptr, end-ptr, "marker=%d(%#x)%s '%s'",
1146 marker->cm_step, marker->cm_flags,
1147 marker->cm_tgtname, marker->cm_comment);
1149 for (i = 0; i < lcfg->lcfg_bufcount; i++) {
1150 ptr += snprintf(ptr, end-ptr, "%d:%s ", i,
1151 lustre_cfg_string(lcfg, i));
1154 LCONSOLE(D_WARNING, " %s\n", outstr);
1156 LCONSOLE(D_WARNING, "unhandled lrh_type: %#x\n", rec->lrh_type);
1160 OBD_FREE(outstr, 256);
1164 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
1165 struct config_llog_instance *cfg)
1167 struct llog_handle *llh;
1171 LCONSOLE_INFO("Dumping config log %s\n", name);
1173 rc = llog_create(ctxt, &llh, NULL, name);
1177 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1179 GOTO(parse_out, rc);
1181 rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
1183 rc2 = llog_close(llh);
1187 LCONSOLE_INFO("End config log %s\n", name);
1192 /* Cleanup and detach */
1193 int class_manual_cleanup(struct obd_device *obd)
1195 struct lustre_cfg *lcfg;
1196 struct lustre_cfg_bufs bufs;
1202 CERROR("empty cleanup\n");
1211 CDEBUG(D_CONFIG, "Manual cleanup of %s (flags='%s')\n",
1212 obd->obd_name, flags);
1214 lustre_cfg_bufs_reset(&bufs, obd->obd_name);
1215 lustre_cfg_bufs_set_string(&bufs, 1, flags);
1216 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
1218 rc = class_process_config(lcfg);
1220 CERROR("cleanup failed %d: %s\n", rc, obd->obd_name);
1224 /* the lcfg is almost the same for both ops */
1225 lcfg->lcfg_command = LCFG_DETACH;
1226 rc = class_process_config(lcfg);
1228 CERROR("detach failed %d: %s\n", rc, obd->obd_name);
1230 lustre_cfg_free(lcfg);