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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
24 * These are the only exported functions, they provide some generic
25 * infrastructure for managing object devices
28 #define DEBUG_SUBSYSTEM S_CLASS
30 #include <liblustre.h>
33 #include <obd_class.h>
34 #include <lprocfs_status.h>
36 extern struct list_head obd_types;
37 spinlock_t obd_types_lock;
39 cfs_mem_cache_t *obd_device_cachep;
40 cfs_mem_cache_t *obdo_cachep;
41 EXPORT_SYMBOL(obdo_cachep);
42 cfs_mem_cache_t *import_cachep;
44 struct list_head obd_zombie_imports;
45 struct list_head obd_zombie_exports;
46 spinlock_t obd_zombie_impexp_lock;
47 void (*obd_zombie_impexp_notify)(void) = NULL;
48 EXPORT_SYMBOL(obd_zombie_impexp_notify);
51 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
54 * support functions: we could use inter-module communication, but this
55 * is more portable to other OS's
57 static struct obd_device *obd_device_alloc(void)
59 struct obd_device *obd;
61 OBD_SLAB_ALLOC_PTR(obd, obd_device_cachep);
63 obd->obd_magic = OBD_DEVICE_MAGIC;
67 EXPORT_SYMBOL(obd_device_alloc);
69 static void obd_device_free(struct obd_device *obd)
72 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n",
73 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
74 OBD_SLAB_FREE_PTR(obd, obd_device_cachep);
76 EXPORT_SYMBOL(obd_device_free);
78 struct obd_type *class_search_type(const char *name)
80 struct list_head *tmp;
81 struct obd_type *type;
83 spin_lock(&obd_types_lock);
84 list_for_each(tmp, &obd_types) {
85 type = list_entry(tmp, struct obd_type, typ_chain);
86 if (strcmp(type->typ_name, name) == 0) {
87 spin_unlock(&obd_types_lock);
91 spin_unlock(&obd_types_lock);
95 struct obd_type *class_get_type(const char *name)
97 struct obd_type *type = class_search_type(name);
101 const char *modname = name;
102 if (!request_module(modname)) {
103 CDEBUG(D_INFO, "Loaded module '%s'\n", modname);
104 type = class_search_type(name);
106 LCONSOLE_ERROR_MSG(0x158, "Can't load module '%s'\n",
112 spin_lock(&type->obd_type_lock);
114 try_module_get(type->typ_dt_ops->o_owner);
115 spin_unlock(&type->obd_type_lock);
120 void class_put_type(struct obd_type *type)
123 spin_lock(&type->obd_type_lock);
125 module_put(type->typ_dt_ops->o_owner);
126 spin_unlock(&type->obd_type_lock);
129 #define CLASS_MAX_NAME 1024
131 int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops,
132 struct lprocfs_vars *vars, const char *name,
133 struct lu_device_type *ldt)
135 struct obd_type *type;
140 LASSERT(strnlen(name, CLASS_MAX_NAME) < CLASS_MAX_NAME);
142 if (class_search_type(name)) {
143 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
148 OBD_ALLOC(type, sizeof(*type));
152 OBD_ALLOC_PTR(type->typ_dt_ops);
153 OBD_ALLOC_PTR(type->typ_md_ops);
154 OBD_ALLOC(type->typ_name, strlen(name) + 1);
156 if (type->typ_dt_ops == NULL ||
157 type->typ_md_ops == NULL ||
158 type->typ_name == NULL)
161 *(type->typ_dt_ops) = *dt_ops;
162 /* md_ops is optional */
164 *(type->typ_md_ops) = *md_ops;
165 strcpy(type->typ_name, name);
166 spin_lock_init(&type->obd_type_lock);
169 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
171 if (IS_ERR(type->typ_procroot)) {
172 rc = PTR_ERR(type->typ_procroot);
173 type->typ_procroot = NULL;
179 rc = ldt->ldt_ops->ldto_init(ldt);
184 spin_lock(&obd_types_lock);
185 list_add(&type->typ_chain, &obd_types);
186 spin_unlock(&obd_types_lock);
191 if (type->typ_name != NULL)
192 OBD_FREE(type->typ_name, strlen(name) + 1);
193 if (type->typ_md_ops != NULL)
194 OBD_FREE_PTR(type->typ_md_ops);
195 if (type->typ_dt_ops != NULL)
196 OBD_FREE_PTR(type->typ_dt_ops);
197 OBD_FREE(type, sizeof(*type));
201 int class_unregister_type(const char *name)
203 struct obd_type *type = class_search_type(name);
207 CERROR("unknown obd type\n");
211 if (type->typ_refcnt) {
212 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
213 /* This is a bad situation, let's make the best of it */
214 /* Remove ops, but leave the name for debugging */
215 OBD_FREE_PTR(type->typ_dt_ops);
216 OBD_FREE_PTR(type->typ_md_ops);
220 if (type->typ_procroot) {
221 lprocfs_remove(&type->typ_procroot);
225 type->typ_lu->ldt_ops->ldto_fini(type->typ_lu);
227 spin_lock(&obd_types_lock);
228 list_del(&type->typ_chain);
229 spin_unlock(&obd_types_lock);
230 OBD_FREE(type->typ_name, strlen(name) + 1);
231 if (type->typ_dt_ops != NULL)
232 OBD_FREE_PTR(type->typ_dt_ops);
233 if (type->typ_md_ops != NULL)
234 OBD_FREE_PTR(type->typ_md_ops);
235 OBD_FREE(type, sizeof(*type));
237 } /* class_unregister_type */
239 struct obd_device *class_newdev(const char *type_name, const char *name)
241 struct obd_device *result = NULL;
242 struct obd_device *newdev;
243 struct obd_type *type = NULL;
245 int new_obd_minor = 0;
247 if (strlen(name) > MAX_OBD_NAME) {
248 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
249 RETURN(ERR_PTR(-EINVAL));
252 type = class_get_type(type_name);
254 CERROR("OBD: unknown type: %s\n", type_name);
255 RETURN(ERR_PTR(-ENODEV));
258 newdev = obd_device_alloc();
259 if (newdev == NULL) {
260 class_put_type(type);
261 RETURN(ERR_PTR(-ENOMEM));
263 LASSERT(newdev->obd_magic == OBD_DEVICE_MAGIC);
265 spin_lock(&obd_dev_lock);
266 for (i = 0; i < class_devno_max(); i++) {
267 struct obd_device *obd = class_num2obd(i);
268 if (obd && obd->obd_name &&
269 (strcmp(name, obd->obd_name) == 0)) {
270 CERROR("Device %s already exists, won't add\n", name);
272 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
273 "%p obd_magic %08x != %08x\n", result,
274 result->obd_magic, OBD_DEVICE_MAGIC);
275 LASSERTF(result->obd_minor == new_obd_minor,
276 "%p obd_minor %d != %d\n", result,
277 result->obd_minor, new_obd_minor);
279 obd_devs[result->obd_minor] = NULL;
280 result->obd_name[0]='\0';
282 result = ERR_PTR(-EEXIST);
285 if (!result && !obd) {
287 result->obd_minor = i;
289 result->obd_type = type;
290 memcpy(result->obd_name, name, strlen(name));
291 obd_devs[i] = result;
294 spin_unlock(&obd_dev_lock);
296 if (result == NULL && i >= class_devno_max()) {
297 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
299 result = ERR_PTR(-EOVERFLOW);
302 if (IS_ERR(result)) {
303 obd_device_free(newdev);
304 class_put_type(type);
306 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
307 result->obd_name, result);
312 void class_release_dev(struct obd_device *obd)
314 struct obd_type *obd_type = obd->obd_type;
316 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
317 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
318 LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
319 obd, obd->obd_minor, obd_devs[obd->obd_minor]);
320 LASSERT(obd_type != NULL);
322 CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
323 obd->obd_name,obd->obd_type->typ_name);
325 spin_lock(&obd_dev_lock);
326 obd_devs[obd->obd_minor] = NULL;
327 spin_unlock(&obd_dev_lock);
328 obd_device_free(obd);
330 class_put_type(obd_type);
333 int class_name2dev(const char *name)
340 spin_lock(&obd_dev_lock);
341 for (i = 0; i < class_devno_max(); i++) {
342 struct obd_device *obd = class_num2obd(i);
343 if (obd && obd->obd_name && strcmp(name, obd->obd_name) == 0) {
344 /* Make sure we finished attaching before we give
345 out any references */
346 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
347 if (obd->obd_attached) {
348 spin_unlock(&obd_dev_lock);
354 spin_unlock(&obd_dev_lock);
359 struct obd_device *class_name2obd(const char *name)
361 int dev = class_name2dev(name);
363 if (dev < 0 || dev > class_devno_max())
365 return class_num2obd(dev);
368 int class_uuid2dev(struct obd_uuid *uuid)
372 spin_lock(&obd_dev_lock);
373 for (i = 0; i < class_devno_max(); i++) {
374 struct obd_device *obd = class_num2obd(i);
375 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
376 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
377 spin_unlock(&obd_dev_lock);
381 spin_unlock(&obd_dev_lock);
386 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
388 int dev = class_uuid2dev(uuid);
391 return class_num2obd(dev);
394 struct obd_device *class_num2obd(int num)
396 struct obd_device *obd = NULL;
398 if (num < class_devno_max()) {
404 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
405 "%p obd_magic %08x != %08x\n",
406 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
407 LASSERTF(obd->obd_minor == num,
408 "%p obd_minor %0d != %0d\n",
409 obd, obd->obd_minor, num);
415 void class_obd_list(void)
420 spin_lock(&obd_dev_lock);
421 for (i = 0; i < class_devno_max(); i++) {
422 struct obd_device *obd = class_num2obd(i);
425 if (obd->obd_stopping)
427 else if (obd->obd_set_up)
429 else if (obd->obd_attached)
433 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
434 i, status, obd->obd_type->typ_name,
435 obd->obd_name, obd->obd_uuid.uuid,
436 atomic_read(&obd->obd_refcount));
438 spin_unlock(&obd_dev_lock);
442 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
443 specified, then only the client with that uuid is returned,
444 otherwise any client connected to the tgt is returned. */
445 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
446 const char * typ_name,
447 struct obd_uuid *grp_uuid)
451 spin_lock(&obd_dev_lock);
452 for (i = 0; i < class_devno_max(); i++) {
453 struct obd_device *obd = class_num2obd(i);
456 if ((strncmp(obd->obd_type->typ_name, typ_name,
457 strlen(typ_name)) == 0)) {
458 if (obd_uuid_equals(tgt_uuid,
459 &obd->u.cli.cl_target_uuid) &&
460 ((grp_uuid)? obd_uuid_equals(grp_uuid,
461 &obd->obd_uuid) : 1)) {
462 spin_unlock(&obd_dev_lock);
467 spin_unlock(&obd_dev_lock);
472 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
473 struct obd_uuid *grp_uuid)
475 struct obd_device *obd;
477 obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
479 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
484 /* Iterate the obd_device list looking devices have grp_uuid. Start
485 searching at *next, and if a device is found, the next index to look
486 at is saved in *next. If next is NULL, then the first matching device
487 will always be returned. */
488 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
494 else if (*next >= 0 && *next < class_devno_max())
499 spin_lock(&obd_dev_lock);
500 for (; i < class_devno_max(); i++) {
501 struct obd_device *obd = class_num2obd(i);
504 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
507 spin_unlock(&obd_dev_lock);
511 spin_unlock(&obd_dev_lock);
517 void obd_cleanup_caches(void)
522 if (obd_device_cachep) {
523 rc = cfs_mem_cache_destroy(obd_device_cachep);
524 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
525 obd_device_cachep = NULL;
528 rc = cfs_mem_cache_destroy(obdo_cachep);
529 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
533 rc = cfs_mem_cache_destroy(import_cachep);
534 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
535 import_cachep = NULL;
538 rc = cfs_mem_cache_destroy(capa_cachep);
539 LASSERTF(rc == 0, "Cannot destory capa_cache\n");
545 int obd_init_caches(void)
549 LASSERT(obd_device_cachep == NULL);
550 obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
551 sizeof(struct obd_device),
553 if (!obd_device_cachep)
556 LASSERT(obdo_cachep == NULL);
557 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
562 LASSERT(import_cachep == NULL);
563 import_cachep = cfs_mem_cache_create("ll_import_cache",
564 sizeof(struct obd_import),
569 LASSERT(capa_cachep == NULL);
570 capa_cachep = cfs_mem_cache_create("capa_cache",
571 sizeof(struct obd_capa), 0, 0);
577 obd_cleanup_caches();
582 /* map connection to client */
583 struct obd_export *class_conn2export(struct lustre_handle *conn)
585 struct obd_export *export;
589 CDEBUG(D_CACHE, "looking for null handle\n");
593 if (conn->cookie == -1) { /* this means assign a new connection */
594 CDEBUG(D_CACHE, "want a new connection\n");
598 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
599 export = class_handle2object(conn->cookie);
603 struct obd_device *class_exp2obd(struct obd_export *exp)
610 struct obd_device *class_conn2obd(struct lustre_handle *conn)
612 struct obd_export *export;
613 export = class_conn2export(conn);
615 struct obd_device *obd = export->exp_obd;
616 class_export_put(export);
622 struct obd_import *class_exp2cliimp(struct obd_export *exp)
624 struct obd_device *obd = exp->exp_obd;
627 return obd->u.cli.cl_import;
630 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
632 struct obd_device *obd = class_conn2obd(conn);
635 return obd->u.cli.cl_import;
638 /* Export management functions */
639 static void export_handle_addref(void *export)
641 class_export_get(export);
644 void __class_export_put(struct obd_export *exp)
646 if (atomic_dec_and_test(&exp->exp_refcount)) {
647 LASSERT (list_empty(&exp->exp_obd_chain));
649 CDEBUG(D_IOCTL, "final put %p/%s\n",
650 exp, exp->exp_client_uuid.uuid);
652 spin_lock(&obd_zombie_impexp_lock);
653 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
654 spin_unlock(&obd_zombie_impexp_lock);
656 if (obd_zombie_impexp_notify != NULL)
657 obd_zombie_impexp_notify();
660 EXPORT_SYMBOL(__class_export_put);
662 void class_export_destroy(struct obd_export *exp)
664 struct obd_device *obd = exp->exp_obd;
667 LASSERT (atomic_read(&exp->exp_refcount) == 0);
669 CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
670 exp->exp_client_uuid.uuid, obd->obd_name);
672 LASSERT(obd != NULL);
674 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
675 if (exp->exp_connection)
676 ptlrpc_put_connection_superhack(exp->exp_connection);
678 LASSERT(list_empty(&exp->exp_outstanding_replies));
679 obd_destroy_export(exp);
681 OBD_FREE_RCU(exp, sizeof(*exp), &exp->exp_handle);
686 /* Creates a new export, adds it to the hash table, and returns a
687 * pointer to it. The refcount is 2: one for the hash reference, and
688 * one for the pointer returned by this function. */
689 struct obd_export *class_new_export(struct obd_device *obd,
690 struct obd_uuid *cluuid)
692 struct obd_export *export, *tmp;
694 OBD_ALLOC(export, sizeof(*export));
696 return ERR_PTR(-ENOMEM);
698 export->exp_conn_cnt = 0;
699 atomic_set(&export->exp_refcount, 2);
700 atomic_set(&export->exp_rpc_count, 0);
701 export->exp_obd = obd;
702 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
703 /* XXX this should be in LDLM init */
704 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
705 spin_lock_init(&export->exp_ldlm_data.led_lock);
707 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
708 class_handle_hash(&export->exp_handle, export_handle_addref);
709 export->exp_last_request_time = CURRENT_SECONDS;
710 spin_lock_init(&export->exp_lock);
712 export->exp_client_uuid = *cluuid;
713 obd_init_export(export);
715 spin_lock(&obd->obd_dev_lock);
716 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
717 list_for_each_entry(tmp, &obd->obd_exports, exp_obd_chain) {
718 if (obd_uuid_equals(cluuid, &tmp->exp_client_uuid)) {
719 spin_unlock(&obd->obd_dev_lock);
720 CWARN("%s: denying duplicate export for %s\n",
721 obd->obd_name, cluuid->uuid);
722 class_handle_unhash(&export->exp_handle);
723 OBD_FREE_PTR(export);
724 return ERR_PTR(-EALREADY);
728 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
730 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
731 list_add_tail(&export->exp_obd_chain_timed,
732 &export->exp_obd->obd_exports_timed);
733 export->exp_obd->obd_num_exports++;
734 spin_unlock(&obd->obd_dev_lock);
738 EXPORT_SYMBOL(class_new_export);
740 void class_unlink_export(struct obd_export *exp)
742 class_handle_unhash(&exp->exp_handle);
744 spin_lock(&exp->exp_obd->obd_dev_lock);
745 list_del_init(&exp->exp_obd_chain);
746 list_del_init(&exp->exp_obd_chain_timed);
747 exp->exp_obd->obd_num_exports--;
748 spin_unlock(&exp->exp_obd->obd_dev_lock);
750 class_export_put(exp);
752 EXPORT_SYMBOL(class_unlink_export);
754 /* Import management functions */
755 static void import_handle_addref(void *import)
757 class_import_get(import);
760 struct obd_import *class_import_get(struct obd_import *import)
762 LASSERT(atomic_read(&import->imp_refcount) >= 0);
763 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
764 atomic_inc(&import->imp_refcount);
765 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
766 atomic_read(&import->imp_refcount));
769 EXPORT_SYMBOL(class_import_get);
771 void class_import_put(struct obd_import *import)
775 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
776 atomic_read(&import->imp_refcount) - 1);
778 LASSERT(atomic_read(&import->imp_refcount) > 0);
779 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
780 LASSERT(list_empty(&import->imp_zombie_chain));
782 if (atomic_dec_and_test(&import->imp_refcount)) {
784 CDEBUG(D_INFO, "final put import %p\n", import);
786 spin_lock(&obd_zombie_impexp_lock);
787 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
788 spin_unlock(&obd_zombie_impexp_lock);
790 if (obd_zombie_impexp_notify != NULL)
791 obd_zombie_impexp_notify();
797 void class_import_destroy(struct obd_import *import)
801 CDEBUG(D_IOCTL, "destroying import %p for %s\n", import,
802 import->imp_obd->obd_name);
804 LASSERT(atomic_read(&import->imp_refcount) == 0);
806 ptlrpc_put_connection_superhack(import->imp_connection);
808 while (!list_empty(&import->imp_conn_list)) {
809 struct obd_import_conn *imp_conn;
811 imp_conn = list_entry(import->imp_conn_list.next,
812 struct obd_import_conn, oic_item);
813 list_del(&imp_conn->oic_item);
814 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
815 OBD_FREE(imp_conn, sizeof(*imp_conn));
818 LASSERT(import->imp_sec == NULL);
819 class_decref(import->imp_obd);
820 OBD_FREE_RCU(import, sizeof(*import), &import->imp_handle);
823 EXPORT_SYMBOL(class_import_put);
825 struct obd_import *class_new_import(struct obd_device *obd)
827 struct obd_import *imp;
829 OBD_ALLOC(imp, sizeof(*imp));
833 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
834 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
835 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
836 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
837 spin_lock_init(&imp->imp_lock);
838 imp->imp_last_success_conn = 0;
839 imp->imp_state = LUSTRE_IMP_NEW;
840 imp->imp_obd = class_incref(obd);
841 cfs_waitq_init(&imp->imp_recovery_waitq);
843 atomic_set(&imp->imp_refcount, 2);
844 atomic_set(&imp->imp_inflight, 0);
845 atomic_set(&imp->imp_replay_inflight, 0);
846 atomic_set(&imp->imp_inval_count, 0);
847 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
848 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
849 class_handle_hash(&imp->imp_handle, import_handle_addref);
851 /* the default magic is V1, will be used in connect RPC, and
852 * then adjusted according to the flags in request/reply. */
853 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
857 EXPORT_SYMBOL(class_new_import);
859 void class_destroy_import(struct obd_import *import)
861 LASSERT(import != NULL);
862 LASSERT(import != LP_POISON);
864 class_handle_unhash(&import->imp_handle);
866 spin_lock(&import->imp_lock);
867 import->imp_generation++;
868 spin_unlock(&import->imp_lock);
869 class_import_put(import);
871 EXPORT_SYMBOL(class_destroy_import);
873 /* A connection defines an export context in which preallocation can
874 be managed. This releases the export pointer reference, and returns
875 the export handle, so the export refcount is 1 when this function
877 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
878 struct obd_uuid *cluuid)
880 struct obd_export *export;
881 LASSERT(conn != NULL);
882 LASSERT(obd != NULL);
883 LASSERT(cluuid != NULL);
886 export = class_new_export(obd, cluuid);
888 RETURN(PTR_ERR(export));
890 conn->cookie = export->exp_handle.h_cookie;
891 class_export_put(export);
893 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
894 cluuid->uuid, conn->cookie);
897 EXPORT_SYMBOL(class_connect);
899 /* if export is involved in recovery then clean up related things */
900 void class_export_recovery_cleanup(struct obd_export *exp)
902 struct obd_device *obd = exp->exp_obd;
904 spin_lock_bh(&obd->obd_processing_task_lock);
905 if (obd->obd_recovering && exp->exp_in_recovery) {
906 spin_lock(&exp->exp_lock);
907 exp->exp_in_recovery = 0;
908 spin_unlock(&exp->exp_lock);
909 obd->obd_connected_clients--;
910 /* each connected client is counted as recoverable */
911 obd->obd_recoverable_clients--;
912 if (exp->exp_req_replay_needed) {
913 spin_lock(&exp->exp_lock);
914 exp->exp_req_replay_needed = 0;
915 spin_unlock(&exp->exp_lock);
916 LASSERT(atomic_read(&obd->obd_req_replay_clients));
917 atomic_dec(&obd->obd_req_replay_clients);
919 if (exp->exp_lock_replay_needed) {
920 spin_lock(&exp->exp_lock);
921 exp->exp_lock_replay_needed = 0;
922 spin_unlock(&exp->exp_lock);
923 LASSERT(atomic_read(&obd->obd_lock_replay_clients));
924 atomic_dec(&obd->obd_lock_replay_clients);
927 spin_unlock_bh(&obd->obd_processing_task_lock);
930 /* This function removes two references from the export: one for the
931 * hash entry and one for the export pointer passed in. The export
932 * pointer passed to this function is destroyed should not be used
934 int class_disconnect(struct obd_export *export)
936 int already_disconnected;
939 if (export == NULL) {
941 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
945 spin_lock(&export->exp_lock);
946 already_disconnected = export->exp_disconnected;
947 export->exp_disconnected = 1;
948 spin_unlock(&export->exp_lock);
950 /* class_cleanup(), abort_recovery(), and class_fail_export()
951 * all end up in here, and if any of them race we shouldn't
952 * call extra class_export_puts(). */
953 if (already_disconnected)
956 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
957 export->exp_handle.h_cookie);
959 class_export_recovery_cleanup(export);
960 class_unlink_export(export);
961 class_export_put(export);
965 static void class_disconnect_export_list(struct list_head *list, int flags)
968 struct lustre_handle fake_conn;
969 struct obd_export *fake_exp, *exp;
972 /* It's possible that an export may disconnect itself, but
973 * nothing else will be added to this list. */
974 while (!list_empty(list)) {
975 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
976 class_export_get(exp);
978 spin_lock(&exp->exp_lock);
979 exp->exp_flags = flags;
980 spin_unlock(&exp->exp_lock);
982 if (obd_uuid_equals(&exp->exp_client_uuid,
983 &exp->exp_obd->obd_uuid)) {
985 "exp %p export uuid == obd uuid, don't discon\n",
987 /* Need to delete this now so we don't end up pointing
988 * to work_list later when this export is cleaned up. */
989 list_del_init(&exp->exp_obd_chain);
990 class_export_put(exp);
994 fake_conn.cookie = exp->exp_handle.h_cookie;
995 fake_exp = class_conn2export(&fake_conn);
997 class_export_put(exp);
1001 spin_lock(&fake_exp->exp_lock);
1002 fake_exp->exp_flags = flags;
1003 spin_unlock(&fake_exp->exp_lock);
1005 rc = obd_disconnect(fake_exp);
1006 class_export_put(exp);
1008 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
1011 CDEBUG(D_HA, "export %p disconnected\n", exp);
1017 static inline int get_exp_flags_from_obd(struct obd_device *obd)
1019 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
1020 (obd->obd_force ? OBD_OPT_FORCE : 0));
1023 void class_disconnect_exports(struct obd_device *obd)
1025 struct list_head work_list;
1028 /* Move all of the exports from obd_exports to a work list, en masse. */
1029 spin_lock(&obd->obd_dev_lock);
1030 list_add(&work_list, &obd->obd_exports);
1031 list_del_init(&obd->obd_exports);
1032 spin_unlock(&obd->obd_dev_lock);
1034 if (!list_empty(&work_list)) {
1035 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
1036 "disconnecting them\n", obd->obd_minor, obd);
1037 class_disconnect_export_list(&work_list,
1038 get_exp_flags_from_obd(obd));
1040 CDEBUG(D_HA, "OBD device %d (%p) has no exports\n",
1041 obd->obd_minor, obd);
1044 EXPORT_SYMBOL(class_disconnect_exports);
1046 /* Remove exports that have not completed recovery.
1048 int class_disconnect_stale_exports(struct obd_device *obd,
1049 int (*test_export)(struct obd_export *))
1051 struct list_head work_list;
1052 struct list_head *pos, *n;
1053 struct obd_export *exp;
1057 CFS_INIT_LIST_HEAD(&work_list);
1058 spin_lock(&obd->obd_dev_lock);
1059 list_for_each_safe(pos, n, &obd->obd_exports) {
1060 exp = list_entry(pos, struct obd_export, exp_obd_chain);
1061 if (test_export(exp))
1064 list_del(&exp->exp_obd_chain);
1065 list_add(&exp->exp_obd_chain, &work_list);
1066 /* don't count self-export as client */
1067 if (obd_uuid_equals(&exp->exp_client_uuid,
1068 &exp->exp_obd->obd_uuid))
1072 CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
1073 obd->obd_name, exp->exp_client_uuid.uuid,
1074 exp->exp_connection == NULL ? "<unknown>" :
1075 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1077 spin_unlock(&obd->obd_dev_lock);
1079 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
1080 obd->obd_name, cnt);
1081 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
1084 EXPORT_SYMBOL(class_disconnect_stale_exports);
1086 int oig_init(struct obd_io_group **oig_out)
1088 struct obd_io_group *oig;
1091 OBD_ALLOC(oig, sizeof(*oig));
1095 spin_lock_init(&oig->oig_lock);
1097 oig->oig_pending = 0;
1098 atomic_set(&oig->oig_refcount, 1);
1099 cfs_waitq_init(&oig->oig_waitq);
1100 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1105 EXPORT_SYMBOL(oig_init);
1107 static inline void oig_grab(struct obd_io_group *oig)
1109 atomic_inc(&oig->oig_refcount);
1112 void oig_release(struct obd_io_group *oig)
1114 if (atomic_dec_and_test(&oig->oig_refcount))
1115 OBD_FREE(oig, sizeof(*oig));
1117 EXPORT_SYMBOL(oig_release);
1119 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1122 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1123 spin_lock(&oig->oig_lock);
1129 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1131 spin_unlock(&oig->oig_lock);
1136 EXPORT_SYMBOL(oig_add_one);
1138 void oig_complete_one(struct obd_io_group *oig,
1139 struct oig_callback_context *occ, int rc)
1141 cfs_waitq_t *wake = NULL;
1144 spin_lock(&oig->oig_lock);
1147 list_del_init(&occ->occ_oig_item);
1149 old_rc = oig->oig_rc;
1150 if (oig->oig_rc == 0 && rc != 0)
1153 if (--oig->oig_pending <= 0)
1154 wake = &oig->oig_waitq;
1156 spin_unlock(&oig->oig_lock);
1158 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1159 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1162 cfs_waitq_signal(wake);
1165 EXPORT_SYMBOL(oig_complete_one);
1167 static int oig_done(struct obd_io_group *oig)
1170 spin_lock(&oig->oig_lock);
1171 if (oig->oig_pending <= 0)
1173 spin_unlock(&oig->oig_lock);
1177 static void interrupted_oig(void *data)
1179 struct obd_io_group *oig = data;
1180 struct oig_callback_context *occ;
1182 spin_lock(&oig->oig_lock);
1183 /* We need to restart the processing each time we drop the lock, as
1184 * it is possible other threads called oig_complete_one() to remove
1185 * an entry elsewhere in the list while we dropped lock. We need to
1186 * drop the lock because osc_ap_completion() calls oig_complete_one()
1187 * which re-gets this lock ;-) as well as a lock ordering issue. */
1189 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1190 if (occ->interrupted)
1192 occ->interrupted = 1;
1193 spin_unlock(&oig->oig_lock);
1194 occ->occ_interrupted(occ);
1195 spin_lock(&oig->oig_lock);
1198 spin_unlock(&oig->oig_lock);
1201 int oig_wait(struct obd_io_group *oig)
1203 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1206 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1209 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1210 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1211 /* we can't continue until the oig has emptied and stopped
1212 * referencing state that the caller will free upon return */
1214 lwi = (struct l_wait_info){ 0, };
1215 } while (rc == -EINTR);
1217 LASSERTF(oig->oig_pending == 0,
1218 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1221 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1224 EXPORT_SYMBOL(oig_wait);
1226 void class_fail_export(struct obd_export *exp)
1228 int rc, already_failed;
1230 spin_lock(&exp->exp_lock);
1231 already_failed = exp->exp_failed;
1232 exp->exp_failed = 1;
1233 spin_unlock(&exp->exp_lock);
1235 if (already_failed) {
1236 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1237 exp, exp->exp_client_uuid.uuid);
1241 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1242 exp, exp->exp_client_uuid.uuid);
1244 if (obd_dump_on_timeout)
1245 libcfs_debug_dumplog();
1247 /* Most callers into obd_disconnect are removing their own reference
1248 * (request, for example) in addition to the one from the hash table.
1249 * We don't have such a reference here, so make one. */
1250 class_export_get(exp);
1251 rc = obd_disconnect(exp);
1253 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1255 CDEBUG(D_HA, "disconnected export %p/%s\n",
1256 exp, exp->exp_client_uuid.uuid);
1258 EXPORT_SYMBOL(class_fail_export);
1260 char *obd_export_nid2str(struct obd_export *exp)
1262 if (exp->exp_connection != NULL)
1263 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1267 EXPORT_SYMBOL(obd_export_nid2str);
1269 #define EVICT_BATCH 32
1270 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid)
1272 struct obd_export *doomed_exp[EVICT_BATCH] = { NULL };
1273 struct list_head *p;
1274 int exports_evicted = 0, num_to_evict = 0, i;
1277 spin_lock(&obd->obd_dev_lock);
1278 list_for_each(p, &obd->obd_exports) {
1279 doomed_exp[num_to_evict] = list_entry(p, struct obd_export,
1281 if (strcmp(obd_export_nid2str(doomed_exp[num_to_evict]),
1283 class_export_get(doomed_exp[num_to_evict]);
1284 if (++num_to_evict == EVICT_BATCH)
1288 spin_unlock(&obd->obd_dev_lock);
1290 for (i = 0; i < num_to_evict; i++) {
1292 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1293 obd->obd_name, nid, doomed_exp[i]->exp_client_uuid.uuid,
1295 class_fail_export(doomed_exp[i]);
1296 class_export_put(doomed_exp[i]);
1298 if (num_to_evict == EVICT_BATCH) {
1303 if (!exports_evicted)
1304 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1305 obd->obd_name, nid);
1306 return exports_evicted;
1308 EXPORT_SYMBOL(obd_export_evict_by_nid);
1310 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
1312 struct obd_export *doomed_exp = NULL;
1313 struct list_head *p;
1314 struct obd_uuid doomed;
1315 int exports_evicted = 0;
1317 obd_str2uuid(&doomed, uuid);
1318 if(obd_uuid_equals(&doomed, &obd->obd_uuid)) {
1319 CERROR("%s: can't evict myself\n", obd->obd_name);
1320 return exports_evicted;
1323 spin_lock(&obd->obd_dev_lock);
1324 list_for_each(p, &obd->obd_exports) {
1325 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
1327 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
1328 class_export_get(doomed_exp);
1333 spin_unlock(&obd->obd_dev_lock);
1335 if (doomed_exp == NULL) {
1336 CERROR("%s: can't disconnect %s: no exports found\n",
1337 obd->obd_name, uuid);
1339 CWARN("%s: evicting %s at adminstrative request\n",
1340 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1341 class_fail_export(doomed_exp);
1342 class_export_put(doomed_exp);
1346 return exports_evicted;
1348 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1350 void obd_zombie_impexp_cull(void)
1352 struct obd_import *import;
1353 struct obd_export *export;
1357 spin_lock (&obd_zombie_impexp_lock);
1360 if (!list_empty(&obd_zombie_imports)) {
1361 import = list_entry(obd_zombie_imports.next,
1364 list_del(&import->imp_zombie_chain);
1368 if (!list_empty(&obd_zombie_exports)) {
1369 export = list_entry(obd_zombie_exports.next,
1372 list_del_init(&export->exp_obd_chain);
1375 spin_unlock(&obd_zombie_impexp_lock);
1378 class_import_destroy(import);
1381 class_export_destroy(export);
1383 } while (import != NULL || export != NULL);
1386 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1388 void obd_zombie_impexp_init(void)
1390 INIT_LIST_HEAD(&obd_zombie_imports);
1391 INIT_LIST_HEAD(&obd_zombie_exports);
1392 spin_lock_init(&obd_zombie_impexp_lock);