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;
27 int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
29 /* I would prefer if these next four functions were in ptlrpc, to be honest,
30 * but obdclass uses them for the netregression ioctls. -phil */
31 static int sync_io_timeout(void *data)
33 struct io_cb_data *cbd = data;
34 struct ptlrpc_bulk_desc *desc;
41 CERROR("no desc for timed-out BRW, reopen Bugzilla 214!\n");
42 RETURN(0); /* back to sleep -- someone had better wake us up! */
45 LASSERT(desc->bd_connection);
47 CERROR("IO of %d pages to/from %s:%d (conn %p) timed out\n",
48 desc->bd_page_count, desc->bd_connection->c_remote_uuid,
49 desc->bd_portal, desc->bd_connection);
50 desc->bd_connection->c_level = LUSTRE_CONN_RECOVD;
51 desc->bd_flags |= PTL_RPC_FL_TIMEOUT;
52 if (desc->bd_connection && class_signal_connection_failure) {
53 class_signal_connection_failure(desc->bd_connection);
55 /* We go back to sleep, until we're resumed or interrupted. */
59 /* If we can't be recovered, just abort the syscall with -ETIMEDOUT. */
63 static int sync_io_intr(void *data)
65 struct io_cb_data *cbd = data;
66 struct ptlrpc_bulk_desc *desc = cbd->desc;
69 desc->bd_flags |= PTL_RPC_FL_INTR;
70 RETURN(1); /* ignored, as of this writing */
73 int ll_sync_io_cb(struct io_cb_data *data, int err, int phase)
78 if (phase == CB_PHASE_START) {
79 struct l_wait_info lwi;
80 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, sync_io_timeout,
82 ret = l_wait_event(data->waitq, data->complete, &lwi);
83 if (atomic_dec_and_test(&data->refcount))
84 OBD_FREE(data, sizeof(*data));
87 } else if (phase == CB_PHASE_FINISH) {
90 wake_up(&data->waitq);
91 if (atomic_dec_and_test(&data->refcount))
92 OBD_FREE(data, sizeof(*data));
100 struct io_cb_data *ll_init_cb(void)
102 struct io_cb_data *d;
104 OBD_ALLOC(d, sizeof(*d));
106 init_waitqueue_head(&d->waitq);
107 atomic_set(&d->refcount, 2);
113 * support functions: we could use inter-module communication, but this
114 * is more portable to other OS's
116 static struct obd_type *class_search_type(char *nm)
118 struct list_head *tmp;
119 struct obd_type *type;
120 CDEBUG(D_INFO, "SEARCH %s\n", nm);
123 list_for_each(tmp, &obd_types) {
124 type = list_entry(tmp, struct obd_type, typ_chain);
125 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
126 if (strlen(type->typ_name) == strlen(nm) &&
127 strcmp(type->typ_name, nm) == 0 ) {
134 struct obd_type *class_nm_to_type(char *nm)
136 struct obd_type *type = class_search_type(nm);
140 if ( !request_module(nm) ) {
141 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
142 type = class_search_type(nm);
144 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
151 int class_register_type(struct obd_ops *ops, char *nm)
153 struct obd_type *type;
157 if (class_search_type(nm)) {
158 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
162 OBD_ALLOC(type, sizeof(*type));
163 OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
164 OBD_ALLOC(type->typ_name, strlen(nm) + 1);
167 INIT_LIST_HEAD(&type->typ_chain);
169 list_add(&type->typ_chain, &obd_types);
170 memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
171 strcpy(type->typ_name, nm);
175 int class_unregister_type(char *nm)
177 struct obd_type *type = class_nm_to_type(nm);
182 CERROR("unknown obd type\n");
186 if (type->typ_refcnt) {
187 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
188 /* This is a bad situation, let's make the best of it */
189 /* Remove ops, but leave the name for debugging */
190 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
194 list_del(&type->typ_chain);
195 OBD_FREE(type->typ_name, strlen(nm) + 1);
196 if (type->typ_ops != NULL)
197 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
198 OBD_FREE(type, sizeof(*type));
201 } /* class_unregister_type */
203 int class_name2dev(char *name)
211 for (i=0; i < MAX_OBD_DEVICES; i++) {
212 struct obd_device *obd = &obd_dev[i];
213 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
222 int class_uuid2dev(char *uuid)
227 for (i=0; i < MAX_OBD_DEVICES; i++) {
228 struct obd_device *obd = &obd_dev[i];
229 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
239 struct obd_device *class_uuid2obd(char *uuid)
243 for (i=0; i < MAX_OBD_DEVICES; i++) {
244 struct obd_device *obd = &obd_dev[i];
245 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
252 void obd_cleanup_caches(void)
257 rc = kmem_cache_destroy(obdo_cachep);
259 CERROR("Cannot destory ll_obdo_cache\n");
263 rc = kmem_cache_destroy(import_cachep);
265 CERROR("Cannot destory ll_import_cache\n");
266 import_cachep = NULL;
269 rc = kmem_cache_destroy(export_cachep);
271 CERROR("Cannot destory ll_export_cache\n");
272 export_cachep = NULL;
277 int obd_init_caches(void)
280 LASSERT(obdo_cachep == NULL);
281 obdo_cachep = kmem_cache_create("ll_obdo_cache", sizeof(struct obdo),
286 LASSERT(export_cachep == NULL);
287 export_cachep = kmem_cache_create("ll_export_cache",
288 sizeof(struct obd_export),
293 LASSERT(import_cachep == NULL);
294 import_cachep = kmem_cache_create("ll_import_cache",
295 sizeof(struct obd_import),
302 obd_cleanup_caches();
307 /* map connection to client */
308 struct obd_export *class_conn2export(struct lustre_handle *conn)
310 struct obd_export *export;
313 CDEBUG(D_CACHE, "looking for null handle\n");
317 if (conn->addr == -1) { /* this means assign a new connection */
318 CDEBUG(D_CACHE, "want a new connection\n");
323 CDEBUG(D_CACHE, "looking for null addr\n");
328 CDEBUG(D_IOCTL, "looking for export addr "LPX64" cookie "LPX64"\n",
329 conn->addr, conn->cookie);
330 export = (struct obd_export *) (unsigned long)conn->addr;
331 if (!kmem_cache_validate(export_cachep, (void *)export))
334 if (export->exp_cookie != conn->cookie)
337 } /* class_conn2export */
339 struct obd_device *class_conn2obd(struct lustre_handle *conn)
341 struct obd_export *export;
342 export = class_conn2export(conn);
344 return export->exp_obd;
349 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
351 return &class_conn2obd(conn)->u.cli.cl_import;
354 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
356 return &class_conn2export(conn)->exp_ldlm_data.led_import;
359 struct obd_export *class_new_export(struct obd_device *obddev)
361 struct obd_export * export;
363 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
365 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
369 memset(export, 0, sizeof(*export));
370 get_random_bytes(&export->exp_cookie, sizeof(export->exp_cookie));
371 export->exp_obd = obddev;
372 /* XXX this should be in LDLM init */
373 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
374 INIT_LIST_HEAD(&export->exp_conn_chain);
375 spin_lock(&obddev->obd_dev_lock);
376 list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
377 spin_unlock(&obddev->obd_dev_lock);
381 void class_destroy_export(struct obd_export *exp)
385 LASSERT(exp->exp_cookie != DEAD_HANDLE_MAGIC);
387 spin_lock(&exp->exp_obd->obd_dev_lock);
388 list_del(&exp->exp_obd_chain);
389 spin_unlock(&exp->exp_obd->obd_dev_lock);
391 /* XXXshaver no connection here... */
392 if (exp->exp_connection)
393 spin_lock(&exp->exp_connection->c_lock);
394 list_del(&exp->exp_conn_chain);
395 if (exp->exp_connection) {
396 spin_unlock(&exp->exp_connection->c_lock);
397 ptlrpc_put_connection_superhack(exp->exp_connection);
400 exp->exp_cookie = DEAD_HANDLE_MAGIC;
401 kmem_cache_free(export_cachep, exp);
406 /* a connection defines an export context in which preallocation can
408 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
411 struct obd_export * export;
422 export = class_new_export(obd);
426 conn->addr = (__u64) (unsigned long)export;
427 conn->cookie = export->exp_cookie;
429 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
430 (long long)conn->addr, (long long)conn->cookie);
434 int class_disconnect(struct lustre_handle *conn)
436 struct obd_export *export;
439 if (!(export = class_conn2export(conn))) {
441 CDEBUG(D_IOCTL, "disconnect: attempting to free "
442 "nonexistent client "LPX64"\n", conn->addr);
446 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
447 (long long)conn->addr, (long long)conn->cookie);
449 class_destroy_export(export);
454 void class_disconnect_all(struct obd_device *obddev)
459 spin_lock(&obddev->obd_dev_lock);
460 if (!list_empty(&obddev->obd_exports)) {
461 struct obd_export *export;
462 struct lustre_handle conn;
465 export = list_entry(obddev->obd_exports.next,
468 conn.addr = (__u64)(unsigned long)export;
469 conn.cookie = export->exp_cookie;
470 spin_unlock(&obddev->obd_dev_lock);
471 CERROR("force disconnecting export %p\n", export);
472 rc = obd_disconnect(&conn);
474 /* AED: not so sure about this... We can't
475 * loop here forever, yet we shouldn't leak
476 * exports on a struct we will soon destroy.
478 CERROR("destroy export %p with err: rc = %d\n",
480 class_destroy_export(export);
483 spin_unlock(&obddev->obd_dev_lock);
491 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
493 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
499 for (p = data, count = 0; p < (char *)data + len; count++) {
501 int tmp = simple_strtoul(p, &end, 0);
504 CERROR("invalid device ID starting at: %s\n", p);
505 GOTO(err_disconnect, rc = -EINVAL);
508 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
509 CERROR("Trying to sub dev %d - dev no too large\n",
511 GOTO(err_disconnect, rc = -EINVAL);
514 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
516 CERROR("cannot connect to device %d: rc = %d\n", tmp,
518 GOTO(err_disconnect, rc);
521 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
522 obd_dev[tmp].obd_type->typ_name);
527 obddev->obd_multi_count = count;
532 for (count--; count >= 0; count--)
533 obd_disconnect(&obddev->obd_multi_conn[count]);
538 * remove all connections to this device
539 * close all connections to lower devices
540 * needed for forced unloads of OBD client drivers
542 int class_multi_cleanup(struct obd_device *obddev)
546 for (i = 0; i < obddev->obd_multi_count; i++) {
548 struct obd_device *obd =
549 class_conn2obd(&obddev->obd_multi_conn[i]);
552 CERROR("no such device [i %d]\n", i);
556 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
558 CERROR("disconnect failure %d\n", obd->obd_minor);