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;
39 LASSERT(desc->bd_connection);
41 desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
42 desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
43 if (desc->bd_connection && class_signal_connection_failure) {
44 class_signal_connection_failure(desc->bd_connection);
46 /* We go back to sleep, until we're resumed or interrupted. */
50 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
54 static int sync_io_intr(void *data)
56 struct io_cb_data *cbd = data;
57 struct ptlrpc_bulk_desc *desc = cbd->desc;
60 desc->bd_flags |= PTL_RPC_FL_INTR;
61 RETURN(1); /* ignored, as of this writing */
64 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
69 if (phase == CB_PHASE_START) {
70 struct l_wait_info lwi;
71 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
73 ret = l_wait_event(data->waitq, data->complete, &lwi);
74 if (atomic_dec_and_test(&data->refcount))
75 OBD_FREE(data, sizeof(*data));
76 if (ret == -ERESTARTSYS)
78 } else if (phase == CB_PHASE_FINISH) {
81 wake_up(&data->waitq);
82 if (atomic_dec_and_test(&data->refcount))
83 OBD_FREE(data, sizeof(*data));
91 struct io_cb_data *ll_init_cb(void)
95 OBD_ALLOC(d, sizeof(*d));
97 init_waitqueue_head(&d->waitq);
98 atomic_set(&d->refcount, 2);
104 * support functions: we could use inter-module communication, but this
105 * is more portable to other OS's
107 static struct obd_type *class_search_type(char *nm)
109 struct list_head *tmp;
110 struct obd_type *type;
111 CDEBUG(D_INFO, "SEARCH %s\n", nm);
114 list_for_each(tmp, &obd_types) {
115 type = list_entry(tmp, struct obd_type, typ_chain);
116 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
117 if (strlen(type->typ_name) == strlen(nm) &&
118 strcmp(type->typ_name, nm) == 0 ) {
125 struct obd_type *class_nm_to_type(char *nm)
127 struct obd_type *type = class_search_type(nm);
131 if ( !request_module(nm) ) {
132 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
133 type = class_search_type(nm);
135 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
142 int class_register_type(struct obd_ops *ops, char *nm)
144 struct obd_type *type;
148 if (class_search_type(nm)) {
149 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
153 OBD_ALLOC(type, sizeof(*type));
154 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
155 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
158 INIT_LIST_HEAD(&type->typ_chain);
160 list_add(&type->typ_chain, &obd_types);
161 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
162 strcpy(type->typ_name, nm);
166 int class_unregister_type(char *nm)
168 struct obd_type *type = class_nm_to_type(nm);
173 CERROR("unknown obd type\n");
177 if (type->typ_refcnt) {
178 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
179 /* This is a bad situation, let's make the best of it */
180 /* Remove ops, but leave the name for debugging */
181 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
185 list_del(&type->typ_chain);
186 OBD_FREE(type->typ_name, strlen(nm) + 1);
187 if (type->typ_ops != NULL)
188 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
189 OBD_FREE(type, sizeof(*type));
192 } /* class_unregister_type */
194 int class_name2dev(char *name)
202 for (i=0; i < MAX_OBD_DEVICES; i++) {
203 struct obd_device *obd = &obd_dev[i];
204 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
213 int class_uuid2dev(char *uuid)
218 for (i=0; i < MAX_OBD_DEVICES; i++) {
219 struct obd_device *obd = &obd_dev[i];
220 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
230 struct obd_device *class_uuid2obd(char *uuid)
234 for (i=0; i < MAX_OBD_DEVICES; i++) {
235 struct obd_device *obd = &obd_dev[i];
236 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
243 void obd_cleanup_caches(void)
248 rc = kmem_cache_destroy(obdo_cachep);
250 CERROR("Cannot destory obdo_cachep\n");
254 rc = kmem_cache_destroy(import_cachep);
256 CERROR("Cannot destory import_cachep\n");
257 import_cachep = NULL;
260 rc = kmem_cache_destroy(export_cachep);
262 CERROR("Cannot destory import_cachep\n");
263 export_cachep = NULL;
268 int obd_init_caches(void)
271 if (obdo_cachep == NULL) {
272 obdo_cachep = kmem_cache_create("obdo_cache",
274 0, SLAB_HWCACHE_ALIGN,
276 if (obdo_cachep == NULL)
280 if (export_cachep == NULL) {
281 export_cachep = kmem_cache_create("export_cache",
282 sizeof(struct obd_export),
283 0, SLAB_HWCACHE_ALIGN,
285 if (export_cachep == NULL)
289 if (import_cachep == NULL) {
290 import_cachep = kmem_cache_create("import_cache",
291 sizeof(struct obd_import),
292 0, SLAB_HWCACHE_ALIGN,
294 if (import_cachep == NULL)
299 obd_cleanup_caches();
304 /* map connection to client */
305 struct obd_export *class_conn2export(struct lustre_handle *conn)
307 struct obd_export *export;
310 CDEBUG(D_CACHE, "looking for null handle\n");
314 if (conn->addr == -1) { /* this means assign a new connection */
315 CDEBUG(D_CACHE, "want a new connection\n");
320 CDEBUG(D_CACHE, "looking for null addr\n");
325 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
326 conn->addr, conn->cookie);
327 export = (struct obd_export *) (unsigned long)conn->addr;
328 if (!kmem_cache_validate(export_cachep, (void *)export))
331 if (export->exp_cookie != conn->cookie)
334 } /* class_conn2export */
336 struct obd_device *class_conn2obd(struct lustre_handle *conn)
338 struct obd_export *export;
339 export = class_conn2export(conn);
341 return export->exp_obd;
346 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
348 return &class_conn2obd(conn)->u.cli.cl_import;
351 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
353 return &class_conn2export(conn)->exp_ldlm_data.led_import;
356 struct obd_export *class_new_export(struct obd_device *obddev)
358 struct obd_export * export;
360 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
362 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
366 memset(export, 0, sizeof(*export));
367 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
368 export->exp_obd = obddev;
369 /* XXX should these be in MDS and LDLM init functions? */
370 INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
371 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
372 INIT_LIST_HEAD(&export->exp_conn_chain);
373 spin_lock(&obddev->obd_dev_lock);
374 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
375 spin_unlock(&obddev->obd_dev_lock);
379 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)
389 spin_lock(&exp->exp_connection->c_lock);
390 list_del(&exp->exp_conn_chain);
391 if (exp->exp_connection) {
392 spin_unlock(&exp->exp_connection->c_lock);
393 //ptlrpc_put_connection(exp->exp_connection);
396 kmem_cache_free(export_cachep, exp);
401 /* a connection defines an export context in which preallocation can
403 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
406 struct obd_export * export;
417 export = class_new_export(obd);
422 conn->addr = (__u64) (unsigned long)export;
423 conn->cookie = export->exp_cookie;
425 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
426 (long long)conn->addr, (long long)conn->cookie);
430 int class_disconnect(struct lustre_handle *conn)
432 struct obd_export *export;
435 if (!(export = class_conn2export(conn))) {
437 CDEBUG(D_IOCTL, "disconnect: attempting to free "
438 "nonexistent client "LPX64"\n", conn->addr);
442 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
443 (long long)conn->addr, (long long)conn->cookie);
445 class_destroy_export(export);
450 void class_disconnect_all(struct obd_device *obddev)
455 spin_lock(&obddev->obd_dev_lock);
456 if (!list_empty(&obddev->obd_exports)) {
457 struct obd_export *export;
458 struct lustre_handle conn;
461 export = list_entry(obddev->obd_exports.next,
464 conn.addr = (__u64)(unsigned long)export;
465 conn.cookie = export->exp_cookie;
466 spin_unlock(&obddev->obd_dev_lock);
467 CERROR("force disconnecting export %p\n", export);
468 rc = obd_disconnect(&conn);
470 /* AED: not so sure about this... We can't
471 * loop here forever, yet we shouldn't leak
472 * exports on a struct we will soon destroy.
474 CERROR("destroy export %p with err: rc = %d\n",
476 class_destroy_export(export);
479 spin_unlock(&obddev->obd_dev_lock);
487 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
489 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
495 for (p = data, count = 0; p < (char *)data + len; count++) {
497 int tmp = simple_strtoul(p, &end, 0);
500 CERROR("invalid device ID starting at: %s\n", p);
501 GOTO(err_disconnect, rc = -EINVAL);
504 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
505 CERROR("Trying to sub dev %d - dev no too large\n",
507 GOTO(err_disconnect, rc = -EINVAL);
510 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
512 CERROR("cannot connect to device %d: rc = %d\n", tmp,
514 GOTO(err_disconnect, rc);
517 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
518 obd_dev[tmp].obd_type->typ_name);
523 obddev->obd_multi_count = count;
528 for (count--; count >= 0; count--)
529 obd_disconnect(&obddev->obd_multi_conn[count]);
534 * remove all connections to this device
535 * close all connections to lower devices
536 * needed for forced unloads of OBD client drivers
538 int class_multi_cleanup(struct obd_device *obddev)
542 for (i = 0; i < obddev->obd_multi_count; i++) {
544 struct obd_device *obd =
545 class_conn2obd(&obddev->obd_multi_conn[i]);
548 CERROR("no such device [i %d]\n", i);
552 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
554 CERROR("disconnect failure %d\n", obd->obd_minor);