1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_CLASS
27 #include <linux/kmod.h> /* for request_module() */
28 #include <linux/module.h>
29 #include <linux/obd_class.h>
30 #include <linux/random.h>
31 #include <linux/slab.h>
32 #include <linux/pagemap.h>
34 #include <liblustre.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
38 #include <linux/lustre_log.h>
39 #include <linux/lprocfs_status.h>
40 #include <libcfs/list.h>
43 /* Create a new device and set the type, name and uuid. If
44 * successful, the new device can be accessed by either name or uuid.
46 static int class_attach(struct lustre_cfg *lcfg)
48 struct obd_type *type;
49 struct obd_device *obd;
50 char *typename, *name, *uuid;
51 int rc, len, cleanup_phase = 0;
53 if (!lcfg->lcfg_inllen1 || !lcfg->lcfg_inlbuf1) {
54 CERROR("No type passed!\n");
57 if (lcfg->lcfg_inlbuf1[lcfg->lcfg_inllen1 - 1] != 0) {
58 CERROR("Type not nul terminated!\n");
61 typename = lcfg->lcfg_inlbuf1;
63 if (!lcfg->lcfg_dev_namelen || !lcfg->lcfg_dev_name) {
64 CERROR("No name passed!\n");
67 if (lcfg->lcfg_dev_name[lcfg->lcfg_dev_namelen - 1] != 0) {
68 CERROR("Name not nul terminated!\n");
71 name = lcfg->lcfg_dev_name;
73 if (!lcfg->lcfg_inllen2 || !lcfg->lcfg_inlbuf2) {
74 CERROR("No UUID passed!\n");
77 if (lcfg->lcfg_inlbuf2[lcfg->lcfg_inllen2 - 1] != 0) {
78 CERROR("UUID not nul terminated!\n");
81 uuid = lcfg->lcfg_inlbuf2;
83 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
84 MKSTR(lcfg->lcfg_inlbuf1), MKSTR(lcfg->lcfg_dev_name),
85 MKSTR(lcfg->lcfg_inlbuf2));
88 type = class_get_type(typename);
90 CERROR("OBD: unknown type: %s\n", typename);
93 cleanup_phase = 1; /* class_put_type */
95 obd = class_name2obd(name);
97 CERROR("obd %s already attached\n", name);
98 GOTO(out, rc = -EEXIST);
101 obd = class_newdev(type);
103 GOTO(out, rc = -EINVAL);
105 cleanup_phase = 2; /* class_release_dev */
107 INIT_LIST_HEAD(&obd->obd_exports);
108 obd->obd_num_exports = 0;
109 spin_lock_init(&obd->obd_dev_lock);
110 spin_lock_init(&obd->obd_osfs_lock);
111 obd->obd_osfs_age = jiffies - 1000 * HZ;
112 init_waitqueue_head(&obd->obd_refcount_waitq);
114 /* XXX belongs in setup not attach */
116 init_timer(&obd->obd_recovery_timer);
117 spin_lock_init(&obd->obd_processing_task_lock);
118 init_waitqueue_head(&obd->obd_next_transno_waitq);
119 INIT_LIST_HEAD(&obd->obd_recovery_queue);
120 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
122 spin_lock_init(&obd->obd_uncommitted_replies_lock);
123 INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
125 len = strlen(name) + 1;
126 OBD_ALLOC(obd->obd_name, len);
128 GOTO(out, rc = -ENOMEM);
129 memcpy(obd->obd_name, name, len);
131 cleanup_phase = 3; /* free obd_name */
134 if (len >= sizeof(obd->obd_uuid)) {
135 CERROR("uuid must be < "LPSZ" bytes long\n",
136 sizeof(obd->obd_uuid));
137 GOTO(out, rc = -EINVAL);
139 memcpy(obd->obd_uuid.uuid, uuid, len);
142 if (OBP(obd, attach)) {
143 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
145 GOTO(out, rc = -EINVAL);
148 obd->obd_attached = 1;
150 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
151 obd->obd_minor, typename);
154 switch (cleanup_phase) {
156 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
158 class_release_dev(obd);
160 class_put_type(type);
165 static int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
168 struct obd_export *exp;
171 LASSERT(obd == (obd_dev + obd->obd_minor));
173 /* have we attached a type to this device? */
174 if (!obd->obd_attached) {
175 CERROR("Device %d not attached\n", obd->obd_minor);
179 /* has this been done already? */
180 if (obd->obd_set_up) {
181 CERROR("Device %d already setup (type %s)\n",
182 obd->obd_minor, obd->obd_type->typ_name);
186 atomic_set(&obd->obd_refcount, 0);
188 exp = class_new_export(obd);
191 memcpy(&exp->exp_client_uuid, &obd->obd_uuid,
192 sizeof(exp->exp_client_uuid));
193 obd->obd_self_export = exp;
194 class_export_put(exp);
196 err = obd_setup(obd, sizeof(*lcfg), lcfg);
200 obd->obd_type->typ_refcnt++;
206 class_unlink_export(obd->obd_self_export);
207 obd->obd_self_export = NULL;
211 static int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
216 if (obd->obd_set_up) {
217 CERROR("OBD device %d still set up\n", obd->obd_minor);
220 if (!obd->obd_attached) {
221 CERROR("OBD device %d not attached\n", obd->obd_minor);
224 if (OBP(obd, detach))
225 err = OBP(obd,detach)(obd);
228 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
229 obd->obd_name = NULL;
231 CERROR("device %d: no name at detach\n", obd->obd_minor);
234 obd->obd_attached = 0;
235 obd->obd_type->typ_refcnt--;
236 class_put_type(obd->obd_type);
237 class_release_dev(obd);
241 static void dump_exports(struct obd_device *obd)
243 struct obd_export *exp, *n;
245 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
246 struct ptlrpc_reply_state *rs;
247 struct ptlrpc_reply_state *first_reply = NULL;
250 list_for_each_entry (rs, &exp->exp_outstanding_replies,
257 CERROR("%s: %p %s %d %d %d: %p %s\n",
258 obd->obd_name, exp, exp->exp_client_uuid.uuid,
259 atomic_read(&exp->exp_refcount),
260 exp->exp_failed, nreplies, first_reply,
261 nreplies > 3 ? "..." : "");
265 static int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
272 if (!obd->obd_set_up) {
273 CERROR("Device %d not setup\n", obd->obd_minor);
277 if (lcfg->lcfg_inlbuf1) {
278 for (flag = lcfg->lcfg_inlbuf1; *flag != 0; flag++)
281 flags |= OBD_OPT_FORCE;
284 flags |= OBD_OPT_FAILOVER;
287 CERROR("unrecognised flag '%c'\n",
292 /* The one reference that should be remaining is the
294 if (atomic_read(&obd->obd_refcount) <= 1 ||
295 flags & OBD_OPT_FORCE) {
296 /* this will stop new connections, and need to
297 do it before class_disconnect_exports() */
298 obd->obd_stopping = 1;
301 if (atomic_read(&obd->obd_refcount) > 1) {
302 struct l_wait_info lwi = LWI_TIMEOUT_INTR(1 * HZ, NULL,
306 if (!(flags & OBD_OPT_FORCE)) {
307 CERROR("OBD device %d (%p,%s) has refcount %d\n",
308 obd->obd_minor, obd, obd->obd_name,
309 atomic_read(&obd->obd_refcount));
311 GOTO(out, err = -EBUSY);
313 class_disconnect_exports(obd, flags);
315 "%s: waiting for obd refs to go away: %d\n",
316 obd->obd_name, atomic_read(&obd->obd_refcount));
318 rc = l_wait_event(obd->obd_refcount_waitq,
319 atomic_read(&obd->obd_refcount) < 2, &lwi);
321 LASSERT(atomic_read(&obd->obd_refcount) == 1);
323 CERROR("wait cancelled cleaning anyway. "
325 atomic_read(&obd->obd_refcount));
328 CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n",
332 if (obd->obd_self_export) {
333 err = obd_precleanup(obd, flags);
336 class_unlink_export(obd->obd_self_export);
337 obd->obd_self_export = NULL;
340 err = obd_cleanup(obd, flags);
343 obd->obd_set_up = obd->obd_stopping = 0;
344 obd->obd_type->typ_refcnt--;
345 /* XXX this should be an LASSERT */
346 if (atomic_read(&obd->obd_refcount) > 0) {
347 CERROR("%s (%p) still has refcount %d after "
348 "cleanup.\n", obd->obd_name, obd,
349 atomic_read(&obd->obd_refcount));
357 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
359 struct obd_import *imp;
360 struct obd_uuid uuid;
364 if (lcfg->lcfg_inllen1 <= 0 ||
365 lcfg->lcfg_inllen1 > sizeof(struct obd_uuid)) {
366 CERROR("invalid conn_uuid\n");
369 if (lcfg->lcfg_inllen2 != sizeof(int)) {
370 CERROR("invalid priority\n");
373 if (strcmp(obd->obd_type->typ_name, "mdc") &&
374 strcmp(obd->obd_type->typ_name, "osc")) {
375 CERROR("can't add connection on non-client dev\n");
379 imp = obd->u.cli.cl_import;
381 CERROR("try to add conn on immature client dev\n");
385 obd_str2uuid(&uuid, lcfg->lcfg_inlbuf1);
386 priority = *((int*) lcfg->lcfg_inlbuf2);
387 rc = obd_add_conn(imp, &uuid, priority);
391 int class_del_conn(struct obd_device *obd, struct lustre_cfg *lcfg)
393 struct obd_import *imp;
394 struct obd_uuid uuid;
398 if (lcfg->lcfg_inllen1 <= 0 ||
399 lcfg->lcfg_inllen1 > sizeof(struct obd_uuid)) {
400 CERROR("invalid conn_uuid\n");
403 if (strcmp(obd->obd_type->typ_name, "mdc") &&
404 strcmp(obd->obd_type->typ_name, "osc")) {
405 CERROR("can't add connection on non-client dev\n");
409 imp = obd->u.cli.cl_import;
411 CERROR("try to del conn on immature client dev\n");
415 obd_str2uuid(&uuid, lcfg->lcfg_inlbuf1);
416 rc = obd_del_conn(imp, &uuid);
421 LIST_HEAD(lustre_profile_list);
423 struct lustre_profile *class_get_profile(char * prof)
425 struct lustre_profile *lprof;
427 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
428 if (!strcmp(lprof->lp_profile, prof)) {
435 int class_add_profile(int proflen, char *prof,
436 int lovlen, char *lov,
437 int lmvlen, char *lmv)
439 struct lustre_profile *lprof;
442 OBD_ALLOC(lprof, sizeof(*lprof));
444 GOTO(out, err = -ENOMEM);
445 INIT_LIST_HEAD(&lprof->lp_list);
447 LASSERT(proflen == (strlen(prof) + 1));
448 OBD_ALLOC(lprof->lp_profile, proflen);
449 if (lprof->lp_profile == NULL)
450 GOTO(out, err = -ENOMEM);
451 memcpy(lprof->lp_profile, prof, proflen);
453 LASSERT(lovlen == (strlen(lov) + 1));
454 OBD_ALLOC(lprof->lp_lov, lovlen);
455 if (lprof->lp_profile == NULL)
456 GOTO(out, err = -ENOMEM);
457 memcpy(lprof->lp_lov, lov, lovlen);
460 LASSERT(lmvlen == (strlen(lmv) + 1));
461 OBD_ALLOC(lprof->lp_lmv, lmvlen);
462 if (lprof->lp_lmv == NULL)
463 GOTO(out, err = -ENOMEM);
464 memcpy(lprof->lp_lmv, lmv, lmvlen);
467 list_add(&lprof->lp_list, &lustre_profile_list);
473 void class_del_profile(char *prof)
475 struct lustre_profile *lprof;
477 lprof = class_get_profile(prof);
479 list_del(&lprof->lp_list);
480 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
481 OBD_FREE(lprof->lp_lov, strlen(lprof->lp_lov) + 1);
483 OBD_FREE(lprof->lp_lmv, strlen(lprof->lp_lmv) + 1);
484 OBD_FREE(lprof, sizeof *lprof);
488 int class_process_config(struct lustre_cfg *lcfg)
490 struct obd_device *obd;
491 char str[PTL_NALFMT_SIZE];
495 LASSERT(lcfg && !IS_ERR(lcfg));
497 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
499 /* Commands that don't need a device */
500 switch(lcfg->lcfg_command) {
502 err = class_attach(lcfg);
505 case LCFG_ADD_UUID: {
506 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
507 " (%s), nal %x\n", lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
508 portals_nid2str(lcfg->lcfg_nal, lcfg->lcfg_nid, str),
511 err = class_add_uuid(lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
515 case LCFG_DEL_UUID: {
516 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
517 lcfg->lcfg_inlbuf1 == NULL ? "<all uuids>" :
520 err = class_del_uuid(lcfg->lcfg_inlbuf1);
523 case LCFG_MOUNTOPT: {
524 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
525 lcfg->lcfg_inlbuf1, lcfg->lcfg_inlbuf2,
527 /* set these mount options somewhere, so ll_fill_super
529 err = class_add_profile(lcfg->lcfg_inllen1, lcfg->lcfg_inlbuf1,
530 lcfg->lcfg_inllen2, lcfg->lcfg_inlbuf2,
531 lcfg->lcfg_inllen3, lcfg->lcfg_inlbuf3);
534 case LCFG_DEL_MOUNTOPT: {
535 CDEBUG(D_IOCTL, "mountopt: profile %s\n", lcfg->lcfg_inlbuf1);
536 /* set these mount options somewhere, so ll_fill_super
538 class_del_profile(lcfg->lcfg_inlbuf1);
541 case LCFG_SET_TIMEOUT: {
542 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
545 obd_timeout = lcfg->lcfg_num;
548 case LCFG_SET_UPCALL: {
549 CDEBUG(D_IOCTL, "setting lustre ucpall to: %s\n",
551 if (lcfg->lcfg_inllen1 > sizeof obd_lustre_upcall)
552 GOTO(out, err = -EINVAL);
553 memcpy(obd_lustre_upcall, lcfg->lcfg_inlbuf1,
559 /* Commands that require a device */
560 obd = class_name2obd(lcfg->lcfg_dev_name);
562 if (lcfg->lcfg_dev_name == NULL)
563 CERROR("this lcfg command requires a device name\n");
565 CERROR("no device for: %s\n", lcfg->lcfg_dev_name);
567 GOTO(out, err = -EINVAL);
570 switch(lcfg->lcfg_command) {
572 err = class_setup(obd, lcfg);
576 err = class_detach(obd, lcfg);
580 err = class_cleanup(obd, lcfg);
583 case LCFG_ADD_CONN: {
584 err = class_add_conn(obd, lcfg);
587 case LCFG_DEL_CONN: {
588 err = class_del_conn(obd, lcfg);
592 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
601 static int class_config_parse_handler(struct llog_handle * handle,
602 struct llog_rec_hdr *rec, void *data)
604 struct config_llog_instance *cfg = data;
605 int cfg_len = rec->lrh_len;
606 char *cfg_buf = (char*) (rec + 1);
610 if (rec->lrh_type == OBD_CFG_REC) {
612 struct lustre_cfg *lcfg;
613 char *old_name = NULL;
615 char *old_uuid = NULL;
616 int old_uuid_len = 0;
617 char *inst_name = NULL;
620 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
623 lcfg = (struct lustre_cfg* ) buf;
625 if (cfg && cfg->cfg_instance && lcfg->lcfg_dev_name) {
626 inst_len = strlen(lcfg->lcfg_dev_name) +
627 strlen(cfg->cfg_instance) + 2;
628 OBD_ALLOC(inst_name, inst_len);
629 if (inst_name == NULL)
630 GOTO(out, rc = -ENOMEM);
631 sprintf(inst_name, "%s-%s", lcfg->lcfg_dev_name,
633 old_name = lcfg->lcfg_dev_name;
634 old_len = lcfg->lcfg_dev_namelen;
635 lcfg->lcfg_dev_name = inst_name;
636 lcfg->lcfg_dev_namelen = strlen(inst_name) + 1;
639 if (cfg && lcfg->lcfg_command == LCFG_ATTACH) {
640 old_uuid = lcfg->lcfg_inlbuf2;
641 old_uuid_len = lcfg->lcfg_inllen2;
643 lcfg->lcfg_inlbuf2 = (char*)&cfg->cfg_uuid.uuid;
644 lcfg->lcfg_inllen2 = sizeof(cfg->cfg_uuid);
647 rc = class_process_config(lcfg);
650 lcfg->lcfg_dev_name = old_name;
651 lcfg->lcfg_dev_namelen = old_len;
652 OBD_FREE(inst_name, inst_len);
656 lcfg->lcfg_inlbuf2 = old_uuid;
657 lcfg->lcfg_inllen2 = old_uuid_len;
660 lustre_cfg_freedata(buf, cfg_len);
661 } else if (rec->lrh_type == PTL_CFG_REC) {
662 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
663 if (pcfg->pcfg_command == NAL_CMD_REGISTER_MYNID &&
664 cfg->cfg_local_nid != PTL_NID_ANY) {
665 pcfg->pcfg_nid = cfg->cfg_local_nid;
668 rc = libcfs_nal_cmd(pcfg);
670 CERROR("unrecognized record type: 0x%x\n", rec->lrh_type);
676 int class_config_process_llog(struct llog_ctxt *ctxt, char *name,
677 struct config_llog_instance *cfg)
679 struct llog_handle *llh;
683 rc = llog_open(ctxt, &llh, NULL, name, 0);
687 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
689 rc = llog_process(llh, class_config_parse_handler, cfg, NULL);
691 rc2 = llog_close(llh);
698 int class_config_dump_handler(struct llog_handle * handle,
699 struct llog_rec_hdr *rec, void *data)
701 int cfg_len = rec->lrh_len;
702 char *cfg_buf = (char*) (rec + 1);
706 if (rec->lrh_type == OBD_CFG_REC) {
708 struct lustre_cfg *lcfg;
710 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
713 lcfg = (struct lustre_cfg* ) buf;
715 CDEBUG(D_INFO, "lcfg command: 0x%x\n", lcfg->lcfg_command);
716 if (lcfg->lcfg_dev_name)
717 CDEBUG(D_INFO, " devname: %s\n",
718 lcfg->lcfg_dev_name);
719 if (lcfg->lcfg_flags)
720 CDEBUG(D_INFO, " flags: 0x%x\n", lcfg->lcfg_flags);
722 CDEBUG(D_INFO, " nid: "LPX64"\n",
725 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_nal);
727 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_num);
728 if (lcfg->lcfg_inlbuf1)
729 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf1);
730 if (lcfg->lcfg_inlbuf2)
731 CDEBUG(D_INFO, " inlbuf2: %s\n",lcfg->lcfg_inlbuf2);
732 if (lcfg->lcfg_inlbuf3)
733 CDEBUG(D_INFO, " inlbuf3: %s\n",lcfg->lcfg_inlbuf3);
734 if (lcfg->lcfg_inlbuf4)
735 CDEBUG(D_INFO, " inlbuf4: %s\n",lcfg->lcfg_inlbuf4);
736 if (lcfg->lcfg_inlbuf5)
737 CDEBUG(D_INFO, " inlbuf5: %s\n",lcfg->lcfg_inlbuf5);
738 if (lcfg->lcfg_inlbuf6)
739 CDEBUG(D_INFO, " inlbuf6: %s\n",lcfg->lcfg_inlbuf6);
741 lustre_cfg_freedata(buf, cfg_len);
742 } else if (rec->lrh_type == PTL_CFG_REC) {
743 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
745 CDEBUG(D_INFO, "pcfg command: 0x%x\n", pcfg->pcfg_command);
747 CDEBUG(D_INFO, " nal: %x\n",
749 if (pcfg->pcfg_gw_nal)
750 CDEBUG(D_INFO, " gw_nal: %x\n",
753 CDEBUG(D_INFO, " nid: "LPX64"\n",
756 CDEBUG(D_INFO, " nid2: "LPX64"\n",
759 CDEBUG(D_INFO, " nid3: "LPX64"\n",
762 CDEBUG(D_INFO, " misc: %d\n",
765 CDEBUG(D_INFO, " id: 0x%x\n",
767 if (pcfg->pcfg_flags)
768 CDEBUG(D_INFO, " flags: 0x%x\n",
771 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
778 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
779 struct config_llog_instance *cfg)
781 struct llog_handle *llh;
785 rc = llog_open(ctxt, &llh, NULL, name, 0);
789 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
791 rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
793 rc2 = llog_close(llh);