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 OBD_SLAB_FREE_PTR(obd, obd_device_cachep);
77 EXPORT_SYMBOL(obd_device_free);
79 struct obd_type *class_search_type(const char *name)
81 struct list_head *tmp;
82 struct obd_type *type;
84 spin_lock(&obd_types_lock);
85 list_for_each(tmp, &obd_types) {
86 type = list_entry(tmp, struct obd_type, typ_chain);
87 if (strcmp(type->typ_name, name) == 0) {
88 spin_unlock(&obd_types_lock);
92 spin_unlock(&obd_types_lock);
96 struct obd_type *class_get_type(const char *name)
98 struct obd_type *type = class_search_type(name);
102 const char *modname = name;
103 if (!request_module(modname)) {
104 CDEBUG(D_INFO, "Loaded module '%s'\n", modname);
105 type = class_search_type(name);
107 LCONSOLE_ERROR_MSG(0x158, "Can't load module '%s'\n",
113 spin_lock(&type->obd_type_lock);
115 try_module_get(type->typ_dt_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_dt_ops->o_owner);
127 spin_unlock(&type->obd_type_lock);
130 #define CLASS_MAX_NAME 1024
132 int class_register_type(struct obd_ops *dt_ops, struct md_ops *md_ops,
133 struct lprocfs_vars *vars, const char *name,
134 struct lu_device_type *ldt)
136 struct obd_type *type;
141 LASSERT(strnlen(name, CLASS_MAX_NAME) < CLASS_MAX_NAME);
143 if (class_search_type(name)) {
144 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
149 OBD_ALLOC(type, sizeof(*type));
153 OBD_ALLOC_PTR(type->typ_dt_ops);
154 OBD_ALLOC_PTR(type->typ_md_ops);
155 OBD_ALLOC(type->typ_name, strlen(name) + 1);
157 if (type->typ_dt_ops == NULL ||
158 type->typ_md_ops == NULL ||
159 type->typ_name == NULL)
162 *(type->typ_dt_ops) = *dt_ops;
163 /* md_ops is optional */
165 *(type->typ_md_ops) = *md_ops;
166 strcpy(type->typ_name, name);
167 spin_lock_init(&type->obd_type_lock);
170 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
172 if (IS_ERR(type->typ_procroot)) {
173 rc = PTR_ERR(type->typ_procroot);
174 type->typ_procroot = NULL;
180 rc = ldt->ldt_ops->ldto_init(ldt);
185 spin_lock(&obd_types_lock);
186 list_add(&type->typ_chain, &obd_types);
187 spin_unlock(&obd_types_lock);
192 if (type->typ_name != NULL)
193 OBD_FREE(type->typ_name, strlen(name) + 1);
194 if (type->typ_md_ops != NULL)
195 OBD_FREE_PTR(type->typ_md_ops);
196 if (type->typ_dt_ops != NULL)
197 OBD_FREE_PTR(type->typ_dt_ops);
198 OBD_FREE(type, sizeof(*type));
202 int class_unregister_type(const char *name)
204 struct obd_type *type = class_search_type(name);
208 CERROR("unknown obd type\n");
212 if (type->typ_refcnt) {
213 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
214 /* This is a bad situation, let's make the best of it */
215 /* Remove ops, but leave the name for debugging */
216 OBD_FREE_PTR(type->typ_dt_ops);
217 OBD_FREE_PTR(type->typ_md_ops);
221 if (type->typ_procroot) {
222 lprocfs_remove(&type->typ_procroot);
226 type->typ_lu->ldt_ops->ldto_fini(type->typ_lu);
228 spin_lock(&obd_types_lock);
229 list_del(&type->typ_chain);
230 spin_unlock(&obd_types_lock);
231 OBD_FREE(type->typ_name, strlen(name) + 1);
232 if (type->typ_dt_ops != NULL)
233 OBD_FREE_PTR(type->typ_dt_ops);
234 if (type->typ_md_ops != NULL)
235 OBD_FREE_PTR(type->typ_md_ops);
236 OBD_FREE(type, sizeof(*type));
238 } /* class_unregister_type */
240 struct obd_device *class_newdev(const char *type_name, const char *name)
242 struct obd_device *result = NULL;
243 struct obd_device *newdev;
244 struct obd_type *type = NULL;
246 int new_obd_minor = 0;
248 if (strlen(name) > MAX_OBD_NAME) {
249 CERROR("name/uuid must be < %u bytes long\n", MAX_OBD_NAME);
250 RETURN(ERR_PTR(-EINVAL));
253 type = class_get_type(type_name);
255 CERROR("OBD: unknown type: %s\n", type_name);
256 RETURN(ERR_PTR(-ENODEV));
259 newdev = obd_device_alloc();
260 if (newdev == NULL) {
261 class_put_type(type);
262 RETURN(ERR_PTR(-ENOMEM));
264 LASSERT(newdev->obd_magic == OBD_DEVICE_MAGIC);
266 spin_lock(&obd_dev_lock);
267 for (i = 0; i < class_devno_max(); i++) {
268 struct obd_device *obd = class_num2obd(i);
269 if (obd && obd->obd_name &&
270 (strcmp(name, obd->obd_name) == 0)) {
271 CERROR("Device %s already exists, won't add\n", name);
273 LASSERTF(result->obd_magic == OBD_DEVICE_MAGIC,
274 "%p obd_magic %08x != %08x\n", result,
275 result->obd_magic, OBD_DEVICE_MAGIC);
276 LASSERTF(result->obd_minor == new_obd_minor,
277 "%p obd_minor %d != %d\n", result,
278 result->obd_minor, new_obd_minor);
280 obd_devs[result->obd_minor] = NULL;
281 result->obd_name[0]='\0';
283 result = ERR_PTR(-EEXIST);
286 if (!result && !obd) {
288 result->obd_minor = i;
290 result->obd_type = type;
291 memcpy(result->obd_name, name, strlen(name));
292 obd_devs[i] = result;
295 spin_unlock(&obd_dev_lock);
297 if (result == NULL && i >= class_devno_max()) {
298 CERROR("all %u OBD devices used, increase MAX_OBD_DEVICES\n",
300 result = ERR_PTR(-EOVERFLOW);
303 if (IS_ERR(result)) {
304 obd_device_free(newdev);
305 class_put_type(type);
307 CDEBUG(D_IOCTL, "Adding new device %s (%p)\n",
308 result->obd_name, result);
313 void class_release_dev(struct obd_device *obd)
315 struct obd_type *obd_type = obd->obd_type;
317 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC, "%p obd_magic %08x != %08x\n",
318 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
319 LASSERTF(obd == obd_devs[obd->obd_minor], "obd %p != obd_devs[%d] %p\n",
320 obd, obd->obd_minor, obd_devs[obd->obd_minor]);
321 LASSERT(obd_type != NULL);
323 CDEBUG(D_INFO, "Release obd device %s obd_type name =%s\n",
324 obd->obd_name,obd->obd_type->typ_name);
326 spin_lock(&obd_dev_lock);
327 obd_devs[obd->obd_minor] = NULL;
328 spin_unlock(&obd_dev_lock);
329 obd_device_free(obd);
331 class_put_type(obd_type);
334 int class_name2dev(const char *name)
341 spin_lock(&obd_dev_lock);
342 for (i = 0; i < class_devno_max(); i++) {
343 struct obd_device *obd = class_num2obd(i);
344 if (obd && obd->obd_name && strcmp(name, obd->obd_name) == 0) {
345 /* Make sure we finished attaching before we give
346 out any references */
347 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
348 if (obd->obd_attached) {
349 spin_unlock(&obd_dev_lock);
355 spin_unlock(&obd_dev_lock);
360 struct obd_device *class_name2obd(const char *name)
362 int dev = class_name2dev(name);
364 if (dev < 0 || dev > class_devno_max())
366 return class_num2obd(dev);
369 int class_uuid2dev(struct obd_uuid *uuid)
373 spin_lock(&obd_dev_lock);
374 for (i = 0; i < class_devno_max(); i++) {
375 struct obd_device *obd = class_num2obd(i);
376 if (obd && obd_uuid_equals(uuid, &obd->obd_uuid)) {
377 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
378 spin_unlock(&obd_dev_lock);
382 spin_unlock(&obd_dev_lock);
387 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
389 int dev = class_uuid2dev(uuid);
392 return class_num2obd(dev);
395 struct obd_device *class_num2obd(int num)
397 struct obd_device *obd = NULL;
399 if (num < class_devno_max()) {
405 LASSERTF(obd->obd_magic == OBD_DEVICE_MAGIC,
406 "%p obd_magic %08x != %08x\n",
407 obd, obd->obd_magic, OBD_DEVICE_MAGIC);
408 LASSERTF(obd->obd_minor == num,
409 "%p obd_minor %0d != %0d\n",
410 obd, obd->obd_minor, num);
416 void class_obd_list(void)
421 spin_lock(&obd_dev_lock);
422 for (i = 0; i < class_devno_max(); i++) {
423 struct obd_device *obd = class_num2obd(i);
426 if (obd->obd_stopping)
428 else if (obd->obd_set_up)
430 else if (obd->obd_attached)
434 LCONSOLE(D_CONFIG, "%3d %s %s %s %s %d\n",
435 i, status, obd->obd_type->typ_name,
436 obd->obd_name, obd->obd_uuid.uuid,
437 atomic_read(&obd->obd_refcount));
439 spin_unlock(&obd_dev_lock);
443 /* Search for a client OBD connected to tgt_uuid. If grp_uuid is
444 specified, then only the client with that uuid is returned,
445 otherwise any client connected to the tgt is returned. */
446 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
447 const char * typ_name,
448 struct obd_uuid *grp_uuid)
452 spin_lock(&obd_dev_lock);
453 for (i = 0; i < class_devno_max(); i++) {
454 struct obd_device *obd = class_num2obd(i);
457 if ((strncmp(obd->obd_type->typ_name, typ_name,
458 strlen(typ_name)) == 0)) {
459 if (obd_uuid_equals(tgt_uuid,
460 &obd->u.cli.cl_target_uuid) &&
461 ((grp_uuid)? obd_uuid_equals(grp_uuid,
462 &obd->obd_uuid) : 1)) {
463 spin_unlock(&obd_dev_lock);
468 spin_unlock(&obd_dev_lock);
473 struct obd_device *class_find_client_notype(struct obd_uuid *tgt_uuid,
474 struct obd_uuid *grp_uuid)
476 struct obd_device *obd;
478 obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME, NULL);
480 obd = class_find_client_obd(tgt_uuid, LUSTRE_OSC_NAME,
485 /* Iterate the obd_device list looking devices have grp_uuid. Start
486 searching at *next, and if a device is found, the next index to look
487 at is saved in *next. If next is NULL, then the first matching device
488 will always be returned. */
489 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid, int *next)
495 else if (*next >= 0 && *next < class_devno_max())
500 spin_lock(&obd_dev_lock);
501 for (; i < class_devno_max(); i++) {
502 struct obd_device *obd = class_num2obd(i);
505 if (obd_uuid_equals(grp_uuid, &obd->obd_uuid)) {
508 spin_unlock(&obd_dev_lock);
512 spin_unlock(&obd_dev_lock);
518 void obd_cleanup_caches(void)
523 if (obd_device_cachep) {
524 rc = cfs_mem_cache_destroy(obd_device_cachep);
525 LASSERTF(rc == 0, "Cannot destropy ll_obd_device_cache: rc %d\n", rc);
526 obd_device_cachep = NULL;
529 rc = cfs_mem_cache_destroy(obdo_cachep);
530 LASSERTF(rc == 0, "Cannot destory ll_obdo_cache\n");
534 rc = cfs_mem_cache_destroy(import_cachep);
535 LASSERTF(rc == 0, "Cannot destory ll_import_cache\n");
536 import_cachep = NULL;
539 rc = cfs_mem_cache_destroy(capa_cachep);
540 LASSERTF(rc == 0, "Cannot destory capa_cache\n");
546 int obd_init_caches(void)
550 LASSERT(obd_device_cachep == NULL);
551 obd_device_cachep = cfs_mem_cache_create("ll_obd_dev_cache",
552 sizeof(struct obd_device),
554 if (!obd_device_cachep)
557 LASSERT(obdo_cachep == NULL);
558 obdo_cachep = cfs_mem_cache_create("ll_obdo_cache", sizeof(struct obdo),
563 LASSERT(import_cachep == NULL);
564 import_cachep = cfs_mem_cache_create("ll_import_cache",
565 sizeof(struct obd_import),
570 LASSERT(capa_cachep == NULL);
571 capa_cachep = cfs_mem_cache_create("capa_cache",
572 sizeof(struct obd_capa), 0, 0);
578 obd_cleanup_caches();
583 /* map connection to client */
584 struct obd_export *class_conn2export(struct lustre_handle *conn)
586 struct obd_export *export;
590 CDEBUG(D_CACHE, "looking for null handle\n");
594 if (conn->cookie == -1) { /* this means assign a new connection */
595 CDEBUG(D_CACHE, "want a new connection\n");
599 CDEBUG(D_INFO, "looking for export cookie "LPX64"\n", conn->cookie);
600 export = class_handle2object(conn->cookie);
604 struct obd_device *class_exp2obd(struct obd_export *exp)
611 struct obd_device *class_conn2obd(struct lustre_handle *conn)
613 struct obd_export *export;
614 export = class_conn2export(conn);
616 struct obd_device *obd = export->exp_obd;
617 class_export_put(export);
623 struct obd_import *class_exp2cliimp(struct obd_export *exp)
625 struct obd_device *obd = exp->exp_obd;
628 return obd->u.cli.cl_import;
631 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
633 struct obd_device *obd = class_conn2obd(conn);
636 return obd->u.cli.cl_import;
639 /* Export management functions */
640 static void export_handle_addref(void *export)
642 class_export_get(export);
645 void __class_export_put(struct obd_export *exp)
647 if (atomic_dec_and_test(&exp->exp_refcount)) {
648 LASSERT (list_empty(&exp->exp_obd_chain));
650 CDEBUG(D_IOCTL, "final put %p/%s\n",
651 exp, exp->exp_client_uuid.uuid);
653 spin_lock(&obd_zombie_impexp_lock);
654 list_add(&exp->exp_obd_chain, &obd_zombie_exports);
655 spin_unlock(&obd_zombie_impexp_lock);
657 if (obd_zombie_impexp_notify != NULL)
658 obd_zombie_impexp_notify();
661 EXPORT_SYMBOL(__class_export_put);
663 void class_export_destroy(struct obd_export *exp)
665 struct obd_device *obd = exp->exp_obd;
668 LASSERT (atomic_read(&exp->exp_refcount) == 0);
670 CDEBUG(D_IOCTL, "destroying export %p/%s for %s\n", exp,
671 exp->exp_client_uuid.uuid, obd->obd_name);
673 LASSERT(obd != NULL);
675 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
676 if (exp->exp_connection)
677 ptlrpc_put_connection_superhack(exp->exp_connection);
679 LASSERT(list_empty(&exp->exp_outstanding_replies));
680 obd_destroy_export(exp);
682 OBD_FREE_RCU(exp, sizeof(*exp), &exp->exp_handle);
687 /* Creates a new export, adds it to the hash table, and returns a
688 * pointer to it. The refcount is 2: one for the hash reference, and
689 * one for the pointer returned by this function. */
690 struct obd_export *class_new_export(struct obd_device *obd,
691 struct obd_uuid *cluuid)
693 struct obd_export *export;
696 OBD_ALLOC_PTR(export);
698 return ERR_PTR(-ENOMEM);
700 export->exp_conn_cnt = 0;
701 atomic_set(&export->exp_refcount, 2);
702 atomic_set(&export->exp_rpc_count, 0);
703 export->exp_obd = obd;
704 CFS_INIT_LIST_HEAD(&export->exp_outstanding_replies);
705 /* XXX this should be in LDLM init */
706 CFS_INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
707 spin_lock_init(&export->exp_ldlm_data.led_lock);
709 CFS_INIT_LIST_HEAD(&export->exp_handle.h_link);
710 class_handle_hash(&export->exp_handle, export_handle_addref);
711 export->exp_last_request_time = CURRENT_SECONDS;
712 spin_lock_init(&export->exp_lock);
713 INIT_HLIST_NODE(&export->exp_uuid_hash);
714 INIT_HLIST_NODE(&export->exp_nid_hash);
716 export->exp_client_uuid = *cluuid;
717 obd_init_export(export);
719 spin_lock(&obd->obd_dev_lock);
720 if (!obd_uuid_equals(cluuid, &obd->obd_uuid)) {
721 rc = lustre_hash_additem_unique(obd->obd_uuid_hash_body, cluuid,
722 &export->exp_uuid_hash);
724 CWARN("%s: denying duplicate export for %s\n",
725 obd->obd_name, cluuid->uuid);
726 spin_unlock(&obd->obd_dev_lock);
727 class_handle_unhash(&export->exp_handle);
728 OBD_FREE_PTR(export);
729 return ERR_PTR(-EALREADY);
733 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
735 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
736 list_add_tail(&export->exp_obd_chain_timed,
737 &export->exp_obd->obd_exports_timed);
738 export->exp_obd->obd_num_exports++;
739 spin_unlock(&obd->obd_dev_lock);
743 EXPORT_SYMBOL(class_new_export);
745 void class_unlink_export(struct obd_export *exp)
747 class_handle_unhash(&exp->exp_handle);
749 spin_lock(&exp->exp_obd->obd_dev_lock);
750 /* delete an uuid-export hashitem from hashtables */
751 if (!hlist_unhashed(&exp->exp_uuid_hash)) {
752 lustre_hash_delitem(exp->exp_obd->obd_uuid_hash_body,
753 &exp->exp_client_uuid, &exp->exp_uuid_hash);
755 list_del_init(&exp->exp_obd_chain);
756 list_del_init(&exp->exp_obd_chain_timed);
757 exp->exp_obd->obd_num_exports--;
758 spin_unlock(&exp->exp_obd->obd_dev_lock);
760 class_export_put(exp);
762 EXPORT_SYMBOL(class_unlink_export);
764 /* Import management functions */
765 static void import_handle_addref(void *import)
767 class_import_get(import);
770 struct obd_import *class_import_get(struct obd_import *import)
772 LASSERT(atomic_read(&import->imp_refcount) >= 0);
773 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
774 atomic_inc(&import->imp_refcount);
775 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
776 atomic_read(&import->imp_refcount));
779 EXPORT_SYMBOL(class_import_get);
781 void class_import_put(struct obd_import *import)
785 CDEBUG(D_INFO, "import %p refcount=%d\n", import,
786 atomic_read(&import->imp_refcount) - 1);
788 LASSERT(atomic_read(&import->imp_refcount) > 0);
789 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
790 LASSERT(list_empty(&import->imp_zombie_chain));
792 if (atomic_dec_and_test(&import->imp_refcount)) {
794 CDEBUG(D_INFO, "final put import %p\n", import);
796 spin_lock(&obd_zombie_impexp_lock);
797 list_add(&import->imp_zombie_chain, &obd_zombie_imports);
798 spin_unlock(&obd_zombie_impexp_lock);
800 if (obd_zombie_impexp_notify != NULL)
801 obd_zombie_impexp_notify();
807 void class_import_destroy(struct obd_import *import)
811 CDEBUG(D_IOCTL, "destroying import %p for %s\n", import,
812 import->imp_obd->obd_name);
814 LASSERT(atomic_read(&import->imp_refcount) == 0);
816 ptlrpc_put_connection_superhack(import->imp_connection);
818 while (!list_empty(&import->imp_conn_list)) {
819 struct obd_import_conn *imp_conn;
821 imp_conn = list_entry(import->imp_conn_list.next,
822 struct obd_import_conn, oic_item);
823 list_del(&imp_conn->oic_item);
824 ptlrpc_put_connection_superhack(imp_conn->oic_conn);
825 OBD_FREE(imp_conn, sizeof(*imp_conn));
828 LASSERT(import->imp_sec == NULL);
829 class_decref(import->imp_obd);
830 OBD_FREE_RCU(import, sizeof(*import), &import->imp_handle);
833 EXPORT_SYMBOL(class_import_put);
835 struct obd_import *class_new_import(struct obd_device *obd)
837 struct obd_import *imp;
839 OBD_ALLOC(imp, sizeof(*imp));
843 CFS_INIT_LIST_HEAD(&imp->imp_zombie_chain);
844 CFS_INIT_LIST_HEAD(&imp->imp_replay_list);
845 CFS_INIT_LIST_HEAD(&imp->imp_sending_list);
846 CFS_INIT_LIST_HEAD(&imp->imp_delayed_list);
847 spin_lock_init(&imp->imp_lock);
848 imp->imp_last_success_conn = 0;
849 imp->imp_state = LUSTRE_IMP_NEW;
850 imp->imp_obd = class_incref(obd);
851 cfs_waitq_init(&imp->imp_recovery_waitq);
853 atomic_set(&imp->imp_refcount, 2);
854 atomic_set(&imp->imp_inflight, 0);
855 atomic_set(&imp->imp_replay_inflight, 0);
856 atomic_set(&imp->imp_inval_count, 0);
857 CFS_INIT_LIST_HEAD(&imp->imp_conn_list);
858 CFS_INIT_LIST_HEAD(&imp->imp_handle.h_link);
859 class_handle_hash(&imp->imp_handle, import_handle_addref);
861 /* the default magic is V1, will be used in connect RPC, and
862 * then adjusted according to the flags in request/reply. */
863 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V1;
867 EXPORT_SYMBOL(class_new_import);
869 void class_destroy_import(struct obd_import *import)
871 LASSERT(import != NULL);
872 LASSERT(import != LP_POISON);
874 class_handle_unhash(&import->imp_handle);
876 spin_lock(&import->imp_lock);
877 import->imp_generation++;
878 spin_unlock(&import->imp_lock);
879 class_import_put(import);
881 EXPORT_SYMBOL(class_destroy_import);
883 /* A connection defines an export context in which preallocation can
884 be managed. This releases the export pointer reference, and returns
885 the export handle, so the export refcount is 1 when this function
887 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
888 struct obd_uuid *cluuid)
890 struct obd_export *export;
891 LASSERT(conn != NULL);
892 LASSERT(obd != NULL);
893 LASSERT(cluuid != NULL);
896 export = class_new_export(obd, cluuid);
898 RETURN(PTR_ERR(export));
900 conn->cookie = export->exp_handle.h_cookie;
901 class_export_put(export);
903 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
904 cluuid->uuid, conn->cookie);
907 EXPORT_SYMBOL(class_connect);
909 /* if export is involved in recovery then clean up related things */
910 void class_export_recovery_cleanup(struct obd_export *exp)
912 struct obd_device *obd = exp->exp_obd;
914 spin_lock_bh(&obd->obd_processing_task_lock);
915 if (obd->obd_recovering && exp->exp_in_recovery) {
916 spin_lock(&exp->exp_lock);
917 exp->exp_in_recovery = 0;
918 spin_unlock(&exp->exp_lock);
919 obd->obd_connected_clients--;
920 /* each connected client is counted as recoverable */
921 obd->obd_recoverable_clients--;
922 if (exp->exp_req_replay_needed) {
923 spin_lock(&exp->exp_lock);
924 exp->exp_req_replay_needed = 0;
925 spin_unlock(&exp->exp_lock);
926 LASSERT(atomic_read(&obd->obd_req_replay_clients));
927 atomic_dec(&obd->obd_req_replay_clients);
929 if (exp->exp_lock_replay_needed) {
930 spin_lock(&exp->exp_lock);
931 exp->exp_lock_replay_needed = 0;
932 spin_unlock(&exp->exp_lock);
933 LASSERT(atomic_read(&obd->obd_lock_replay_clients));
934 atomic_dec(&obd->obd_lock_replay_clients);
937 spin_unlock_bh(&obd->obd_processing_task_lock);
940 /* This function removes two references from the export: one for the
941 * hash entry and one for the export pointer passed in. The export
942 * pointer passed to this function is destroyed should not be used
944 int class_disconnect(struct obd_export *export)
946 int already_disconnected;
949 if (export == NULL) {
951 CDEBUG(D_IOCTL, "attempting to free NULL export %p\n", export);
955 spin_lock(&export->exp_lock);
956 already_disconnected = export->exp_disconnected;
957 export->exp_disconnected = 1;
959 if (!hlist_unhashed(&export->exp_nid_hash)) {
960 lustre_hash_delitem(export->exp_obd->obd_nid_hash_body,
961 &export->exp_connection->c_peer.nid, &export->exp_nid_hash);
963 spin_unlock(&export->exp_lock);
965 /* class_cleanup(), abort_recovery(), and class_fail_export()
966 * all end up in here, and if any of them race we shouldn't
967 * call extra class_export_puts(). */
968 if (already_disconnected)
971 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n",
972 export->exp_handle.h_cookie);
974 class_export_recovery_cleanup(export);
975 class_unlink_export(export);
976 class_export_put(export);
980 static void class_disconnect_export_list(struct list_head *list, int flags)
983 struct lustre_handle fake_conn;
984 struct obd_export *fake_exp, *exp;
987 /* It's possible that an export may disconnect itself, but
988 * nothing else will be added to this list. */
989 while (!list_empty(list)) {
990 exp = list_entry(list->next, struct obd_export, exp_obd_chain);
991 class_export_get(exp);
993 spin_lock(&exp->exp_lock);
994 exp->exp_flags = flags;
995 spin_unlock(&exp->exp_lock);
997 if (obd_uuid_equals(&exp->exp_client_uuid,
998 &exp->exp_obd->obd_uuid)) {
1000 "exp %p export uuid == obd uuid, don't discon\n",
1002 /* Need to delete this now so we don't end up pointing
1003 * to work_list later when this export is cleaned up. */
1004 list_del_init(&exp->exp_obd_chain);
1005 class_export_put(exp);
1009 fake_conn.cookie = exp->exp_handle.h_cookie;
1010 fake_exp = class_conn2export(&fake_conn);
1012 class_export_put(exp);
1016 spin_lock(&fake_exp->exp_lock);
1017 fake_exp->exp_flags = flags;
1018 spin_unlock(&fake_exp->exp_lock);
1020 rc = obd_disconnect(fake_exp);
1021 class_export_put(exp);
1022 CDEBUG(D_HA, "disconnecting export %s (%p): rc %d\n",
1023 exp->exp_client_uuid.uuid, exp, rc);
1028 static inline int get_exp_flags_from_obd(struct obd_device *obd)
1030 return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
1031 (obd->obd_force ? OBD_OPT_FORCE : 0));
1034 void class_disconnect_exports(struct obd_device *obd)
1036 struct list_head work_list;
1039 /* Move all of the exports from obd_exports to a work list, en masse. */
1040 spin_lock(&obd->obd_dev_lock);
1041 list_add(&work_list, &obd->obd_exports);
1042 list_del_init(&obd->obd_exports);
1043 spin_unlock(&obd->obd_dev_lock);
1045 if (!list_empty(&work_list)) {
1046 CDEBUG(D_HA, "OBD device %d (%p) has exports, "
1047 "disconnecting them\n", obd->obd_minor, obd);
1048 class_disconnect_export_list(&work_list,
1049 get_exp_flags_from_obd(obd));
1051 CDEBUG(D_HA, "OBD device %d (%p) has no exports\n",
1052 obd->obd_minor, obd);
1055 EXPORT_SYMBOL(class_disconnect_exports);
1057 /* Remove exports that have not completed recovery.
1059 int class_disconnect_stale_exports(struct obd_device *obd,
1060 int (*test_export)(struct obd_export *))
1062 struct list_head work_list;
1063 struct list_head *pos, *n;
1064 struct obd_export *exp;
1068 CFS_INIT_LIST_HEAD(&work_list);
1069 spin_lock(&obd->obd_dev_lock);
1070 list_for_each_safe(pos, n, &obd->obd_exports) {
1071 exp = list_entry(pos, struct obd_export, exp_obd_chain);
1072 if (test_export(exp))
1075 list_del(&exp->exp_obd_chain);
1076 list_add(&exp->exp_obd_chain, &work_list);
1077 /* don't count self-export as client */
1078 if (obd_uuid_equals(&exp->exp_client_uuid,
1079 &exp->exp_obd->obd_uuid))
1083 CDEBUG(D_ERROR, "%s: disconnect stale client %s@%s\n",
1084 obd->obd_name, exp->exp_client_uuid.uuid,
1085 exp->exp_connection == NULL ? "<unknown>" :
1086 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1088 spin_unlock(&obd->obd_dev_lock);
1090 CDEBUG(D_ERROR, "%s: disconnecting %d stale clients\n",
1091 obd->obd_name, cnt);
1092 class_disconnect_export_list(&work_list, get_exp_flags_from_obd(obd));
1095 EXPORT_SYMBOL(class_disconnect_stale_exports);
1097 int oig_init(struct obd_io_group **oig_out)
1099 struct obd_io_group *oig;
1102 OBD_ALLOC(oig, sizeof(*oig));
1106 spin_lock_init(&oig->oig_lock);
1108 oig->oig_pending = 0;
1109 atomic_set(&oig->oig_refcount, 1);
1110 cfs_waitq_init(&oig->oig_waitq);
1111 CFS_INIT_LIST_HEAD(&oig->oig_occ_list);
1116 EXPORT_SYMBOL(oig_init);
1118 static inline void oig_grab(struct obd_io_group *oig)
1120 atomic_inc(&oig->oig_refcount);
1123 void oig_release(struct obd_io_group *oig)
1125 if (atomic_dec_and_test(&oig->oig_refcount))
1126 OBD_FREE(oig, sizeof(*oig));
1128 EXPORT_SYMBOL(oig_release);
1130 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ)
1133 CDEBUG(D_CACHE, "oig %p ready to roll\n", oig);
1134 spin_lock(&oig->oig_lock);
1140 list_add_tail(&occ->occ_oig_item, &oig->oig_occ_list);
1142 spin_unlock(&oig->oig_lock);
1147 EXPORT_SYMBOL(oig_add_one);
1149 void oig_complete_one(struct obd_io_group *oig,
1150 struct oig_callback_context *occ, int rc)
1152 cfs_waitq_t *wake = NULL;
1155 spin_lock(&oig->oig_lock);
1158 list_del_init(&occ->occ_oig_item);
1160 old_rc = oig->oig_rc;
1161 if (oig->oig_rc == 0 && rc != 0)
1164 if (--oig->oig_pending <= 0)
1165 wake = &oig->oig_waitq;
1167 spin_unlock(&oig->oig_lock);
1169 CDEBUG(D_CACHE, "oig %p completed, rc %d -> %d via %d, %d now "
1170 "pending (racey)\n", oig, old_rc, oig->oig_rc, rc,
1173 cfs_waitq_signal(wake);
1176 EXPORT_SYMBOL(oig_complete_one);
1178 static int oig_done(struct obd_io_group *oig)
1181 spin_lock(&oig->oig_lock);
1182 if (oig->oig_pending <= 0)
1184 spin_unlock(&oig->oig_lock);
1188 static void interrupted_oig(void *data)
1190 struct obd_io_group *oig = data;
1191 struct oig_callback_context *occ;
1193 spin_lock(&oig->oig_lock);
1194 /* We need to restart the processing each time we drop the lock, as
1195 * it is possible other threads called oig_complete_one() to remove
1196 * an entry elsewhere in the list while we dropped lock. We need to
1197 * drop the lock because osc_ap_completion() calls oig_complete_one()
1198 * which re-gets this lock ;-) as well as a lock ordering issue. */
1200 list_for_each_entry(occ, &oig->oig_occ_list, occ_oig_item) {
1201 if (occ->interrupted)
1203 occ->interrupted = 1;
1204 spin_unlock(&oig->oig_lock);
1205 occ->occ_interrupted(occ);
1206 spin_lock(&oig->oig_lock);
1209 spin_unlock(&oig->oig_lock);
1212 int oig_wait(struct obd_io_group *oig)
1214 struct l_wait_info lwi = LWI_INTR(interrupted_oig, oig);
1217 CDEBUG(D_CACHE, "waiting for oig %p\n", oig);
1220 rc = l_wait_event(oig->oig_waitq, oig_done(oig), &lwi);
1221 LASSERTF(rc == 0 || rc == -EINTR, "rc: %d\n", rc);
1222 /* we can't continue until the oig has emptied and stopped
1223 * referencing state that the caller will free upon return */
1225 lwi = (struct l_wait_info){ 0, };
1226 } while (rc == -EINTR);
1228 LASSERTF(oig->oig_pending == 0,
1229 "exiting oig_wait(oig = %p) with %d pending\n", oig,
1232 CDEBUG(D_CACHE, "done waiting on oig %p rc %d\n", oig, oig->oig_rc);
1235 EXPORT_SYMBOL(oig_wait);
1237 void class_fail_export(struct obd_export *exp)
1239 int rc, already_failed;
1241 spin_lock(&exp->exp_lock);
1242 already_failed = exp->exp_failed;
1243 exp->exp_failed = 1;
1244 spin_unlock(&exp->exp_lock);
1246 if (already_failed) {
1247 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
1248 exp, exp->exp_client_uuid.uuid);
1252 CDEBUG(D_HA, "disconnecting export %p/%s\n",
1253 exp, exp->exp_client_uuid.uuid);
1255 if (obd_dump_on_timeout)
1256 libcfs_debug_dumplog();
1258 /* Most callers into obd_disconnect are removing their own reference
1259 * (request, for example) in addition to the one from the hash table.
1260 * We don't have such a reference here, so make one. */
1261 class_export_get(exp);
1262 rc = obd_disconnect(exp);
1264 CERROR("disconnecting export %p failed: %d\n", exp, rc);
1266 CDEBUG(D_HA, "disconnected export %p/%s\n",
1267 exp, exp->exp_client_uuid.uuid);
1269 EXPORT_SYMBOL(class_fail_export);
1271 char *obd_export_nid2str(struct obd_export *exp)
1273 if (exp->exp_connection != NULL)
1274 return libcfs_nid2str(exp->exp_connection->c_peer.nid);
1278 EXPORT_SYMBOL(obd_export_nid2str);
1280 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid)
1282 struct obd_export *doomed_exp = NULL;
1283 int exports_evicted = 0;
1285 lnet_nid_t nid_key = libcfs_str2nid(nid);
1288 doomed_exp = lustre_hash_get_object_by_key(obd->obd_nid_hash_body,
1290 if (doomed_exp == NULL)
1293 LASSERTF(doomed_exp->exp_connection->c_peer.nid == nid_key,
1294 "nid %s found, wanted nid %s, requested nid %s\n",
1295 obd_export_nid2str(doomed_exp),
1296 libcfs_nid2str(nid_key), nid);
1297 LASSERTF(doomed_exp != obd->obd_self_export,
1298 "self-export is hashed by NID?\n");
1300 CWARN("%s: evict NID '%s' (%s) #%d at adminstrative request\n",
1301 obd->obd_name, nid, doomed_exp->exp_client_uuid.uuid,
1303 class_fail_export(doomed_exp);
1304 class_export_put(doomed_exp);
1307 if (!exports_evicted)
1308 CDEBUG(D_HA,"%s: can't disconnect NID '%s': no exports found\n",
1309 obd->obd_name, nid);
1310 return exports_evicted;
1312 EXPORT_SYMBOL(obd_export_evict_by_nid);
1314 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid)
1316 struct obd_export *doomed_exp = NULL;
1317 struct obd_uuid doomed;
1318 int exports_evicted = 0;
1320 obd_str2uuid(&doomed, uuid);
1321 if (obd_uuid_equals(&doomed, &obd->obd_uuid)) {
1322 CERROR("%s: can't evict myself\n", obd->obd_name);
1323 return exports_evicted;
1326 doomed_exp = lustre_hash_get_object_by_key(obd->obd_uuid_hash_body,
1329 if (doomed_exp == NULL) {
1330 CERROR("%s: can't disconnect %s: no exports found\n",
1331 obd->obd_name, uuid);
1333 CWARN("%s: evicting %s at adminstrative request\n",
1334 obd->obd_name, doomed_exp->exp_client_uuid.uuid);
1335 class_fail_export(doomed_exp);
1336 class_export_put(doomed_exp);
1340 return exports_evicted;
1342 EXPORT_SYMBOL(obd_export_evict_by_uuid);
1344 void obd_zombie_impexp_cull(void)
1346 struct obd_import *import;
1347 struct obd_export *export;
1351 spin_lock (&obd_zombie_impexp_lock);
1354 if (!list_empty(&obd_zombie_imports)) {
1355 import = list_entry(obd_zombie_imports.next,
1358 list_del(&import->imp_zombie_chain);
1362 if (!list_empty(&obd_zombie_exports)) {
1363 export = list_entry(obd_zombie_exports.next,
1366 list_del_init(&export->exp_obd_chain);
1369 spin_unlock(&obd_zombie_impexp_lock);
1372 class_import_destroy(import);
1375 class_export_destroy(export);
1377 } while (import != NULL || export != NULL);
1380 EXPORT_SYMBOL(obd_zombie_impexp_cull);
1382 void obd_zombie_impexp_init(void)
1384 INIT_LIST_HEAD(&obd_zombie_imports);
1385 INIT_LIST_HEAD(&obd_zombie_exports);
1386 spin_lock_init(&obd_zombie_impexp_lock);