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 md_ops *md_ops,
95 struct lprocfs_vars *vars, char *name)
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);
116 OBD_ALLOC(type->typ_md_ops, sizeof(*type->typ_md_ops));
117 if (type->typ_ops == NULL || type->typ_name == NULL ||
118 (md_ops && type->typ_md_ops == NULL))
121 *(type->typ_ops) = *ops;
123 *(type->typ_md_ops) = *md_ops;
125 type->typ_md_ops = NULL;
126 strcpy(type->typ_name, name);
129 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
132 if (IS_ERR(type->typ_procroot)) {
133 rc = PTR_ERR(type->typ_procroot);
134 type->typ_procroot = NULL;
138 spin_lock(&obd_types_lock);
139 list_add(&type->typ_chain, &obd_types);
140 spin_unlock(&obd_types_lock);
145 if (type->typ_name != NULL)
146 OBD_FREE(type->typ_name, strlen(name) + 1);
147 if (type->typ_ops != NULL)
148 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
149 if (type->typ_md_ops != NULL)
150 OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
151 OBD_FREE(type, sizeof(*type));
155 int class_unregister_type(char *name)
157 struct obd_type *type = class_search_type(name);
161 CERROR("unknown obd type\n");
165 if (type->typ_refcnt) {
166 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
167 /* This is a bad situation, let's make the best of it */
168 /* Remove ops, but leave the name for debugging */
169 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
173 if (type->typ_procroot) {
174 lprocfs_remove(type->typ_procroot);
175 type->typ_procroot = NULL;
178 spin_lock(&obd_types_lock);
179 list_del(&type->typ_chain);
180 spin_unlock(&obd_types_lock);
181 OBD_FREE(type->typ_name, strlen(name) + 1);
182 if (type->typ_ops != NULL)
183 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
184 if (type->typ_md_ops != NULL)
185 OBD_FREE (type->typ_md_ops, sizeof (*type->typ_md_ops));
186 OBD_FREE(type, sizeof(*type));
188 } /* class_unregister_type */
190 struct obd_device *class_newdev(int *dev)
192 struct obd_device *result = NULL;
195 for (i = 0 ; i < MAX_OBD_DEVICES ; i++) {
196 struct obd_device *obd = &obd_dev[i];
197 if (!obd->obd_type) {
207 int class_name2dev(char *name)
214 for (i = 0; i < MAX_OBD_DEVICES; i++) {
215 struct obd_device *obd = &obd_dev[i];
216 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
223 struct obd_device *class_name2obd(char *name)
225 int dev = class_name2dev(name);
228 return &obd_dev[dev];
231 int class_uuid2dev(struct obd_uuid *uuid)
235 for (i = 0; i < MAX_OBD_DEVICES; i++) {
236 struct obd_device *obd = &obd_dev[i];
237 if (obd_uuid_equals(uuid, &obd->obd_uuid))
244 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
246 int dev = class_uuid2dev(uuid);
249 return &obd_dev[dev];
252 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
253 specified, then only the client with that uuid is returned,
254 otherwise any client connected to the tgt is returned. */
255 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
257 struct obd_uuid *grp_uuid)
261 for (i = 0; i < MAX_OBD_DEVICES; i++) {
262 struct obd_device *obd = &obd_dev[i];
263 if (obd->obd_type == NULL)
265 if ((strncmp(obd->obd_type->typ_name, typ_name,
266 strlen(typ_name)) == 0)) {
267 struct client_obd *cli = &obd->u.cli;
268 struct obd_import *imp = cli->cl_import;
269 if (obd_uuid_equals(tgt_uuid, &imp->imp_target_uuid) &&
270 ((grp_uuid)? obd_uuid_equals(grp_uuid,
271 &obd->obd_uuid) : 1)) {
280 /* Iterate the obd_device list looking devices have grp_uuid. Start
281 searching at *next, and if a device is found, the next index to look
282 it is saved in *next. If next is NULL, then the first matching device
283 will always be returned. */
284 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
289 else if (*next >= 0 && *next < MAX_OBD_DEVICES)
294 for (; i < MAX_OBD_DEVICES; i++) {
295 struct obd_device *obd = &obd_dev[i];
296 if (obd->obd_type == NULL)
298 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
309 void obd_cleanup_caches(void)
314 rc = kmem_cache_destroy(obdo_cachep);
316 CERROR("Cannot destory ll_obdo_cache\n");
320 rc = kmem_cache_destroy(import_cachep);
322 CERROR("Cannot destory ll_import_cache\n");
323 import_cachep = NULL;
328 int obd_init_caches(void)
331 LASSERT(obdo_cachep == NULL);
332 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
337 LASSERT(import_cachep == NULL);
338 import_cachep = kmem_cache_create("ll_import_cache",
339 sizeof(struct obd_import),
346 obd_cleanup_caches();
351 /* map connection to client */
352 struct obd_export *class_conn2export(struct lustre_handle *conn)
354 struct obd_export *export;
358 CDEBUG(D_CACHE, "looking for null handle\n");
362 if (conn->cookie == -1) { /* this means assign a new connection */
363 CDEBUG(D_CACHE, "want a new connection\n");
367 CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
368 export = class_handle2object(conn->cookie);
372 struct obd_device *class_exp2obd(struct obd_export *exp)
379 struct obd_device *class_conn2obd(struct lustre_handle *conn)
381 struct obd_export *export;
382 export = class_conn2export(conn);
384 struct obd_device *obd = export->exp_obd;
385 class_export_put(export);
391 struct obd_import *class_exp2cliimp(struct obd_export *exp)
393 struct obd_device *obd = exp->exp_obd;
396 return obd->u.cli.cl_import;
399 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
401 struct obd_device *obd = class_conn2obd(conn);
404 return obd->u.cli.cl_import;
407 /* Export management functions */
408 static void export_handle_addref(void *export)
410 class_export_get(export);
413 void __class_export_put(struct obd_export *exp)
415 if (atomic_dec_and_test(&exp->exp_refcount)) {
416 struct obd_device *obd = exp->exp_obd;
417 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
418 exp->exp_client_uuid.uuid);
420 LASSERT(obd != NULL);
422 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
423 if (exp->exp_connection)
424 ptlrpc_put_connection_superhack(exp->exp_connection);
426 LASSERT(list_empty(&exp->exp_outstanding_replies));
427 LASSERT(list_empty(&exp->exp_handle.h_link));
428 obd_destroy_export(exp);
430 OBD_FREE(exp, sizeof(*exp));
431 if (obd->obd_set_up) {
432 atomic_dec(&obd->obd_refcount);
433 wake_up(&obd->obd_refcount_waitq);
438 /* Creates a new export, adds it to the hash table, and returns a
439 * pointer to it. The refcount is 2: one for the hash reference, and
440 * one for the pointer returned by this function. */
441 struct obd_export *class_new_export(struct obd_device *obd)
443 struct obd_export *export;
445 OBD_ALLOC(export, sizeof(*export));
447 CERROR("no memory! (minor %d)\n", obd->obd_minor);
451 export->exp_conn_cnt = 0;
452 atomic_set(&export->exp_refcount, 2);
453 export->exp_obd = obd;
454 INIT_LIST_HEAD(&export->exp_outstanding_replies);
455 /* XXX this should be in LDLM init */
456 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
458 INIT_LIST_HEAD(&export->exp_handle.h_link);
459 class_handle_hash(&export->exp_handle, export_handle_addref);
460 spin_lock_init(&export->exp_lock);
462 spin_lock(&obd->obd_dev_lock);
463 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
464 atomic_inc(&obd->obd_refcount);
465 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
466 export->exp_obd->obd_num_exports++;
467 spin_unlock(&obd->obd_dev_lock);
468 obd_init_export(export);
472 void class_unlink_export(struct obd_export *exp)
474 class_handle_unhash(&exp->exp_handle);
476 spin_lock(&exp->exp_obd->obd_dev_lock);
477 list_del_init(&exp->exp_obd_chain);
478 exp->exp_obd->obd_num_exports--;
479 spin_unlock(&exp->exp_obd->obd_dev_lock);
481 class_export_put(exp);
484 /* Import management functions */
485 static void import_handle_addref(void *import)
487 class_import_get(import);
490 struct obd_import *class_import_get(struct obd_import *import)
492 atomic_inc(&import->imp_refcount);
493 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
494 atomic_read(&import->imp_refcount));
498 void class_import_put(struct obd_import *import)
502 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
503 atomic_read(&import->imp_refcount) - 1);
505 LASSERT(atomic_read(&import->imp_refcount) > 0);
506 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
507 if (!atomic_dec_and_test(&import->imp_refcount)) {
512 CDEBUG(D_IOCTL, "destroying import %p\n", import);
514 ptlrpc_put_connection_superhack(import->imp_connection);
516 LASSERT(list_empty(&import->imp_handle.h_link));
517 OBD_FREE(import, sizeof(*import));
521 struct obd_import *class_new_import(void)
523 struct obd_import *imp;
525 OBD_ALLOC(imp, sizeof(*imp));
529 INIT_LIST_HEAD(&imp->imp_replay_list);
530 INIT_LIST_HEAD(&imp->imp_sending_list);
531 INIT_LIST_HEAD(&imp->imp_delayed_list);
532 spin_lock_init(&imp->imp_lock);
533 imp->imp_conn_cnt = 0;
534 imp->imp_max_transno = 0;
535 imp->imp_peer_committed_transno = 0;
536 imp->imp_state = LUSTRE_IMP_NEW;
537 init_waitqueue_head(&imp->imp_recovery_waitq);
539 atomic_set(&imp->imp_refcount, 2);
540 atomic_set(&imp->imp_inflight, 0);
541 atomic_set(&imp->imp_replay_inflight, 0);
542 INIT_LIST_HEAD(&imp->imp_handle.h_link);
543 class_handle_hash(&imp->imp_handle, import_handle_addref);
548 void class_destroy_import(struct obd_import *import)
550 LASSERT(import != NULL);
551 LASSERT((unsigned long)import != 0x5a5a5a5a);
553 class_handle_unhash(&import->imp_handle);
555 /* Abort any inflight DLM requests and NULL out their (about to be
557 /* Invalidate all requests on import, would be better to call
558 ptlrpc_set_import_active(imp, 0); */
559 import->imp_generation++;
560 ptlrpc_abort_inflight_superhack(import);
562 class_import_put(import);
565 /* A connection defines an export context in which preallocation can
566 be managed. This releases the export pointer reference, and returns
567 the export handle, so the export refcount is 1 when this function
569 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
570 struct obd_uuid *cluuid)
572 struct obd_export *export;
573 LASSERT(conn != NULL);
574 LASSERT(obd != NULL);
575 LASSERT(cluuid != NULL);
578 export = class_new_export(obd);
582 conn->cookie = export->exp_handle.h_cookie;
583 memcpy(&export->exp_client_uuid, cluuid,
584 sizeof(export->exp_client_uuid));
585 class_export_put(export);
587 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
588 cluuid->uuid, conn->cookie);
592 /* This function removes two references from the export: one for the
593 * hash entry and one for the export pointer passed in. The export
594 * pointer passed to this function is destroyed should not be used
596 int class_disconnect(struct obd_export *export, int flags)
600 if (export == NULL) {
602 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
606 /* XXX this shouldn't have to be here, but double-disconnect will crash
607 * otherwise, and sometimes double-disconnect happens. abort_recovery,
609 if (list_empty(&export->exp_handle.h_link))
612 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
613 export->exp_handle.h_cookie);
615 class_unlink_export(export);
616 class_export_put(export);
620 void class_disconnect_exports(struct obd_device *obd, int flags)
623 struct list_head *tmp, *n, work_list;
624 struct lustre_handle fake_conn;
625 struct obd_export *fake_exp, *exp;
628 /* Move all of the exports from obd_exports to a work list, en masse. */
629 spin_lock(&obd->obd_dev_lock);
630 list_add(&work_list, &obd->obd_exports);
631 list_del_init(&obd->obd_exports);
632 spin_unlock(&obd->obd_dev_lock);
634 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
635 "disconnecting them\n", obd->obd_minor, obd);
636 list_for_each_safe(tmp, n, &work_list) {
637 exp = list_entry(tmp, struct obd_export, exp_obd_chain);
638 class_export_get(exp);
640 if (obd_uuid_equals(&exp->exp_client_uuid,
641 &exp->exp_obd->obd_uuid)) {
643 "exp %p export uuid == obd uuid, don't discon\n",
645 /* Need to delete this now so we don't end up pointing
646 * to work_list later when this export is cleaned up. */
647 list_del_init(&exp->exp_obd_chain);
648 class_export_put(exp);
652 fake_conn.cookie = exp->exp_handle.h_cookie;
653 fake_exp = class_conn2export(&fake_conn);
655 class_export_put(exp);
658 rc = obd_disconnect(fake_exp, flags);
659 class_export_put(exp);
661 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
664 CDEBUG(D_HA, "export %p disconnected\n", exp);
670 int oig_init(struct obd_io_group **oig_out)
672 struct obd_io_group *oig;
675 OBD_ALLOC(oig, sizeof(*oig));
679 spin_lock_init(&oig->oig_lock);
681 oig->oig_pending = 0;
682 atomic_set(&oig->oig_refcount, 1);
683 init_waitqueue_head(&oig->oig_waitq);
684 INIT_LIST_HEAD(&oig->oig_occ_list);
690 static inline void oig_grab(struct obd_io_group *oig)
692 atomic_inc(&oig->oig_refcount);
694 void oig_release(struct obd_io_group *oig)
696 if (atomic_dec_and_test(&oig->oig_refcount))
697 OBD_FREE(oig, sizeof(*oig));
700 void oig_add_one(struct obd_io_group *oig,
701 struct oig_callback_context *occ)
704 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
705 spin_lock_irqsave(&oig->oig_lock, flags);
708 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
709 spin_unlock_irqrestore(&oig->oig_lock, flags);
713 void oig_complete_one(struct obd_io_group *oig,
714 struct oig_callback_context *occ, int rc)
717 wait_queue_head_t *wake = NULL;
720 spin_lock_irqsave(&oig->oig_lock, flags);
723 list_del_init(&occ->occ_oig_item);
725 old_rc = oig->oig_rc;
726 if (oig->oig_rc == 0 && rc != 0)
729 if (--oig->oig_pending <= 0)
730 wake = &oig->oig_waitq;
732 spin_unlock_irqrestore(&oig->oig_lock, flags);
734 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
735 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
742 static int oig_done(struct obd_io_group *oig)
746 spin_lock_irqsave(&oig->oig_lock, flags);
747 if (oig->oig_pending <= 0)
749 spin_unlock_irqrestore(&oig->oig_lock, flags);
753 static void interrupted_oig(void *data)
755 struct obd_io_group *oig = data;
756 struct list_head *pos;
757 struct oig_callback_context *occ;
760 spin_lock_irqsave(&oig->oig_lock, flags);
761 list_for_each(pos, &oig->oig_occ_list) {
762 occ = list_entry(pos, struct oig_callback_context,
764 occ->occ_interrupted(occ);
766 spin_unlock_irqrestore(&oig->oig_lock, flags);
769 int oig_wait(struct obd_io_group *oig)
771 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
774 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
777 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
778 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
779 /* we can't continue until the oig has emptied and stopped
780 * referencing state that the caller will free upon return */
782 lwi = (struct l_wait_info){ 0, };
783 } while (rc == -EINTR);
785 LASSERTF(oig->oig_pending == 0,
786 "exiting oig_wait(oig = %p) with %d pending\n", oig,
789 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);