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.
21 * These are the only exported functions, they provide some generic
22 * infrastructure for managing object devices
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/lprocfs_status.h>
40 extern struct list_head obd_types;
41 static spinlock_t obd_types_lock = SPIN_LOCK_UNLOCKED;
42 kmem_cache_t *obdo_cachep = NULL;
43 kmem_cache_t *import_cachep = NULL;
45 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
46 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
49 * support functions: we could use inter-module communication, but this
50 * is more portable to other OS's
52 static struct obd_type *class_search_type(char *name)
54 struct list_head *tmp;
55 struct obd_type *type;
57 spin_lock(&obd_types_lock);
58 list_for_each(tmp, &obd_types) {
59 type = list_entry(tmp, struct obd_type, typ_chain);
60 if (strlen(type->typ_name) == strlen(name) &&
61 strcmp(type->typ_name, name) == 0) {
62 spin_unlock(&obd_types_lock);
66 spin_unlock(&obd_types_lock);
70 struct obd_type *class_get_type(char *name)
72 struct obd_type *type = class_search_type(name);
76 if (!request_module(name)) {
77 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
78 type = class_search_type(name);
80 CDEBUG(D_INFO, "Can't load module '%s'\n", name);
84 try_module_get(type->typ_ops->o_owner);
88 void class_put_type(struct obd_type *type)
91 module_put(type->typ_ops->o_owner);
94 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
97 struct obd_type *type;
101 LASSERT(strnlen(name, 1024) < 1024); /* sanity check */
103 if (class_search_type(name)) {
104 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
109 OBD_ALLOC(type, sizeof(*type));
113 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
114 OBD_ALLOC(type->typ_name, strlen(name) + 1);
115 if (type->typ_ops == NULL || type->typ_name == NULL)
118 *(type->typ_ops) = *ops;
119 strcpy(type->typ_name, name);
122 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
125 if (IS_ERR(type->typ_procroot)) {
126 rc = PTR_ERR(type->typ_procroot);
127 type->typ_procroot = NULL;
131 spin_lock(&obd_types_lock);
132 list_add(&type->typ_chain, &obd_types);
133 spin_unlock(&obd_types_lock);
138 if (type->typ_name != NULL)
139 OBD_FREE(type->typ_name, strlen(name) + 1);
140 if (type->typ_ops != NULL)
141 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
142 OBD_FREE(type, sizeof(*type));
146 int class_unregister_type(char *name)
148 struct obd_type *type = class_search_type(name);
152 CERROR("unknown obd type\n");
156 if (type->typ_refcnt) {
157 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
158 /* This is a bad situation, let's make the best of it */
159 /* Remove ops, but leave the name for debugging */
160 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
164 if (type->typ_procroot) {
165 lprocfs_remove(type->typ_procroot);
166 type->typ_procroot = NULL;
169 spin_lock(&obd_types_lock);
170 list_del(&type->typ_chain);
171 spin_unlock(&obd_types_lock);
172 OBD_FREE(type->typ_name, strlen(name) + 1);
173 if (type->typ_ops != NULL)
174 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
175 OBD_FREE(type, sizeof(*type));
177 } /* class_unregister_type */
179 struct obd_device *class_newdev(int *dev)
181 struct obd_device *result = NULL;
184 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
185 struct obd_device *obd = &obd_dev[i];
186 if (!obd->obd_type) {
196 int class_name2dev(char *name)
203 for (i = 0; i < MAX_OBD_DEVICES; i++) {
204 struct obd_device *obd = &obd_dev[i];
205 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
212 struct obd_device *class_name2obd(char *name)
214 int dev = class_name2dev(name);
217 return &obd_dev[dev];
220 int class_uuid2dev(struct obd_uuid *uuid)
224 for (i = 0; i < MAX_OBD_DEVICES; i++) {
225 struct obd_device *obd = &obd_dev[i];
226 if (obd_uuid_equals(uuid, &obd->obd_uuid))
233 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
235 int dev = class_uuid2dev(uuid);
238 return &obd_dev[dev];
241 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
242 specified, then only the client with that uuid is returned,
243 otherwise any client connected to the tgt is returned. */
244 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
246 struct obd_uuid *grp_uuid)
250 for (i = 0; i < MAX_OBD_DEVICES; i++) {
251 struct obd_device *obd = &obd_dev[i];
252 if (obd->obd_type == NULL)
254 if ((strncmp(obd->obd_type->typ_name, typ_name,
255 strlen(typ_name)) == 0)) {
256 struct client_obd *cli = &obd->u.cli;
257 struct obd_import *imp = cli->cl_import;
258 if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
259 ((grp_uuid)? obd_uuid_equals(grp_uuid,
260 &obd->obd_uuid) : 1)) {
269 /* Iterate the obd_device list looking devices have grp_uuid. Start
270 searching at *next, and if a device is found, the next index to look
271 it is saved in *next. If next is NULL, then the first matching device
272 will always be returned. */
273 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
278 else if (*next >= 0 && *next < MAX_OBD_DEVICES)
283 for (; i < MAX_OBD_DEVICES; i++) {
284 struct obd_device *obd = &obd_dev[i];
285 if (obd->obd_type == NULL)
287 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
298 void obd_cleanup_caches(void)
303 rc = kmem_cache_destroy(obdo_cachep);
305 CERROR("Cannot destory ll_obdo_cache\n");
309 rc = kmem_cache_destroy(import_cachep);
311 CERROR("Cannot destory ll_import_cache\n");
312 import_cachep = NULL;
317 int obd_init_caches(void)
320 LASSERT(obdo_cachep == NULL);
321 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
326 LASSERT(import_cachep == NULL);
327 import_cachep = kmem_cache_create("ll_import_cache",
328 sizeof(struct obd_import),
335 obd_cleanup_caches();
340 /* map connection to client */
341 struct obd_export *class_conn2export(struct lustre_handle *conn)
343 struct obd_export *export;
347 CDEBUG(D_CACHE, "looking for null handle\n");
351 if (conn->cookie == -1) { /* this means assign a new connection */
352 CDEBUG(D_CACHE, "want a new connection\n");
356 CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
357 export = class_handle2object(conn->cookie);
361 struct obd_device *class_exp2obd(struct obd_export *exp)
368 struct obd_device *class_conn2obd(struct lustre_handle *conn)
370 struct obd_export *export;
371 export = class_conn2export(conn);
373 struct obd_device *obd = export->exp_obd;
374 class_export_put(export);
380 struct obd_import *class_exp2cliimp(struct obd_export *exp)
382 struct obd_device *obd = exp->exp_obd;
385 return obd->u.cli.cl_import;
388 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
390 struct obd_device *obd = class_conn2obd(conn);
393 return obd->u.cli.cl_import;
397 /* Export management functions */
398 static void export_handle_addref(void *export)
400 class_export_get(export);
403 void __class_export_put(struct obd_export *exp)
405 if (atomic_dec_and_test(&exp->exp_refcount)) {
406 struct obd_device *obd = exp->exp_obd;
407 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
408 exp->exp_client_uuid.uuid);
410 LASSERT(obd != NULL);
412 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
413 if (exp->exp_connection)
414 ptlrpc_put_connection_superhack(exp->exp_connection);
416 LASSERT(list_empty(&exp->exp_outstanding_replies));
417 LASSERT(list_empty(&exp->exp_handle.h_link));
418 obd_destroy_export(exp);
420 OBD_FREE(exp, sizeof(*exp));
421 if (obd->obd_set_up) {
422 atomic_dec(&obd->obd_refcount);
423 wake_up(&obd->obd_refcount_waitq);
428 /* Creates a new export, adds it to the hash table, and returns a
429 * pointer to it. The refcount is 2: one for the hash reference, and
430 * one for the pointer returned by this function. */
431 struct obd_export *class_new_export(struct obd_device *obd)
433 struct obd_export *export;
435 OBD_ALLOC(export, sizeof(*export));
437 CERROR("no memory! (minor %d)\n", obd->obd_minor);
441 export->exp_conn_cnt = 0;
442 atomic_set(&export->exp_refcount, 2);
443 export->exp_obd = obd;
444 INIT_LIST_HEAD(&export->exp_outstanding_replies);
445 /* XXX this should be in LDLM init */
446 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
448 INIT_LIST_HEAD(&export->exp_handle.h_link);
449 class_handle_hash(&export->exp_handle, export_handle_addref);
450 spin_lock_init(&export->exp_lock);
452 spin_lock(&obd->obd_dev_lock);
453 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
454 atomic_inc(&obd->obd_refcount);
455 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
456 export->exp_obd->obd_num_exports++;
457 spin_unlock(&obd->obd_dev_lock);
458 obd_init_export(export);
462 void class_unlink_export(struct obd_export *exp)
464 class_handle_unhash(&exp->exp_handle);
466 spin_lock(&exp->exp_obd->obd_dev_lock);
467 list_del_init(&exp->exp_obd_chain);
468 exp->exp_obd->obd_num_exports--;
469 spin_unlock(&exp->exp_obd->obd_dev_lock);
471 class_export_put(exp);
474 /* Import management functions */
475 static void import_handle_addref(void *import)
477 class_import_get(import);
480 struct obd_import *class_import_get(struct obd_import *import)
482 atomic_inc(&import->imp_refcount);
483 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
484 atomic_read(&import->imp_refcount));
488 void class_import_put(struct obd_import *import)
492 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
493 atomic_read(&import->imp_refcount) - 1);
495 LASSERT(atomic_read(&import->imp_refcount) > 0);
496 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
497 if (!atomic_dec_and_test(&import->imp_refcount)) {
502 CDEBUG(D_IOCTL, "destroying import %p\n", import);
504 ptlrpc_put_connection_superhack(import->imp_connection);
506 LASSERT(list_empty(&import->imp_handle.h_link));
507 OBD_FREE(import, sizeof(*import));
511 struct obd_import *class_new_import(void)
513 struct obd_import *imp;
515 OBD_ALLOC(imp, sizeof(*imp));
519 INIT_LIST_HEAD(&imp->imp_replay_list);
520 INIT_LIST_HEAD(&imp->imp_sending_list);
521 INIT_LIST_HEAD(&imp->imp_delayed_list);
522 spin_lock_init(&imp->imp_lock);
523 imp->imp_conn_cnt = 0;
524 imp->imp_max_transno = 0;
525 imp->imp_peer_committed_transno = 0;
526 imp->imp_state = LUSTRE_IMP_NEW;
527 init_waitqueue_head(&imp->imp_recovery_waitq);
529 atomic_set(&imp->imp_refcount, 2);
530 atomic_set(&imp->imp_replay_inflight, 0);
531 INIT_LIST_HEAD(&imp->imp_handle.h_link);
532 class_handle_hash(&imp->imp_handle, import_handle_addref);
537 void class_destroy_import(struct obd_import *import)
539 LASSERT(import != NULL);
540 LASSERT((unsigned long)import != 0x5a5a5a5a);
542 class_handle_unhash(&import->imp_handle);
544 /* Abort any inflight DLM requests and NULL out their (about to be
546 /* Invalidate all requests on import, would be better to call
547 ptlrpc_set_import_active(imp, 0); */
548 import->imp_generation++;
549 ptlrpc_abort_inflight_superhack(import);
551 class_import_put(import);
554 /* A connection defines an export context in which preallocation can
555 be managed. This releases the export pointer reference, and returns
556 the export handle, so the export refcount is 1 when this function
558 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
559 struct obd_uuid *cluuid)
561 struct obd_export *export;
562 LASSERT(conn != NULL);
563 LASSERT(obd != NULL);
564 LASSERT(cluuid != NULL);
567 export = class_new_export(obd);
571 conn->cookie = export->exp_handle.h_cookie;
572 memcpy(&export->exp_client_uuid, cluuid,
573 sizeof(export->exp_client_uuid));
574 class_export_put(export);
576 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
577 cluuid->uuid, conn->cookie);
581 /* This function removes two references from the export: one for the
582 * hash entry and one for the export pointer passed in. The export
583 * pointer passed to this function is destroyed should not be used
585 int class_disconnect(struct obd_export *export, int flags)
589 if (export == NULL) {
591 CDEBUG(D_IOCTL, "disconnect: attempting to free "
592 "null export %p\n", export);
596 /* XXX this shouldn't have to be here, but double-disconnect will crash
597 * otherwise, and sometimes double-disconnect happens. abort_recovery,
599 if (list_empty(&export->exp_handle.h_link))
602 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
603 export->exp_handle.h_cookie);
605 class_unlink_export(export);
606 class_export_put(export);
610 void class_disconnect_exports(struct obd_device *obd, int flags)
613 struct list_head *tmp, *n, work_list;
614 struct lustre_handle fake_conn;
615 struct obd_export *fake_exp, *exp;
618 /* Move all of the exports from obd_exports to a work list, en masse. */
619 spin_lock(&obd->obd_dev_lock);
620 list_add(&work_list, &obd->obd_exports);
621 list_del_init(&obd->obd_exports);
622 spin_unlock(&obd->obd_dev_lock);
624 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
625 "disconnecting them\n", obd->obd_minor, obd);
626 list_for_each_safe(tmp, n, &work_list) {
627 exp = list_entry(tmp, struct obd_export, exp_obd_chain);
628 class_export_get(exp);
630 if (obd_uuid_equals(&exp->exp_client_uuid,
631 &exp->exp_obd->obd_uuid)) {
633 "exp %p export uuid == obd uuid, don't discon\n",
635 class_export_put(exp);
639 fake_conn.cookie = exp->exp_handle.h_cookie;
640 fake_exp = class_conn2export(&fake_conn);
642 class_export_put(exp);
645 rc = obd_disconnect(fake_exp, flags);
646 class_export_put(exp);
648 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
651 CDEBUG(D_HA, "export %p disconnected\n", exp);
657 int oig_init(struct obd_io_group **oig_out)
659 struct obd_io_group *oig;
662 OBD_ALLOC(oig, sizeof(*oig));
666 spin_lock_init(&oig->oig_lock);
668 oig->oig_pending = 0;
669 atomic_set(&oig->oig_refcount, 1);
670 init_waitqueue_head(&oig->oig_waitq);
671 INIT_LIST_HEAD(&oig->oig_occ_list);
677 static inline void oig_grab(struct obd_io_group *oig)
679 atomic_inc(&oig->oig_refcount);
681 void oig_release(struct obd_io_group *oig)
683 if (atomic_dec_and_test(&oig->oig_refcount))
684 OBD_FREE(oig, sizeof(*oig));
687 void oig_add_one(struct obd_io_group *oig,
688 struct oig_callback_context *occ)
691 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
692 spin_lock_irqsave(&oig->oig_lock, flags);
695 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
696 spin_unlock_irqrestore(&oig->oig_lock, flags);
700 void oig_complete_one(struct obd_io_group *oig,
701 struct oig_callback_context *occ, int rc)
704 wait_queue_head_t *wake = NULL;
707 spin_lock_irqsave(&oig->oig_lock, flags);
710 list_del_init(&occ->occ_oig_item);
712 old_rc = oig->oig_rc;
713 if (oig->oig_rc == 0 && rc != 0)
716 if (--oig->oig_pending <= 0)
717 wake = &oig->oig_waitq;
719 spin_unlock_irqrestore(&oig->oig_lock, flags);
721 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
722 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
729 static int oig_done(struct obd_io_group *oig)
733 spin_lock_irqsave(&oig->oig_lock, flags);
734 if (oig->oig_pending <= 0)
736 spin_unlock_irqrestore(&oig->oig_lock, flags);
740 static void interrupted_oig(void *data)
742 struct obd_io_group *oig = data;
743 struct list_head *pos;
744 struct oig_callback_context *occ;
747 spin_lock_irqsave(&oig->oig_lock, flags);
748 list_for_each(pos, &oig->oig_occ_list) {
749 occ = list_entry(pos, struct oig_callback_context,
751 occ->occ_interrupted(occ);
753 spin_unlock_irqrestore(&oig->oig_lock, flags);
756 int oig_wait(struct obd_io_group *oig)
758 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
761 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
764 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
765 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
766 /* we can't continue until the oig has emptied and stopped
767 * referencing state that the caller will free upon return */
769 lwi = (struct l_wait_info){ 0, };
770 } while (rc == -EINTR);
772 LASSERTF(oig->oig_pending == 0,
773 "exiting oig_wait(oig = %p) with %d pending\n", oig,
776 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);