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 *import_cachep = NULL;
25 kmem_cache_t *export_cachep = NULL;
26 kmem_cache_t *handle_cachep = NULL;
28 /* I would prefer if these next four functions were in ptlrpc, to be honest,
29 * but obdclass uses them for the netregression ioctls. -phil */
30 static int sync_io_timeout(void *data)
32 struct io_cb_data *cbd = data;
33 struct ptlrpc_bulk_desc *desc;
40 LASSERT(desc->bd_connection);
42 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
43 desc->bd_page_count, desc->bd_connection->c_remote_uuid,
44 desc->bd_portal, desc->bd_connection);
45 desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
46 desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
47 if (desc->bd_connection && class_signal_connection_failure) {
48 class_signal_connection_failure(desc->bd_connection);
50 /* We go back to sleep, until we're resumed or interrupted. */
54 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
58 static int sync_io_intr(void *data)
60 struct io_cb_data *cbd = data;
61 struct ptlrpc_bulk_desc *desc = cbd->desc;
64 desc->bd_flags |= PTL_RPC_FL_INTR;
65 RETURN(1); /* ignored, as of this writing */
68 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
73 if (phase == CB_PHASE_START) {
74 struct l_wait_info lwi;
75 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
77 ret = l_wait_event(data->waitq, data->complete, &lwi);
78 if (atomic_dec_and_test(&data->refcount))
79 OBD_FREE(data, sizeof(*data));
82 } else if (phase == CB_PHASE_FINISH) {
85 wake_up(&data->waitq);
86 if (atomic_dec_and_test(&data->refcount))
87 OBD_FREE(data, sizeof(*data));
95 struct io_cb_data *ll_init_cb(void)
99 OBD_ALLOC(d, sizeof(*d));
101 init_waitqueue_head(&d->waitq);
102 atomic_set(&d->refcount, 2);
108 * support functions: we could use inter-module communication, but this
109 * is more portable to other OS's
111 static struct obd_type *class_search_type(char *nm)
113 struct list_head *tmp;
114 struct obd_type *type;
115 CDEBUG(D_INFO, "SEARCH %s\n", nm);
118 list_for_each(tmp, &obd_types) {
119 type = list_entry(tmp, struct obd_type, typ_chain);
120 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
121 if (strlen(type->typ_name) == strlen(nm) &&
122 strcmp(type->typ_name, nm) == 0 ) {
129 struct obd_type *class_nm_to_type(char *nm)
131 struct obd_type *type = class_search_type(nm);
135 if ( !request_module(nm) ) {
136 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
137 type = class_search_type(nm);
139 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
146 int class_register_type(struct obd_ops *ops, char *nm)
148 struct obd_type *type;
152 if (class_search_type(nm)) {
153 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
157 OBD_ALLOC(type, sizeof(*type));
158 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
159 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
162 INIT_LIST_HEAD(&type->typ_chain);
164 list_add(&type->typ_chain, &obd_types);
165 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
166 strcpy(type->typ_name, nm);
170 int class_unregister_type(char *nm)
172 struct obd_type *type = class_nm_to_type(nm);
177 CERROR("unknown obd type\n");
181 if (type->typ_refcnt) {
182 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
183 /* This is a bad situation, let's make the best of it */
184 /* Remove ops, but leave the name for debugging */
185 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
189 list_del(&type->typ_chain);
190 OBD_FREE(type->typ_name, strlen(nm) + 1);
191 if (type->typ_ops != NULL)
192 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
193 OBD_FREE(type, sizeof(*type));
196 } /* class_unregister_type */
198 int class_name2dev(char *name)
206 for (i=0; i < MAX_OBD_DEVICES; i++) {
207 struct obd_device *obd = &obd_dev[i];
208 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
217 int class_uuid2dev(char *uuid)
222 for (i=0; i < MAX_OBD_DEVICES; i++) {
223 struct obd_device *obd = &obd_dev[i];
224 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
234 struct obd_device *class_uuid2obd(char *uuid)
238 for (i=0; i < MAX_OBD_DEVICES; i++) {
239 struct obd_device *obd = &obd_dev[i];
240 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
247 void obd_cleanup_caches(void)
252 rc = kmem_cache_destroy(obdo_cachep);
254 CERROR("Cannot destory ll_obdo_cache\n");
258 rc = kmem_cache_destroy(import_cachep);
260 CERROR("Cannot destory ll_import_cache\n");
261 import_cachep = NULL;
264 rc = kmem_cache_destroy(export_cachep);
266 CERROR("Cannot destory ll_export_cache\n");
267 export_cachep = NULL;
270 rc = kmem_cache_destroy(handle_cachep);
272 CERROR("Cannot destory ll_handle_cache\n");
273 handle_cachep = NULL;
278 int obd_init_caches(void)
281 LASSERT(obdo_cachep == NULL);
282 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
287 LASSERT(export_cachep == NULL);
288 export_cachep = kmem_cache_create("ll_export_cache",
289 sizeof(struct obd_export),
294 LASSERT(import_cachep == NULL);
295 import_cachep = kmem_cache_create("ll_import_cache",
296 sizeof(struct obd_import),
301 LASSERT(handle_cachep == NULL);
302 handle_cachep = kmem_cache_create("ll_handle_cache",
303 sizeof(struct lustre_handle),
310 obd_cleanup_caches();
315 /* map connection to client */
316 struct obd_export *class_conn2export(struct lustre_handle *conn)
318 struct obd_export *export;
321 CDEBUG(D_CACHE, "looking for null handle\n");
325 if (conn->addr == -1) { /* this means assign a new connection */
326 CDEBUG(D_CACHE, "want a new connection\n");
331 CDEBUG(D_CACHE, "looking for null addr\n");
336 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
337 conn->addr, conn->cookie);
338 export = (struct obd_export *) (unsigned long)conn->addr;
339 if (!kmem_cache_validate(export_cachep, (void *)export))
342 if (export->exp_cookie != conn->cookie)
345 } /* class_conn2export */
347 struct obd_device *class_conn2obd(struct lustre_handle *conn)
349 struct obd_export *export;
350 export = class_conn2export(conn);
352 return export->exp_obd;
357 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
359 return &class_conn2obd(conn)->u.cli.cl_import;
362 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
364 return &class_conn2export(conn)->exp_ldlm_data.led_import;
367 struct obd_export *class_new_export(struct obd_device *obddev)
369 struct obd_export * export;
371 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
373 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
377 memset(export, 0, sizeof(*export));
378 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
379 export->exp_obd = obddev;
380 /* XXX this should be in LDLM init */
381 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
382 INIT_LIST_HEAD(&export->exp_conn_chain);
383 spin_lock(&obddev->obd_dev_lock);
384 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
385 spin_unlock(&obddev->obd_dev_lock);
389 void class_destroy_export(struct obd_export *exp)
393 spin_lock(&exp->exp_obd->obd_dev_lock);
394 list_del(&exp->exp_obd_chain);
395 spin_unlock(&exp->exp_obd->obd_dev_lock);
397 /* XXXshaver no connection here... */
398 if (exp->exp_connection)
399 spin_lock(&exp->exp_connection->c_lock);
400 list_del(&exp->exp_conn_chain);
401 if (exp->exp_connection) {
402 spin_unlock(&exp->exp_connection->c_lock);
403 //ptlrpc_put_connection(exp->exp_connection);
406 exp->exp_cookie = DEAD_HANDLE_MAGIC;
407 kmem_cache_free(export_cachep, exp);
412 /* a connection defines an export context in which preallocation can
414 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
417 struct obd_export * export;
428 export = class_new_export(obd);
432 conn->addr = (__u64) (unsigned long)export;
433 conn->cookie = export->exp_cookie;
435 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
436 (long long)conn->addr, (long long)conn->cookie);
440 int class_disconnect(struct lustre_handle *conn)
442 struct obd_export *export;
445 if (!(export = class_conn2export(conn))) {
447 CDEBUG(D_IOCTL, "disconnect: attempting to free "
448 "nonexistent client "LPX64"\n", conn->addr);
452 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
453 (long long)conn->addr, (long long)conn->cookie);
455 class_destroy_export(export);
460 void class_disconnect_all(struct obd_device *obddev)
465 spin_lock(&obddev->obd_dev_lock);
466 if (!list_empty(&obddev->obd_exports)) {
467 struct obd_export *export;
468 struct lustre_handle conn;
471 export = list_entry(obddev->obd_exports.next,
474 conn.addr = (__u64)(unsigned long)export;
475 conn.cookie = export->exp_cookie;
476 spin_unlock(&obddev->obd_dev_lock);
477 CERROR("force disconnecting export %p\n", export);
478 rc = obd_disconnect(&conn);
480 /* AED: not so sure about this... We can't
481 * loop here forever, yet we shouldn't leak
482 * exports on a struct we will soon destroy.
484 CERROR("destroy export %p with err: rc = %d\n",
486 class_destroy_export(export);
489 spin_unlock(&obddev->obd_dev_lock);
497 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
499 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
505 for (p = data, count = 0; p < (char *)data + len; count++) {
507 int tmp = simple_strtoul(p, &end, 0);
510 CERROR("invalid device ID starting at: %s\n", p);
511 GOTO(err_disconnect, rc = -EINVAL);
514 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
515 CERROR("Trying to sub dev %d - dev no too large\n",
517 GOTO(err_disconnect, rc = -EINVAL);
520 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
522 CERROR("cannot connect to device %d: rc = %d\n", tmp,
524 GOTO(err_disconnect, rc);
527 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
528 obd_dev[tmp].obd_type->typ_name);
533 obddev->obd_multi_count = count;
538 for (count--; count >= 0; count--)
539 obd_disconnect(&obddev->obd_multi_conn[count]);
544 * remove all connections to this device
545 * close all connections to lower devices
546 * needed for forced unloads of OBD client drivers
548 int class_multi_cleanup(struct obd_device *obddev)
552 for (i = 0; i < obddev->obd_multi_count; i++) {
554 struct obd_device *obd =
555 class_conn2obd(&obddev->obd_multi_conn[i]);
558 CERROR("no such device [i %d]\n", i);
562 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
564 CERROR("disconnect failure %d\n", obd->obd_minor);