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 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 spin_lock_init(&obd->obd_processing_task_lock);
132 init_waitqueue_head(&obd->obd_next_transno_waitq);
133 INIT_LIST_HEAD(&obd->obd_recovery_queue);
134 INIT_LIST_HEAD(&obd->obd_delayed_reply_queue);
136 init_waitqueue_head(&obd->obd_commit_waitq);
138 len = strlen(name) + 1;
139 OBD_ALLOC(obd->obd_name, len);
140 if (!obd->obd_name) {
141 class_put_type(obd->obd_type);
142 obd->obd_type = NULL;
145 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 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
152 class_put_type(obd->obd_type);
153 obd->obd_type = NULL;
156 memcpy(obd->obd_uuid.uuid, uuid, len);
159 if (OBP(obd, attach))
160 err = OBP(obd,attach)(obd, sizeof *lcfg, lcfg);
163 OBD_FREE(obd->obd_name, strlen(obd->obd_name) + 1);
164 class_put_type(obd->obd_type);
165 obd->obd_type = NULL;
167 obd->obd_attached = 1;
169 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
170 obd->obd_minor, typename);
175 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
178 struct obd_export *exp;
181 LASSERT(obd == (obd_dev + obd->obd_minor));
183 /* have we attached a type to this device? */
184 if (!obd->obd_attached) {
185 CERROR("Device %d not attached\n", obd->obd_minor);
189 /* has this been done already? */
190 if (obd->obd_set_up) {
191 CERROR("Device %d already setup (type %s)\n",
192 obd->obd_minor, obd->obd_type->typ_name);
196 atomic_set(&obd->obd_refcount, 0);
198 err = obd_setup(obd, sizeof(*lcfg), lcfg);
203 obd->obd_type->typ_refcnt++;
206 exp = class_new_export(obd);
208 GOTO(err_cleanup, err = -ENOMEM);
210 memcpy(&exp->exp_client_uuid, &obd->obd_uuid,
211 sizeof(exp->exp_client_uuid));
212 obd->obd_self_export = exp;
213 class_export_put(exp);
215 if (OBT(obd) && OBP(obd, postsetup)) {
216 err = obd_postsetup(obd);
224 class_unlink_export(obd->obd_self_export);
225 obd->obd_self_export = NULL;
227 obd->obd_stopping = 1;
229 obd->obd_set_up = obd->obd_stopping = 0;
230 obd->obd_type->typ_refcnt--;
234 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg)
240 if (obd->obd_set_up) {
241 CERROR("OBD device %d still set up\n", obd->obd_minor);
244 if (!obd->obd_attached) {
245 CERROR("OBD device %d not attached\n", obd->obd_minor);
248 if (OBP(obd, detach))
249 err = OBP(obd,detach)(obd);
252 OBD_FREE(obd->obd_name, strlen(obd->obd_name)+1);
253 obd->obd_name = NULL;
255 CERROR("device %d: no name at detach\n", obd->obd_minor);
257 obd->obd_attached = 0;
258 obd->obd_type->typ_refcnt--;
259 class_put_type(obd->obd_type);
260 obd->obd_type = NULL;
261 minor = obd->obd_minor;
262 memset(obd, 0, sizeof(*obd));
263 obd->obd_minor = minor;
267 static void dump_exports(struct obd_device *obd)
269 struct obd_export *exp, *n;
271 list_for_each_entry_safe(exp, n, &obd->obd_exports, exp_obd_chain) {
272 CERROR("%s: %p %s %d %d %p\n",
273 obd->obd_name, exp, exp->exp_client_uuid.uuid,
274 atomic_read(&exp->exp_refcount),
275 exp->exp_failed, exp->exp_outstanding_reply );
279 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg)
286 if (!obd->obd_set_up) {
287 CERROR("Device %d not setup\n", obd->obd_minor);
291 if (lcfg->lcfg_inlbuf1) {
292 for (flag = lcfg->lcfg_inlbuf1; *flag != 0; flag++)
295 flags |= OBD_OPT_FORCE;
298 flags |= OBD_OPT_FAILOVER;
301 CERROR("unrecognised flag '%c'\n",
306 /* The one reference that should be remaining is the
308 if (atomic_read(&obd->obd_refcount) <= 1 ||
309 flags & OBD_OPT_FORCE) {
310 /* this will stop new connections, and need to
311 do it before class_disconnect_exports() */
312 obd->obd_stopping = 1;
315 if (atomic_read(&obd->obd_refcount) > 1) {
316 struct l_wait_info lwi = LWI_TIMEOUT_INTR(1 * HZ, NULL,
320 if (!(flags & OBD_OPT_FORCE)) {
321 CERROR("OBD device %d (%p) has refcount %d\n",
323 atomic_read(&obd->obd_refcount));
325 GOTO(out, err = -EBUSY);
327 class_disconnect_exports(obd, flags);
329 "%s: waiting for obd refs to go away: %d\n",
330 obd->obd_name, atomic_read(&obd->obd_refcount));
332 rc = l_wait_event(obd->obd_refcount_waitq,
333 atomic_read(&obd->obd_refcount) < 2, &lwi);
335 LASSERT(atomic_read(&obd->obd_refcount) == 1);
337 CERROR("wait cancelled cleaning anyway. "
339 atomic_read(&obd->obd_refcount));
342 CDEBUG(D_IOCTL, "%s: awake, now finishing cleanup\n",
346 if (obd->obd_self_export) {
347 err = obd_precleanup(obd, flags);
350 class_unlink_export(obd->obd_self_export);
351 obd->obd_self_export = NULL;
354 err = obd_cleanup(obd, flags);
357 obd->obd_set_up = obd->obd_stopping = 0;
358 obd->obd_type->typ_refcnt--;
359 /* XXX this should be an LASSERT */
360 if (atomic_read(&obd->obd_refcount) > 0)
361 CERROR("%s still has refcount %d after "
362 "cleanup.\n", obd->obd_name,
363 atomic_read(&obd->obd_refcount));
370 LIST_HEAD(lustre_profile_list);
372 struct lustre_profile *class_get_profile(char * prof)
374 struct lustre_profile *lprof;
376 list_for_each_entry(lprof, &lustre_profile_list, lp_list) {
377 if (!strcmp(lprof->lp_profile, prof)) {
384 int class_add_profile(int proflen, char *prof,
385 int osclen, char *osc,
386 int mdclen, char *mdc)
388 struct lustre_profile *lprof;
391 OBD_ALLOC(lprof, sizeof(*lprof));
393 GOTO(out, err = -ENOMEM);
394 INIT_LIST_HEAD(&lprof->lp_list);
396 LASSERT(proflen == (strlen(prof) + 1));
397 OBD_ALLOC(lprof->lp_profile, proflen);
398 if (lprof->lp_profile == NULL)
399 GOTO(out, err = -ENOMEM);
400 memcpy(lprof->lp_profile, prof, proflen);
402 LASSERT(osclen == (strlen(osc) + 1));
403 OBD_ALLOC(lprof->lp_osc, osclen);
404 if (lprof->lp_profile == NULL)
405 GOTO(out, err = -ENOMEM);
406 memcpy(lprof->lp_osc, osc, osclen);
409 LASSERT(mdclen == (strlen(mdc) + 1));
410 OBD_ALLOC(lprof->lp_mdc, mdclen);
411 if (lprof->lp_mdc == NULL)
412 GOTO(out, err = -ENOMEM);
413 memcpy(lprof->lp_mdc, mdc, mdclen);
416 list_add(&lprof->lp_list, &lustre_profile_list);
422 void class_del_profile(char *prof)
424 struct lustre_profile *lprof;
426 lprof = class_get_profile(prof);
428 list_del(&lprof->lp_list);
429 OBD_FREE(lprof->lp_profile, strlen(lprof->lp_profile) + 1);
430 OBD_FREE(lprof->lp_osc, strlen(lprof->lp_osc) + 1);
432 OBD_FREE(lprof->lp_mdc, strlen(lprof->lp_mdc) + 1);
433 OBD_FREE(lprof, sizeof *lprof);
437 int class_process_config(struct lustre_cfg *lcfg)
439 struct obd_device *obd;
440 char str[PTL_NALFMT_SIZE];
443 LASSERT(lcfg && !IS_ERR(lcfg));
445 CDEBUG(D_IOCTL, "processing cmd: %x\n", lcfg->lcfg_command);
447 /* Commands that don't need a device */
448 switch(lcfg->lcfg_command) {
450 err = class_attach(lcfg);
453 case LCFG_ADD_UUID: {
454 CDEBUG(D_IOCTL, "adding mapping from uuid %s to nid "LPX64
455 " (%s), nal %d\n", lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
456 portals_nid2str(lcfg->lcfg_nal, lcfg->lcfg_nid, str),
459 err = class_add_uuid(lcfg->lcfg_inlbuf1, lcfg->lcfg_nid,
463 case LCFG_DEL_UUID: {
464 CDEBUG(D_IOCTL, "removing mappings for uuid %s\n",
465 lcfg->lcfg_inlbuf1 == NULL ? "<all uuids>" :
468 err = class_del_uuid(lcfg->lcfg_inlbuf1);
471 case LCFG_MOUNTOPT: {
472 CDEBUG(D_IOCTL, "mountopt: profile %s osc %s mdc %s\n",
473 lcfg->lcfg_inlbuf1, lcfg->lcfg_inlbuf2, lcfg->lcfg_inlbuf3);
474 /* set these mount options somewhere, so ll_fill_super
476 err = class_add_profile(lcfg->lcfg_inllen1, lcfg->lcfg_inlbuf1,
477 lcfg->lcfg_inllen2, lcfg->lcfg_inlbuf2,
478 lcfg->lcfg_inllen3, lcfg->lcfg_inlbuf3);
481 case LCFG_DEL_MOUNTOPT: {
482 CDEBUG(D_IOCTL, "mountopt: profile %s\n", lcfg->lcfg_inlbuf1);
483 /* set these mount options somewhere, so ll_fill_super
485 class_del_profile(lcfg->lcfg_inlbuf1);
488 case LCFG_SET_TIMEOUT: {
489 CDEBUG(D_IOCTL, "changing lustre timeout from %d to %d\n",
492 obd_timeout = lcfg->lcfg_num;
495 case LCFG_SET_UPCALL: {
496 CDEBUG(D_IOCTL, "setting lustre ucpall to: %s\n",
498 if (lcfg->lcfg_inllen1 > sizeof obd_lustre_upcall)
499 GOTO(out, err = -EINVAL);
500 memcpy(obd_lustre_upcall, lcfg->lcfg_inlbuf1,
507 /* Commands that require a device */
508 obd = class_name2obd(lcfg->lcfg_dev_name);
510 if (lcfg->lcfg_dev_name == NULL) {
511 CERROR("this lcfg command requires a device name\n");
513 CERROR("no device for: %s\n", lcfg->lcfg_dev_name);
515 GOTO(out, err = -EINVAL);
518 switch(lcfg->lcfg_command) {
520 err = class_setup(obd, lcfg);
524 err = class_detach(obd, lcfg);
528 err = class_cleanup(obd, lcfg);
532 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
533 GOTO(out, err = -EINVAL);
541 static int class_config_llog_handler(struct llog_handle * handle,
542 struct llog_rec_hdr *rec, void *data)
544 struct config_llog_instance *cfg = data;
545 int cfg_len = rec->lrh_len;
546 char *cfg_buf = (char*) (rec + 1);
549 if (rec->lrh_type == OBD_CFG_REC) {
551 struct lustre_cfg *lcfg;
552 char *old_name = NULL;
554 char *old_uuid = NULL;
555 int old_uuid_len = 0;
556 char *inst_name = NULL;
559 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
562 lcfg = (struct lustre_cfg* ) buf;
564 if (cfg && cfg->cfg_instance && lcfg->lcfg_dev_name) {
565 inst_len = strlen(lcfg->lcfg_dev_name) +
566 strlen(cfg->cfg_instance) + 2;
567 OBD_ALLOC(inst_name, inst_len);
568 if (inst_name == NULL)
569 GOTO(out, rc = -ENOMEM);
570 sprintf(inst_name, "%s-%s", lcfg->lcfg_dev_name,
572 old_name = lcfg->lcfg_dev_name;
573 old_len = lcfg->lcfg_dev_namelen;
574 lcfg->lcfg_dev_name = inst_name;
575 lcfg->lcfg_dev_namelen = strlen(inst_name) + 1;
578 if (cfg && lcfg->lcfg_command == LCFG_ATTACH) {
579 old_uuid = lcfg->lcfg_inlbuf2;
580 old_uuid_len = lcfg->lcfg_inllen2;
582 lcfg->lcfg_inlbuf2 = (char*)&cfg->cfg_uuid.uuid;
583 lcfg->lcfg_inllen2 = sizeof(cfg->cfg_uuid);
586 rc = class_process_config(lcfg);
589 lcfg->lcfg_dev_name = old_name;
590 lcfg->lcfg_dev_namelen = old_len;
591 OBD_FREE(inst_name, inst_len);
595 lcfg->lcfg_inlbuf2 = old_uuid;
596 lcfg->lcfg_inllen2 = old_uuid_len;
599 lustre_cfg_freedata(buf, cfg_len);
600 } else if (rec->lrh_type == PTL_CFG_REC) {
601 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
602 if (pcfg->pcfg_command ==NAL_CMD_REGISTER_MYNID &&
603 cfg->cfg_local_nid != PTL_NID_ANY) {
604 pcfg->pcfg_nid = cfg->cfg_local_nid;
607 rc = kportal_nal_cmd(pcfg);
613 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
614 struct config_llog_instance *cfg)
616 struct llog_handle *llh;
620 rc = llog_create(ctxt, &llh, NULL, name);
624 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
628 rc = llog_process(llh, class_config_llog_handler, cfg);
630 rc2 = llog_close(llh);
638 static int class_config_dump_handler(struct llog_handle * handle,
639 struct llog_rec_hdr *rec, void *data)
641 int cfg_len = rec->lrh_len;
642 char *cfg_buf = (char*) (rec + 1);
645 if (rec->lrh_type == OBD_CFG_REC) {
647 struct lustre_cfg *lcfg;
649 rc = lustre_cfg_getdata(&buf, cfg_len, cfg_buf, 1);
652 lcfg = (struct lustre_cfg* ) buf;
654 CDEBUG(D_INFO, "lcfg command: %x\n", lcfg->lcfg_command);
655 if (lcfg->lcfg_dev_name)
656 CDEBUG(D_INFO, " devname: %s\n",
657 lcfg->lcfg_dev_name);
658 if (lcfg->lcfg_flags)
659 CDEBUG(D_INFO, " flags: %x\n", lcfg->lcfg_flags);
661 CDEBUG(D_INFO, " nid: "LPX64"\n",
664 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_nal);
666 CDEBUG(D_INFO, " nal: %x\n", lcfg->lcfg_num);
667 if (lcfg->lcfg_inlbuf1)
668 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf1);
669 if (lcfg->lcfg_inlbuf2)
670 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf2);
671 if (lcfg->lcfg_inlbuf3)
672 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf3);
673 if (lcfg->lcfg_inlbuf4)
674 CDEBUG(D_INFO, " inlbuf1: %s\n",lcfg->lcfg_inlbuf4);
676 lustre_cfg_freedata(buf, cfg_len);
677 } else if (rec->lrh_type == PTL_CFG_REC) {
678 struct portals_cfg *pcfg = (struct portals_cfg *)cfg_buf;
680 CDEBUG(D_INFO, "pcfg command: %d\n", pcfg->pcfg_command);
682 CDEBUG(D_INFO, " nal: %d\n",
684 if (pcfg->pcfg_gw_nal)
685 CDEBUG(D_INFO, " gw_nal: %d\n",
688 CDEBUG(D_INFO, " nid: "LPX64"\n",
691 CDEBUG(D_INFO, " nid: "LPX64"\n",
694 CDEBUG(D_INFO, " nid: "LPX64"\n",
697 CDEBUG(D_INFO, " nid: %d\n",
700 CDEBUG(D_INFO, " id: %x\n",
702 if (pcfg->pcfg_flags)
703 CDEBUG(D_INFO, " flags: %x\n",
710 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
711 struct config_llog_instance *cfg)
713 struct llog_handle *llh;
717 rc = llog_create(ctxt, &llh, NULL, name);
721 rc = llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
725 rc = llog_process(llh, class_config_dump_handler, cfg);
727 rc2 = llog_close(llh);