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>
35 #include <class_hash.h>
37 extern struct list_head obd_types;
38 spinlock_t obd_types_lock;
40 cfs_mem_cache_t *obd_device_cachep;
41 cfs_mem_cache_t *obdo_cachep;
42 EXPORT_SYMBOL(obdo_cachep);
43 cfs_mem_cache_t *import_cachep;
45 struct list_head obd_zombie_imports;
46 struct list_head obd_zombie_exports;
47 spinlock_t obd_zombie_impexp_lock;
48 void (*obd_zombie_impexp_notify)(void) = NULL;
49 EXPORT_SYMBOL(obd_zombie_impexp_notify);
52 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
55 * support functions: we could use inter-module communication, but this
56 * is more portable to other OS's
58 static struct obd_device *obd_device_alloc(void)
60 struct obd_device *obd;
62 OBD_SLAB_ALLOC_PTR(obd, obd_device_cachep);
64 obd->obd_magic = OBD_DEVICE_MAGIC;
68 EXPORT_SYMBOL(obd_device_alloc);
70 static void obd_device_free(struct obd_device *obd)
73 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "obd %p obd_magic %08x != %08x\n",
74 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
75 if (obd->obd_namespace != NULL) {
76 CERROR("obd %p: namespace %p was not properly cleaned up (obd_force=%d)!\n",
77 obd, obd->obd_namespace, obd->obd_force);
80 OBD_SLAB_FREE_PTR(obd, obd_device_cachep);
82 EXPORT_SYMBOL(obd_device_free);
84 struct obd_type *class_search_type(const char *name)
86 struct list_head *tmp;
87 struct obd_type *type;
89 spin_lock(&obd_types_lock);
90 list_for_each(tmp, &obd_types) {
91 type = list_entry(tmp, struct obd_type, typ_chain);
92 if (strcmp(type->typ_name, name) == 0) {
93 spin_unlock(&obd_types_lock);
97 spin_unlock(&obd_types_lock);
101 struct obd_type *class_get_type(const char *name)
103 struct obd_type *type = class_search_type(name);
107 const char *modname = name;
108 if (!request_module(modname)) {
109 CDEBUG(D_INFO, "Loaded module '%s'\n", modname);
110 type = class_search_type(name);
112 LCONSOLE_ERROR_MSG(0x158, "Can't load module '%s'\n",
118 spin_lock(&type->obd_type_lock);
120 try_module_get(type->typ_dt_ops->o_owner);
121 spin_unlock(&type->obd_type_lock);
126 void class_put_type(struct obd_type *type)
129 spin_lock(&type->obd_type_lock);
131 module_put(type->typ_dt_ops->o_owner);
132 spin_unlock(&type->obd_type_lock);
135 #define CLASS_MAX_NAME 1024
137 int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops,
138 struct lprocfs_vars *vars, const char *name,
139 struct lu_device_type *ldt)
141 struct obd_type *type;
146 LASSERT(strnlen(name, CLASS_MAX_NAME) < CLASS_MAX_NAME);
148 if (class_search_type(name)) {
149 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
154 OBD_ALLOC(type, sizeof(*type));
158 OBD_ALLOC_PTR(type->typ_dt_ops);
159 OBD_ALLOC_PTR(type->typ_md_ops);
160 OBD_ALLOC(type->typ_name, strlen(name) + 1);
162 if (type->typ_dt_ops == NULL ||
163 type->typ_md_ops == NULL ||
164 type->typ_name == NULL)
167 *(type->typ_dt_ops) = *dt_ops;
168 /* md_ops is optional */
170 *(type->typ_md_ops) = *md_ops;
171 strcpy(type->typ_name, name);
172 spin_lock_init(&type->obd_type_lock);
175 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
177 if (IS_ERR(type->typ_procroot)) {
178 rc = PTR_ERR(type->typ_procroot);
179 type->typ_procroot = NULL;
185 rc = ldt->ldt_ops->ldto_init(ldt);
190 spin_lock(&obd_types_lock);
191 list_add(&type->typ_chain, &obd_types);
192 spin_unlock(&obd_types_lock);
197 if (type->typ_name != NULL)
198 OBD_FREE(type->typ_name, strlen(name) + 1);
199 if (type->typ_md_ops != NULL)
200 OBD_FREE_PTR(type->typ_md_ops);
201 if (type->typ_dt_ops != NULL)
202 OBD_FREE_PTR(type->typ_dt_ops);
203 OBD_FREE(type, sizeof(*type));
207 int class_unregister_type(const char *name)
209 struct obd_type *type = class_search_type(name);
213 CERROR("unknown obd type\n");
217 if (type->typ_refcnt) {
218 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
219 /* This is a bad situation, let's make the best of it */
220 /* Remove ops, but leave the name for debugging */
221 OBD_FREE_PTR(type->typ_dt_ops);
222 OBD_FREE_PTR(type->typ_md_ops);
226 if (type->typ_procroot) {
227 lprocfs_remove(&type->typ_procroot);
231 type->typ_lu->ldt_ops->ldto_fini(type->typ_lu);
233 spin_lock(&obd_types_lock);
234 list_del(&type->typ_chain);
235 spin_unlock(&obd_types_lock);
236 OBD_FREE(type->typ_name, strlen(name) + 1);
237 if (type->typ_dt_ops != NULL)
238 OBD_FREE_PTR(type->typ_dt_ops);
239 if (type->typ_md_ops != NULL)
240 OBD_FREE_PTR(type->typ_md_ops);
241 OBD_FREE(type, sizeof(*type));
243 } /* class_unregister_type */
245 struct obd_device *class_newdev(const char *type_name, const char *name)
247 struct obd_device *result = NULL;
248 struct obd_device *newdev;
249 struct obd_type *type = NULL;
251 int new_obd_minor = 0;
253 if (strlen(name) > MAX_OBD_NAME) {
254 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
255 RETURN(ERR_PTR(-EINVAL));
258 type = class_get_type(type_name);
260 CERROR("OBD: unknown type: %s\n", type_name);
261 RETURN(ERR_PTR(-ENODEV));
264 newdev = obd_device_alloc();
265 if (newdev == NULL) {
266 class_put_type(type);
267 RETURN(ERR_PTR(-ENOMEM));
269 LASSERT(newdev->obd_magic == OBD_DEVICE_MAGIC);
271 spin_lock(&obd_dev_lock);
272 for (i = 0; i < class_devno_max(); i++) {
273 struct obd_device *obd = class_num2obd(i);
274 if (obd && obd->obd_name &&
275 (strcmp(name, obd->obd_name) == 0)) {
276 CERROR("Device %s already exists, won't add\n", name);
278 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
279 "%p obd_magic %08x != %08x\n", result,
280 result->obd_magic, OBD_DEVICE_MAGIC);
281 LASSERTF(result->obd_minor == new_obd_minor,
282 "%p obd_minor %d != %d\n", result,
283 result->obd_minor, new_obd_minor);
285 obd_devs[result->obd_minor] = NULL;
286 result->obd_name[0]='\0';
288 result = ERR_PTR(-EEXIST);
291 if (!result && !obd) {
293 result->obd_minor = i;
295 result->obd_type = type;
296 memcpy(result->obd_name, name, strlen(name));
297 obd_devs[i] = result;
300 spin_unlock(&obd_dev_lock);
302 if (result == NULL && i >= class_devno_max()) {
303 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
305 result = ERR_PTR(-EOVERFLOW);
308 if (IS_ERR(result)) {
309 obd_device_free(newdev);
310 class_put_type(type);
312 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
313 result->obd_name, result);
318 void class_release_dev(struct obd_device *obd)
320 struct obd_type *obd_type = obd->obd_type;
322 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
323 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
324 LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
325 obd, obd->obd_minor, obd_devs[obd->obd_minor]);
326 LASSERT(obd_type != NULL);
328 CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
329 obd->obd_name,obd->obd_type->typ_name);
331 spin_lock(&obd_dev_lock);
332 obd_devs[obd->obd_minor] = NULL;
333 spin_unlock(&obd_dev_lock);
334 obd_device_free(obd);
336 class_put_type(obd_type);
339 int class_name2dev(const char *name)
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->obd_name && strcmp(name, obd->obd_name) == 0) {
350 /* Make sure we finished attaching before we give
351 out any references */
352 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
353 if (obd->obd_attached) {
354 spin_unlock(&obd_dev_lock);
360 spin_unlock(&obd_dev_lock);
365 struct obd_device *class_name2obd(const char *name)
367 int dev = class_name2dev(name);
369 if (dev < 0 || dev > class_devno_max())
371 return class_num2obd(dev);
374 int class_uuid2dev(struct obd_uuid *uuid)
378 spin_lock(&obd_dev_lock);
379 for (i = 0; i < class_devno_max(); i++) {
380 struct obd_device *obd = class_num2obd(i);
381 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
382 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
383 spin_unlock(&obd_dev_lock);
387 spin_unlock(&obd_dev_lock);
392 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
394 int dev = class_uuid2dev(uuid);
397 return class_num2obd(dev);
400 struct obd_device *class_num2obd(int num)
402 struct obd_device *obd = NULL;
404 if (num < class_devno_max()) {
410 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
411 "%p obd_magic %08x != %08x\n",
412 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
413 LASSERTF(obd->obd_minor == num,
414 "%p obd_minor %0d != %0d\n",
415 obd, obd->obd_minor, num);
421 void class_obd_list(void)
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 (obd->obd_stopping)
433 else if (obd->obd_set_up)
435 else if (obd->obd_attached)
439 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
440 i, status, obd->obd_type->typ_name,
441 obd->obd_name, obd->obd_uuid.uuid,
442 atomic_read(&obd->obd_refcount));
444 spin_unlock(&obd_dev_lock);
448 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
449 specified, then only the client with that uuid is returned,
450 otherwise any client connected to the tgt is returned. */
451 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
452 const char * typ_name,
453 struct obd_uuid *grp_uuid)
457 spin_lock(&obd_dev_lock);
458 for (i = 0; i < class_devno_max(); i++) {
459 struct obd_device *obd = class_num2obd(i);
462 if ((strncmp(obd->obd_type->typ_name, typ_name,
463 strlen(typ_name)) == 0)) {
464 if (obd_uuid_equals(tgt_uuid,
465 &obd->u.cli.cl_target_uuid) &&
466 ((grp_uuid)? obd_uuid_equals(grp_uuid,
467 &obd->obd_uuid) : 1)) {
468 spin_unlock(&obd_dev_lock);
473 spin_unlock(&obd_dev_lock);
478 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
479 struct obd_uuid *grp_uuid)
481 struct obd_device *obd;
483 obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
485 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
490 /* Iterate the obd_device list looking devices have grp_uuid. Start
491 searching at *next, and if a device is found, the next index to look
492 at is saved in *next. If next is NULL, then the first matching device
493 will always be returned. */
494 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
500 else if (*next >= 0 && *next < class_devno_max())
505 spin_lock(&obd_dev_lock);
506 for (; i < class_devno_max(); i++) {
507 struct obd_device *obd = class_num2obd(i);
510 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
513 spin_unlock(&obd_dev_lock);
517 spin_unlock(&obd_dev_lock);
523 void obd_cleanup_caches(void)
528 if (obd_device_cachep) {
529 rc = cfs_mem_cache_destroy(obd_device_cachep);
530 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
531 obd_device_cachep = NULL;
534 rc = cfs_mem_cache_destroy(obdo_cachep);
535 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
539 rc = cfs_mem_cache_destroy(import_cachep);
540 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
541 import_cachep = NULL;
544 rc = cfs_mem_cache_destroy(capa_cachep);
545 LASSERTF(rc == 0, "Cannot destory capa_cache\n");
551 int obd_init_caches(void)
555 LASSERT(obd_device_cachep == NULL);
556 obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
557 sizeof(struct obd_device),
559 if (!obd_device_cachep)
562 LASSERT(obdo_cachep == NULL);
563 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
568 LASSERT(import_cachep == NULL);
569 import_cachep = cfs_mem_cache_create("ll_import_cache",
570 sizeof(struct obd_import),
575 LASSERT(capa_cachep == NULL);
576 capa_cachep = cfs_mem_cache_create("capa_cache",
577 sizeof(struct obd_capa), 0, 0);
583 obd_cleanup_caches();
588 /* map connection to client */
589 struct obd_export *class_conn2export(struct lustre_handle *conn)
591 struct obd_export *export;
595 CDEBUG(D_CACHE, "looking for null handle\n");
599 if (conn->cookie == -1) { /* this means assign a new connection */
600 CDEBUG(D_CACHE, "want a new connection\n");
604 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
605 export = class_handle2object(conn->cookie);
609 struct obd_device *class_exp2obd(struct obd_export *exp)
616 struct obd_device *class_conn2obd(struct lustre_handle *conn)
618 struct obd_export *export;
619 export = class_conn2export(conn);
621 struct obd_device *obd = export->exp_obd;
622 class_export_put(export);
628 struct obd_import *class_exp2cliimp(struct obd_export *exp)
630 struct obd_device *obd = exp->exp_obd;
633 return obd->u.cli.cl_import;
636 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
638 struct obd_device *obd = class_conn2obd(conn);
641 return obd->u.cli.cl_import;
644 /* Export management functions */
645 static void export_handle_addref(void *export)
647 class_export_get(export);
650 void __class_export_put(struct obd_export *exp)
652 if (atomic_dec_and_test(&exp->exp_refcount)) {
653 LASSERT (list_empty(&exp->exp_obd_chain));
655 CDEBUG(D_IOCTL, "final put %p/%s\n",
656 exp, exp->exp_client_uuid.uuid);
658 spin_lock(&obd_zombie_impexp_lock);
659 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
660 spin_unlock(&obd_zombie_impexp_lock);
662 if (obd_zombie_impexp_notify != NULL)
663 obd_zombie_impexp_notify();
666 EXPORT_SYMBOL(__class_export_put);
668 void class_export_destroy(struct obd_export *exp)
670 struct obd_device *obd = exp->exp_obd;
673 LASSERT (atomic_read(&exp->exp_refcount) == 0);
675 CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
676 exp->exp_client_uuid.uuid, obd->obd_name);
678 LASSERT(obd != NULL);
680 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
681 if (exp->exp_connection)
682 ptlrpc_put_connection_superhack(exp->exp_connection);
684 LASSERT(list_empty(&exp->exp_outstanding_replies));
685 obd_destroy_export(exp);
687 OBD_FREE_RCU(exp, sizeof(*exp), &exp->exp_handle);
692 /* Creates a new export, adds it to the hash table, and returns a
693 * pointer to it. The refcount is 2: one for the hash reference, and
694 * one for the pointer returned by this function. */
695 struct obd_export *class_new_export(struct obd_device *obd,
696 struct obd_uuid *cluuid)
698 struct obd_export *export;
701 OBD_ALLOC_PTR(export);
703 return ERR_PTR(-ENOMEM);
705 export->exp_conn_cnt = 0;
706 atomic_set(&export->exp_refcount, 2);
707 atomic_set(&export->exp_rpc_count, 0);
708 export->exp_obd = obd;
709 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
710 /* XXX this should be in LDLM init */
711 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
712 spin_lock_init(&export->exp_ldlm_data.led_lock);
714 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
715 class_handle_hash(&export->exp_handle, export_handle_addref);
716 export->exp_last_request_time = cfs_time_current_sec();
717 spin_lock_init(&export->exp_lock);
718 INIT_HLIST_NODE(&export->exp_uuid_hash);
719 INIT_HLIST_NODE(&export->exp_nid_hash);
721 export->exp_sp_peer = LUSTRE_SP_ANY;
722 export->exp_flvr.sf_rpc = SPTLRPC_FLVR_INVALID;
723 export->exp_client_uuid = *cluuid;
724 obd_init_export(export);
726 spin_lock(&obd->obd_dev_lock);
727 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
728 rc = lustre_hash_additem_unique(obd->obd_uuid_hash_body, cluuid,
729 &export->exp_uuid_hash);
731 CWARN("%s: denying duplicate export for %s\n",
732 obd->obd_name, cluuid->uuid);
733 spin_unlock(&obd->obd_dev_lock);
734 class_handle_unhash(&export->exp_handle);
735 OBD_FREE_PTR(export);
736 return ERR_PTR(-EALREADY);
740 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
742 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
743 list_add_tail(&export->exp_obd_chain_timed,
744 &export->exp_obd->obd_exports_timed);
745 export->exp_obd->obd_num_exports++;
746 spin_unlock(&obd->obd_dev_lock);
750 EXPORT_SYMBOL(class_new_export);
752 void class_unlink_export(struct obd_export *exp)
754 class_handle_unhash(&exp->exp_handle);
756 spin_lock(&exp->exp_obd->obd_dev_lock);
757 /* delete an uuid-export hashitem from hashtables */
758 if (!hlist_unhashed(&exp->exp_uuid_hash)) {
759 lustre_hash_delitem(exp->exp_obd->obd_uuid_hash_body,
760 &exp->exp_client_uuid, &exp->exp_uuid_hash);
762 list_del_init(&exp->exp_obd_chain);
763 list_del_init(&exp->exp_obd_chain_timed);
764 exp->exp_obd->obd_num_exports--;
765 spin_unlock(&exp->exp_obd->obd_dev_lock);
767 class_export_put(exp);
769 EXPORT_SYMBOL(class_unlink_export);
771 /* Import management functions */
772 static void import_handle_addref(void *import)
774 class_import_get(import);
777 struct obd_import *class_import_get(struct obd_import *import)
779 LASSERT(atomic_read(&import->imp_refcount) >= 0);
780 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
781 atomic_inc(&import->imp_refcount);
782 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
783 atomic_read(&import->imp_refcount));
786 EXPORT_SYMBOL(class_import_get);
788 void class_import_put(struct obd_import *import)
792 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
793 atomic_read(&import->imp_refcount) - 1);
795 LASSERT(atomic_read(&import->imp_refcount) > 0);
796 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
797 LASSERT(list_empty(&import->imp_zombie_chain));
799 if (atomic_dec_and_test(&import->imp_refcount)) {
801 CDEBUG(D_INFO, "final put import %p\n", import);
803 spin_lock(&obd_zombie_impexp_lock);
804 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
805 spin_unlock(&obd_zombie_impexp_lock);
807 if (obd_zombie_impexp_notify != NULL)
808 obd_zombie_impexp_notify();
814 void class_import_destroy(struct obd_import *import)
818 CDEBUG(D_IOCTL, "destroying import %p for %s\n", import,
819 import->imp_obd->obd_name);
821 LASSERT(atomic_read(&import->imp_refcount) == 0);
823 ptlrpc_put_connection_superhack(import->imp_connection);
825 while (!list_empty(&import->imp_conn_list)) {
826 struct obd_import_conn *imp_conn;
828 imp_conn = list_entry(import->imp_conn_list.next,
829 struct obd_import_conn, oic_item);
830 list_del(&imp_conn->oic_item);
831 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
832 OBD_FREE(imp_conn, sizeof(*imp_conn));
835 LASSERT(import->imp_sec == NULL);
836 class_decref(import->imp_obd);
837 OBD_FREE_RCU(import, sizeof(*import), &import->imp_handle);
840 EXPORT_SYMBOL(class_import_put);
842 struct obd_import *class_new_import(struct obd_device *obd)
844 struct obd_import *imp;
846 OBD_ALLOC(imp, sizeof(*imp));
850 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
851 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
852 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
853 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
854 spin_lock_init(&imp->imp_lock);
855 imp->imp_last_success_conn = 0;
856 imp->imp_state = LUSTRE_IMP_NEW;
857 imp->imp_obd = class_incref(obd);
858 sema_init(&imp->imp_sec_mutex, 1);
859 cfs_waitq_init(&imp->imp_recovery_waitq);
861 atomic_set(&imp->imp_refcount, 2);
862 atomic_set(&imp->imp_inflight, 0);
863 atomic_set(&imp->imp_replay_inflight, 0);
864 atomic_set(&imp->imp_inval_count, 0);
865 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
866 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
867 class_handle_hash(&imp->imp_handle, import_handle_addref);
869 /* the default magic is V2, will be used in connect RPC, and
870 * then adjusted according to the flags in request/reply. */
871 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
875 EXPORT_SYMBOL(class_new_import);
877 void class_destroy_import(struct obd_import *import)
879 LASSERT(import != NULL);
880 LASSERT(import != LP_POISON);
882 class_handle_unhash(&import->imp_handle);
884 spin_lock(&import->imp_lock);
885 import->imp_generation++;
886 spin_unlock(&import->imp_lock);
887 class_import_put(import);
889 EXPORT_SYMBOL(class_destroy_import);
891 /* A connection defines an export context in which preallocation can
892 be managed. This releases the export pointer reference, and returns
893 the export handle, so the export refcount is 1 when this function
895 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
896 struct obd_uuid *cluuid)
898 struct obd_export *export;
899 LASSERT(conn != NULL);
900 LASSERT(obd != NULL);
901 LASSERT(cluuid != NULL);
904 export = class_new_export(obd, cluuid);
906 RETURN(PTR_ERR(export));
908 conn->cookie = export->exp_handle.h_cookie;
909 class_export_put(export);
911 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
912 cluuid->uuid, conn->cookie);
915 EXPORT_SYMBOL(class_connect);
917 /* if export is involved in recovery then clean up related things */
918 void class_export_recovery_cleanup(struct obd_export *exp)
920 struct obd_device *obd = exp->exp_obd;
922 spin_lock_bh(&obd->obd_processing_task_lock);
923 if (obd->obd_recovering && exp->exp_in_recovery) {
924 spin_lock(&exp->exp_lock);
925 exp->exp_in_recovery = 0;
926 spin_unlock(&exp->exp_lock);
927 obd->obd_connected_clients--;
928 /* each connected client is counted as recoverable */
929 obd->obd_recoverable_clients--;
930 if (exp->exp_req_replay_needed) {
931 spin_lock(&exp->exp_lock);
932 exp->exp_req_replay_needed = 0;
933 spin_unlock(&exp->exp_lock);
934 LASSERT(atomic_read(&obd->obd_req_replay_clients));
935 atomic_dec(&obd->obd_req_replay_clients);
937 if (exp->exp_lock_replay_needed) {
938 spin_lock(&exp->exp_lock);
939 exp->exp_lock_replay_needed = 0;
940 spin_unlock(&exp->exp_lock);
941 LASSERT(atomic_read(&obd->obd_lock_replay_clients));
942 atomic_dec(&obd->obd_lock_replay_clients);
945 spin_unlock_bh(&obd->obd_processing_task_lock);
948 /* This function removes two references from the export: one for the
949 * hash entry and one for the export pointer passed in. The export
950 * pointer passed to this function is destroyed should not be used
952 int class_disconnect(struct obd_export *export)
954 int already_disconnected;
957 if (export == NULL) {
959 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
963 spin_lock(&export->exp_lock);
964 already_disconnected = export->exp_disconnected;
965 export->exp_disconnected = 1;
967 if (!hlist_unhashed(&export->exp_nid_hash)) {
968 lustre_hash_delitem(export->exp_obd->obd_nid_hash_body,
969 &export->exp_connection->c_peer.nid, &export->exp_nid_hash);
971 spin_unlock(&export->exp_lock);
973 /* class_cleanup(), abort_recovery(), and class_fail_export()
974 * all end up in here, and if any of them race we shouldn't
975 * call extra class_export_puts(). */
976 if (already_disconnected)
979 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
980 export->exp_handle.h_cookie);
982 class_export_recovery_cleanup(export);
983 class_unlink_export(export);
984 class_export_put(export);
988 static void class_disconnect_export_list(struct list_head *list, int flags)
991 struct lustre_handle fake_conn;
992 struct obd_export *fake_exp, *exp;
995 /* It's possible that an export may disconnect itself, but
996 * nothing else will be added to this list. */
997 while (!list_empty(list)) {
998 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
999 class_export_get(exp);
1001 spin_lock(&exp->exp_lock);
1002 exp->exp_flags = flags;
1003 spin_unlock(&exp->exp_lock);
1005 if (obd_uuid_equals(&exp->exp_client_uuid,
1006 &exp->exp_obd->obd_uuid)) {
1008 "exp %p export uuid == obd uuid, don't discon\n",
1010 /* Need to delete this now so we don't end up pointing
1011 * to work_list later when this export is cleaned up. */
1012 list_del_init(&exp->exp_obd_chain);
1013 class_export_put(exp);
1017 fake_conn.cookie = exp->exp_handle.h_cookie;
1018 fake_exp = class_conn2export(&fake_conn);
1020 class_export_put(exp);
1024 spin_lock(&fake_exp->exp_lock);
1025 fake_exp->exp_flags = flags;
1026 spin_unlock(&fake_exp->exp_lock);
1028 rc = obd_disconnect(fake_exp);
1029 class_export_put(exp);
1030 CDEBUG(D_HA, "disconnecting export %s (%p): rc %d\n",
1031 exp->exp_client_uuid.uuid, exp, rc);
1036 static inline int get_exp_flags_from_obd(struct obd_device *obd)
1038 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
1039 (obd->obd_force ? OBD_OPT_FORCE : 0));
1042 void class_disconnect_exports(struct obd_device *obd)
1044 struct list_head work_list;
1047 /* Move all of the exports from obd_exports to a work list, en masse. */
1048 spin_lock(&obd->obd_dev_lock);
1049 list_add(&work_list, &obd->obd_exports);
1050 list_del_init(&obd->obd_exports);
1051 spin_unlock(&obd->obd_dev_lock);
1053 if (!list_empty(&work_list)) {
1054 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
1055 "disconnecting them\n", obd->obd_minor, obd);
1056 class_disconnect_export_list(&work_list,
1057 get_exp_flags_from_obd(obd));
1059 CDEBUG(D_HA, "OBD device %d (%p) has no exports\n",
1060 obd->obd_minor, obd);
1063 EXPORT_SYMBOL(class_disconnect_exports);
1065 /* Remove exports that have not completed recovery.
1067 int class_disconnect_stale_exports(struct obd_device *obd,
1068 int (*test_export)(struct obd_export *))
1070 struct list_head work_list;
1071 struct list_head *pos, *n;
1072 struct obd_export *exp;
1076 CFS_INIT_LIST_HEAD(&work_list);
1077 spin_lock(&obd->obd_dev_lock);
1078 list_for_each_safe(pos, n, &obd->obd_exports) {
1079 exp = list_entry(pos, struct obd_export, exp_obd_chain);
1080 if (test_export(exp))
1083 list_del(&exp->exp_obd_chain);
1084 list_add(&exp->exp_obd_chain, &work_list);
1085 /* don't count self-export as client */
1086 if (obd_uuid_equals(&exp->exp_client_uuid,
1087 &exp->exp_obd->obd_uuid))
1091 CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
1092 obd->obd_name, exp->exp_client_uuid.uuid,
1093 exp->exp_connection == NULL ? "<unknown>" :
1094 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1096 spin_unlock(&obd->obd_dev_lock);
1098 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
1099 obd->obd_name, cnt);
1100 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
1103 EXPORT_SYMBOL(class_disconnect_stale_exports);
1105 int oig_init(struct obd_io_group **oig_out)
1107 struct obd_io_group *oig;
1110 OBD_ALLOC(oig, sizeof(*oig));
1114 spin_lock_init(&oig->oig_lock);
1116 oig->oig_pending = 0;
1117 atomic_set(&oig->oig_refcount, 1);
1118 cfs_waitq_init(&oig->oig_waitq);
1119 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1124 EXPORT_SYMBOL(oig_init);
1126 static inline void oig_grab(struct obd_io_group *oig)
1128 atomic_inc(&oig->oig_refcount);
1131 void oig_release(struct obd_io_group *oig)
1133 if (atomic_dec_and_test(&oig->oig_refcount))
1134 OBD_FREE(oig, sizeof(*oig));
1136 EXPORT_SYMBOL(oig_release);
1138 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1141 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1142 spin_lock(&oig->oig_lock);
1148 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1150 spin_unlock(&oig->oig_lock);
1155 EXPORT_SYMBOL(oig_add_one);
1157 void oig_complete_one(struct obd_io_group *oig,
1158 struct oig_callback_context *occ, int rc)
1160 cfs_waitq_t *wake = NULL;
1163 spin_lock(&oig->oig_lock);
1166 list_del_init(&occ->occ_oig_item);
1168 old_rc = oig->oig_rc;
1169 if (oig->oig_rc == 0 && rc != 0)
1172 if (--oig->oig_pending <= 0)
1173 wake = &oig->oig_waitq;
1175 spin_unlock(&oig->oig_lock);
1177 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1178 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1181 cfs_waitq_signal(wake);
1184 EXPORT_SYMBOL(oig_complete_one);
1186 static int oig_done(struct obd_io_group *oig)
1189 spin_lock(&oig->oig_lock);
1190 if (oig->oig_pending <= 0)
1192 spin_unlock(&oig->oig_lock);
1196 static void interrupted_oig(void *data)
1198 struct obd_io_group *oig = data;
1199 struct oig_callback_context *occ;
1201 spin_lock(&oig->oig_lock);
1202 /* We need to restart the processing each time we drop the lock, as
1203 * it is possible other threads called oig_complete_one() to remove
1204 * an entry elsewhere in the list while we dropped lock. We need to
1205 * drop the lock because osc_ap_completion() calls oig_complete_one()
1206 * which re-gets this lock ;-) as well as a lock ordering issue. */
1208 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1209 if (occ->interrupted)
1211 occ->interrupted = 1;
1212 spin_unlock(&oig->oig_lock);
1213 occ->occ_interrupted(occ);
1214 spin_lock(&oig->oig_lock);
1217 spin_unlock(&oig->oig_lock);
1220 int oig_wait(struct obd_io_group *oig)
1222 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1225 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1228 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1229 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1230 /* we can't continue until the oig has emptied and stopped
1231 * referencing state that the caller will free upon return */
1233 lwi = (struct l_wait_info){ 0, };
1234 } while (rc == -EINTR);
1236 LASSERTF(oig->oig_pending == 0,
1237 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1240 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1243 EXPORT_SYMBOL(oig_wait);
1245 void class_fail_export(struct obd_export *exp)
1247 int rc, already_failed;
1249 spin_lock(&exp->exp_lock);
1250 already_failed = exp->exp_failed;
1251 exp->exp_failed = 1;
1252 spin_unlock(&exp->exp_lock);
1254 if (already_failed) {
1255 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1256 exp, exp->exp_client_uuid.uuid);
1260 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1261 exp, exp->exp_client_uuid.uuid);
1263 if (obd_dump_on_timeout)
1264 libcfs_debug_dumplog();
1266 /* Most callers into obd_disconnect are removing their own reference
1267 * (request, for example) in addition to the one from the hash table.
1268 * We don't have such a reference here, so make one. */
1269 class_export_get(exp);
1270 rc = obd_disconnect(exp);
1272 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1274 CDEBUG(D_HA, "disconnected export %p/%s\n",
1275 exp, exp->exp_client_uuid.uuid);
1277 EXPORT_SYMBOL(class_fail_export);
1279 char *obd_export_nid2str(struct obd_export *exp)
1281 if (exp->exp_connection != NULL)
1282 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1286 EXPORT_SYMBOL(obd_export_nid2str);
1288 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid)
1290 struct obd_export *doomed_exp = NULL;
1291 int exports_evicted = 0;
1293 lnet_nid_t nid_key = libcfs_str2nid((char *)nid);
1296 doomed_exp = lustre_hash_get_object_by_key(obd->obd_nid_hash_body,
1298 if (doomed_exp == NULL)
1301 LASSERTF(doomed_exp->exp_connection->c_peer.nid == nid_key,
1302 "nid %s found, wanted nid %s, requested nid %s\n",
1303 obd_export_nid2str(doomed_exp),
1304 libcfs_nid2str(nid_key), nid);
1305 LASSERTF(doomed_exp != obd->obd_self_export,
1306 "self-export is hashed by NID?\n");
1308 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1309 obd->obd_name, nid, doomed_exp->exp_client_uuid.uuid,
1311 class_fail_export(doomed_exp);
1312 class_export_put(doomed_exp);
1315 if (!exports_evicted)
1316 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1317 obd->obd_name, nid);
1318 return exports_evicted;
1320 EXPORT_SYMBOL(obd_export_evict_by_nid);
1322 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
1324 struct obd_export *doomed_exp = NULL;
1325 struct obd_uuid doomed;
1326 int exports_evicted = 0;
1328 obd_str2uuid(&doomed, uuid);
1329 if (obd_uuid_equals(&doomed, &obd->obd_uuid)) {
1330 CERROR("%s: can't evict myself\n", obd->obd_name);
1331 return exports_evicted;
1334 doomed_exp = lustre_hash_get_object_by_key(obd->obd_uuid_hash_body,
1337 if (doomed_exp == NULL) {
1338 CERROR("%s: can't disconnect %s: no exports found\n",
1339 obd->obd_name, uuid);
1341 CWARN("%s: evicting %s at adminstrative request\n",
1342 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1343 class_fail_export(doomed_exp);
1344 class_export_put(doomed_exp);
1348 return exports_evicted;
1350 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1352 void obd_zombie_impexp_cull(void)
1354 struct obd_import *import;
1355 struct obd_export *export;
1359 spin_lock (&obd_zombie_impexp_lock);
1362 if (!list_empty(&obd_zombie_imports)) {
1363 import = list_entry(obd_zombie_imports.next,
1366 list_del(&import->imp_zombie_chain);
1370 if (!list_empty(&obd_zombie_exports)) {
1371 export = list_entry(obd_zombie_exports.next,
1374 list_del_init(&export->exp_obd_chain);
1377 spin_unlock(&obd_zombie_impexp_lock);
1380 class_import_destroy(import);
1383 class_export_destroy(export);
1385 } while (import != NULL || export != NULL);
1388 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1390 void obd_zombie_impexp_init(void)
1392 CFS_INIT_LIST_HEAD(&obd_zombie_imports);
1393 CFS_INIT_LIST_HEAD(&obd_zombie_exports);
1394 spin_lock_init(&obd_zombie_impexp_lock);