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 <portals/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 minor, rc, len, dev, stage = 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),
85 MKSTR(lcfg->lcfg_dev_name), MKSTR(lcfg->lcfg_inlbuf2));
88 type = class_get_type(typename);
90 CERROR("OBD: unknown type: %s\n", typename);
95 obd = class_name2obd(name);
97 CERROR("obd %s already attached\n", name);
98 GOTO(out, rc = -EEXIST);
101 obd = class_newdev(&dev);
103 GOTO(out, rc = -EINVAL);
105 /* have we attached a type to this device */
106 if (obd->obd_attached) {
107 CERROR("OBD: Device %d already attached.\n", obd->obd_minor);
108 GOTO(out, rc = -EBUSY);
110 if (obd->obd_type != NULL) {
111 CERROR("OBD: Device %d already typed as %s.\n",
112 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
113 GOTO(out, rc = -EBUSY);
116 LASSERT(obd == (obd_dev + obd->obd_minor));
118 minor = obd->obd_minor;
119 memset(obd, 0, sizeof(*obd));
120 obd->obd_minor = minor;
121 obd->obd_type = type;
122 INIT_LIST_HEAD(&obd->obd_exports);
123 obd->obd_num_exports = 0;
124 spin_lock_init(&obd->obd_dev_lock);
125 spin_lock_init(&obd->obd_osfs_lock);
126 obd->obd_osfs_age = jiffies - 1000 * HZ;
127 init_waitqueue_head(&obd->obd_refcount_waitq);
129 /* XXX belongs in setup not attach */
131 init_timer(&obd->obd_recovery_timer);
132 spin_lock_init(&obd->obd_processing_task_lock);
133 init_waitqueue_head(&obd->obd_next_transno_waitq);
134 INIT_LIST_HEAD(&obd->obd_recovery_queue);
135 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
137 spin_lock_init(&obd->obd_uncommitted_replies_lock);
138 INIT_LIST_HEAD(&obd->obd_uncommitted_replies);
140 len = strlen(name) + 1;
141 OBD_ALLOC(obd->obd_name, len);
143 GOTO(out, rc = -ENOMEM);
144 memcpy(obd->obd_name, name, len);
148 if (len >= sizeof(obd->obd_uuid)) {
149 CERROR("uuid must be < "LPSZ" bytes long\n",
150 sizeof(obd->obd_uuid));
151 GOTO(out, rc = -EINVAL);
153 memcpy(obd->obd_uuid.uuid, uuid, len);
156 if (OBP(obd, attach)) {
157 rc = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
159 GOTO(out, rc = -EINVAL);
162 obd->obd_attached = 1;
164 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
165 obd->obd_minor, typename);
170 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
172 class_put_type(type);
173 obd->obd_type = NULL;
178 static int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
181 struct obd_export *exp;
184 LASSERT(obd == (obd_dev + obd->obd_minor));
186 /* have we attached a type to this device? */
187 if (!obd->obd_attached) {
188 CERROR("Device %d not attached\n", obd->obd_minor);
192 /* has this been done already? */
193 if (obd->obd_set_up) {
194 CERROR("Device %d already setup (type %s)\n",
195 obd->obd_minor, obd->obd_type->typ_name);
199 atomic_set(&obd->obd_refcount, 0);
201 exp = class_new_export(obd);
204 memcpy(&exp->exp_client_uuid, &obd->obd_uuid,
205 sizeof(exp->exp_client_uuid));
206 obd->obd_self_export = exp;
207 class_export_put(exp);
209 err = obd_setup(obd, sizeof(*lcfg), lcfg);
213 obd->obd_type->typ_refcnt++;
219 class_unlink_export(obd->obd_self_export);
220 obd->obd_self_export = NULL;
224 static int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
230 if (obd->obd_set_up) {
231 CERROR("OBD device %d still set up\n", obd->obd_minor);
234 if (!obd->obd_attached) {
235 CERROR("OBD device %d not attached\n", obd->obd_minor);
238 if (OBP(obd, detach))
239 err = OBP(obd,detach)(obd);
242 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
243 obd->obd_name = NULL;
245 CERROR("device %d: no name at detach\n", obd->obd_minor);
248 obd->obd_attached = 0;
249 obd->obd_type->typ_refcnt--;
250 class_put_type(obd->obd_type);
251 obd->obd_type = NULL;
252 minor = obd->obd_minor;
253 memset(obd, 0, sizeof(*obd));
254 obd->obd_minor = minor;
258 static void dump_exports(struct obd_device *obd)
260 struct obd_export *exp, *n;
262 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
263 struct ptlrpc_reply_state *rs;
264 struct ptlrpc_reply_state *first_reply = NULL;
267 list_for_each_entry (rs, &exp->exp_outstanding_replies,
274 CERROR("%s: %p %s %d %d %d: %p %s\n",
275 obd->obd_name, exp, exp->exp_client_uuid.uuid,
276 atomic_read(&exp->exp_refcount),
277 exp->exp_failed, nreplies, first_reply,
278 nreplies > 3 ? "..." : "");
282 static int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
289 if (!obd->obd_set_up) {
290 CERROR("Device %d not setup\n", obd->obd_minor);
294 if (lcfg->lcfg_inlbuf1) {
295 for (flag = lcfg->lcfg_inlbuf1; *flag != 0; flag++)
298 flags |= OBD_OPT_FORCE;
301 flags |= OBD_OPT_FAILOVER;
304 CERROR("unrecognised flag '%c'\n",
309 /* The one reference that should be remaining is the
311 if (atomic_read(&obd->obd_refcount) <= 1 ||
312 flags & OBD_OPT_FORCE) {
313 /* this will stop new connections, and need to
314 do it before class_disconnect_exports() */
315 obd->obd_stopping = 1;
318 if (atomic_read(&obd->obd_refcount) > 1) {
319 struct l_wait_info lwi = LWI_TIMEOUT_INTR(1 * HZ, NULL,
323 if (!(flags & OBD_OPT_FORCE)) {
324 CERROR("OBD device %d (%p,%s) has refcount %d\n",
325 obd->obd_minor, obd, obd->obd_name,
326 atomic_read(&obd->obd_refcount));
328 GOTO(out, err = -EBUSY);
330 class_disconnect_exports(obd, flags);
332 "%s: waiting for obd refs to go away: %d\n",
333 obd->obd_name, atomic_read(&obd->obd_refcount));
335 rc = l_wait_event(obd->obd_refcount_waitq,
336 atomic_read(&obd->obd_refcount) < 2, &lwi);
338 LASSERT(atomic_read(&obd->obd_refcount) == 1);
340 CERROR("wait cancelled cleaning anyway. "
342 atomic_read(&obd->obd_refcount));
345 CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n",
349 if (obd->obd_self_export) {
350 err = obd_precleanup(obd, flags);
353 class_unlink_export(obd->obd_self_export);
354 obd->obd_self_export = NULL;
357 err = obd_cleanup(obd, flags);
360 obd->obd_set_up = obd->obd_stopping = 0;
361 obd->obd_type->typ_refcnt--;
362 /* XXX this should be an LASSERT */
363 if (atomic_read(&obd->obd_refcount) > 0) {
364 CERROR("%s (%p) still has refcount %d after "
365 "cleanup.\n", obd->obd_name, obd,
366 atomic_read(&obd->obd_refcount));
375 LIST_HEAD(lustre_profile_list);
377 struct lustre_profile *class_get_profile(char * prof)
379 struct lustre_profile *lprof;
381 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
382 if (!strcmp(lprof->lp_profile, prof)) {
389 int class_add_profile(int proflen, char *prof, int osclen, char *osc,
390 int mdclen, char *mdc)
392 struct lustre_profile *lprof;
395 OBD_ALLOC(lprof, sizeof(*lprof));
397 GOTO(out, err = -ENOMEM);
398 INIT_LIST_HEAD(&lprof->lp_list);
400 LASSERT(proflen == (strlen(prof) + 1));
401 OBD_ALLOC(lprof->lp_profile, proflen);
402 if (lprof->lp_profile == NULL)
403 GOTO(out, err = -ENOMEM);
404 memcpy(lprof->lp_profile, prof, proflen);
406 LASSERT(osclen == (strlen(osc) + 1));
407 OBD_ALLOC(lprof->lp_osc, osclen);
408 if (lprof->lp_profile == NULL)
409 GOTO(out, err = -ENOMEM);
410 memcpy(lprof->lp_osc, osc, osclen);
413 LASSERT(mdclen == (strlen(mdc) + 1));
414 OBD_ALLOC(lprof->lp_mdc, mdclen);
415 if (lprof->lp_mdc == NULL)
416 GOTO(out, err = -ENOMEM);
417 memcpy(lprof->lp_mdc, mdc, mdclen);
420 list_add(&lprof->lp_list, &lustre_profile_list);
426 void class_del_profile(char *prof)
428 struct lustre_profile *lprof;
430 lprof = class_get_profile(prof);
432 list_del(&lprof->lp_list);
433 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
434 OBD_FREE(lprof->lp_osc, strlen(lprof->lp_osc) + 1);
436 OBD_FREE(lprof->lp_mdc, strlen(lprof->lp_mdc) + 1);
437 OBD_FREE(lprof, sizeof *lprof);
441 int class_process_config(struct lustre_cfg *lcfg)
443 struct obd_device *obd;
444 char str[PTL_NALFMT_SIZE];
448 LASSERT(lcfg && !IS_ERR(lcfg));
450 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
452 /* Commands that don't need a device */
453 switch(lcfg->lcfg_command) {
455 err = class_attach(lcfg);
458 case LCFG_ADD_UUID: {
459 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
460 " (%s), nal %d\n", lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
461 portals_nid2str(lcfg->lcfg_nal, lcfg->lcfg_nid, str),
464 err = class_add_uuid(lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
468 case LCFG_DEL_UUID: {
469 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
470 lcfg->lcfg_inlbuf1 == NULL ? "<all uuids>" :
473 err = class_del_uuid(lcfg->lcfg_inlbuf1);
476 case LCFG_MOUNTOPT: {
477 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
478 lcfg->lcfg_inlbuf1, lcfg->lcfg_inlbuf2,
480 /* set these mount options somewhere, so ll_fill_super
482 err = class_add_profile(lcfg->lcfg_inllen1, lcfg->lcfg_inlbuf1,
483 lcfg->lcfg_inllen2, lcfg->lcfg_inlbuf2,
484 lcfg->lcfg_inllen3, lcfg->lcfg_inlbuf3);
487 case LCFG_DEL_MOUNTOPT: {
488 CDEBUG(D_IOCTL, "mountopt: profile %s\n", lcfg->lcfg_inlbuf1);
489 /* set these mount options somewhere, so ll_fill_super
491 class_del_profile(lcfg->lcfg_inlbuf1);
494 case LCFG_SET_TIMEOUT: {
495 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
498 obd_timeout = lcfg->lcfg_num;
501 case LCFG_SET_UPCALL: {
502 CDEBUG(D_IOCTL, "setting lustre ucpall to: %s\n",
504 if (lcfg->lcfg_inllen1 > sizeof obd_lustre_upcall)
505 GOTO(out, err = -EINVAL);
506 memcpy(obd_lustre_upcall, lcfg->lcfg_inlbuf1,
512 /* Commands that require a device */
513 obd = class_name2obd(lcfg->lcfg_dev_name);
515 if (lcfg->lcfg_dev_name == NULL)
516 CERROR("this lcfg command requires a device name\n");
518 CERROR("no device for: %s\n", lcfg->lcfg_dev_name);
520 GOTO(out, err = -EINVAL);
523 switch(lcfg->lcfg_command) {
525 err = class_setup(obd, lcfg);
529 err = class_detach(obd, lcfg);
533 err = class_cleanup(obd, lcfg);
537 err = obd_process_config(obd, sizeof(*lcfg), lcfg);
546 static int class_config_parse_handler(struct llog_handle * handle,
547 struct llog_rec_hdr *rec, void *data)
549 struct config_llog_instance *cfg = data;
550 int cfg_len = rec->lrh_len;
551 char *cfg_buf = (char*) (rec + 1);
555 if (rec->lrh_type == OBD_CFG_REC) {
557 struct lustre_cfg *lcfg;
558 char *old_name = NULL;
560 char *old_uuid = NULL;
561 int old_uuid_len = 0;
562 char *inst_name = NULL;
565 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
568 lcfg = (struct lustre_cfg* ) buf;
570 if (cfg && cfg->cfg_instance && lcfg->lcfg_dev_name) {
571 inst_len = strlen(lcfg->lcfg_dev_name) +
572 strlen(cfg->cfg_instance) + 2;
573 OBD_ALLOC(inst_name, inst_len);
574 if (inst_name == NULL)
575 GOTO(out, rc = -ENOMEM);
576 sprintf(inst_name, "%s-%s", lcfg->lcfg_dev_name,
578 old_name = lcfg->lcfg_dev_name;
579 old_len = lcfg->lcfg_dev_namelen;
580 lcfg->lcfg_dev_name = inst_name;
581 lcfg->lcfg_dev_namelen = strlen(inst_name) + 1;
584 if (cfg && lcfg->lcfg_command == LCFG_ATTACH) {
585 old_uuid = lcfg->lcfg_inlbuf2;
586 old_uuid_len = lcfg->lcfg_inllen2;
588 lcfg->lcfg_inlbuf2 = (char*)&cfg->cfg_uuid.uuid;
589 lcfg->lcfg_inllen2 = sizeof(cfg->cfg_uuid);
592 rc = class_process_config(lcfg);
595 lcfg->lcfg_dev_name = old_name;
596 lcfg->lcfg_dev_namelen = old_len;
597 OBD_FREE(inst_name, inst_len);
601 lcfg->lcfg_inlbuf2 = old_uuid;
602 lcfg->lcfg_inllen2 = old_uuid_len;
605 lustre_cfg_freedata(buf, cfg_len);
606 } else if (rec->lrh_type == PTL_CFG_REC) {
607 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
608 if (pcfg->pcfg_command == NAL_CMD_REGISTER_MYNID &&
609 cfg->cfg_local_nid != PTL_NID_ANY) {
610 pcfg->pcfg_nid = cfg->cfg_local_nid;
613 rc = libcfs_nal_cmd(pcfg);
615 CERROR("unrecognized record type: 0x%x\n", rec->lrh_type);
621 int class_config_process_llog(struct llog_ctxt *ctxt, char *name,
622 struct config_llog_instance *cfg)
624 struct llog_handle *llh;
628 rc = llog_open(ctxt, &llh, NULL, name, 0);
632 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
634 rc = llog_process(llh, class_config_parse_handler, cfg, NULL);
636 rc2 = llog_close(llh);
643 static int class_config_dump_handler(struct llog_handle * handle,
644 struct llog_rec_hdr *rec, void *data)
646 int cfg_len = rec->lrh_len;
647 char *cfg_buf = (char*) (rec + 1);
651 if (rec->lrh_type == OBD_CFG_REC) {
653 struct lustre_cfg *lcfg;
655 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
658 lcfg = (struct lustre_cfg* ) buf;
660 CDEBUG(D_INFO, "lcfg command: 0x%x\n", lcfg->lcfg_command);
661 if (lcfg->lcfg_dev_name)
662 CDEBUG(D_INFO, " devname: %s\n",
663 lcfg->lcfg_dev_name);
664 if (lcfg->lcfg_flags)
665 CDEBUG(D_INFO, " flags: 0x%x\n", lcfg->lcfg_flags);
667 CDEBUG(D_INFO, " nid: "LPX64"\n",
670 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_nal);
672 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_num);
673 if (lcfg->lcfg_inlbuf1)
674 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf1);
675 if (lcfg->lcfg_inlbuf2)
676 CDEBUG(D_INFO, " inlbuf2: %s\n",lcfg->lcfg_inlbuf2);
677 if (lcfg->lcfg_inlbuf3)
678 CDEBUG(D_INFO, " inlbuf3: %s\n",lcfg->lcfg_inlbuf3);
679 if (lcfg->lcfg_inlbuf4)
680 CDEBUG(D_INFO, " inlbuf4: %s\n",lcfg->lcfg_inlbuf4);
681 if (lcfg->lcfg_inlbuf5)
682 CDEBUG(D_INFO, " inlbuf5: %s\n",lcfg->lcfg_inlbuf5);
683 if (lcfg->lcfg_inlbuf6)
684 CDEBUG(D_INFO, " inlbuf6: %s\n",lcfg->lcfg_inlbuf6);
686 lustre_cfg_freedata(buf, cfg_len);
687 } else if (rec->lrh_type == PTL_CFG_REC) {
688 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
690 CDEBUG(D_INFO, "pcfg command: 0x%x\n", pcfg->pcfg_command);
692 CDEBUG(D_INFO, " nal: %d\n",
694 if (pcfg->pcfg_gw_nal)
695 CDEBUG(D_INFO, " gw_nal: %d\n",
698 CDEBUG(D_INFO, " nid: "LPX64"\n",
701 CDEBUG(D_INFO, " nid2: "LPX64"\n",
704 CDEBUG(D_INFO, " nid3: "LPX64"\n",
707 CDEBUG(D_INFO, " misc: %d\n",
710 CDEBUG(D_INFO, " id: 0x%x\n",
712 if (pcfg->pcfg_flags)
713 CDEBUG(D_INFO, " flags: 0x%x\n",
716 CERROR("unhandled lrh_type: %#x\n", rec->lrh_type);
723 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
724 struct config_llog_instance *cfg)
726 struct llog_handle *llh;
730 rc = llog_open(ctxt, &llh, NULL, name, 0);
734 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
736 rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
738 rc2 = llog_close(llh);