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_device *newdev;
218 struct obd_type *type = NULL;
220 int new_obd_minor = 0;
222 if (strlen(name) > MAX_OBD_NAME) {
223 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
224 RETURN(ERR_PTR(-EINVAL));
227 type = class_get_type(type_name);
229 CERROR("OBD: unknown type: %s\n", type_name);
230 RETURN(ERR_PTR(-ENODEV));
233 newdev = obd_device_alloc();
234 if (newdev == NULL) {
235 class_put_type(type);
236 RETURN(ERR_PTR(-ENOMEM));
238 LASSERT(newdev->obd_magic == OBD_DEVICE_MAGIC);
240 spin_lock(&obd_dev_lock);
241 for (i = 0; i < class_devno_max(); i++) {
242 struct obd_device *obd = class_num2obd(i);
243 if (obd && obd->obd_name && (strcmp(name, obd->obd_name) == 0)){
244 CERROR("Device %s already exists, won't add\n", name);
246 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
247 "%p obd_magic %08x != %08x\n", result,
248 result->obd_magic, OBD_DEVICE_MAGIC);
249 LASSERTF(result->obd_minor == new_obd_minor,
250 "%p obd_minor %d != %d\n", result,
251 result->obd_minor, new_obd_minor);
253 obd_devs[result->obd_minor] = NULL;
254 result->obd_name[0]='\0';
256 result = ERR_PTR(-EEXIST);
259 if (!result && !obd) {
261 result->obd_minor = i;
263 result->obd_type = type;
264 memcpy(result->obd_name, name, strlen(name));
265 obd_devs[i] = result;
268 spin_unlock(&obd_dev_lock);
270 if (result == NULL && i >= class_devno_max()) {
271 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
273 result = ERR_PTR(-EOVERFLOW);
276 if (IS_ERR(result)) {
277 obd_device_free(newdev);
278 class_put_type(type);
280 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
281 result->obd_name, result);
286 void class_release_dev(struct obd_device *obd)
288 struct obd_type *obd_type = obd->obd_type;
290 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
291 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
292 LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
293 obd, obd->obd_minor, obd_devs[obd->obd_minor]);
294 LASSERT(obd_type != NULL);
296 CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
297 obd->obd_name,obd->obd_type->typ_name);
299 spin_lock(&obd_dev_lock);
300 obd_devs[obd->obd_minor] = NULL;
301 spin_unlock(&obd_dev_lock);
302 obd_device_free(obd);
304 class_put_type(obd_type);
307 int class_name2dev(const char *name)
314 spin_lock(&obd_dev_lock);
315 for (i = 0; i < class_devno_max(); i++) {
316 struct obd_device *obd = class_num2obd(i);
317 if (obd && obd->obd_name && strcmp(name, obd->obd_name) == 0) {
318 /* Make sure we finished attaching before we give
319 out any references */
320 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
321 if (obd->obd_attached) {
322 spin_unlock(&obd_dev_lock);
328 spin_unlock(&obd_dev_lock);
333 struct obd_device *class_name2obd(const char *name)
335 int dev = class_name2dev(name);
337 if (dev < 0 || dev > class_devno_max())
339 return class_num2obd(dev);
342 int class_uuid2dev(struct obd_uuid *uuid)
346 spin_lock(&obd_dev_lock);
347 for (i = 0; i < class_devno_max(); i++) {
348 struct obd_device *obd = class_num2obd(i);
349 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
350 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
351 spin_unlock(&obd_dev_lock);
355 spin_unlock(&obd_dev_lock);
360 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
362 int dev = class_uuid2dev(uuid);
365 return class_num2obd(dev);
368 struct obd_device *class_num2obd(int num)
370 struct obd_device *obd = NULL;
372 if (num < class_devno_max()) {
378 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
379 "%p obd_magic %08x != %08x\n",
380 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
381 LASSERTF(obd->obd_minor == num,
382 "%p obd_minor %0d != %0d\n",
383 obd, obd->obd_minor, num);
389 void class_obd_list(void)
394 spin_lock(&obd_dev_lock);
395 for (i = 0; i < class_devno_max(); i++) {
396 struct obd_device *obd = class_num2obd(i);
399 if (obd->obd_stopping)
401 else if (obd->obd_set_up)
403 else if (obd->obd_attached)
407 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
408 i, status, obd->obd_type->typ_name,
409 obd->obd_name, obd->obd_uuid.uuid,
410 atomic_read(&obd->obd_refcount));
412 spin_unlock(&obd_dev_lock);
416 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
417 specified, then only the client with that uuid is returned,
418 otherwise any client connected to the tgt is returned. */
419 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
420 const char * typ_name,
421 struct obd_uuid *grp_uuid)
425 spin_lock(&obd_dev_lock);
426 for (i = 0; i < class_devno_max(); i++) {
427 struct obd_device *obd = class_num2obd(i);
430 if ((strncmp(obd->obd_type->typ_name, typ_name,
431 strlen(typ_name)) == 0)) {
432 if (obd_uuid_equals(tgt_uuid,
433 &obd->u.cli.cl_target_uuid) &&
434 ((grp_uuid)? obd_uuid_equals(grp_uuid,
435 &obd->obd_uuid) : 1)) {
436 spin_unlock(&obd_dev_lock);
441 spin_unlock(&obd_dev_lock);
446 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
447 struct obd_uuid *grp_uuid)
449 struct obd_device *obd;
451 obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
453 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
458 /* Iterate the obd_device list looking devices have grp_uuid. Start
459 searching at *next, and if a device is found, the next index to look
460 at is saved in *next. If next is NULL, then the first matching device
461 will always be returned. */
462 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
468 else if (*next >= 0 && *next < class_devno_max())
473 spin_lock(&obd_dev_lock);
474 for (; i < class_devno_max(); i++) {
475 struct obd_device *obd = class_num2obd(i);
478 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
481 spin_unlock(&obd_dev_lock);
485 spin_unlock(&obd_dev_lock);
491 void obd_cleanup_caches(void)
496 if (obd_device_cachep) {
497 rc = cfs_mem_cache_destroy(obd_device_cachep);
498 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
499 obd_device_cachep = NULL;
502 rc = cfs_mem_cache_destroy(obdo_cachep);
503 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
507 rc = cfs_mem_cache_destroy(import_cachep);
508 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
509 import_cachep = NULL;
514 int obd_init_caches(void)
518 LASSERT(obd_device_cachep == NULL);
519 obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
520 sizeof(struct obd_device), 0, 0);
521 if (!obd_device_cachep)
524 LASSERT(obdo_cachep == NULL);
525 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
530 LASSERT(import_cachep == NULL);
531 import_cachep = cfs_mem_cache_create("ll_import_cache",
532 sizeof(struct obd_import),
539 obd_cleanup_caches();
544 /* map connection to client */
545 struct obd_export *class_conn2export(struct lustre_handle *conn)
547 struct obd_export *export;
551 CDEBUG(D_CACHE, "looking for null handle\n");
555 if (conn->cookie == -1) { /* this means assign a new connection */
556 CDEBUG(D_CACHE, "want a new connection\n");
560 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
561 export = class_handle2object(conn->cookie);
565 struct obd_device *class_exp2obd(struct obd_export *exp)
572 struct obd_device *class_conn2obd(struct lustre_handle *conn)
574 struct obd_export *export;
575 export = class_conn2export(conn);
577 struct obd_device *obd = export->exp_obd;
578 class_export_put(export);
584 struct obd_import *class_exp2cliimp(struct obd_export *exp)
586 struct obd_device *obd = exp->exp_obd;
589 return obd->u.cli.cl_import;
592 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
594 struct obd_device *obd = class_conn2obd(conn);
597 return obd->u.cli.cl_import;
600 /* Export management functions */
601 static void export_handle_addref(void *export)
603 class_export_get(export);
606 void __class_export_put(struct obd_export *exp)
608 if (atomic_dec_and_test(&exp->exp_refcount)) {
609 LASSERT (list_empty(&exp->exp_obd_chain));
611 CDEBUG(D_IOCTL, "final put %p/%s\n",
612 exp, exp->exp_client_uuid.uuid);
614 spin_lock(&obd_zombie_impexp_lock);
615 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
616 spin_unlock(&obd_zombie_impexp_lock);
618 if (obd_zombie_impexp_notify != NULL)
619 obd_zombie_impexp_notify();
622 EXPORT_SYMBOL(__class_export_put);
624 void class_export_destroy(struct obd_export *exp)
626 struct obd_device *obd = exp->exp_obd;
628 LASSERT (atomic_read(&exp->exp_refcount) == 0);
630 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
631 exp->exp_client_uuid.uuid);
633 LASSERT(obd != NULL);
635 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
636 if (exp->exp_connection)
637 ptlrpc_put_connection_superhack(exp->exp_connection);
639 LASSERT(list_empty(&exp->exp_outstanding_replies));
640 LASSERT(list_empty(&exp->exp_handle.h_link));
641 obd_destroy_export(exp);
643 OBD_FREE(exp, sizeof(*exp));
647 /* Creates a new export, adds it to the hash table, and returns a
648 * pointer to it. The refcount is 2: one for the hash reference, and
649 * one for the pointer returned by this function. */
650 struct obd_export *class_new_export(struct obd_device *obd,
651 struct obd_uuid *cluuid)
653 struct obd_export *export, *tmp;
655 OBD_ALLOC(export, sizeof(*export));
657 return ERR_PTR(-ENOMEM);
659 export->exp_conn_cnt = 0;
660 atomic_set(&export->exp_refcount, 2);
661 atomic_set(&export->exp_rpc_count, 0);
662 export->exp_obd = obd;
663 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
664 /* XXX this should be in LDLM init */
665 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
666 spin_lock_init(&export->exp_ldlm_data.led_lock);
668 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
669 class_handle_hash(&export->exp_handle, export_handle_addref);
670 export->exp_last_request_time = CURRENT_SECONDS;
671 spin_lock_init(&export->exp_lock);
673 export->exp_client_uuid = *cluuid;
674 obd_init_export(export);
676 spin_lock(&obd->obd_dev_lock);
677 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
678 list_for_each_entry(tmp, &obd->obd_exports, exp_obd_chain) {
679 if (obd_uuid_equals(cluuid, &tmp->exp_client_uuid)) {
680 spin_unlock(&obd->obd_dev_lock);
681 CWARN("%s: denying duplicate export for %s\n",
682 obd->obd_name, cluuid->uuid);
683 class_handle_unhash(&export->exp_handle);
684 OBD_FREE_PTR(export);
685 return ERR_PTR(-EALREADY);
689 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
691 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
692 list_add_tail(&export->exp_obd_chain_timed,
693 &export->exp_obd->obd_exports_timed);
694 export->exp_obd->obd_num_exports++;
695 spin_unlock(&obd->obd_dev_lock);
699 EXPORT_SYMBOL(class_new_export);
701 void class_unlink_export(struct obd_export *exp)
703 class_handle_unhash(&exp->exp_handle);
705 spin_lock(&exp->exp_obd->obd_dev_lock);
706 list_del_init(&exp->exp_obd_chain);
707 list_del_init(&exp->exp_obd_chain_timed);
708 exp->exp_obd->obd_num_exports--;
709 spin_unlock(&exp->exp_obd->obd_dev_lock);
711 class_export_put(exp);
713 EXPORT_SYMBOL(class_unlink_export);
715 /* Import management functions */
716 static void import_handle_addref(void *import)
718 class_import_get(import);
721 struct obd_import *class_import_get(struct obd_import *import)
723 LASSERT(atomic_read(&import->imp_refcount) >= 0);
724 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
725 atomic_inc(&import->imp_refcount);
726 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
727 atomic_read(&import->imp_refcount));
730 EXPORT_SYMBOL(class_import_get);
732 void class_import_put(struct obd_import *import)
736 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
737 atomic_read(&import->imp_refcount) - 1);
739 LASSERT(atomic_read(&import->imp_refcount) > 0);
740 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
741 LASSERT(list_empty(&import->imp_zombie_chain));
743 if (atomic_dec_and_test(&import->imp_refcount)) {
745 CDEBUG(D_INFO, "final put import %p\n", import);
747 spin_lock(&obd_zombie_impexp_lock);
748 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
749 spin_unlock(&obd_zombie_impexp_lock);
751 if (obd_zombie_impexp_notify != NULL)
752 obd_zombie_impexp_notify();
758 void class_import_destroy(struct obd_import *import)
762 CDEBUG(D_IOCTL, "destroying import %p\n", import);
764 LASSERT(atomic_read(&import->imp_refcount) == 0);
766 ptlrpc_put_connection_superhack(import->imp_connection);
768 while (!list_empty(&import->imp_conn_list)) {
769 struct obd_import_conn *imp_conn;
771 imp_conn = list_entry(import->imp_conn_list.next,
772 struct obd_import_conn, oic_item);
773 list_del(&imp_conn->oic_item);
774 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
775 OBD_FREE(imp_conn, sizeof(*imp_conn));
778 LASSERT(list_empty(&import->imp_handle.h_link));
779 class_decref(import->imp_obd);
780 OBD_FREE(import, sizeof(*import));
784 EXPORT_SYMBOL(class_import_put);
786 struct obd_import *class_new_import(struct obd_device *obd)
788 struct obd_import *imp;
790 OBD_ALLOC(imp, sizeof(*imp));
794 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
795 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
796 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
797 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
798 spin_lock_init(&imp->imp_lock);
799 imp->imp_last_success_conn = 0;
800 imp->imp_state = LUSTRE_IMP_NEW;
801 imp->imp_obd = class_incref(obd);
802 cfs_waitq_init(&imp->imp_recovery_waitq);
804 atomic_set(&imp->imp_refcount, 2);
805 atomic_set(&imp->imp_inflight, 0);
806 atomic_set(&imp->imp_replay_inflight, 0);
807 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
808 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
809 class_handle_hash(&imp->imp_handle, import_handle_addref);
811 /* the default magic is V1, will be used in connect RPC, and
812 * then adjusted according to the flags in request/reply. */
813 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
817 EXPORT_SYMBOL(class_new_import);
819 void class_destroy_import(struct obd_import *import)
821 LASSERT(import != NULL);
822 LASSERT(import != LP_POISON);
824 class_handle_unhash(&import->imp_handle);
826 import->imp_generation++;
827 class_import_put(import);
829 EXPORT_SYMBOL(class_destroy_import);
831 /* A connection defines an export context in which preallocation can
832 be managed. This releases the export pointer reference, and returns
833 the export handle, so the export refcount is 1 when this function
835 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
836 struct obd_uuid *cluuid)
838 struct obd_export *export;
839 LASSERT(conn != NULL);
840 LASSERT(obd != NULL);
841 LASSERT(cluuid != NULL);
844 export = class_new_export(obd, cluuid);
846 RETURN(PTR_ERR(export));
848 conn->cookie = export->exp_handle.h_cookie;
849 class_export_put(export);
851 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
852 cluuid->uuid, conn->cookie);
855 EXPORT_SYMBOL(class_connect);
857 /* This function removes two references from the export: one for the
858 * hash entry and one for the export pointer passed in. The export
859 * pointer passed to this function is destroyed should not be used
861 int class_disconnect(struct obd_export *export)
863 int already_disconnected;
866 if (export == NULL) {
868 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
872 spin_lock(&export->exp_lock);
873 already_disconnected = export->exp_disconnected;
874 export->exp_disconnected = 1;
875 spin_unlock(&export->exp_lock);
877 /* class_cleanup(), abort_recovery(), and class_fail_export()
878 * all end up in here, and if any of them race we shouldn't
879 * call extra class_export_puts(). */
880 if (already_disconnected)
883 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
884 export->exp_handle.h_cookie);
886 class_unlink_export(export);
887 class_export_put(export);
891 static void class_disconnect_export_list(struct list_head *list, int flags)
894 struct lustre_handle fake_conn;
895 struct obd_export *fake_exp, *exp;
898 /* It's possible that an export may disconnect itself, but
899 * nothing else will be added to this list. */
900 while (!list_empty(list)) {
901 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
902 class_export_get(exp);
903 exp->exp_flags = flags;
905 if (obd_uuid_equals(&exp->exp_client_uuid,
906 &exp->exp_obd->obd_uuid)) {
908 "exp %p export uuid == obd uuid, don't discon\n",
910 /* Need to delete this now so we don't end up pointing
911 * to work_list later when this export is cleaned up. */
912 list_del_init(&exp->exp_obd_chain);
913 class_export_put(exp);
917 fake_conn.cookie = exp->exp_handle.h_cookie;
918 fake_exp = class_conn2export(&fake_conn);
920 class_export_put(exp);
923 fake_exp->exp_flags = flags;
924 rc = obd_disconnect(fake_exp);
925 class_export_put(exp);
927 CDEBUG(D_HA, "disconnecting export %p failed: %d\n",
930 CDEBUG(D_HA, "export %p disconnected\n", exp);
936 static inline int get_exp_flags_from_obd(struct obd_device *obd)
938 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
939 (obd->obd_force ? OBD_OPT_FORCE : 0));
942 void class_disconnect_exports(struct obd_device *obd)
944 struct list_head work_list;
947 /* Move all of the exports from obd_exports to a work list, en masse. */
948 spin_lock(&obd->obd_dev_lock);
949 list_add(&work_list, &obd->obd_exports);
950 list_del_init(&obd->obd_exports);
951 spin_unlock(&obd->obd_dev_lock);
953 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
954 "disconnecting them\n", obd->obd_minor, obd);
955 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
958 EXPORT_SYMBOL(class_disconnect_exports);
960 /* Remove exports that have not completed recovery.
962 void class_disconnect_stale_exports(struct obd_device *obd)
964 struct list_head work_list;
965 struct list_head *pos, *n;
966 struct obd_export *exp;
970 CFS_INIT_LIST_HEAD(&work_list);
971 spin_lock(&obd->obd_dev_lock);
972 list_for_each_safe(pos, n, &obd->obd_exports) {
973 exp = list_entry(pos, struct obd_export, exp_obd_chain);
974 if (exp->exp_replay_needed) {
975 list_del(&exp->exp_obd_chain);
976 list_add(&exp->exp_obd_chain, &work_list);
980 spin_unlock(&obd->obd_dev_lock);
982 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
984 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
987 EXPORT_SYMBOL(class_disconnect_stale_exports);
989 int oig_init(struct obd_io_group **oig_out)
991 struct obd_io_group *oig;
994 OBD_ALLOC(oig, sizeof(*oig));
998 spin_lock_init(&oig->oig_lock);
1000 oig->oig_pending = 0;
1001 atomic_set(&oig->oig_refcount, 1);
1002 cfs_waitq_init(&oig->oig_waitq);
1003 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1008 EXPORT_SYMBOL(oig_init);
1010 static inline void oig_grab(struct obd_io_group *oig)
1012 atomic_inc(&oig->oig_refcount);
1015 void oig_release(struct obd_io_group *oig)
1017 if (atomic_dec_and_test(&oig->oig_refcount))
1018 OBD_FREE(oig, sizeof(*oig));
1020 EXPORT_SYMBOL(oig_release);
1022 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1025 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1026 spin_lock(&oig->oig_lock);
1032 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1034 spin_unlock(&oig->oig_lock);
1039 EXPORT_SYMBOL(oig_add_one);
1041 void oig_complete_one(struct obd_io_group *oig,
1042 struct oig_callback_context *occ, int rc)
1044 cfs_waitq_t *wake = NULL;
1047 spin_lock(&oig->oig_lock);
1050 list_del_init(&occ->occ_oig_item);
1052 old_rc = oig->oig_rc;
1053 if (oig->oig_rc == 0 && rc != 0)
1056 if (--oig->oig_pending <= 0)
1057 wake = &oig->oig_waitq;
1059 spin_unlock(&oig->oig_lock);
1061 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1062 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1065 cfs_waitq_signal(wake);
1068 EXPORT_SYMBOL(oig_complete_one);
1070 static int oig_done(struct obd_io_group *oig)
1073 spin_lock(&oig->oig_lock);
1074 if (oig->oig_pending <= 0)
1076 spin_unlock(&oig->oig_lock);
1080 static void interrupted_oig(void *data)
1082 struct obd_io_group *oig = data;
1083 struct oig_callback_context *occ;
1085 spin_lock(&oig->oig_lock);
1086 /* We need to restart the processing each time we drop the lock, as
1087 * it is possible other threads called oig_complete_one() to remove
1088 * an entry elsewhere in the list while we dropped lock. We need to
1089 * drop the lock because osc_ap_completion() calls oig_complete_one()
1090 * which re-gets this lock ;-) as well as a lock ordering issue. */
1092 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1093 if (occ->interrupted)
1095 occ->interrupted = 1;
1096 spin_unlock(&oig->oig_lock);
1097 occ->occ_interrupted(occ);
1098 spin_lock(&oig->oig_lock);
1101 spin_unlock(&oig->oig_lock);
1104 int oig_wait(struct obd_io_group *oig)
1106 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1109 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1112 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1113 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1114 /* we can't continue until the oig has emptied and stopped
1115 * referencing state that the caller will free upon return */
1117 lwi = (struct l_wait_info){ 0, };
1118 } while (rc == -EINTR);
1120 LASSERTF(oig->oig_pending == 0,
1121 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1124 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1127 EXPORT_SYMBOL(oig_wait);
1129 void class_fail_export(struct obd_export *exp)
1131 int rc, already_failed;
1133 spin_lock(&exp->exp_lock);
1134 already_failed = exp->exp_failed;
1135 exp->exp_failed = 1;
1136 spin_unlock(&exp->exp_lock);
1138 if (already_failed) {
1139 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1140 exp, exp->exp_client_uuid.uuid);
1144 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1145 exp, exp->exp_client_uuid.uuid);
1147 if (obd_dump_on_timeout)
1148 libcfs_debug_dumplog();
1150 /* Most callers into obd_disconnect are removing their own reference
1151 * (request, for example) in addition to the one from the hash table.
1152 * We don't have such a reference here, so make one. */
1153 class_export_get(exp);
1154 rc = obd_disconnect(exp);
1156 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1158 CDEBUG(D_HA, "disconnected export %p/%s\n",
1159 exp, exp->exp_client_uuid.uuid);
1161 EXPORT_SYMBOL(class_fail_export);
1163 char *obd_export_nid2str(struct obd_export *exp)
1165 if (exp->exp_connection != NULL)
1166 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1170 EXPORT_SYMBOL(obd_export_nid2str);
1172 #define EVICT_BATCH 32
1173 int obd_export_evict_by_nid(struct obd_device *obd, char *nid)
1175 struct obd_export *doomed_exp[EVICT_BATCH] = { NULL };
1176 struct list_head *p;
1177 int exports_evicted = 0, num_to_evict = 0, i;
1180 spin_lock(&obd->obd_dev_lock);
1181 list_for_each(p, &obd->obd_exports) {
1182 doomed_exp[num_to_evict] = list_entry(p, struct obd_export,
1184 if (strcmp(obd_export_nid2str(doomed_exp[num_to_evict]),
1186 class_export_get(doomed_exp[num_to_evict]);
1187 if (++num_to_evict == EVICT_BATCH)
1191 spin_unlock(&obd->obd_dev_lock);
1193 for (i = 0; i < num_to_evict; i++) {
1195 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1196 obd->obd_name, nid, doomed_exp[i]->exp_client_uuid.uuid,
1198 class_fail_export(doomed_exp[i]);
1199 class_export_put(doomed_exp[i]);
1201 if (num_to_evict == EVICT_BATCH) {
1206 if (!exports_evicted)
1207 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1208 obd->obd_name, nid);
1209 return exports_evicted;
1211 EXPORT_SYMBOL(obd_export_evict_by_nid);
1213 int obd_export_evict_by_uuid(struct obd_device *obd, char *uuid)
1215 struct obd_export *doomed_exp = NULL;
1216 struct list_head *p;
1217 struct obd_uuid doomed;
1218 int exports_evicted = 0;
1220 obd_str2uuid(&doomed, uuid);
1222 spin_lock(&obd->obd_dev_lock);
1223 list_for_each(p, &obd->obd_exports) {
1224 doomed_exp = list_entry(p, struct obd_export, exp_obd_chain);
1226 if (obd_uuid_equals(&doomed, &doomed_exp->exp_client_uuid)) {
1227 class_export_get(doomed_exp);
1232 spin_unlock(&obd->obd_dev_lock);
1234 if (doomed_exp == NULL) {
1235 CERROR("%s: can't disconnect %s: no exports found\n",
1236 obd->obd_name, uuid);
1238 CWARN("%s: evicting %s at adminstrative request\n",
1239 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1240 class_fail_export(doomed_exp);
1241 class_export_put(doomed_exp);
1245 return exports_evicted;
1247 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1249 void obd_zombie_impexp_cull(void)
1251 struct obd_import *import;
1252 struct obd_export *export;
1255 spin_lock (&obd_zombie_impexp_lock);
1258 if (!list_empty(&obd_zombie_imports)) {
1259 import = list_entry(obd_zombie_imports.next,
1262 list_del(&import->imp_zombie_chain);
1266 if (!list_empty(&obd_zombie_exports)) {
1267 export = list_entry(obd_zombie_exports.next,
1270 list_del_init(&export->exp_obd_chain);
1273 spin_unlock(&obd_zombie_impexp_lock);
1276 class_import_destroy(import);
1279 class_export_destroy(export);
1281 } while (import != NULL || export != NULL);
1283 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1285 void obd_zombie_impexp_init(void)
1287 INIT_LIST_HEAD(&obd_zombie_imports);
1288 INIT_LIST_HEAD(&obd_zombie_exports);
1289 spin_lock_init(&obd_zombie_impexp_lock);