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 try_module_get(type->typ_ops->o_owner);
89 void class_put_type(struct obd_type *type)
92 module_put(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 struct obd_device *class_name2obd(char *name)
195 int dev = class_name2dev(name);
198 return &obd_dev[dev];
201 int class_uuid2dev(struct obd_uuid *uuid)
205 for (i = 0; i < MAX_OBD_DEVICES; i++) {
206 struct obd_device *obd = &obd_dev[i];
207 if (obd_uuid_equals(uuid, &obd->obd_uuid))
214 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
216 int dev = class_uuid2dev(uuid);
219 return &obd_dev[dev];
222 void obd_cleanup_caches(void)
227 rc = kmem_cache_destroy(obdo_cachep);
229 CERROR("Cannot destory ll_obdo_cache\n");
233 rc = kmem_cache_destroy(import_cachep);
235 CERROR("Cannot destory ll_import_cache\n");
236 import_cachep = NULL;
241 int obd_init_caches(void)
244 LASSERT(obdo_cachep == NULL);
245 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
250 LASSERT(import_cachep == NULL);
251 import_cachep = kmem_cache_create("ll_import_cache",
252 sizeof(struct obd_import),
259 obd_cleanup_caches();
264 /* map connection to client */
265 struct obd_export *class_conn2export(struct lustre_handle *conn)
267 struct obd_export *export;
271 CDEBUG(D_CACHE, "looking for null handle\n");
275 if (conn->cookie == -1) { /* this means assign a new connection */
276 CDEBUG(D_CACHE, "want a new connection\n");
280 CDEBUG(D_IOCTL, "looking for export cookie "LPX64"\n", conn->cookie);
281 export = class_handle2object(conn->cookie);
285 struct obd_device *class_conn2obd(struct lustre_handle *conn)
287 struct obd_export *export;
288 export = class_conn2export(conn);
290 struct obd_device *obd = export->exp_obd;
291 class_export_put(export);
297 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
299 struct obd_device *obd = class_conn2obd(conn);
302 return obd->u.cli.cl_import;
305 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
307 struct obd_export *export;
308 export = class_conn2export(conn);
310 struct obd_import *imp = export->exp_ldlm_data.led_import;
311 class_export_put(export);
318 /* Export management functions */
319 static void export_handle_addref(void *export)
321 class_export_get(export);
324 struct obd_export *class_export_get(struct obd_export *exp)
326 atomic_inc(&exp->exp_refcount);
327 CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp,
328 atomic_read(&exp->exp_refcount));
332 void class_export_put(struct obd_export *exp)
335 LASSERT(exp != NULL);
338 CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", exp,
339 atomic_read(&exp->exp_refcount) - 1);
340 LASSERT(atomic_read(&exp->exp_refcount) > 0);
341 LASSERT(atomic_read(&exp->exp_refcount) < 0x5a5a5a);
342 if (atomic_dec_and_test(&exp->exp_refcount)) {
343 struct obd_device *obd = exp->exp_obd;
344 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
345 exp->exp_client_uuid.uuid);
347 LASSERT(obd != NULL);
349 /* "Local" exports (lctl, LOV->{mdc,osc}) have no connection. */
350 if (exp->exp_connection)
351 ptlrpc_put_connection_superhack(exp->exp_connection);
353 LASSERT(list_empty(&exp->exp_handle.h_link));
355 obd_destroy_export(exp);
357 OBD_FREE(exp, sizeof(*exp));
358 atomic_dec(&obd->obd_refcount);
359 wake_up(&obd->obd_refcount_waitq);
364 struct obd_export *class_new_export(struct obd_device *obd)
366 struct obd_export *export;
368 OBD_ALLOC(export, sizeof(*export));
370 CERROR("no memory! (minor %d)\n", obd->obd_minor);
374 atomic_set(&export->exp_refcount, 2);
375 export->exp_obd = obd;
376 /* XXX this should be in LDLM init */
377 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
379 INIT_LIST_HEAD(&export->exp_handle.h_link);
380 class_handle_hash(&export->exp_handle, export_handle_addref);
381 spin_lock_init(&export->exp_lock);
383 spin_lock(&obd->obd_dev_lock);
384 LASSERT(!obd->obd_stopping); /* shouldn't happen, but might race */
385 atomic_inc(&obd->obd_refcount);
386 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
387 export->exp_obd->obd_num_exports++;
388 spin_unlock(&obd->obd_dev_lock);
392 void class_unlink_export(struct obd_export *exp)
394 class_handle_unhash(&exp->exp_handle);
396 spin_lock(&exp->exp_obd->obd_dev_lock);
397 list_del_init(&exp->exp_obd_chain);
398 exp->exp_obd->obd_num_exports--;
399 spin_unlock(&exp->exp_obd->obd_dev_lock);
401 class_export_put(exp);
404 /* Import management functions */
405 static void import_handle_addref(void *import)
407 class_import_get(import);
410 struct obd_import *class_import_get(struct obd_import *import)
412 atomic_inc(&import->imp_refcount);
413 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
414 atomic_read(&import->imp_refcount));
418 void class_import_put(struct obd_import *import)
422 CDEBUG(D_IOCTL, "import %p refcount=%d\n", import,
423 atomic_read(&import->imp_refcount) - 1);
425 LASSERT(atomic_read(&import->imp_refcount) > 0);
426 LASSERT(atomic_read(&import->imp_refcount) < 0x5a5a5a);
427 if (!atomic_dec_and_test(&import->imp_refcount)) {
432 CDEBUG(D_IOCTL, "destroying import %p\n", import);
434 ptlrpc_put_connection_superhack(import->imp_connection);
436 LASSERT(list_empty(&import->imp_handle.h_link));
437 OBD_FREE(import, sizeof(*import));
441 struct obd_import *class_new_import(void)
443 struct obd_import *imp;
445 OBD_ALLOC(imp, sizeof(*imp));
449 INIT_LIST_HEAD(&imp->imp_replay_list);
450 INIT_LIST_HEAD(&imp->imp_sending_list);
451 INIT_LIST_HEAD(&imp->imp_delayed_list);
452 spin_lock_init(&imp->imp_lock);
453 imp->imp_max_transno = 0;
454 imp->imp_peer_committed_transno = 0;
456 atomic_set(&imp->imp_refcount, 2);
457 INIT_LIST_HEAD(&imp->imp_handle.h_link);
458 class_handle_hash(&imp->imp_handle, import_handle_addref);
463 void class_destroy_import(struct obd_import *import)
465 LASSERT(import != NULL);
467 class_handle_unhash(&import->imp_handle);
469 /* Abort any inflight DLM requests and NULL out their (about to be
471 /* Invalidate all requests on import, would be better to call
472 ptlrpc_set_import_active(imp, 0); */
473 import->imp_generation++;
474 ptlrpc_abort_inflight_superhack(import);
476 class_import_put(import);
479 /* a connection defines an export context in which preallocation can
481 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
482 struct obd_uuid *cluuid)
484 struct obd_export *export;
485 LASSERT(conn != NULL);
486 LASSERT(obd != NULL);
487 LASSERT(cluuid != NULL);
489 export = class_new_export(obd);
493 conn->cookie = export->exp_handle.h_cookie;
494 memcpy(&export->exp_client_uuid, cluuid,
495 sizeof(export->exp_client_uuid));
496 class_export_put(export);
498 CDEBUG(D_IOCTL, "connect: client %s, cookie "LPX64"\n",
499 cluuid->uuid, conn->cookie);
503 int class_disconnect(struct lustre_handle *conn, int flags)
505 struct obd_export *export = class_conn2export(conn);
508 if (export == NULL) {
510 CDEBUG(D_IOCTL, "disconnect: attempting to free "
511 "nonexistent client "LPX64"\n", conn->cookie);
515 CDEBUG(D_IOCTL, "disconnect: cookie "LPX64"\n", conn->cookie);
517 class_unlink_export(export);
518 class_export_put(export);
522 void class_disconnect_exports(struct obd_device *obd, int flags)
525 struct list_head *tmp, *n, work_list;
526 struct lustre_handle fake_conn;
529 /* Move all of the exports from obd_exports to a work list, en masse. */
530 spin_lock(&obd->obd_dev_lock);
531 list_add(&work_list, &obd->obd_exports);
532 list_del_init(&obd->obd_exports);
533 spin_unlock(&obd->obd_dev_lock);
535 CDEBUG(D_IOCTL, "OBD device %d (%p) has exports, "
536 "disconnecting them\n", obd->obd_minor, obd);
537 list_for_each_safe(tmp, n, &work_list) {
538 struct obd_export *exp = list_entry(tmp, struct obd_export,
541 class_export_get(exp);
542 fake_conn.cookie = exp->exp_handle.h_cookie;
543 rc = obd_disconnect(&fake_conn, flags);
544 /* exports created from last_rcvd data, and "fake"
545 exports created by lctl don't have an import */
546 if (exp->exp_ldlm_data.led_import != NULL)
547 class_destroy_import(exp->exp_ldlm_data.led_import);
548 class_export_put(exp);
551 CDEBUG(D_IOCTL, "disconnecting export %p failed: %d\n",
554 CDEBUG(D_IOCTL, "export %p disconnected\n", exp);