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 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
30 /* I would prefer if these next four functions were in ptlrpc, to be honest,
31 * but obdclass uses them for the netregression ioctls. -phil */
32 static int sync_io_timeout(void *data)
34 struct io_cb_data *cbd = data;
35 struct ptlrpc_bulk_desc *desc;
42 LASSERT(desc->bd_connection);
44 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
45 desc->bd_page_count, desc->bd_connection->c_remote_uuid,
46 desc->bd_portal, desc->bd_connection);
47 desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
48 desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
49 if (desc->bd_connection && class_signal_connection_failure) {
50 class_signal_connection_failure(desc->bd_connection);
52 /* We go back to sleep, until we're resumed or interrupted. */
56 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
60 static int sync_io_intr(void *data)
62 struct io_cb_data *cbd = data;
63 struct ptlrpc_bulk_desc *desc = cbd->desc;
66 desc->bd_flags |= PTL_RPC_FL_INTR;
67 RETURN(1); /* ignored, as of this writing */
70 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
75 if (phase == CB_PHASE_START) {
76 struct l_wait_info lwi;
77 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
79 ret = l_wait_event(data->waitq, data->complete, &lwi);
80 if (atomic_dec_and_test(&data->refcount))
81 OBD_FREE(data, sizeof(*data));
84 } else if (phase == CB_PHASE_FINISH) {
87 wake_up(&data->waitq);
88 if (atomic_dec_and_test(&data->refcount))
89 OBD_FREE(data, sizeof(*data));
97 struct io_cb_data *ll_init_cb(void)
101 OBD_ALLOC(d, sizeof(*d));
103 init_waitqueue_head(&d->waitq);
104 atomic_set(&d->refcount, 2);
110 * support functions: we could use inter-module communication, but this
111 * is more portable to other OS's
113 static struct obd_type *class_search_type(char *nm)
115 struct list_head *tmp;
116 struct obd_type *type;
117 CDEBUG(D_INFO, "SEARCH %s\n", nm);
120 list_for_each(tmp, &obd_types) {
121 type = list_entry(tmp, struct obd_type, typ_chain);
122 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
123 if (strlen(type->typ_name) == strlen(nm) &&
124 strcmp(type->typ_name, nm) == 0 ) {
131 struct obd_type *class_nm_to_type(char *nm)
133 struct obd_type *type = class_search_type(nm);
137 if ( !request_module(nm) ) {
138 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
139 type = class_search_type(nm);
141 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
148 int class_register_type(struct obd_ops *ops, char *nm)
150 struct obd_type *type;
154 if (class_search_type(nm)) {
155 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
159 OBD_ALLOC(type, sizeof(*type));
160 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
161 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
164 INIT_LIST_HEAD(&type->typ_chain);
166 list_add(&type->typ_chain, &obd_types);
167 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
168 strcpy(type->typ_name, nm);
172 int class_unregister_type(char *nm)
174 struct obd_type *type = class_nm_to_type(nm);
179 CERROR("unknown obd type\n");
183 if (type->typ_refcnt) {
184 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
185 /* This is a bad situation, let's make the best of it */
186 /* Remove ops, but leave the name for debugging */
187 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
191 list_del(&type->typ_chain);
192 OBD_FREE(type->typ_name, strlen(nm) + 1);
193 if (type->typ_ops != NULL)
194 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
195 OBD_FREE(type, sizeof(*type));
198 } /* class_unregister_type */
200 int class_name2dev(char *name)
208 for (i=0; i < MAX_OBD_DEVICES; i++) {
209 struct obd_device *obd = &obd_dev[i];
210 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
219 int class_uuid2dev(char *uuid)
224 for (i=0; i < MAX_OBD_DEVICES; i++) {
225 struct obd_device *obd = &obd_dev[i];
226 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
236 struct obd_device *class_uuid2obd(char *uuid)
240 for (i=0; i < MAX_OBD_DEVICES; i++) {
241 struct obd_device *obd = &obd_dev[i];
242 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
249 void obd_cleanup_caches(void)
254 rc = kmem_cache_destroy(obdo_cachep);
256 CERROR("Cannot destory ll_obdo_cache\n");
260 rc = kmem_cache_destroy(import_cachep);
262 CERROR("Cannot destory ll_import_cache\n");
263 import_cachep = NULL;
266 rc = kmem_cache_destroy(export_cachep);
268 CERROR("Cannot destory ll_export_cache\n");
269 export_cachep = NULL;
272 rc = kmem_cache_destroy(handle_cachep);
274 CERROR("Cannot destory ll_handle_cache\n");
275 handle_cachep = NULL;
280 int obd_init_caches(void)
283 LASSERT(obdo_cachep == NULL);
284 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
289 LASSERT(export_cachep == NULL);
290 export_cachep = kmem_cache_create("ll_export_cache",
291 sizeof(struct obd_export),
296 LASSERT(import_cachep == NULL);
297 import_cachep = kmem_cache_create("ll_import_cache",
298 sizeof(struct obd_import),
303 LASSERT(handle_cachep == NULL);
304 handle_cachep = kmem_cache_create("ll_handle_cache",
305 sizeof(struct lustre_handle),
312 obd_cleanup_caches();
317 /* map connection to client */
318 struct obd_export *class_conn2export(struct lustre_handle *conn)
320 struct obd_export *export;
323 CDEBUG(D_CACHE, "looking for null handle\n");
327 if (conn->addr == -1) { /* this means assign a new connection */
328 CDEBUG(D_CACHE, "want a new connection\n");
333 CDEBUG(D_CACHE, "looking for null addr\n");
338 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
339 conn->addr, conn->cookie);
340 export = (struct obd_export *) (unsigned long)conn->addr;
341 if (!kmem_cache_validate(export_cachep, (void *)export))
344 if (export->exp_cookie != conn->cookie)
347 } /* class_conn2export */
349 struct obd_device *class_conn2obd(struct lustre_handle *conn)
351 struct obd_export *export;
352 export = class_conn2export(conn);
354 return export->exp_obd;
359 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
361 return &class_conn2obd(conn)->u.cli.cl_import;
364 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
366 return &class_conn2export(conn)->exp_ldlm_data.led_import;
369 struct obd_export *class_new_export(struct obd_device *obddev)
371 struct obd_export * export;
373 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
375 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
379 memset(export, 0, sizeof(*export));
380 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
381 export->exp_obd = obddev;
382 /* XXX this should be in LDLM init */
383 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
384 INIT_LIST_HEAD(&export->exp_conn_chain);
385 spin_lock(&obddev->obd_dev_lock);
386 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
387 spin_unlock(&obddev->obd_dev_lock);
391 void class_destroy_export(struct obd_export *exp)
395 LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
397 spin_lock(&exp->exp_obd->obd_dev_lock);
398 list_del(&exp->exp_obd_chain);
399 spin_unlock(&exp->exp_obd->obd_dev_lock);
401 /* XXXshaver no connection here... */
402 if (exp->exp_connection)
403 spin_lock(&exp->exp_connection->c_lock);
404 list_del(&exp->exp_conn_chain);
405 if (exp->exp_connection) {
406 spin_unlock(&exp->exp_connection->c_lock);
407 ptlrpc_put_connection_superhack(exp->exp_connection);
410 exp->exp_cookie = DEAD_HANDLE_MAGIC;
411 kmem_cache_free(export_cachep, exp);
416 /* a connection defines an export context in which preallocation can
418 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
421 struct obd_export * export;
432 export = class_new_export(obd);
436 conn->addr = (__u64) (unsigned long)export;
437 conn->cookie = export->exp_cookie;
439 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
440 (long long)conn->addr, (long long)conn->cookie);
444 int class_disconnect(struct lustre_handle *conn)
446 struct obd_export *export;
449 if (!(export = class_conn2export(conn))) {
451 CDEBUG(D_IOCTL, "disconnect: attempting to free "
452 "nonexistent client "LPX64"\n", conn->addr);
456 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
457 (long long)conn->addr, (long long)conn->cookie);
459 class_destroy_export(export);
464 void class_disconnect_all(struct obd_device *obddev)
469 spin_lock(&obddev->obd_dev_lock);
470 if (!list_empty(&obddev->obd_exports)) {
471 struct obd_export *export;
472 struct lustre_handle conn;
475 export = list_entry(obddev->obd_exports.next,
478 conn.addr = (__u64)(unsigned long)export;
479 conn.cookie = export->exp_cookie;
480 spin_unlock(&obddev->obd_dev_lock);
481 CERROR("force disconnecting export %p\n", export);
482 rc = obd_disconnect(&conn);
484 /* AED: not so sure about this... We can't
485 * loop here forever, yet we shouldn't leak
486 * exports on a struct we will soon destroy.
488 CERROR("destroy export %p with err: rc = %d\n",
490 class_destroy_export(export);
493 spin_unlock(&obddev->obd_dev_lock);
501 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
503 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
509 for (p = data, count = 0; p < (char *)data + len; count++) {
511 int tmp = simple_strtoul(p, &end, 0);
514 CERROR("invalid device ID starting at: %s\n", p);
515 GOTO(err_disconnect, rc = -EINVAL);
518 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
519 CERROR("Trying to sub dev %d - dev no too large\n",
521 GOTO(err_disconnect, rc = -EINVAL);
524 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
526 CERROR("cannot connect to device %d: rc = %d\n", tmp,
528 GOTO(err_disconnect, rc);
531 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
532 obd_dev[tmp].obd_type->typ_name);
537 obddev->obd_multi_count = count;
542 for (count--; count >= 0; count--)
543 obd_disconnect(&obddev->obd_multi_conn[count]);
548 * remove all connections to this device
549 * close all connections to lower devices
550 * needed for forced unloads of OBD client drivers
552 int class_multi_cleanup(struct obd_device *obddev)
556 for (i = 0; i < obddev->obd_multi_count; i++) {
558 struct obd_device *obd =
559 class_conn2obd(&obddev->obd_multi_conn[i]);
562 CERROR("no such device [i %d]\n", i);
566 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
568 CERROR("disconnect failure %d\n", obd->obd_minor);