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 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
24 kmem_cache_t *obdo_cachep = NULL;
25 kmem_cache_t *export_cachep = NULL;
26 kmem_cache_t *import_cachep = NULL;
29 * support functions: we could use inter-module communication, but this
30 * is more portable to other OS's
32 static struct obd_type *class_search_type(char *nm)
34 struct list_head *tmp;
35 struct obd_type *type;
36 CDEBUG(D_INFO, "SEARCH %s\n", nm);
39 list_for_each(tmp, &obd_types) {
40 type = list_entry(tmp, struct obd_type, typ_chain);
41 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
42 if (strlen(type->typ_name) == strlen(nm) &&
43 strcmp(type->typ_name, nm) == 0 ) {
50 struct obd_type *class_nm_to_type(char *nm)
52 struct obd_type *type = class_search_type(nm);
56 if ( !request_module(nm) ) {
57 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
58 type = class_search_type(nm);
60 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
67 int class_register_type(struct obd_ops *ops, char *nm)
69 struct obd_type *type;
73 if (class_search_type(nm)) {
74 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
78 OBD_ALLOC(type, sizeof(*type));
81 INIT_LIST_HEAD(&type->typ_chain);
83 list_add(&type->typ_chain, &obd_types);
89 int class_unregister_type(char *nm)
91 struct obd_type *type = class_nm_to_type(nm);
96 CERROR("unknown obd type\n");
100 if (type->typ_refcnt) {
101 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
105 list_del(&type->typ_chain);
106 OBD_FREE(type, sizeof(*type));
109 } /* class_unregister_type */
111 int class_name2dev(char *name)
119 for (i=0; i < MAX_OBD_DEVICES; i++) {
120 struct obd_device *obd = &obd_dev[i];
121 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
130 int class_uuid2dev(char *name)
135 for (i=0; i < MAX_OBD_DEVICES; i++) {
136 struct obd_device *obd = &obd_dev[i];
137 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
147 struct obd_device *class_uuid2obd(char *name)
151 for (i=0; i < MAX_OBD_DEVICES; i++) {
152 struct obd_device *obd = &obd_dev[i];
153 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
161 void obd_cleanup_caches(void)
166 rc = kmem_cache_destroy(obdo_cachep);
168 CERROR("Cannot destory obdo_cachep\n");
172 rc = kmem_cache_destroy(import_cachep);
174 CERROR("Cannot destory import_cachep\n");
175 import_cachep = NULL;
178 rc = kmem_cache_destroy(export_cachep);
180 CERROR("Cannot destory import_cachep\n");
181 export_cachep = NULL;
186 int obd_init_caches(void)
189 if (obdo_cachep == NULL) {
190 obdo_cachep = kmem_cache_create("obdo_cache",
192 0, SLAB_HWCACHE_ALIGN,
194 if (obdo_cachep == NULL)
198 if (export_cachep == NULL) {
199 export_cachep = kmem_cache_create("export_cache",
200 sizeof(struct obd_export),
201 0, SLAB_HWCACHE_ALIGN,
203 if (export_cachep == NULL)
207 if (import_cachep == NULL) {
208 import_cachep = kmem_cache_create("import_cache",
209 sizeof(struct obd_import),
210 0, SLAB_HWCACHE_ALIGN,
212 if (import_cachep == NULL)
217 obd_cleanup_caches();
222 /* map connection to client */
223 struct obd_export *class_conn2export(struct lustre_handle *conn)
225 struct obd_export *export;
228 CDEBUG(D_CACHE, "looking for null handle\n");
232 if (conn->addr == -1) { /* this means assign a new connection */
233 CDEBUG(D_CACHE, "want a new connection\n");
238 CDEBUG(D_CACHE, "looking for null addr\n");
243 CDEBUG(D_IOCTL, "looking for export addr %Lx cookie %Lx\n",
244 conn->addr, conn->cookie);
245 export = (struct obd_export *) (unsigned long)conn->addr;
246 if (!kmem_cache_validate(export_cachep, (void *)export))
249 if (export->exp_cookie != conn->cookie)
252 } /* class_conn2export */
254 struct obd_device *class_conn2obd(struct lustre_handle *conn)
256 struct obd_export *export;
257 export = class_conn2export(conn);
259 return export->exp_obd;
264 struct obd_export *class_new_export(struct obd_device *obddev)
266 struct obd_export * export;
268 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
270 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
274 memset(export, 0, sizeof(*export));
275 get_random_bytes(&export->exp_cookie, sizeof(__u64));
276 export->exp_obd = obddev;
277 /* XXX should these be in MDS and LDLM init functions? */
278 INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
279 INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
280 list_add(&(export->exp_chain), &export->exp_obd->obd_exports);
284 void class_destroy_export(struct obd_export *exp)
289 spin_lock(&exp->exp_connection->c_lock);
290 list_del(&exp->exp_chain);
291 spin_unlock(&exp->exp_connection->c_lock);
293 /* XXXshaver these bits want to be hung off the export, instead of
294 * XXXshaver hard-coded here.
296 if (mds_destroy_export) {
297 rc = mds_destroy_export(exp);
299 CERROR("error freeing mds client data: rc = %d\n", rc);
301 if (ldlm_destroy_export) {
302 rc = ldlm_destroy_export(exp);
304 CERROR("error freeing dlm client data: rc = %d\n", rc);
306 kmem_cache_free(export_cachep, exp);
311 /* a connection defines an export context in which preallocation can
313 int class_connect (struct lustre_handle *conn, struct obd_device *obd,
316 struct obd_export * export;
327 export = class_new_export(obd);
331 export->exp_rconnh.addr = conn->addr;
332 export->exp_rconnh.cookie = conn->cookie;
334 conn->addr = (__u64) (unsigned long)export;
335 conn->cookie = export->exp_cookie;
336 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
337 (long long)conn->addr, (long long)conn->cookie);
341 int class_rconn2export(struct lustre_handle *conn, struct lustre_handle *rconn)
343 struct obd_export *export = class_conn2export(conn);
348 export->exp_rconnh.addr = rconn->addr;
349 export->exp_rconnh.cookie = rconn->cookie;
354 int class_disconnect(struct lustre_handle *conn)
356 struct obd_export *export;
359 if (!(export = class_conn2export(conn))) {
361 CDEBUG(D_IOCTL, "disconnect: attempting to free "
362 "nonexistent client %Lx\n", conn->addr);
365 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
366 (long long)conn->addr, (long long)conn->cookie);
368 class_destroy_export(export);
373 void class_disconnect_all(struct obd_device *obddev)
375 struct list_head *tmp, *next;
377 list_for_each_safe(tmp, next, &obddev->obd_exports) {
378 struct obd_export *export;
379 struct lustre_handle conn;
381 export = list_entry(tmp, struct obd_export, exp_chain);
382 conn.addr = (__u64) (unsigned long)export;
383 conn.cookie = export->exp_cookie;
384 obd_disconnect(&conn);
390 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
392 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
398 for (p = data, count = 0; p < (char *)data + len; count++) {
400 int tmp = simple_strtoul(p, &end, 0);
403 CERROR("invalid device ID starting at: %s\n", p);
404 GOTO(err_disconnect, rc = -EINVAL);
407 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
408 CERROR("Trying to sub dev %d - dev no too large\n",
410 GOTO(err_disconnect, rc = -EINVAL);
413 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
415 CERROR("cannot connect to device %d: rc = %d\n", tmp,
417 GOTO(err_disconnect, rc);
420 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
421 obd_dev[tmp].obd_type->typ_name);
426 obddev->obd_multi_count = count;
431 for (count--; count >= 0; count--)
432 obd_disconnect(&obddev->obd_multi_conn[count]);
437 * remove all connections to this device
438 * close all connections to lower devices
439 * needed for forced unloads of OBD client drivers
441 int class_multi_cleanup(struct obd_device *obddev)
445 for (i = 0; i < obddev->obd_multi_count; i++) {
447 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
450 CERROR("no such device [i %d]\n", i);
454 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
456 CERROR("disconnect failure %d\n", obd->obd_minor);