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(obd, obd_device_cachep, SLAB_KERNEL, sizeof(*obd));
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(obd, obd_device_cachep, sizeof(*obd));
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 (strcmp(modname, LUSTRE_MDT_NAME) == 0)
103 modname = LUSTRE_MDS_NAME;
104 if (!request_module(modname)) {
105 CDEBUG(D_INFO, "Loaded module '%s'\n", modname);
106 type = class_search_type(name);
108 LCONSOLE_ERROR("Can't load module '%s'\n", modname);
113 spin_lock(&type->obd_type_lock);
115 try_module_get(type->typ_ops->o_owner);
116 spin_unlock(&type->obd_type_lock);
121 void class_put_type(struct obd_type *type)
124 spin_lock(&type->obd_type_lock);
126 module_put(type->typ_ops->o_owner);
127 spin_unlock(&type->obd_type_lock);
130 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
133 struct obd_type *type;
137 LASSERT(strnlen(name, 1024) < 1024); /* sanity check */
139 if (class_search_type(name)) {
140 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
145 OBD_ALLOC(type, sizeof(*type));
149 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
150 OBD_ALLOC(type->typ_name, strlen(name) + 1);
151 if (type->typ_ops == NULL || type->typ_name == NULL)
154 *(type->typ_ops) = *ops;
155 strcpy(type->typ_name, name);
156 spin_lock_init(&type->obd_type_lock);
159 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
161 if (IS_ERR(type->typ_procroot)) {
162 rc = PTR_ERR(type->typ_procroot);
163 type->typ_procroot = NULL;
168 spin_lock(&obd_types_lock);
169 list_add(&type->typ_chain, &obd_types);
170 spin_unlock(&obd_types_lock);
175 if (type->typ_name != NULL)
176 OBD_FREE(type->typ_name, strlen(name) + 1);
177 if (type->typ_ops != NULL)
178 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
179 OBD_FREE(type, sizeof(*type));
183 int class_unregister_type(const char *name)
185 struct obd_type *type = class_search_type(name);
189 CERROR("unknown obd type\n");
193 if (type->typ_refcnt) {
194 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
195 /* This is a bad situation, let's make the best of it */
196 /* Remove ops, but leave the name for debugging */
197 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
201 if (type->typ_procroot)
202 lprocfs_remove(&type->typ_procroot);
204 spin_lock(&obd_types_lock);
205 list_del(&type->typ_chain);
206 spin_unlock(&obd_types_lock);
207 OBD_FREE(type->typ_name, strlen(name) + 1);
208 if (type->typ_ops != NULL)
209 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
210 OBD_FREE(type, sizeof(*type));
212 } /* class_unregister_type */
214 struct obd_device *class_newdev(const char *type_name, const char *name)
216 struct obd_device *result = NULL;
217 struct obd_type *type = NULL;
219 int new_obd_minor = 0;
221 if (strlen(name) > MAX_OBD_NAME) {
222 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
223 RETURN(ERR_PTR(-EINVAL));
226 type = class_get_type(type_name);
228 CERROR("OBD: unknown type: %s\n", type_name);
229 RETURN(ERR_PTR(-ENODEV));
232 spin_lock(&obd_dev_lock);
233 for (i = 0; i < class_devno_max(); i++) {
234 struct obd_device *obd = class_num2obd(i);
235 if (obd && obd->obd_name && (strcmp(name, obd->obd_name) == 0)){
236 CERROR("Device %s already exists, won't add\n", name);
238 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
239 "%p obd_magic %08x != %08x\n", result,
240 result->obd_magic, OBD_DEVICE_MAGIC);
241 LASSERTF(result->obd_minor == new_obd_minor,
242 "%p obd_minor %d != %d\n", result,
243 result->obd_minor, new_obd_minor);
245 obd_devs[result->obd_minor] = NULL;
246 result->obd_name[0]='\0';
247 obd_device_free(result);
249 result = ERR_PTR(-EEXIST);
252 if (!result && !obd) {
253 obd = obd_device_alloc();
255 result = ERR_PTR(-ENOMEM);
259 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
262 obd->obd_type = type;
263 memcpy(obd->obd_name, name, strlen(name));
266 obd_devs[i] = result;
270 spin_unlock(&obd_dev_lock);
272 if (result == NULL && i >= class_devno_max()) {
273 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
275 result = ERR_PTR(-EOVERFLOW);
279 class_put_type(type);
281 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
282 result->obd_name, result);
287 void class_release_dev(struct obd_device *obd)
289 struct obd_type *obd_type = obd->obd_type;
291 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
292 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
293 LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
294 obd, obd->obd_minor, obd_devs[obd->obd_minor]);
295 LASSERT(obd_type != NULL);
297 CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
298 obd->obd_name,obd->obd_type->typ_name);
300 spin_lock(&obd_dev_lock);
301 obd_devs[obd->obd_minor] = NULL;
302 obd_device_free(obd);
303 spin_unlock(&obd_dev_lock);
305 class_put_type(obd_type);
308 int class_name2dev(const char *name)
315 spin_lock(&obd_dev_lock);
316 for (i = 0; i < class_devno_max(); i++) {
317 struct obd_device *obd = class_num2obd(i);
318 if (obd && obd->obd_name && strcmp(name, obd->obd_name) == 0) {
319 /* Make sure we finished attaching before we give
320 out any references */
321 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
322 if (obd->obd_attached) {
323 spin_unlock(&obd_dev_lock);
329 spin_unlock(&obd_dev_lock);
334 struct obd_device *class_name2obd(const char *name)
336 int dev = class_name2dev(name);
338 if (dev < 0 || dev > class_devno_max())
340 return class_num2obd(dev);
343 int class_uuid2dev(struct obd_uuid *uuid)
347 spin_lock(&obd_dev_lock);
348 for (i = 0; i < class_devno_max(); i++) {
349 struct obd_device *obd = class_num2obd(i);
350 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
351 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
352 spin_unlock(&obd_dev_lock);
356 spin_unlock(&obd_dev_lock);
361 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
363 int dev = class_uuid2dev(uuid);
366 return class_num2obd(dev);
369 struct obd_device *class_num2obd(int num)
371 struct obd_device *obd = NULL;
373 if (num < class_devno_max()) {
379 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
380 "%p obd_magic %08x != %08x\n",
381 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
382 LASSERTF(obd->obd_minor == num,
383 "%p obd_minor %0d != %0d\n",
384 obd, obd->obd_minor, num);
390 void class_obd_list(void)
395 spin_lock(&obd_dev_lock);
396 for (i = 0; i < class_devno_max(); i++) {
397 struct obd_device *obd = class_num2obd(i);
400 if (obd->obd_stopping)
402 else if (obd->obd_set_up)
404 else if (obd->obd_attached)
408 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
409 i, status, obd->obd_type->typ_name,
410 obd->obd_name, obd->obd_uuid.uuid,
411 atomic_read(&obd->obd_refcount));
413 spin_unlock(&obd_dev_lock);
417 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
418 specified, then only the client with that uuid is returned,
419 otherwise any client connected to the tgt is returned. */
420 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
421 const char * typ_name,
422 struct obd_uuid *grp_uuid)
426 spin_lock(&obd_dev_lock);
427 for (i = 0; i < class_devno_max(); i++) {
428 struct obd_device *obd = class_num2obd(i);
431 if ((strncmp(obd->obd_type->typ_name, typ_name,
432 strlen(typ_name)) == 0)) {
433 if (obd_uuid_equals(tgt_uuid,
434 &obd->u.cli.cl_target_uuid) &&
435 ((grp_uuid)? obd_uuid_equals(grp_uuid,
436 &obd->obd_uuid) : 1)) {
437 spin_unlock(&obd_dev_lock);
442 spin_unlock(&obd_dev_lock);
447 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
448 struct obd_uuid *grp_uuid)
450 struct obd_device *obd;
452 obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
454 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
459 /* Iterate the obd_device list looking devices have grp_uuid. Start
460 searching at *next, and if a device is found, the next index to look
461 at is saved in *next. If next is NULL, then the first matching device
462 will always be returned. */
463 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
469 else if (*next >= 0 && *next < class_devno_max())
474 spin_lock(&obd_dev_lock);
475 for (; i < class_devno_max(); i++) {
476 struct obd_device *obd = class_num2obd(i);
479 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
482 spin_unlock(&obd_dev_lock);
486 spin_unlock(&obd_dev_lock);
492 void obd_cleanup_caches(void)
497 if (obd_device_cachep) {
498 rc = cfs_mem_cache_destroy(obd_device_cachep);
499 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
500 obd_device_cachep = NULL;
503 rc = cfs_mem_cache_destroy(obdo_cachep);
504 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
508 rc = cfs_mem_cache_destroy(import_cachep);
509 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
510 import_cachep = NULL;
515 int obd_init_caches(void)
519 LASSERT(obd_device_cachep == NULL);
520 obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
521 sizeof(struct obd_device), 0, 0);
522 if (!obd_device_cachep)
525 LASSERT(obdo_cachep == NULL);
526 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
531 LASSERT(import_cachep == NULL);
532 import_cachep = cfs_mem_cache_create("ll_import_cache",
533 sizeof(struct obd_import),
540 obd_cleanup_caches();
545 /* map connection to client */
546 struct obd_export *class_conn2export(struct lustre_handle *conn)
548 struct obd_export *export;
552 CDEBUG(D_CACHE, "looking for null handle\n");
556 if (conn->cookie == -1) { /* this means assign a new connection */
557 CDEBUG(D_CACHE, "want a new connection\n");
561 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
562 export = class_handle2object(conn->cookie);
566 struct obd_device *class_exp2obd(struct obd_export *exp)
573 struct obd_device *class_conn2obd(struct lustre_handle *conn)
575 struct obd_export *export;
576 export = class_conn2export(conn);
578 struct obd_device *obd = export->exp_obd;
579 class_export_put(export);
585 struct obd_import *class_exp2cliimp(struct obd_export *exp)
587 struct obd_device *obd = exp->exp_obd;
590 return obd->u.cli.cl_import;
593 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
595 struct obd_device *obd = class_conn2obd(conn);
598 return obd->u.cli.cl_import;
601 /* Export management functions */
602 static void export_handle_addref(void *export)
604 class_export_get(export);
607 void __class_export_put(struct obd_export *exp)
609 if (atomic_dec_and_test(&exp->exp_refcount)) {
610 LASSERT (list_empty(&exp->exp_obd_chain));
612 CDEBUG(D_IOCTL, "final put %p/%s\n",
613 exp, exp->exp_client_uuid.uuid);
615 spin_lock(&obd_zombie_impexp_lock);
616 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
617 spin_unlock(&obd_zombie_impexp_lock);
619 if (obd_zombie_impexp_notify != NULL)
620 obd_zombie_impexp_notify();
623 EXPORT_SYMBOL(__class_export_put);
625 void class_export_destroy(struct obd_export *exp)
627 struct obd_device *obd = exp->exp_obd;
629 LASSERT (atomic_read(&exp->exp_refcount) == 0);
631 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
632 exp->exp_client_uuid.uuid);
634 LASSERT(obd != NULL);
636 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
637 if (exp->exp_connection)
638 ptlrpc_put_connection_superhack(exp->exp_connection);
640 LASSERT(list_empty(&exp->exp_outstanding_replies));
641 LASSERT(list_empty(&exp->exp_handle.h_link));
642 obd_destroy_export(exp);
644 OBD_FREE(exp, sizeof(*exp));
648 /* Creates a new export, adds it to the hash table, and returns a
649 * pointer to it. The refcount is 2: one for the hash reference, and
650 * one for the pointer returned by this function. */
651 struct obd_export *class_new_export(struct obd_device *obd,
652 struct obd_uuid *cluuid)
654 struct obd_export *export, *tmp;
656 OBD_ALLOC(export, sizeof(*export));
658 return ERR_PTR(-ENOMEM);
660 export->exp_conn_cnt = 0;
661 atomic_set(&export->exp_refcount, 2);
662 atomic_set(&export->exp_rpc_count, 0);
663 export->exp_obd = obd;
664 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
665 /* XXX this should be in LDLM init */
666 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
667 spin_lock_init(&export->exp_ldlm_data.led_lock);
669 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
670 class_handle_hash(&export->exp_handle, export_handle_addref);
671 export->exp_last_request_time = CURRENT_SECONDS;
672 spin_lock_init(&export->exp_lock);
674 export->exp_client_uuid = *cluuid;
675 obd_init_export(export);
677 spin_lock(&obd->obd_dev_lock);
678 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
679 list_for_each_entry(tmp, &obd->obd_exports, exp_obd_chain) {
680 if (obd_uuid_equals(cluuid, &tmp->exp_client_uuid)) {
681 spin_unlock(&obd->obd_dev_lock);
682 CWARN("%s: denying duplicate export for %s\n",
683 obd->obd_name, cluuid->uuid);
684 class_handle_unhash(&export->exp_handle);
685 OBD_FREE_PTR(export);
686 return ERR_PTR(-EALREADY);
690 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
692 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
693 list_add_tail(&export->exp_obd_chain_timed,
694 &export->exp_obd->obd_exports_timed);
695 export->exp_obd->obd_num_exports++;
696 spin_unlock(&obd->obd_dev_lock);
700 EXPORT_SYMBOL(class_new_export);
702 void class_unlink_export(struct obd_export *exp)
704 class_handle_unhash(&exp->exp_handle);
706 spin_lock(&exp->exp_obd->obd_dev_lock);
707 list_del_init(&exp->exp_obd_chain);
708 list_del_init(&exp->exp_obd_chain_timed);
709 exp->exp_obd->obd_num_exports--;
710 spin_unlock(&exp->exp_obd->obd_dev_lock);
712 class_export_put(exp);
714 EXPORT_SYMBOL(class_unlink_export);
716 /* Import management functions */
717 static void import_handle_addref(void *import)
719 class_import_get(import);
722 struct obd_import *class_import_get(struct obd_import *import)
724 LASSERT(atomic_read(&import->imp_refcount) >= 0);
725 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
726 atomic_inc(&import->imp_refcount);
727 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
728 atomic_read(&import->imp_refcount));
731 EXPORT_SYMBOL(class_import_get);
733 void class_import_put(struct obd_import *import)
737 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
738 atomic_read(&import->imp_refcount) - 1);
740 LASSERT(atomic_read(&import->imp_refcount) > 0);
741 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
742 LASSERT(list_empty(&import->imp_zombie_chain));
744 if (atomic_dec_and_test(&import->imp_refcount)) {
746 CDEBUG(D_INFO, "final put import %p\n", import);
748 spin_lock(&obd_zombie_impexp_lock);
749 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
750 spin_unlock(&obd_zombie_impexp_lock);
752 if (obd_zombie_impexp_notify != NULL)
753 obd_zombie_impexp_notify();
759 void class_import_destroy(struct obd_import *import)
763 CDEBUG(D_IOCTL, "destroying import %p\n", import);
765 LASSERT(atomic_read(&import->imp_refcount) == 0);
767 ptlrpc_put_connection_superhack(import->imp_connection);
769 while (!list_empty(&import->imp_conn_list)) {
770 struct obd_import_conn *imp_conn;
772 imp_conn = list_entry(import->imp_conn_list.next,
773 struct obd_import_conn, oic_item);
774 list_del(&imp_conn->oic_item);
775 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
776 OBD_FREE(imp_conn, sizeof(*imp_conn));
779 LASSERT(list_empty(&import->imp_handle.h_link));
780 class_decref(import->imp_obd);
781 OBD_FREE(import, sizeof(*import));
785 EXPORT_SYMBOL(class_import_put);
787 struct obd_import *class_new_import(struct obd_device *obd)
789 struct obd_import *imp;
791 OBD_ALLOC(imp, sizeof(*imp));
795 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
796 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
797 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
798 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
799 spin_lock_init(&imp->imp_lock);
800 imp->imp_last_success_conn = 0;
801 imp->imp_state = LUSTRE_IMP_NEW;
802 imp->imp_obd = class_incref(obd);
803 cfs_waitq_init(&imp->imp_recovery_waitq);
805 atomic_set(&imp->imp_refcount, 2);
806 atomic_set(&imp->imp_inflight, 0);
807 atomic_set(&imp->imp_replay_inflight, 0);
808 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
809 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
810 class_handle_hash(&imp->imp_handle, import_handle_addref);
812 /* the default magic is V1, will be used in connect RPC, and
813 * then adjusted according to the flags in request/reply. */
814 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
818 EXPORT_SYMBOL(class_new_import);
820 void class_destroy_import(struct obd_import *import)
822 LASSERT(import != NULL);
823 LASSERT(import != LP_POISON);
825 class_handle_unhash(&import->imp_handle);
827 import->imp_generation++;
828 class_import_put(import);
830 EXPORT_SYMBOL(class_destroy_import);
832 /* A connection defines an export context in which preallocation can
833 be managed. This releases the export pointer reference, and returns
834 the export handle, so the export refcount is 1 when this function
836 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
837 struct obd_uuid *cluuid)
839 struct obd_export *export;
840 LASSERT(conn != NULL);
841 LASSERT(obd != NULL);
842 LASSERT(cluuid != NULL);
845 export = class_new_export(obd, cluuid);
847 RETURN(PTR_ERR(export));
849 conn->cookie = export->exp_handle.h_cookie;
850 class_export_put(export);
852 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
853 cluuid->uuid, conn->cookie);
856 EXPORT_SYMBOL(class_connect);
858 /* This function removes two references from the export: one for the
859 * hash entry and one for the export pointer passed in. The export
860 * pointer passed to this function is destroyed should not be used
862 int class_disconnect(struct obd_export *export)
864 int already_disconnected;
867 if (export == NULL) {
869 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
873 spin_lock(&export->exp_lock);
874 already_disconnected = export->exp_disconnected;
875 export->exp_disconnected = 1;
876 spin_unlock(&export->exp_lock);
878 /* class_cleanup(), abort_recovery(), and class_fail_export()
879 * all end up in here, and if any of them race we shouldn't
880 * call extra class_export_puts(). */
881 if (already_disconnected)
884 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
885 export->exp_handle.h_cookie);
887 class_unlink_export(export);
888 class_export_put(export);
892 static void class_disconnect_export_list(struct list_head *list, int flags)
895 struct lustre_handle fake_conn;
896 struct obd_export *fake_exp, *exp;
899 /* It's possible that an export may disconnect itself, but
900 * nothing else will be added to this list. */
901 while (!list_empty(list)) {
902 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
903 class_export_get(exp);
904 exp->exp_flags = flags;
906 if (obd_uuid_equals(&exp->exp_client_uuid,
907 &exp->exp_obd->obd_uuid)) {
909 "exp %p export uuid == obd uuid, don't discon\n",
911 /* Need to delete this now so we don't end up pointing
912 * to work_list later when this export is cleaned up. */
913 list_del_init(&exp->exp_obd_chain);
914 class_export_put(exp);
918 fake_conn.cookie = exp->exp_handle.h_cookie;
919 fake_exp = class_conn2export(&fake_conn);
921 class_export_put(exp);
924 fake_exp->exp_flags = flags;
925 rc = obd_disconnect(fake_exp);
926 class_export_put(exp);
928 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
931 CDEBUG(D_HA, "export %p disconnected\n", exp);
937 static inline int get_exp_flags_from_obd(struct obd_device *obd)
939 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
940 (obd->obd_force ? OBD_OPT_FORCE : 0));
943 void class_disconnect_exports(struct obd_device *obd)
945 struct list_head work_list;
948 /* Move all of the exports from obd_exports to a work list, en masse. */
949 spin_lock(&obd->obd_dev_lock);
950 list_add(&work_list, &obd->obd_exports);
951 list_del_init(&obd->obd_exports);
952 spin_unlock(&obd->obd_dev_lock);
954 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
955 "disconnecting them\n", obd->obd_minor, obd);
956 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
959 EXPORT_SYMBOL(class_disconnect_exports);
961 /* Remove exports that have not completed recovery.
963 void class_disconnect_stale_exports(struct obd_device *obd)
965 struct list_head work_list;
966 struct list_head *pos, *n;
967 struct obd_export *exp;
971 CFS_INIT_LIST_HEAD(&work_list);
972 spin_lock(&obd->obd_dev_lock);
973 list_for_each_safe(pos, n, &obd->obd_exports) {
974 exp = list_entry(pos, struct obd_export, exp_obd_chain);
975 if (exp->exp_replay_needed) {
976 list_del(&exp->exp_obd_chain);
977 list_add(&exp->exp_obd_chain, &work_list);
981 spin_unlock(&obd->obd_dev_lock);
983 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
985 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
988 EXPORT_SYMBOL(class_disconnect_stale_exports);
990 int oig_init(struct obd_io_group **oig_out)
992 struct obd_io_group *oig;
995 OBD_ALLOC(oig, sizeof(*oig));
999 spin_lock_init(&oig->oig_lock);
1001 oig->oig_pending = 0;
1002 atomic_set(&oig->oig_refcount, 1);
1003 cfs_waitq_init(&oig->oig_waitq);
1004 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1009 EXPORT_SYMBOL(oig_init);
1011 static inline void oig_grab(struct obd_io_group *oig)
1013 atomic_inc(&oig->oig_refcount);
1016 void oig_release(struct obd_io_group *oig)
1018 if (atomic_dec_and_test(&oig->oig_refcount))
1019 OBD_FREE(oig, sizeof(*oig));
1021 EXPORT_SYMBOL(oig_release);
1023 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1026 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1027 spin_lock(&oig->oig_lock);
1033 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1035 spin_unlock(&oig->oig_lock);
1040 EXPORT_SYMBOL(oig_add_one);
1042 void oig_complete_one(struct obd_io_group *oig,
1043 struct oig_callback_context *occ, int rc)
1045 cfs_waitq_t *wake = NULL;
1048 spin_lock(&oig->oig_lock);
1051 list_del_init(&occ->occ_oig_item);
1053 old_rc = oig->oig_rc;
1054 if (oig->oig_rc == 0 && rc != 0)
1057 if (--oig->oig_pending <= 0)
1058 wake = &oig->oig_waitq;
1060 spin_unlock(&oig->oig_lock);
1062 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1063 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1066 cfs_waitq_signal(wake);
1069 EXPORT_SYMBOL(oig_complete_one);
1071 static int oig_done(struct obd_io_group *oig)
1074 spin_lock(&oig->oig_lock);
1075 if (oig->oig_pending <= 0)
1077 spin_unlock(&oig->oig_lock);
1081 static void interrupted_oig(void *data)
1083 struct obd_io_group *oig = data;
1084 struct oig_callback_context *occ;
1086 spin_lock(&oig->oig_lock);
1087 /* We need to restart the processing each time we drop the lock, as
1088 * it is possible other threads called oig_complete_one() to remove
1089 * an entry elsewhere in the list while we dropped lock. We need to
1090 * drop the lock because osc_ap_completion() calls oig_complete_one()
1091 * which re-gets this lock ;-) as well as a lock ordering issue. */
1093 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1094 if (occ->interrupted)
1096 occ->interrupted = 1;
1097 spin_unlock(&oig->oig_lock);
1098 occ->occ_interrupted(occ);
1099 spin_lock(&oig->oig_lock);
1102 spin_unlock(&oig->oig_lock);
1105 int oig_wait(struct obd_io_group *oig)
1107 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1110 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1113 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1114 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1115 /* we can't continue until the oig has emptied and stopped
1116 * referencing state that the caller will free upon return */
1118 lwi = (struct l_wait_info){ 0, };
1119 } while (rc == -EINTR);
1121 LASSERTF(oig->oig_pending == 0,
1122 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1125 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1128 EXPORT_SYMBOL(oig_wait);
1130 void class_fail_export(struct obd_export *exp)
1132 int rc, already_failed;
1134 spin_lock(&exp->exp_lock);
1135 already_failed = exp->exp_failed;
1136 exp->exp_failed = 1;
1137 spin_unlock(&exp->exp_lock);
1139 if (already_failed) {
1140 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1141 exp, exp->exp_client_uuid.uuid);
1145 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1146 exp, exp->exp_client_uuid.uuid);
1148 if (obd_dump_on_timeout)
1149 libcfs_debug_dumplog();
1151 /* Most callers into obd_disconnect are removing their own reference
1152 * (request, for example) in addition to the one from the hash table.
1153 * We don't have such a reference here, so make one. */
1154 class_export_get(exp);
1155 rc = obd_disconnect(exp);
1157 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1159 CDEBUG(D_HA, "disconnected export %p/%s\n",
1160 exp, exp->exp_client_uuid.uuid);
1162 EXPORT_SYMBOL(class_fail_export);
1164 char *obd_export_nid2str(struct obd_export *exp)
1166 if (exp->exp_connection != NULL)
1167 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1171 EXPORT_SYMBOL(obd_export_nid2str);
1173 #define EVICT_BATCH 32
1174 int obd_export_evict_by_nid(struct obd_device *obd, char *nid)
1176 struct obd_export *doomed_exp[EVICT_BATCH] = { NULL };
1177 struct list_head *p;
1178 int exports_evicted = 0, num_to_evict = 0, i;
1181 spin_lock(&obd->obd_dev_lock);
1182 list_for_each(p, &obd->obd_exports) {
1183 doomed_exp[num_to_evict] = list_entry(p, struct obd_export,
1185 if (strcmp(obd_export_nid2str(doomed_exp[num_to_evict]),
1187 class_export_get(doomed_exp[num_to_evict]);
1188 if (++num_to_evict == EVICT_BATCH)
1192 spin_unlock(&obd->obd_dev_lock);
1194 for (i = 0; i < num_to_evict; i++) {
1196 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1197 obd->obd_name, nid, doomed_exp[i]->exp_client_uuid.uuid,
1199 class_fail_export(doomed_exp[i]);
1200 class_export_put(doomed_exp[i]);
1202 if (num_to_evict == EVICT_BATCH) {
1207 if (!exports_evicted)
1208 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1209 obd->obd_name, nid);
1210 return exports_evicted;
1212 EXPORT_SYMBOL(obd_export_evict_by_nid);
1214 int obd_export_evict_by_uuid(struct obd_device *obd, char *uuid)
1216 struct obd_export *doomed_exp = NULL;
1217 struct list_head *p;
1218 struct obd_uuid doomed;
1219 int exports_evicted = 0;
1221 obd_str2uuid(&doomed, uuid);
1223 spin_lock(&obd->obd_dev_lock);
1224 list_for_each(p, &obd->obd_exports) {
1225 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
1227 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
1228 class_export_get(doomed_exp);
1233 spin_unlock(&obd->obd_dev_lock);
1235 if (doomed_exp == NULL) {
1236 CERROR("%s: can't disconnect %s: no exports found\n",
1237 obd->obd_name, uuid);
1239 CWARN("%s: evicting %s at adminstrative request\n",
1240 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1241 class_fail_export(doomed_exp);
1242 class_export_put(doomed_exp);
1246 return exports_evicted;
1248 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1250 void obd_zombie_impexp_cull(void)
1252 struct obd_import *import;
1253 struct obd_export *export;
1256 spin_lock (&obd_zombie_impexp_lock);
1259 if (!list_empty(&obd_zombie_imports)) {
1260 import = list_entry(obd_zombie_imports.next,
1263 list_del(&import->imp_zombie_chain);
1267 if (!list_empty(&obd_zombie_exports)) {
1268 export = list_entry(obd_zombie_exports.next,
1271 list_del_init(&export->exp_obd_chain);
1274 spin_unlock(&obd_zombie_impexp_lock);
1277 class_import_destroy(import);
1280 class_export_destroy(export);
1282 } while (import != NULL || export != NULL);
1284 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1286 void obd_zombie_impexp_init(void)
1288 INIT_LIST_HEAD(&obd_zombie_imports);
1289 INIT_LIST_HEAD(&obd_zombie_exports);
1290 spin_lock_init(&obd_zombie_impexp_lock);