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) {
38 class_signal_connection_failure(desc->bd_connection);
40 /* We go back to sleep, until we're resumed or interrupted. */
44 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
48 static int sync_io_intr(void *data)
50 struct io_cb_data *cbd = data;
51 struct ptlrpc_bulk_desc *desc = cbd->desc;
54 desc->bd_flags |= PTL_RPC_FL_INTR;
55 RETURN(1); /* ignored, as of this writing */
58 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
63 if (phase == CB_PHASE_START) {
64 struct l_wait_info lwi;
65 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
67 ret = l_wait_event(data->waitq, data->complete, &lwi);
68 if (atomic_dec_and_test(&data->refcount))
69 OBD_FREE(data, sizeof(*data));
70 if (ret == -ERESTARTSYS)
72 } else if (phase == CB_PHASE_FINISH) {
75 wake_up(&data->waitq);
76 if (atomic_dec_and_test(&data->refcount))
77 OBD_FREE(data, sizeof(*data));
85 struct io_cb_data *ll_init_cb(void)
89 OBD_ALLOC(d, sizeof(*d));
91 init_waitqueue_head(&d->waitq);
92 atomic_set(&d->refcount, 2);
98 * support functions: we could use inter-module communication, but this
99 * is more portable to other OS's
101 static struct obd_type *class_search_type(char *nm)
103 struct list_head *tmp;
104 struct obd_type *type;
105 CDEBUG(D_INFO, "SEARCH %s\n", nm);
108 list_for_each(tmp, &obd_types) {
109 type = list_entry(tmp, struct obd_type, typ_chain);
110 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
111 if (strlen(type->typ_name) == strlen(nm) &&
112 strcmp(type->typ_name, nm) == 0 ) {
119 struct obd_type *class_nm_to_type(char *nm)
121 struct obd_type *type = class_search_type(nm);
125 if ( !request_module(nm) ) {
126 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
127 type = class_search_type(nm);
129 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
136 int class_register_type(struct obd_ops *ops, char *nm)
138 struct obd_type *type;
142 if (class_search_type(nm)) {
143 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
147 OBD_ALLOC(type, sizeof(*type));
148 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
149 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
152 INIT_LIST_HEAD(&type->typ_chain);
154 list_add(&type->typ_chain, &obd_types);
155 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
156 strcpy(type->typ_name, nm);
160 int class_unregister_type(char *nm)
162 struct obd_type *type = class_nm_to_type(nm);
167 CERROR("unknown obd type\n");
171 if (type->typ_refcnt) {
172 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
173 /* This is a bad situation, let's make the best of it */
174 /* Remove ops, but leave the name for debugging */
175 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
179 list_del(&type->typ_chain);
180 OBD_FREE(type->typ_name, strlen(nm) + 1);
181 if (type->typ_ops != NULL)
182 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
183 OBD_FREE(type, sizeof(*type));
186 } /* class_unregister_type */
188 int class_name2dev(char *name)
196 for (i=0; i < MAX_OBD_DEVICES; i++) {
197 struct obd_device *obd = &obd_dev[i];
198 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
207 int class_uuid2dev(char *uuid)
212 for (i=0; i < MAX_OBD_DEVICES; i++) {
213 struct obd_device *obd = &obd_dev[i];
214 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
224 struct obd_device *class_uuid2obd(char *uuid)
228 for (i=0; i < MAX_OBD_DEVICES; i++) {
229 struct obd_device *obd = &obd_dev[i];
230 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
237 void obd_cleanup_caches(void)
242 rc = kmem_cache_destroy(obdo_cachep);
244 CERROR("Cannot destory obdo_cachep\n");
248 rc = kmem_cache_destroy(import_cachep);
250 CERROR("Cannot destory import_cachep\n");
251 import_cachep = NULL;
254 rc = kmem_cache_destroy(export_cachep);
256 CERROR("Cannot destory import_cachep\n");
257 export_cachep = NULL;
262 int obd_init_caches(void)
265 if (obdo_cachep == NULL) {
266 obdo_cachep = kmem_cache_create("obdo_cache",
268 0, SLAB_HWCACHE_ALIGN,
270 if (obdo_cachep == NULL)
274 if (export_cachep == NULL) {
275 export_cachep = kmem_cache_create("export_cache",
276 sizeof(struct obd_export),
277 0, SLAB_HWCACHE_ALIGN,
279 if (export_cachep == NULL)
283 if (import_cachep == NULL) {
284 import_cachep = kmem_cache_create("import_cache",
285 sizeof(struct obd_import),
286 0, SLAB_HWCACHE_ALIGN,
288 if (import_cachep == NULL)
293 obd_cleanup_caches();
298 /* map connection to client */
299 struct obd_export *class_conn2export(struct lustre_handle *conn)
301 struct obd_export *export;
304 CDEBUG(D_CACHE, "looking for null handle\n");
308 if (conn->addr == -1) { /* this means assign a new connection */
309 CDEBUG(D_CACHE, "want a new connection\n");
314 CDEBUG(D_CACHE, "looking for null addr\n");
319 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
320 conn->addr, conn->cookie);
321 export = (struct obd_export *) (unsigned long)conn->addr;
322 if (!kmem_cache_validate(export_cachep, (void *)export))
325 if (export->exp_cookie != conn->cookie)
328 } /* class_conn2export */
330 struct obd_device *class_conn2obd(struct lustre_handle *conn)
332 struct obd_export *export;
333 export = class_conn2export(conn);
335 return export->exp_obd;
340 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
342 return &class_conn2obd(conn)->u.cli.cl_import;
345 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
347 return &class_conn2export(conn)->exp_ldlm_data.led_import;
350 struct obd_export *class_new_export(struct obd_device *obddev)
352 struct obd_export * export;
354 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
356 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
360 memset(export, 0, sizeof(*export));
361 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
362 export->exp_obd = obddev;
363 /* XXX should these be in MDS and LDLM init functions? */
364 INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
365 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
366 INIT_LIST_HEAD(&export->exp_conn_chain);
367 spin_lock(&obddev->obd_dev_lock);
368 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
369 spin_unlock(&obddev->obd_dev_lock);
373 void class_destroy_export(struct obd_export *exp)
377 spin_lock(&exp->exp_obd->obd_dev_lock);
378 list_del(&exp->exp_obd_chain);
379 spin_unlock(&exp->exp_obd->obd_dev_lock);
381 /* XXXshaver no connection here... */
382 if (exp->exp_connection)
383 spin_lock(&exp->exp_connection->c_lock);
384 list_del(&exp->exp_conn_chain);
385 if (exp->exp_connection) {
386 spin_unlock(&exp->exp_connection->c_lock);
387 ptlrpc_put_connection(exp->exp_connection);
390 kmem_cache_free(export_cachep, exp);
395 /* a connection defines an export context in which preallocation can
397 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
400 struct obd_export * export;
411 export = class_new_export(obd);
416 conn->addr = (__u64) (unsigned long)export;
417 conn->cookie = export->exp_cookie;
419 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
420 (long long)conn->addr, (long long)conn->cookie);
424 int class_disconnect(struct lustre_handle *conn)
426 struct obd_export *export;
429 if (!(export = class_conn2export(conn))) {
431 CDEBUG(D_IOCTL, "disconnect: attempting to free "
432 "nonexistent client "LPX64"\n", conn->addr);
436 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
437 (long long)conn->addr, (long long)conn->cookie);
439 class_destroy_export(export);
444 void class_disconnect_all(struct obd_device *obddev)
449 spin_lock(&obddev->obd_dev_lock);
450 if (!list_empty(&obddev->obd_exports)) {
451 struct obd_export *export;
452 struct lustre_handle conn;
455 export = list_entry(obddev->obd_exports.next,
458 conn.addr = (__u64)(unsigned long)export;
459 conn.cookie = export->exp_cookie;
460 spin_unlock(&obddev->obd_dev_lock);
461 CERROR("force disconnecting export %p\n", export);
462 rc = obd_disconnect(&conn);
464 /* AED: not so sure about this... We can't
465 * loop here forever, yet we shouldn't leak
466 * exports on a struct we will soon destroy.
468 CERROR("destroy export %p with err: rc = %d\n",
470 class_destroy_export(export);
473 spin_unlock(&obddev->obd_dev_lock);
481 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
483 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
489 for (p = data, count = 0; p < (char *)data + len; count++) {
491 int tmp = simple_strtoul(p, &end, 0);
494 CERROR("invalid device ID starting at: %s\n", p);
495 GOTO(err_disconnect, rc = -EINVAL);
498 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
499 CERROR("Trying to sub dev %d - dev no too large\n",
501 GOTO(err_disconnect, rc = -EINVAL);
504 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
506 CERROR("cannot connect to device %d: rc = %d\n", tmp,
508 GOTO(err_disconnect, rc);
511 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
512 obd_dev[tmp].obd_type->typ_name);
517 obddev->obd_multi_count = count;
522 for (count--; count >= 0; count--)
523 obd_disconnect(&obddev->obd_multi_conn[count]);
528 * remove all connections to this device
529 * close all connections to lower devices
530 * needed for forced unloads of OBD client drivers
532 int class_multi_cleanup(struct obd_device *obddev)
536 for (i = 0; i < obddev->obd_multi_count; i++) {
538 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
541 CERROR("no such device [i %d]\n", i);
545 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
547 CERROR("disconnect failure %d\n", obd->obd_minor);