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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * These are the only exported functions, they provide some generic
22 * infrastructure for managing object devices
25 #define DEBUG_SUBSYSTEM S_CLASS
27 #include <linux/kmod.h> /* for request_module() */
28 #include <linux/module.h>
29 #include <linux/obd_class.h>
30 #include <linux/random.h>
31 #include <linux/slab.h>
33 #include <liblustre.h>
34 #include <linux/obd_class.h>
35 #include <linux/obd.h>
37 #include <linux/lprocfs_status.h>
39 extern struct list_head obd_types;
40 static spinlock_t obd_types_lock = SPIN_LOCK_UNLOCKED;
41 kmem_cache_t *obdo_cachep = NULL;
42 kmem_cache_t *import_cachep = NULL;
44 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
45 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
47 struct obd_uuid lctl_fake_uuid = { .uuid = "OBD_CLASS_UUID" };
50 * support functions: we could use inter-module communication, but this
51 * is more portable to other OS's
53 static struct obd_type *class_search_type(char *name)
55 struct list_head *tmp;
56 struct obd_type *type;
58 spin_lock(&obd_types_lock);
59 list_for_each(tmp, &obd_types) {
60 type = list_entry(tmp, struct obd_type, typ_chain);
61 if (strlen(type->typ_name) == strlen(name) &&
62 strcmp(type->typ_name, name) == 0) {
63 spin_unlock(&obd_types_lock);
67 spin_unlock(&obd_types_lock);
71 struct obd_type *class_get_type(char *name)
73 struct obd_type *type = class_search_type(name);
77 if (!request_module(name)) {
78 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
79 type = class_search_type(name);
81 CDEBUG(D_INFO, "Can't load module '%s'\n", name);
85 __MOD_INC_USE_COUNT(type->typ_ops->o_owner);
89 void class_put_type(struct obd_type *type)
92 __MOD_DEC_USE_COUNT(type->typ_ops->o_owner);
95 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
98 struct obd_type *type;
102 LASSERT(strnlen(name, 1024) < 1024); /* sanity check */
104 if (class_search_type(name)) {
105 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
110 OBD_ALLOC(type, sizeof(*type));
114 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
115 OBD_ALLOC(type->typ_name, strlen(name) + 1);
116 if (type->typ_ops == NULL || type->typ_name == NULL)
119 *(type->typ_ops) = *ops;
120 strcpy(type->typ_name, name);
122 type->typ_procroot = lprocfs_register(type->typ_name, proc_lustre_root,
124 if (IS_ERR(type->typ_procroot)) {
125 rc = PTR_ERR(type->typ_procroot);
126 type->typ_procroot = NULL;
130 spin_lock(&obd_types_lock);
131 list_add(&type->typ_chain, &obd_types);
132 spin_unlock(&obd_types_lock);
137 if (type->typ_ops != NULL)
138 OBD_FREE(type->typ_name, strlen(name) + 1);
139 if (type->typ_ops != NULL)
140 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
144 int class_unregister_type(char *name)
146 struct obd_type *type = class_search_type(name);
150 CERROR("unknown obd type\n");
154 if (type->typ_refcnt) {
155 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
156 /* This is a bad situation, let's make the best of it */
157 /* Remove ops, but leave the name for debugging */
158 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
162 if (type->typ_procroot) {
163 lprocfs_remove(type->typ_procroot);
164 type->typ_procroot = NULL;
167 spin_lock(&obd_types_lock);
168 list_del(&type->typ_chain);
169 spin_unlock(&obd_types_lock);
170 OBD_FREE(type->typ_name, strlen(name) + 1);
171 if (type->typ_ops != NULL)
172 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
173 OBD_FREE(type, sizeof(*type));
175 } /* class_unregister_type */
177 int class_name2dev(char *name)
184 for (i = 0; i < MAX_OBD_DEVICES; i++) {
185 struct obd_device *obd = &obd_dev[i];
186 if (obd->obd_name && strcmp(name, obd->obd_name) == 0)
193 int class_uuid2dev(struct obd_uuid *uuid)
197 for (i = 0; i < MAX_OBD_DEVICES; i++) {
198 struct obd_device *obd = &obd_dev[i];
199 if (obd_uuid_equals(uuid, &obd->obd_uuid))
206 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
210 for (i = 0; i < MAX_OBD_DEVICES; i++) {
211 struct obd_device *obd = &obd_dev[i];
212 if (obd_uuid_equals(uuid, &obd->obd_uuid))
219 void obd_cleanup_caches(void)
224 rc = kmem_cache_destroy(obdo_cachep);
226 CERROR("Cannot destory ll_obdo_cache\n");
230 rc = kmem_cache_destroy(import_cachep);
232 CERROR("Cannot destory ll_import_cache\n");
233 import_cachep = NULL;
238 int obd_init_caches(void)
241 LASSERT(obdo_cachep == NULL);
242 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
247 LASSERT(import_cachep == NULL);
248 import_cachep = kmem_cache_create("ll_import_cache",
249 sizeof(struct obd_import),
256 obd_cleanup_caches();
261 /* map connection to client */
262 struct obd_export *class_conn2export(struct lustre_handle *conn)
264 struct obd_export *export;
268 CDEBUG(D_CACHE, "looking for null handle\n");
272 if (conn->cookie == -1) { /* this means assign a new connection */
273 CDEBUG(D_CACHE, "want a new connection\n");
277 CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
278 export = class_handle2object(conn->cookie);
282 struct obd_device *class_conn2obd(struct lustre_handle *conn)
284 struct obd_export *export;
285 export = class_conn2export(conn);
287 struct obd_device *obd = export->exp_obd;
288 class_export_put(export);
294 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
296 return class_conn2obd(conn)->u.cli.cl_import;
299 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
301 struct obd_export *export;
302 export = class_conn2export(conn);
304 struct obd_import *imp = export->exp_ldlm_data.led_import;
305 class_export_put(export);
312 /* Export management functions */
313 static void export_handle_addref(void *export)
315 class_export_get(export);
318 struct obd_export *class_export_get(struct obd_export *exp)
320 atomic_inc(&exp->exp_refcount);
321 CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp,
322 atomic_read(&exp->exp_refcount));
326 void class_export_put(struct obd_export *exp)
330 CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
331 atomic_read(&exp->exp_refcount) - 1);
332 LASSERT(atomic_read(&exp->exp_refcount) > 0);
333 LASSERT(atomic_read(&exp->exp_refcount) < 0x5a5a5a);
334 if (atomic_dec_and_test(&exp->exp_refcount)) {
335 struct obd_device *obd = exp->exp_obd;
336 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
337 exp->exp_client_uuid.uuid);
339 LASSERT(obd != NULL);
341 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
342 if (exp->exp_connection)
343 ptlrpc_put_connection_superhack(exp->exp_connection);
345 LASSERT(list_empty(&exp->exp_handle.h_link));
347 obd_destroy_export(exp);
349 OBD_FREE(exp, sizeof(*exp));
350 atomic_dec(&obd->obd_refcount);
351 wake_up(&obd->obd_refcount_waitq);
356 struct obd_export *class_new_export(struct obd_device *obddev)
358 struct obd_export *export;
360 OBD_ALLOC(export, sizeof(*export));
362 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
366 atomic_set(&export->exp_refcount, 2);
367 export->exp_obd = obddev;
368 /* XXX this should be in LDLM init */
369 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
371 INIT_LIST_HEAD(&export->exp_handle.h_link);
372 class_handle_hash(&export->exp_handle, export_handle_addref);
373 spin_lock_init(&export->exp_lock);
375 spin_lock(&obddev->obd_dev_lock);
376 LASSERT(!obddev->obd_stopping); /* shouldn't happen, but might race */
377 atomic_inc(&obddev->obd_refcount);
378 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
379 spin_unlock(&obddev->obd_dev_lock);
383 void class_unlink_export(struct obd_export *exp)
385 class_handle_unhash(&exp->exp_handle);
387 spin_lock(&exp->exp_obd->obd_dev_lock);
388 list_del_init(&exp->exp_obd_chain);
389 spin_unlock(&exp->exp_obd->obd_dev_lock);
391 class_export_put(exp);
394 /* Import management functions */
395 static void import_handle_addref(void *import)
397 class_import_get(import);
400 struct obd_import *class_import_get(struct obd_import *import)
402 atomic_inc(&import->imp_refcount);
403 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
404 atomic_read(&import->imp_refcount));
408 void class_import_put(struct obd_import *import)
412 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
413 atomic_read(&import->imp_refcount) - 1);
415 LASSERT(atomic_read(&import->imp_refcount) > 0);
416 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
417 if (!atomic_dec_and_test(&import->imp_refcount)) {
422 CDEBUG(D_IOCTL, "destroying import %p\n", import);
424 ptlrpc_put_connection_superhack(import->imp_connection);
426 LASSERT(list_empty(&import->imp_handle.h_link));
427 OBD_FREE(import, sizeof(*import));
431 struct obd_import *class_new_import(void)
433 struct obd_import *imp;
435 OBD_ALLOC(imp, sizeof(*imp));
439 INIT_LIST_HEAD(&imp->imp_replay_list);
440 INIT_LIST_HEAD(&imp->imp_sending_list);
441 INIT_LIST_HEAD(&imp->imp_delayed_list);
442 spin_lock_init(&imp->imp_lock);
443 imp->imp_max_transno = 0;
444 imp->imp_peer_committed_transno = 0;
446 atomic_set(&imp->imp_refcount, 2);
447 INIT_LIST_HEAD(&imp->imp_handle.h_link);
448 class_handle_hash(&imp->imp_handle, import_handle_addref);
453 void class_destroy_import(struct obd_import *import)
455 LASSERT(import != NULL);
457 class_handle_unhash(&import->imp_handle);
459 /* Abort any inflight DLM requests and NULL out their (about to be
461 ptlrpc_abort_inflight_superhack(import);
463 class_import_put(import);
466 /* a connection defines an export context in which preallocation can
468 int class_connect(struct lustre_handle *exporth, struct obd_device *obd,
469 struct obd_uuid *cluuid)
471 struct obd_export *export;
472 LASSERT(exporth != NULL);
473 LASSERT(obd != NULL);
474 LASSERT(cluuid != NULL);
476 export = class_new_export(obd);
480 exporth->cookie = export->exp_handle.h_cookie;
481 memcpy(&export->exp_client_uuid, cluuid,
482 sizeof(export->exp_client_uuid));
483 class_export_put(export);
485 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
486 cluuid->uuid, exporth->cookie);
490 int class_disconnect(struct lustre_handle *conn, int failover)
492 struct obd_export *export = class_conn2export(conn);
495 if (export == NULL) {
497 CDEBUG(D_IOCTL, "disconnect: attempting to free "
498 "nonexistent client "LPX64"\n", conn->cookie);
502 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", conn->cookie);
504 class_unlink_export(export);
505 class_export_put(export);
509 void class_disconnect_exports(struct obd_device *obd, int failover)
512 struct list_head *tmp, *n, work_list;
513 struct lustre_handle fake_conn;
516 /* Move all of the exports from obd_exports to a work list, en masse. */
517 spin_lock(&obd->obd_dev_lock);
518 list_add(&work_list, &obd->obd_exports);
519 list_del_init(&obd->obd_exports);
520 spin_unlock(&obd->obd_dev_lock);
522 CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
523 "disconnecting them\n", obd->obd_minor, obd);
524 list_for_each_safe(tmp, n, &work_list) {
525 struct obd_export *exp = list_entry(tmp, struct obd_export,
528 class_export_get(exp);
529 fake_conn.cookie = exp->exp_handle.h_cookie;
530 rc = obd_disconnect(&fake_conn, failover);
531 /* exports created from last_rcvd data, and "fake"
532 exports created by lctl don't have an import */
533 if (exp->exp_ldlm_data.led_import != NULL)
534 class_destroy_import(exp->exp_ldlm_data.led_import);
535 class_export_put(exp);
538 CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
541 CDEBUG(D_IOCTL, "export %p disconnected\n", exp);