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 kmem_cache_t *obdo_cachep = NULL;
41 kmem_cache_t *import_cachep = NULL;
42 kmem_cache_t *export_cachep = NULL;
44 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
45 void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp,
49 * support functions: we could use inter-module communication, but this
50 * is more portable to other OS's
52 static struct obd_type *class_search_type(char *name)
54 struct list_head *tmp;
55 struct obd_type *type;
56 CDEBUG(D_INFO, "SEARCH %s\n", name);
59 list_for_each(tmp, &obd_types) {
60 type = list_entry(tmp, struct obd_type, typ_chain);
61 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
62 if (strlen(type->typ_name) == strlen(name) &&
63 strcmp(type->typ_name, name) == 0) {
70 struct obd_type *class_get_type(char *name)
72 struct obd_type *type = class_search_type(name);
76 if (!request_module(name)) {
77 CDEBUG(D_INFO, "Loaded module '%s'\n", name);
78 type = class_search_type(name);
80 CDEBUG(D_INFO, "Can't load module '%s'\n", name);
84 __MOD_INC_USE_COUNT(type->typ_ops->o_owner);
88 void class_put_type(struct obd_type *type)
91 __MOD_DEC_USE_COUNT(type->typ_ops->o_owner);
94 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *vars,
97 struct obd_type *type;
101 LASSERT(strnlen(name, 1024) < 1024); /* sanity check */
103 if (class_search_type(name)) {
104 CDEBUG(D_IOCTL, "Type %s already registered\n", name);
109 OBD_ALLOC(type, sizeof(*type));
113 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
114 OBD_ALLOC(type->typ_name, strlen(name) + 1);
115 if (type->typ_ops == NULL || type->typ_name == NULL)
118 *(type->typ_ops) = *ops;
119 strcpy(type->typ_name, name);
120 list_add(&type->typ_chain, &obd_types);
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;
127 list_del(&type->typ_chain);
134 if (type->typ_ops != NULL)
135 OBD_FREE(type->typ_name, strlen(name) + 1);
136 if (type->typ_ops != NULL)
137 OBD_FREE (type->typ_ops, sizeof (*type->typ_ops));
141 int class_unregister_type(char *name)
143 struct obd_type *type = class_search_type(name);
147 CERROR("unknown obd type\n");
151 if (type->typ_refcnt) {
152 CERROR("type %s has refcount (%d)\n", name, type->typ_refcnt);
153 /* This is a bad situation, let's make the best of it */
154 /* Remove ops, but leave the name for debugging */
155 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
159 if (type->typ_procroot) {
160 lprocfs_remove(type->typ_procroot);
161 type->typ_procroot = NULL;
164 list_del(&type->typ_chain);
165 OBD_FREE(type->typ_name, strlen(name) + 1);
166 if (type->typ_ops != NULL)
167 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
168 OBD_FREE(type, sizeof(*type));
170 } /* class_unregister_type */
172 int class_name2dev(char *name)
180 for (i = 0; i < MAX_OBD_DEVICES; i++) {
181 struct obd_device *obd = &obd_dev[i];
182 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
191 int class_uuid2dev(struct obd_uuid *uuid)
196 for (i = 0; i < MAX_OBD_DEVICES; i++) {
197 struct obd_device *obd = &obd_dev[i];
198 if (strncmp(uuid->uuid, obd->obd_uuid.uuid, sizeof(obd->obd_uuid.uuid)) == 0) {
208 struct obd_device *class_uuid2obd(struct obd_uuid *uuid)
212 for (i = 0; i < MAX_OBD_DEVICES; i++) {
213 struct obd_device *obd = &obd_dev[i];
214 if (strncmp(uuid->uuid, obd->obd_uuid.uuid, sizeof(obd->obd_uuid.uuid)) == 0)
221 void obd_cleanup_caches(void)
226 rc = kmem_cache_destroy(obdo_cachep);
228 CERROR("Cannot destory ll_obdo_cache\n");
232 rc = kmem_cache_destroy(import_cachep);
234 CERROR("Cannot destory ll_import_cache\n");
235 import_cachep = NULL;
238 rc = kmem_cache_destroy(export_cachep);
240 CERROR("Cannot destory ll_export_cache\n");
241 export_cachep = NULL;
246 int obd_init_caches(void)
249 LASSERT(obdo_cachep == NULL);
250 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
255 LASSERT(export_cachep == NULL);
256 export_cachep = kmem_cache_create("ll_export_cache",
257 sizeof(struct obd_export),
262 LASSERT(import_cachep == NULL);
263 import_cachep = kmem_cache_create("ll_import_cache",
264 sizeof(struct obd_import),
271 obd_cleanup_caches();
276 /* map connection to client */
277 struct obd_export *class_conn2export(struct lustre_handle *conn)
279 struct obd_export *export;
283 CDEBUG(D_CACHE, "looking for null handle\n");
287 if (conn->addr == -1) { /* this means assign a new connection */
288 CDEBUG(D_CACHE, "want a new connection\n");
293 CDEBUG(D_CACHE, "looking for null addr\n");
298 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
299 conn->addr, conn->cookie);
300 export = (struct obd_export *) (unsigned long)conn->addr;
301 if (!kmem_cache_validate(export_cachep, (void *)export))
304 if (export->exp_cookie != conn->cookie)
307 } /* class_conn2export */
309 struct obd_device *class_conn2obd(struct lustre_handle *conn)
311 struct obd_export *export;
312 export = class_conn2export(conn);
314 return export->exp_obd;
319 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
321 return &class_conn2obd(conn)->u.cli.cl_import;
324 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
326 return &class_conn2export(conn)->exp_ldlm_data.led_import;
329 struct obd_export *class_new_export(struct obd_device *obddev)
331 struct obd_export *export;
333 PORTAL_SLAB_ALLOC(export, export_cachep, sizeof(*export));
335 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
339 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
340 export->exp_obd = obddev;
341 /* XXX this should be in LDLM init */
342 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
343 INIT_LIST_HEAD(&export->exp_conn_chain);
344 spin_lock(&obddev->obd_dev_lock);
345 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
346 spin_unlock(&obddev->obd_dev_lock);
350 void class_destroy_export(struct obd_export *exp)
352 LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
354 CDEBUG(D_IOCTL, "destroying export %p/%s\n", exp,
355 exp->exp_client_uuid.uuid);
357 spin_lock(&exp->exp_obd->obd_dev_lock);
358 list_del(&exp->exp_obd_chain);
359 spin_unlock(&exp->exp_obd->obd_dev_lock);
361 /* XXXshaver no connection here... */
362 if (exp->exp_connection)
363 spin_lock(&exp->exp_connection->c_lock);
364 list_del(&exp->exp_conn_chain);
365 if (exp->exp_connection) {
366 spin_unlock(&exp->exp_connection->c_lock);
367 ptlrpc_put_connection_superhack(exp->exp_connection);
370 /* Abort any inflight DLM requests and NULL out their (about to be
372 if (exp->exp_ldlm_data.led_import.imp_obd)
373 ptlrpc_abort_inflight_superhack(&exp->exp_ldlm_data.led_import,
376 PORTAL_SLAB_FREE(exp, export_cachep, sizeof(*exp));
379 /* a connection defines an export context in which preallocation can
381 int class_connect(struct lustre_handle *exporth, struct obd_device *obd,
382 struct obd_uuid *cluuid)
384 struct obd_export * export;
385 if (exporth == NULL) {
395 if (cluuid == NULL) {
400 export = class_new_export(obd);
404 exporth->addr = (__u64) (unsigned long)export;
405 exporth->cookie = export->exp_cookie;
406 memcpy(&export->exp_client_uuid, cluuid, sizeof(export->exp_client_uuid));
408 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
409 (long long)exporth->addr, (long long)exporth->cookie);
413 int class_disconnect(struct lustre_handle *conn)
415 struct obd_export *export;
418 if (!(export = class_conn2export(conn))) {
420 CDEBUG(D_IOCTL, "disconnect: attempting to free "
421 "nonexistent client "LPX64"\n", conn->addr);
425 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
426 (long long)conn->addr, (long long)conn->cookie);
428 class_destroy_export(export);
433 void class_disconnect_all(struct obd_device *obddev)
438 spin_lock(&obddev->obd_dev_lock);
439 if (!list_empty(&obddev->obd_exports)) {
440 struct obd_export *export;
441 struct lustre_handle conn;
444 export = list_entry(obddev->obd_exports.next,
447 conn.addr = (__u64)(unsigned long)export;
448 conn.cookie = export->exp_cookie;
449 spin_unlock(&obddev->obd_dev_lock);
450 CERROR("force disconnecting %s:%s export %p\n",
451 export->exp_obd->obd_type->typ_name,
452 export->exp_connection ?
453 (char *)export->exp_connection->c_remote_uuid.uuid :
454 "<unconnected>", export);
455 rc = obd_disconnect(&conn);
457 /* AED: not so sure about this... We can't
458 * loop here forever, yet we shouldn't leak
459 * exports on a struct we will soon destroy.
461 CERROR("destroy export %p with err: rc = %d\n",
463 class_destroy_export(export);
466 spin_unlock(&obddev->obd_dev_lock);
474 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
476 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
482 for (p = data, count = 0; p < (char *)data + len; count++) {
484 int tmp = simple_strtoul(p, &end, 0);
487 CERROR("invalid device ID starting at: %s\n", p);
488 GOTO(err_disconnect, rc = -EINVAL);
491 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
492 CERROR("Trying to sub dev %d - dev no too large\n",
494 GOTO(err_disconnect, rc = -EINVAL);
497 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
499 CERROR("cannot connect to device %d: rc = %d\n", tmp,
501 GOTO(err_disconnect, rc);
504 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
505 obd_dev[tmp].obd_type->typ_name);
510 obddev->obd_multi_count = count;
515 for (count--; count >= 0; count--)
516 obd_disconnect(&obddev->obd_multi_conn[count]);
521 * remove all connections to this device
522 * close all connections to lower devices
523 * needed for forced unloads of OBD client drivers
525 int class_multi_cleanup(struct obd_device *obddev)
529 for (i = 0; i < obddev->obd_multi_count; i++) {
531 struct obd_device *obd =
532 class_conn2obd(&obddev->obd_multi_conn[i]);
535 CERROR("no such device [i %d]\n", i);
539 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
541 CERROR("disconnect failure %d\n", obd->obd_minor);