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), 0, 0);
552 if (!obd_device_cachep)
555 LASSERT(obdo_cachep == NULL);
556 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
561 LASSERT(import_cachep == NULL);
562 import_cachep = cfs_mem_cache_create("ll_import_cache",
563 sizeof(struct obd_import),
568 LASSERT(capa_cachep == NULL);
569 capa_cachep = cfs_mem_cache_create("capa_cache",
570 sizeof(struct obd_capa), 0, 0);
576 obd_cleanup_caches();
581 /* map connection to client */
582 struct obd_export *class_conn2export(struct lustre_handle *conn)
584 struct obd_export *export;
588 CDEBUG(D_CACHE, "looking for null handle\n");
592 if (conn->cookie == -1) { /* this means assign a new connection */
593 CDEBUG(D_CACHE, "want a new connection\n");
597 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
598 export = class_handle2object(conn->cookie);
602 struct obd_device *class_exp2obd(struct obd_export *exp)
609 struct obd_device *class_conn2obd(struct lustre_handle *conn)
611 struct obd_export *export;
612 export = class_conn2export(conn);
614 struct obd_device *obd = export->exp_obd;
615 class_export_put(export);
621 struct obd_import *class_exp2cliimp(struct obd_export *exp)
623 struct obd_device *obd = exp->exp_obd;
626 return obd->u.cli.cl_import;
629 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
631 struct obd_device *obd = class_conn2obd(conn);
634 return obd->u.cli.cl_import;
637 /* Export management functions */
638 static void export_handle_addref(void *export)
640 class_export_get(export);
643 void __class_export_put(struct obd_export *exp)
645 if (atomic_dec_and_test(&exp->exp_refcount)) {
646 LASSERT (list_empty(&exp->exp_obd_chain));
648 CDEBUG(D_IOCTL, "final put %p/%s\n",
649 exp, exp->exp_client_uuid.uuid);
651 spin_lock(&obd_zombie_impexp_lock);
652 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
653 spin_unlock(&obd_zombie_impexp_lock);
655 if (obd_zombie_impexp_notify != NULL)
656 obd_zombie_impexp_notify();
659 EXPORT_SYMBOL(__class_export_put);
661 void class_export_destroy(struct obd_export *exp)
663 struct obd_device *obd = exp->exp_obd;
666 LASSERT (atomic_read(&exp->exp_refcount) == 0);
668 CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
669 exp->exp_client_uuid.uuid, obd->obd_name);
671 LASSERT(obd != NULL);
673 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
674 if (exp->exp_connection)
675 ptlrpc_put_connection_superhack(exp->exp_connection);
677 LASSERT(list_empty(&exp->exp_outstanding_replies));
678 obd_destroy_export(exp);
680 OBD_FREE_RCU(exp, sizeof(*exp), &exp->exp_handle);
685 /* Creates a new export, adds it to the hash table, and returns a
686 * pointer to it. The refcount is 2: one for the hash reference, and
687 * one for the pointer returned by this function. */
688 struct obd_export *class_new_export(struct obd_device *obd,
689 struct obd_uuid *cluuid)
691 struct obd_export *export, *tmp;
693 OBD_ALLOC(export, sizeof(*export));
695 return ERR_PTR(-ENOMEM);
697 export->exp_conn_cnt = 0;
698 atomic_set(&export->exp_refcount, 2);
699 atomic_set(&export->exp_rpc_count, 0);
700 export->exp_obd = obd;
701 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
702 /* XXX this should be in LDLM init */
703 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
704 spin_lock_init(&export->exp_ldlm_data.led_lock);
706 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
707 class_handle_hash(&export->exp_handle, export_handle_addref);
708 export->exp_last_request_time = CURRENT_SECONDS;
709 spin_lock_init(&export->exp_lock);
711 export->exp_client_uuid = *cluuid;
712 obd_init_export(export);
714 spin_lock(&obd->obd_dev_lock);
715 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
716 list_for_each_entry(tmp, &obd->obd_exports, exp_obd_chain) {
717 if (obd_uuid_equals(cluuid, &tmp->exp_client_uuid)) {
718 spin_unlock(&obd->obd_dev_lock);
719 CWARN("%s: denying duplicate export for %s\n",
720 obd->obd_name, cluuid->uuid);
721 class_handle_unhash(&export->exp_handle);
722 OBD_FREE_PTR(export);
723 return ERR_PTR(-EALREADY);
727 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
729 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
730 list_add_tail(&export->exp_obd_chain_timed,
731 &export->exp_obd->obd_exports_timed);
732 export->exp_obd->obd_num_exports++;
733 spin_unlock(&obd->obd_dev_lock);
737 EXPORT_SYMBOL(class_new_export);
739 void class_unlink_export(struct obd_export *exp)
741 class_handle_unhash(&exp->exp_handle);
743 spin_lock(&exp->exp_obd->obd_dev_lock);
744 list_del_init(&exp->exp_obd_chain);
745 list_del_init(&exp->exp_obd_chain_timed);
746 exp->exp_obd->obd_num_exports--;
747 spin_unlock(&exp->exp_obd->obd_dev_lock);
749 class_export_put(exp);
751 EXPORT_SYMBOL(class_unlink_export);
753 /* Import management functions */
754 static void import_handle_addref(void *import)
756 class_import_get(import);
759 struct obd_import *class_import_get(struct obd_import *import)
761 LASSERT(atomic_read(&import->imp_refcount) >= 0);
762 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
763 atomic_inc(&import->imp_refcount);
764 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
765 atomic_read(&import->imp_refcount));
768 EXPORT_SYMBOL(class_import_get);
770 void class_import_put(struct obd_import *import)
774 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
775 atomic_read(&import->imp_refcount) - 1);
777 LASSERT(atomic_read(&import->imp_refcount) > 0);
778 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
779 LASSERT(list_empty(&import->imp_zombie_chain));
781 if (atomic_dec_and_test(&import->imp_refcount)) {
783 CDEBUG(D_INFO, "final put import %p\n", import);
785 spin_lock(&obd_zombie_impexp_lock);
786 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
787 spin_unlock(&obd_zombie_impexp_lock);
789 if (obd_zombie_impexp_notify != NULL)
790 obd_zombie_impexp_notify();
796 void class_import_destroy(struct obd_import *import)
800 CDEBUG(D_IOCTL, "destroying import %p for %s\n", import,
801 import->imp_obd->obd_name);
803 LASSERT(atomic_read(&import->imp_refcount) == 0);
805 ptlrpc_put_connection_superhack(import->imp_connection);
807 while (!list_empty(&import->imp_conn_list)) {
808 struct obd_import_conn *imp_conn;
810 imp_conn = list_entry(import->imp_conn_list.next,
811 struct obd_import_conn, oic_item);
812 list_del(&imp_conn->oic_item);
813 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
814 OBD_FREE(imp_conn, sizeof(*imp_conn));
817 LASSERT(import->imp_sec == NULL);
818 class_decref(import->imp_obd);
819 OBD_FREE_RCU(import, sizeof(*import), &import->imp_handle);
822 EXPORT_SYMBOL(class_import_put);
824 struct obd_import *class_new_import(struct obd_device *obd)
826 struct obd_import *imp;
828 OBD_ALLOC(imp, sizeof(*imp));
832 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
833 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
834 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
835 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
836 spin_lock_init(&imp->imp_lock);
837 imp->imp_last_success_conn = 0;
838 imp->imp_state = LUSTRE_IMP_NEW;
839 imp->imp_obd = class_incref(obd);
840 cfs_waitq_init(&imp->imp_recovery_waitq);
842 atomic_set(&imp->imp_refcount, 2);
843 atomic_set(&imp->imp_inflight, 0);
844 atomic_set(&imp->imp_replay_inflight, 0);
845 atomic_set(&imp->imp_inval_count, 0);
846 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
847 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
848 class_handle_hash(&imp->imp_handle, import_handle_addref);
850 /* the default magic is V1, will be used in connect RPC, and
851 * then adjusted according to the flags in request/reply. */
852 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
856 EXPORT_SYMBOL(class_new_import);
858 void class_destroy_import(struct obd_import *import)
860 LASSERT(import != NULL);
861 LASSERT(import != LP_POISON);
863 class_handle_unhash(&import->imp_handle);
865 spin_lock(&import->imp_lock);
866 import->imp_generation++;
867 spin_unlock(&import->imp_lock);
868 class_import_put(import);
870 EXPORT_SYMBOL(class_destroy_import);
872 /* A connection defines an export context in which preallocation can
873 be managed. This releases the export pointer reference, and returns
874 the export handle, so the export refcount is 1 when this function
876 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
877 struct obd_uuid *cluuid)
879 struct obd_export *export;
880 LASSERT(conn != NULL);
881 LASSERT(obd != NULL);
882 LASSERT(cluuid != NULL);
885 export = class_new_export(obd, cluuid);
887 RETURN(PTR_ERR(export));
889 conn->cookie = export->exp_handle.h_cookie;
890 class_export_put(export);
892 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
893 cluuid->uuid, conn->cookie);
896 EXPORT_SYMBOL(class_connect);
898 /* if export is involved in recovery then clean up related things */
899 void class_export_recovery_cleanup(struct obd_export *exp)
901 struct obd_device *obd = exp->exp_obd;
903 spin_lock_bh(&obd->obd_processing_task_lock);
904 if (obd->obd_recovering && exp->exp_in_recovery) {
905 spin_lock(&exp->exp_lock);
906 exp->exp_in_recovery = 0;
907 spin_unlock(&exp->exp_lock);
908 obd->obd_connected_clients--;
909 /* each connected client is counted as recoverable */
910 obd->obd_recoverable_clients--;
911 if (exp->exp_req_replay_needed) {
912 spin_lock(&exp->exp_lock);
913 exp->exp_req_replay_needed = 0;
914 spin_unlock(&exp->exp_lock);
915 LASSERT(atomic_read(&obd->obd_req_replay_clients));
916 atomic_dec(&obd->obd_req_replay_clients);
918 if (exp->exp_lock_replay_needed) {
919 spin_lock(&exp->exp_lock);
920 exp->exp_lock_replay_needed = 0;
921 spin_unlock(&exp->exp_lock);
922 LASSERT(atomic_read(&obd->obd_lock_replay_clients));
923 atomic_dec(&obd->obd_lock_replay_clients);
926 spin_unlock_bh(&obd->obd_processing_task_lock);
929 /* This function removes two references from the export: one for the
930 * hash entry and one for the export pointer passed in. The export
931 * pointer passed to this function is destroyed should not be used
933 int class_disconnect(struct obd_export *export)
935 int already_disconnected;
938 if (export == NULL) {
940 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
944 spin_lock(&export->exp_lock);
945 already_disconnected = export->exp_disconnected;
946 export->exp_disconnected = 1;
947 spin_unlock(&export->exp_lock);
949 /* class_cleanup(), abort_recovery(), and class_fail_export()
950 * all end up in here, and if any of them race we shouldn't
951 * call extra class_export_puts(). */
952 if (already_disconnected)
955 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
956 export->exp_handle.h_cookie);
958 class_export_recovery_cleanup(export);
959 class_unlink_export(export);
960 class_export_put(export);
964 static void class_disconnect_export_list(struct list_head *list, int flags)
967 struct lustre_handle fake_conn;
968 struct obd_export *fake_exp, *exp;
971 /* It's possible that an export may disconnect itself, but
972 * nothing else will be added to this list. */
973 while (!list_empty(list)) {
974 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
975 class_export_get(exp);
977 spin_lock(&exp->exp_lock);
978 exp->exp_flags = flags;
979 spin_unlock(&exp->exp_lock);
981 if (obd_uuid_equals(&exp->exp_client_uuid,
982 &exp->exp_obd->obd_uuid)) {
984 "exp %p export uuid == obd uuid, don't discon\n",
986 /* Need to delete this now so we don't end up pointing
987 * to work_list later when this export is cleaned up. */
988 list_del_init(&exp->exp_obd_chain);
989 class_export_put(exp);
993 fake_conn.cookie = exp->exp_handle.h_cookie;
994 fake_exp = class_conn2export(&fake_conn);
996 class_export_put(exp);
1000 spin_lock(&fake_exp->exp_lock);
1001 fake_exp->exp_flags = flags;
1002 spin_unlock(&fake_exp->exp_lock);
1004 rc = obd_disconnect(fake_exp);
1005 class_export_put(exp);
1007 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
1010 CDEBUG(D_HA, "export %p disconnected\n", exp);
1016 static inline int get_exp_flags_from_obd(struct obd_device *obd)
1018 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
1019 (obd->obd_force ? OBD_OPT_FORCE : 0));
1022 void class_disconnect_exports(struct obd_device *obd)
1024 struct list_head work_list;
1027 /* Move all of the exports from obd_exports to a work list, en masse. */
1028 spin_lock(&obd->obd_dev_lock);
1029 list_add(&work_list, &obd->obd_exports);
1030 list_del_init(&obd->obd_exports);
1031 spin_unlock(&obd->obd_dev_lock);
1033 if (!list_empty(&work_list)) {
1034 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
1035 "disconnecting them\n", obd->obd_minor, obd);
1036 class_disconnect_export_list(&work_list,
1037 get_exp_flags_from_obd(obd));
1039 CDEBUG(D_HA, "OBD device %d (%p) has no exports\n",
1040 obd->obd_minor, obd);
1043 EXPORT_SYMBOL(class_disconnect_exports);
1045 /* Remove exports that have not completed recovery.
1047 int class_disconnect_stale_exports(struct obd_device *obd,
1048 int (*test_export)(struct obd_export *))
1050 struct list_head work_list;
1051 struct list_head *pos, *n;
1052 struct obd_export *exp;
1056 CFS_INIT_LIST_HEAD(&work_list);
1057 spin_lock(&obd->obd_dev_lock);
1058 list_for_each_safe(pos, n, &obd->obd_exports) {
1059 exp = list_entry(pos, struct obd_export, exp_obd_chain);
1060 if (test_export(exp))
1063 list_del(&exp->exp_obd_chain);
1064 list_add(&exp->exp_obd_chain, &work_list);
1065 /* don't count self-export as client */
1066 if (obd_uuid_equals(&exp->exp_client_uuid,
1067 &exp->exp_obd->obd_uuid))
1071 CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
1072 obd->obd_name, exp->exp_client_uuid.uuid,
1073 exp->exp_connection == NULL ? "<unknown>" :
1074 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1076 spin_unlock(&obd->obd_dev_lock);
1078 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
1079 obd->obd_name, cnt);
1080 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
1083 EXPORT_SYMBOL(class_disconnect_stale_exports);
1085 int oig_init(struct obd_io_group **oig_out)
1087 struct obd_io_group *oig;
1090 OBD_ALLOC(oig, sizeof(*oig));
1094 spin_lock_init(&oig->oig_lock);
1096 oig->oig_pending = 0;
1097 atomic_set(&oig->oig_refcount, 1);
1098 cfs_waitq_init(&oig->oig_waitq);
1099 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1104 EXPORT_SYMBOL(oig_init);
1106 static inline void oig_grab(struct obd_io_group *oig)
1108 atomic_inc(&oig->oig_refcount);
1111 void oig_release(struct obd_io_group *oig)
1113 if (atomic_dec_and_test(&oig->oig_refcount))
1114 OBD_FREE(oig, sizeof(*oig));
1116 EXPORT_SYMBOL(oig_release);
1118 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1121 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1122 spin_lock(&oig->oig_lock);
1128 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1130 spin_unlock(&oig->oig_lock);
1135 EXPORT_SYMBOL(oig_add_one);
1137 void oig_complete_one(struct obd_io_group *oig,
1138 struct oig_callback_context *occ, int rc)
1140 cfs_waitq_t *wake = NULL;
1143 spin_lock(&oig->oig_lock);
1146 list_del_init(&occ->occ_oig_item);
1148 old_rc = oig->oig_rc;
1149 if (oig->oig_rc == 0 && rc != 0)
1152 if (--oig->oig_pending <= 0)
1153 wake = &oig->oig_waitq;
1155 spin_unlock(&oig->oig_lock);
1157 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1158 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1161 cfs_waitq_signal(wake);
1164 EXPORT_SYMBOL(oig_complete_one);
1166 static int oig_done(struct obd_io_group *oig)
1169 spin_lock(&oig->oig_lock);
1170 if (oig->oig_pending <= 0)
1172 spin_unlock(&oig->oig_lock);
1176 static void interrupted_oig(void *data)
1178 struct obd_io_group *oig = data;
1179 struct oig_callback_context *occ;
1181 spin_lock(&oig->oig_lock);
1182 /* We need to restart the processing each time we drop the lock, as
1183 * it is possible other threads called oig_complete_one() to remove
1184 * an entry elsewhere in the list while we dropped lock. We need to
1185 * drop the lock because osc_ap_completion() calls oig_complete_one()
1186 * which re-gets this lock ;-) as well as a lock ordering issue. */
1188 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1189 if (occ->interrupted)
1191 occ->interrupted = 1;
1192 spin_unlock(&oig->oig_lock);
1193 occ->occ_interrupted(occ);
1194 spin_lock(&oig->oig_lock);
1197 spin_unlock(&oig->oig_lock);
1200 int oig_wait(struct obd_io_group *oig)
1202 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1205 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1208 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1209 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1210 /* we can't continue until the oig has emptied and stopped
1211 * referencing state that the caller will free upon return */
1213 lwi = (struct l_wait_info){ 0, };
1214 } while (rc == -EINTR);
1216 LASSERTF(oig->oig_pending == 0,
1217 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1220 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1223 EXPORT_SYMBOL(oig_wait);
1225 void class_fail_export(struct obd_export *exp)
1227 int rc, already_failed;
1229 spin_lock(&exp->exp_lock);
1230 already_failed = exp->exp_failed;
1231 exp->exp_failed = 1;
1232 spin_unlock(&exp->exp_lock);
1234 if (already_failed) {
1235 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1236 exp, exp->exp_client_uuid.uuid);
1240 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1241 exp, exp->exp_client_uuid.uuid);
1243 if (obd_dump_on_timeout)
1244 libcfs_debug_dumplog();
1246 /* Most callers into obd_disconnect are removing their own reference
1247 * (request, for example) in addition to the one from the hash table.
1248 * We don't have such a reference here, so make one. */
1249 class_export_get(exp);
1250 rc = obd_disconnect(exp);
1252 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1254 CDEBUG(D_HA, "disconnected export %p/%s\n",
1255 exp, exp->exp_client_uuid.uuid);
1257 EXPORT_SYMBOL(class_fail_export);
1259 char *obd_export_nid2str(struct obd_export *exp)
1261 if (exp->exp_connection != NULL)
1262 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1266 EXPORT_SYMBOL(obd_export_nid2str);
1268 #define EVICT_BATCH 32
1269 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid)
1271 struct obd_export *doomed_exp[EVICT_BATCH] = { NULL };
1272 struct list_head *p;
1273 int exports_evicted = 0, num_to_evict = 0, i;
1276 spin_lock(&obd->obd_dev_lock);
1277 list_for_each(p, &obd->obd_exports) {
1278 doomed_exp[num_to_evict] = list_entry(p, struct obd_export,
1280 if (strcmp(obd_export_nid2str(doomed_exp[num_to_evict]),
1282 class_export_get(doomed_exp[num_to_evict]);
1283 if (++num_to_evict == EVICT_BATCH)
1287 spin_unlock(&obd->obd_dev_lock);
1289 for (i = 0; i < num_to_evict; i++) {
1291 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1292 obd->obd_name, nid, doomed_exp[i]->exp_client_uuid.uuid,
1294 class_fail_export(doomed_exp[i]);
1295 class_export_put(doomed_exp[i]);
1297 if (num_to_evict == EVICT_BATCH) {
1302 if (!exports_evicted)
1303 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1304 obd->obd_name, nid);
1305 return exports_evicted;
1307 EXPORT_SYMBOL(obd_export_evict_by_nid);
1309 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
1311 struct obd_export *doomed_exp = NULL;
1312 struct list_head *p;
1313 struct obd_uuid doomed;
1314 int exports_evicted = 0;
1316 obd_str2uuid(&doomed, uuid);
1318 spin_lock(&obd->obd_dev_lock);
1319 list_for_each(p, &obd->obd_exports) {
1320 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
1322 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
1323 class_export_get(doomed_exp);
1328 spin_unlock(&obd->obd_dev_lock);
1330 if (doomed_exp == NULL) {
1331 CERROR("%s: can't disconnect %s: no exports found\n",
1332 obd->obd_name, uuid);
1334 CWARN("%s: evicting %s at adminstrative request\n",
1335 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1336 class_fail_export(doomed_exp);
1337 class_export_put(doomed_exp);
1341 return exports_evicted;
1343 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1345 void obd_zombie_impexp_cull(void)
1347 struct obd_import *import;
1348 struct obd_export *export;
1352 spin_lock (&obd_zombie_impexp_lock);
1355 if (!list_empty(&obd_zombie_imports)) {
1356 import = list_entry(obd_zombie_imports.next,
1359 list_del(&import->imp_zombie_chain);
1363 if (!list_empty(&obd_zombie_exports)) {
1364 export = list_entry(obd_zombie_exports.next,
1367 list_del_init(&export->exp_obd_chain);
1370 spin_unlock(&obd_zombie_impexp_lock);
1373 class_import_destroy(import);
1376 class_export_destroy(export);
1378 } while (import != NULL || export != NULL);
1381 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1383 void obd_zombie_impexp_init(void)
1385 INIT_LIST_HEAD(&obd_zombie_imports);
1386 INIT_LIST_HEAD(&obd_zombie_exports);
1387 spin_lock_init(&obd_zombie_impexp_lock);