1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * lustre/obdclass/genops.c
5 * Copyright (C) 2001-2002 Cluster File Systems, Inc.
7 * This code is issued under the GNU General Public License.
8 * See the file COPYING in this distribution
10 * These are the only exported functions, they provide some generic
11 * infrastructure for managing object devices
15 #define DEBUG_SUBSYSTEM S_CLASS
16 #include <linux/kmod.h> /* for request_module() */
17 #include <linux/module.h>
18 #include <linux/obd_class.h>
19 #include <linux/random.h>
20 #include <linux/slab.h>
22 extern struct list_head obd_types;
23 kmem_cache_t *obdo_cachep = NULL;
24 kmem_cache_t *export_cachep = NULL;
25 kmem_cache_t *import_cachep = NULL;
27 /* I would prefer if these next four functions were in ptlrpc, to be honest,
28 * but obdclass uses them for the netregression ioctls. -phil */
29 static int sync_io_timeout(void *data)
31 struct io_cb_data *cbd = data;
32 struct ptlrpc_bulk_desc *desc = cbd->desc;
35 desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
36 desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
37 if (desc->bd_connection && class_signal_connection_failure) {
39 /* XXXshaver Do we need a resend strategy, or do we just
40 * XXXshaver return -ERESTARTSYS and punt it?
42 CERROR("signalling failure of conn %p\n", desc->bd_connection);
43 class_signal_connection_failure(desc->bd_connection);
45 /* We go back to sleep, until we're resumed or interrupted. */
49 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
53 static int sync_io_intr(void *data)
55 struct io_cb_data *cbd = data;
56 struct ptlrpc_bulk_desc *desc = cbd->desc;
59 desc->bd_flags |= PTL_RPC_FL_INTR;
60 RETURN(1); /* ignored, as of this writing */
63 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
68 if (phase == CB_PHASE_START) {
69 struct l_wait_info lwi;
70 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
72 ret = l_wait_event(data->waitq, data->complete, &lwi);
73 if (atomic_dec_and_test(&data->refcount))
74 OBD_FREE(data, sizeof(*data));
75 if (ret == -ERESTARTSYS)
77 } else if (phase == CB_PHASE_FINISH) {
80 wake_up(&data->waitq);
81 if (atomic_dec_and_test(&data->refcount))
82 OBD_FREE(data, sizeof(*data));
90 struct io_cb_data *ll_init_cb(void)
94 OBD_ALLOC(d, sizeof(*d));
96 init_waitqueue_head(&d->waitq);
97 atomic_set(&d->refcount, 2);
103 * support functions: we could use inter-module communication, but this
104 * is more portable to other OS's
106 static struct obd_type *class_search_type(char *nm)
108 struct list_head *tmp;
109 struct obd_type *type;
110 CDEBUG(D_INFO, "SEARCH %s\n", nm);
113 list_for_each(tmp, &obd_types) {
114 type = list_entry(tmp, struct obd_type, typ_chain);
115 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
116 if (strlen(type->typ_name) == strlen(nm) &&
117 strcmp(type->typ_name, nm) == 0 ) {
124 struct obd_type *class_nm_to_type(char *nm)
126 struct obd_type *type = class_search_type(nm);
130 if ( !request_module(nm) ) {
131 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
132 type = class_search_type(nm);
134 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
141 int class_register_type(struct obd_ops *ops, char *nm)
143 struct obd_type *type;
147 if (class_search_type(nm)) {
148 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
152 OBD_ALLOC(type, sizeof(*type));
153 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
154 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
157 INIT_LIST_HEAD(&type->typ_chain);
159 list_add(&type->typ_chain, &obd_types);
160 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
161 strcpy(type->typ_name, nm);
165 int class_unregister_type(char *nm)
167 struct obd_type *type = class_nm_to_type(nm);
172 CERROR("unknown obd type\n");
176 if (type->typ_refcnt) {
177 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
178 /* This is a bad situation, let's make the best of it */
179 /* Remove ops, but leave the name for debugging */
180 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
184 list_del(&type->typ_chain);
185 OBD_FREE(type->typ_name, strlen(nm) + 1);
186 if (type->typ_ops != NULL)
187 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
188 OBD_FREE(type, sizeof(*type));
191 } /* class_unregister_type */
193 int class_name2dev(char *name)
201 for (i=0; i < MAX_OBD_DEVICES; i++) {
202 struct obd_device *obd = &obd_dev[i];
203 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
212 int class_uuid2dev(char *uuid)
217 for (i=0; i < MAX_OBD_DEVICES; i++) {
218 struct obd_device *obd = &obd_dev[i];
219 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
229 struct obd_device *class_uuid2obd(char *uuid)
233 for (i=0; i < MAX_OBD_DEVICES; i++) {
234 struct obd_device *obd = &obd_dev[i];
235 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
242 void obd_cleanup_caches(void)
247 rc = kmem_cache_destroy(obdo_cachep);
249 CERROR("Cannot destory obdo_cachep\n");
253 rc = kmem_cache_destroy(import_cachep);
255 CERROR("Cannot destory import_cachep\n");
256 import_cachep = NULL;
259 rc = kmem_cache_destroy(export_cachep);
261 CERROR("Cannot destory import_cachep\n");
262 export_cachep = NULL;
267 int obd_init_caches(void)
270 if (obdo_cachep == NULL) {
271 obdo_cachep = kmem_cache_create("obdo_cache",
273 0, SLAB_HWCACHE_ALIGN,
275 if (obdo_cachep == NULL)
279 if (export_cachep == NULL) {
280 export_cachep = kmem_cache_create("export_cache",
281 sizeof(struct obd_export),
282 0, SLAB_HWCACHE_ALIGN,
284 if (export_cachep == NULL)
288 if (import_cachep == NULL) {
289 import_cachep = kmem_cache_create("import_cache",
290 sizeof(struct obd_import),
291 0, SLAB_HWCACHE_ALIGN,
293 if (import_cachep == NULL)
298 obd_cleanup_caches();
303 /* map connection to client */
304 struct obd_export *class_conn2export(struct lustre_handle *conn)
306 struct obd_export *export;
309 CDEBUG(D_CACHE, "looking for null handle\n");
313 if (conn->addr == -1) { /* this means assign a new connection */
314 CDEBUG(D_CACHE, "want a new connection\n");
319 CDEBUG(D_CACHE, "looking for null addr\n");
324 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
325 conn->addr, conn->cookie);
326 export = (struct obd_export *) (unsigned long)conn->addr;
327 if (!kmem_cache_validate(export_cachep, (void *)export))
330 if (export->exp_cookie != conn->cookie)
333 } /* class_conn2export */
335 struct obd_device *class_conn2obd(struct lustre_handle *conn)
337 struct obd_export *export;
338 export = class_conn2export(conn);
340 return export->exp_obd;
345 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
347 return &class_conn2obd(conn)->u.cli.cl_import;
350 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
352 return &class_conn2export(conn)->exp_ldlm_data.led_import;
355 struct obd_export *class_new_export(struct obd_device *obddev)
357 struct obd_export * export;
359 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
361 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
365 memset(export, 0, sizeof(*export));
366 get_random_bytes(&export->exp_cookie, sizeof(__u64));
367 export->exp_obd = obddev;
368 /* XXX should these be in MDS and LDLM init functions? */
369 INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
370 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
371 INIT_LIST_HEAD(&export->exp_conn_chain);
372 spin_lock(&obddev->obd_dev_lock);
373 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
374 spin_unlock(&obddev->obd_dev_lock);
378 void class_destroy_export(struct obd_export *exp)
383 spin_lock(&exp->exp_obd->obd_dev_lock);
384 list_del(&exp->exp_obd_chain);
385 spin_unlock(&exp->exp_obd->obd_dev_lock);
387 /* XXXshaver no connection here... */
388 if (exp->exp_connection) spin_lock(&exp->exp_connection->c_lock);
389 list_del(&exp->exp_conn_chain);
390 if (exp->exp_connection) spin_unlock(&exp->exp_connection->c_lock);
392 /* XXXshaver these bits want to be hung off the export, instead of
393 * XXXshaver hard-coded here.
395 if (mds_destroy_export) {
396 rc = mds_destroy_export(exp);
398 CERROR("error freeing mds client data: rc = %d\n", rc);
400 if (ldlm_destroy_export) {
401 rc = ldlm_destroy_export(exp);
403 CERROR("error freeing dlm client data: rc = %d\n", rc);
405 kmem_cache_free(export_cachep, exp);
410 /* a connection defines an export context in which preallocation can
412 int class_connect (struct lustre_handle *conn, struct obd_device *obd,
415 struct obd_export * export;
426 export = class_new_export(obd);
431 conn->addr = (__u64) (unsigned long)export;
432 conn->cookie = export->exp_cookie;
434 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
435 (long long)conn->addr, (long long)conn->cookie);
439 int class_disconnect(struct lustre_handle *conn)
441 struct obd_export *export;
444 if (!(export = class_conn2export(conn))) {
446 CDEBUG(D_IOCTL, "disconnect: attempting to free "
447 "nonexistent client "LPX64"\n", conn->addr);
451 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
452 (long long)conn->addr, (long long)conn->cookie);
454 class_destroy_export(export);
459 void class_disconnect_all(struct obd_device *obddev)
464 spin_lock(&obddev->obd_dev_lock);
465 if (!list_empty(&obddev->obd_exports)) {
466 struct obd_export *export;
467 struct lustre_handle conn;
470 export = list_entry(obddev->obd_exports.next,
473 conn.addr = (__u64)(unsigned long)export;
474 conn.cookie = export->exp_cookie;
475 spin_unlock(&obddev->obd_dev_lock);
476 CERROR("force disconnecting export %p\n", export);
477 rc = obd_disconnect(&conn);
479 /* AED: not so sure about this... We can't
480 * loop here forever, yet we shouldn't leak
481 * exports on a struct we will soon destroy.
483 CERROR("destroy export %p with err: rc = %d\n",
485 class_destroy_export(export);
488 spin_unlock(&obddev->obd_dev_lock);
496 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
498 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
504 for (p = data, count = 0; p < (char *)data + len; count++) {
506 int tmp = simple_strtoul(p, &end, 0);
509 CERROR("invalid device ID starting at: %s\n", p);
510 GOTO(err_disconnect, rc = -EINVAL);
513 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
514 CERROR("Trying to sub dev %d - dev no too large\n",
516 GOTO(err_disconnect, rc = -EINVAL);
519 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
521 CERROR("cannot connect to device %d: rc = %d\n", tmp,
523 GOTO(err_disconnect, rc);
526 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
527 obd_dev[tmp].obd_type->typ_name);
532 obddev->obd_multi_count = count;
537 for (count--; count >= 0; count--)
538 obd_disconnect(&obddev->obd_multi_conn[count]);
543 * remove all connections to this device
544 * close all connections to lower devices
545 * needed for forced unloads of OBD client drivers
547 int class_multi_cleanup(struct obd_device *obddev)
551 for (i = 0; i < obddev->obd_multi_count; i++) {
553 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
556 CERROR("no such device [i %d]\n", i);
560 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
562 CERROR("disconnect failure %d\n", obd->obd_minor);