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 int class_attach(struct lustre_cfg *lcfg)
49 struct obd_type *type;
55 struct obd_device *obd;
58 if (!lcfg->lcfg_inllen1 || !lcfg->lcfg_inlbuf1) {
59 CERROR("No type passed!\n");
62 if (lcfg->lcfg_inlbuf1[lcfg->lcfg_inllen1 - 1] != 0) {
63 CERROR("Type not nul terminated!\n");
66 typename = lcfg->lcfg_inlbuf1;
68 if (!lcfg->lcfg_dev_namelen || !lcfg->lcfg_dev_name) {
69 CERROR("No name passed!\n");
72 if (lcfg->lcfg_dev_name[lcfg->lcfg_dev_namelen - 1] != 0) {
73 CERROR("Name not nul terminated!\n");
76 name = lcfg->lcfg_dev_name;
78 if (!lcfg->lcfg_inllen2 || !lcfg->lcfg_inlbuf2) {
79 CERROR("No UUID passed!\n");
82 if (lcfg->lcfg_inlbuf2[lcfg->lcfg_inllen2 - 1] != 0) {
83 CERROR("UUID not nul terminated!\n");
86 uuid = lcfg->lcfg_inlbuf2;
88 CDEBUG(D_IOCTL, "attach type %s name: %s uuid: %s\n",
89 MKSTR(lcfg->lcfg_inlbuf1),
90 MKSTR(lcfg->lcfg_dev_name), MKSTR(lcfg->lcfg_inlbuf2));
93 type = class_get_type(typename);
95 CERROR("OBD: unknown type: %s\n", typename);
99 obd = class_name2obd(name);
101 CERROR("obd %s already attached\n", name);
105 obd = class_newdev(&dev);
109 /* have we attached a type to this device */
110 if (obd->obd_attached || obd->obd_type) {
111 CERROR("OBD: Device %d already typed as %s.\n",
112 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
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 init_waitqueue_head(&obd->obd_refcount_waitq);
127 /* XXX belongs in setup not attach */
129 spin_lock_init(&obd->obd_processing_task_lock);
130 init_waitqueue_head(&obd->obd_next_transno_waitq);
131 INIT_LIST_HEAD(&obd->obd_recovery_queue);
132 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
134 init_waitqueue_head(&obd->obd_commit_waitq);
136 len = strlen(name) + 1;
137 OBD_ALLOC(obd->obd_name, len);
138 if (!obd->obd_name) {
139 class_put_type(obd->obd_type);
140 obd->obd_type = NULL;
143 memcpy(obd->obd_name, name, len);
146 if (len >= sizeof(obd->obd_uuid)) {
147 CERROR("uuid must be < "LPSZ" bytes long\n",
148 sizeof(obd->obd_uuid));
149 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
150 class_put_type(obd->obd_type);
151 obd->obd_type = NULL;
154 memcpy(obd->obd_uuid.uuid, uuid, len);
157 if (OBP(obd, attach))
158 err = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
161 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
162 class_put_type(obd->obd_type);
163 obd->obd_type = NULL;
165 obd->obd_attached = 1;
167 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
168 obd->obd_minor, typename);
173 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
176 struct obd_export *exp;
179 LASSERT(obd == (obd_dev + obd->obd_minor));
181 /* have we attached a type to this device? */
182 if (!obd->obd_attached) {
183 CERROR("Device %d not attached\n", obd->obd_minor);
187 /* has this been done already? */
188 if (obd->obd_set_up) {
189 CERROR("Device %d already setup (type %s)\n",
190 obd->obd_minor, obd->obd_type->typ_name);
194 atomic_set(&obd->obd_refcount, 0);
196 exp = class_new_export(obd);
199 memcpy(&exp->exp_client_uuid, &obd->obd_uuid,
200 sizeof(exp->exp_client_uuid));
201 obd->obd_self_export = exp;
202 class_export_put(exp);
204 err = obd_setup(obd, sizeof(*lcfg), lcfg);
208 obd->obd_type->typ_refcnt++;
214 class_unlink_export(obd->obd_self_export);
215 obd->obd_self_export = NULL;
219 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
225 if (obd->obd_set_up) {
226 CERROR("OBD device %d still set up\n", obd->obd_minor);
229 if (!obd->obd_attached) {
230 CERROR("OBD device %d not attached\n", obd->obd_minor);
233 if (OBP(obd, detach))
234 err = OBP(obd,detach)(obd);
237 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
238 obd->obd_name = NULL;
240 CERROR("device %d: no name at detach\n", obd->obd_minor);
242 obd->obd_attached = 0;
243 obd->obd_type->typ_refcnt--;
244 class_put_type(obd->obd_type);
245 obd->obd_type = NULL;
246 minor = obd->obd_minor;
247 memset(obd, 0, sizeof(*obd));
248 obd->obd_minor = minor;
252 static void dump_exports(struct obd_device *obd)
254 struct obd_export *exp, *n;
256 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
257 CERROR("%s: %p %s %d %d %p\n",
258 obd->obd_name, exp, exp->exp_client_uuid.uuid,
259 atomic_read(&exp->exp_refcount),
260 exp->exp_failed, exp->exp_outstanding_reply );
264 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
271 if (!obd->obd_set_up) {
272 CERROR("Device %d not setup\n", obd->obd_minor);
276 if (lcfg->lcfg_inlbuf1) {
277 for (flag = lcfg->lcfg_inlbuf1; *flag != 0; flag++)
280 flags |= OBD_OPT_FORCE;
283 flags |= OBD_OPT_FAILOVER;
286 CERROR("unrecognised flag '%c'\n",
291 /* The one reference that should be remaining is the
293 if (atomic_read(&obd->obd_refcount) <= 1 ||
294 flags & OBD_OPT_FORCE) {
295 /* this will stop new connections, and need to
296 do it before class_disconnect_exports() */
297 obd->obd_stopping = 1;
300 if (atomic_read(&obd->obd_refcount) > 1) {
301 struct l_wait_info lwi = LWI_TIMEOUT_INTR(1 * HZ, NULL,
305 if (!(flags & OBD_OPT_FORCE)) {
306 CERROR("OBD device %d (%p) has refcount %d\n",
308 atomic_read(&obd->obd_refcount));
310 GOTO(out, err = -EBUSY);
312 class_disconnect_exports(obd, flags);
314 "%s: waiting for obd refs to go away: %d\n",
315 obd->obd_name, atomic_read(&obd->obd_refcount));
317 rc = l_wait_event(obd->obd_refcount_waitq,
318 atomic_read(&obd->obd_refcount) < 2, &lwi);
320 LASSERT(atomic_read(&obd->obd_refcount) == 1);
322 CERROR("wait cancelled cleaning anyway. "
324 atomic_read(&obd->obd_refcount));
327 CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n",
331 if (obd->obd_self_export) {
332 err = obd_precleanup(obd, flags);
335 class_unlink_export(obd->obd_self_export);
336 obd->obd_self_export = NULL;
339 err = obd_cleanup(obd, flags);
342 obd->obd_set_up = obd->obd_stopping = 0;
343 obd->obd_type->typ_refcnt--;
344 /* XXX this should be an LASSERT */
345 if (atomic_read(&obd->obd_refcount) > 0)
346 CERROR("%s still has refcount %d after "
347 "cleanup.\n", obd->obd_name,
348 atomic_read(&obd->obd_refcount));
355 LIST_HEAD(lustre_profile_list);
357 struct lustre_profile *class_get_profile(char * prof)
359 struct lustre_profile *lprof;
361 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
362 if (!strcmp(lprof->lp_profile, prof)) {
369 int class_add_profile(int proflen, char *prof,
370 int osclen, char *osc,
371 int mdclen, char *mdc)
373 struct lustre_profile *lprof;
376 OBD_ALLOC(lprof, sizeof(*lprof));
378 GOTO(out, err = -ENOMEM);
379 INIT_LIST_HEAD(&lprof->lp_list);
381 LASSERT(proflen == (strlen(prof) + 1));
382 OBD_ALLOC(lprof->lp_profile, proflen);
383 if (lprof->lp_profile == NULL)
384 GOTO(out, err = -ENOMEM);
385 memcpy(lprof->lp_profile, prof, proflen);
387 LASSERT(osclen == (strlen(osc) + 1));
388 OBD_ALLOC(lprof->lp_osc, osclen);
389 if (lprof->lp_profile == NULL)
390 GOTO(out, err = -ENOMEM);
391 memcpy(lprof->lp_osc, osc, osclen);
394 LASSERT(mdclen == (strlen(mdc) + 1));
395 OBD_ALLOC(lprof->lp_mdc, mdclen);
396 if (lprof->lp_mdc == NULL)
397 GOTO(out, err = -ENOMEM);
398 memcpy(lprof->lp_mdc, mdc, mdclen);
401 list_add(&lprof->lp_list, &lustre_profile_list);
407 void class_del_profile(char *prof)
409 struct lustre_profile *lprof;
411 lprof = class_get_profile(prof);
413 list_del(&lprof->lp_list);
414 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
415 OBD_FREE(lprof->lp_osc, strlen(lprof->lp_osc) + 1);
417 OBD_FREE(lprof->lp_mdc, strlen(lprof->lp_mdc) + 1);
418 OBD_FREE(lprof, sizeof *lprof);
422 int class_process_config(struct lustre_cfg *lcfg)
424 struct obd_device *obd;
425 char str[PTL_NALFMT_SIZE];
428 LASSERT(lcfg && !IS_ERR(lcfg));
430 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
432 /* Commands that don't need a device */
433 switch(lcfg->lcfg_command) {
435 err = class_attach(lcfg);
438 case LCFG_ADD_UUID: {
439 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
440 " (%s), nal %d\n", lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
441 portals_nid2str(lcfg->lcfg_nal, lcfg->lcfg_nid, str),
444 err = class_add_uuid(lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
448 case LCFG_DEL_UUID: {
449 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
450 lcfg->lcfg_inlbuf1 == NULL ? "<all uuids>" :
453 err = class_del_uuid(lcfg->lcfg_inlbuf1);
456 case LCFG_MOUNTOPT: {
457 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
458 lcfg->lcfg_inlbuf1, lcfg->lcfg_inlbuf2, lcfg->lcfg_inlbuf3);
459 /* set these mount options somewhere, so ll_fill_super
461 err = class_add_profile(lcfg->lcfg_inllen1, lcfg->lcfg_inlbuf1,
462 lcfg->lcfg_inllen2, lcfg->lcfg_inlbuf2,
463 lcfg->lcfg_inllen3, lcfg->lcfg_inlbuf3);
466 case LCFG_DEL_MOUNTOPT: {
467 CDEBUG(D_IOCTL, "mountopt: profile %s\n", lcfg->lcfg_inlbuf1);
468 /* set these mount options somewhere, so ll_fill_super
470 class_del_profile(lcfg->lcfg_inlbuf1);
473 case LCFG_SET_TIMEOUT: {
474 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
477 obd_timeout = lcfg->lcfg_num;
480 case LCFG_SET_UPCALL: {
481 CDEBUG(D_IOCTL, "setting lustre ucpall to: %s\n",
483 if (lcfg->lcfg_inllen1 > sizeof obd_lustre_upcall)
484 GOTO(out, err = -EINVAL);
485 memcpy(obd_lustre_upcall, lcfg->lcfg_inlbuf1,
492 /* Commands that require a device */
493 obd = class_name2obd(lcfg->lcfg_dev_name);
495 if (lcfg->lcfg_dev_name == NULL) {
496 CERROR("this lcfg command requires a device name\n");
498 CERROR("no device for: %s\n", lcfg->lcfg_dev_name);
500 GOTO(out, err = -EINVAL);
503 switch(lcfg->lcfg_command) {
505 err = class_setup(obd, lcfg);
509 err = class_detach(obd, lcfg);
513 err = class_cleanup(obd, lcfg);
517 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
518 GOTO(out, err = -EINVAL);
526 static int class_config_llog_handler(struct llog_handle * handle,
527 struct llog_rec_hdr *rec, void *data)
529 struct config_llog_instance *cfg = data;
530 int cfg_len = rec->lrh_len;
531 char *cfg_buf = (char*) (rec + 1);
534 if (rec->lrh_type == OBD_CFG_REC) {
536 struct lustre_cfg *lcfg;
537 char *old_name = NULL;
539 char *old_uuid = NULL;
540 int old_uuid_len = 0;
541 char *inst_name = NULL;
544 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
547 lcfg = (struct lustre_cfg* ) buf;
549 if (cfg && cfg->cfg_instance && lcfg->lcfg_dev_name) {
550 inst_len = strlen(lcfg->lcfg_dev_name) +
551 strlen(cfg->cfg_instance) + 2;
552 OBD_ALLOC(inst_name, inst_len);
553 if (inst_name == NULL)
554 GOTO(out, rc = -ENOMEM);
555 sprintf(inst_name, "%s-%s", lcfg->lcfg_dev_name,
557 old_name = lcfg->lcfg_dev_name;
558 old_len = lcfg->lcfg_dev_namelen;
559 lcfg->lcfg_dev_name = inst_name;
560 lcfg->lcfg_dev_namelen = strlen(inst_name) + 1;
563 if (cfg && lcfg->lcfg_command == LCFG_ATTACH) {
564 old_uuid = lcfg->lcfg_inlbuf2;
565 old_uuid_len = lcfg->lcfg_inllen2;
567 lcfg->lcfg_inlbuf2 = (char*)&cfg->cfg_uuid.uuid;
568 lcfg->lcfg_inllen2 = sizeof(cfg->cfg_uuid);
571 rc = class_process_config(lcfg);
574 lcfg->lcfg_dev_name = old_name;
575 lcfg->lcfg_dev_namelen = old_len;
576 OBD_FREE(inst_name, inst_len);
580 lcfg->lcfg_inlbuf2 = old_uuid;
581 lcfg->lcfg_inllen2 = old_uuid_len;
584 lustre_cfg_freedata(buf, cfg_len);
585 } else if (rec->lrh_type == PTL_CFG_REC) {
586 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
587 if (pcfg->pcfg_command ==NAL_CMD_REGISTER_MYNID &&
588 cfg->cfg_local_nid != PTL_NID_ANY) {
589 pcfg->pcfg_nid = cfg->cfg_local_nid;
592 rc = kportal_nal_cmd(pcfg);
598 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
599 struct config_llog_instance *cfg)
601 struct llog_handle *llh;
605 rc = llog_create(ctxt, &llh, NULL, name);
609 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
613 rc = llog_process(llh, class_config_llog_handler, cfg, NULL);
615 rc2 = llog_close(llh);
623 static int class_config_dump_handler(struct llog_handle * handle,
624 struct llog_rec_hdr *rec, void *data)
626 int cfg_len = rec->lrh_len;
627 char *cfg_buf = (char*) (rec + 1);
630 if (rec->lrh_type == OBD_CFG_REC) {
632 struct lustre_cfg *lcfg;
634 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
637 lcfg = (struct lustre_cfg* ) buf;
639 CDEBUG(D_INFO, "lcfg command: %x\n", lcfg->lcfg_command);
640 if (lcfg->lcfg_dev_name)
641 CDEBUG(D_INFO, " devname: %s\n",
642 lcfg->lcfg_dev_name);
643 if (lcfg->lcfg_flags)
644 CDEBUG(D_INFO, " flags: %x\n", lcfg->lcfg_flags);
646 CDEBUG(D_INFO, " nid: "LPX64"\n",
649 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_nal);
651 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_num);
652 if (lcfg->lcfg_inlbuf1)
653 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf1);
654 if (lcfg->lcfg_inlbuf2)
655 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf2);
656 if (lcfg->lcfg_inlbuf3)
657 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf3);
658 if (lcfg->lcfg_inlbuf4)
659 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf4);
661 lustre_cfg_freedata(buf, cfg_len);
662 } else if (rec->lrh_type == PTL_CFG_REC) {
663 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
665 CDEBUG(D_INFO, "pcfg command: %d\n", pcfg->pcfg_command);
667 CDEBUG(D_INFO, " nal: %d\n",
669 if (pcfg->pcfg_gw_nal)
670 CDEBUG(D_INFO, " gw_nal: %d\n",
673 CDEBUG(D_INFO, " nid: "LPX64"\n",
676 CDEBUG(D_INFO, " nid: "LPX64"\n",
679 CDEBUG(D_INFO, " nid: "LPX64"\n",
682 CDEBUG(D_INFO, " nid: %d\n",
685 CDEBUG(D_INFO, " id: %x\n",
687 if (pcfg->pcfg_flags)
688 CDEBUG(D_INFO, " flags: %x\n",
695 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
696 struct config_llog_instance *cfg)
698 struct llog_handle *llh;
702 rc = llog_create(ctxt, &llh, NULL, name);
706 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
710 rc = llog_process(llh, class_config_dump_handler, cfg, NULL);
712 rc2 = llog_close(llh);