2 * linux/fs/ext2_obd/sim_obd.c
3 * Copyright (C) 2001 Cluster File Systems, Inc.
5 * This code is issued under the GNU General Public License.
6 * See the file COPYING in this distribution
8 * These are the only exported functions, they provide some generic
9 * infrastructure for managing object devices
13 #define DEBUG_SUBSYSTEM S_CLASS
14 #include <linux/kmod.h> /* for request_module() */
15 #include <linux/module.h>
16 #include <linux/obd_class.h>
17 #include <linux/random.h>
18 #include <linux/slab.h>
20 extern struct list_head obd_types;
21 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
22 kmem_cache_t *obdo_cachep = NULL;
23 kmem_cache_t *export_cachep = NULL;
24 kmem_cache_t *import_cachep = NULL;
27 * support functions: we could use inter-module communication, but this
28 * is more portable to other OS's
30 static struct obd_type *class_search_type(char *nm)
32 struct list_head *tmp;
33 struct obd_type *type;
34 CDEBUG(D_INFO, "SEARCH %s\n", nm);
37 while ( (tmp = tmp->next) != &obd_types ) {
38 type = list_entry(tmp, struct obd_type, typ_chain);
39 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
40 if (strlen(type->typ_name) == strlen(nm) &&
41 strcmp(type->typ_name, nm) == 0 ) {
48 struct obd_type *class_nm_to_type(char *nm)
50 struct obd_type *type = class_search_type(nm);
54 if ( !request_module(nm) ) {
55 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
56 type = class_search_type(nm);
58 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
65 int class_register_type(struct obd_ops *ops, char *nm)
67 struct obd_type *type;
71 if (class_search_type(nm)) {
72 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
76 OBD_ALLOC(type, sizeof(*type));
79 INIT_LIST_HEAD(&type->typ_chain);
81 list_add(&type->typ_chain, obd_types.next);
87 int class_unregister_type(char *nm)
89 struct obd_type *type = class_nm_to_type(nm);
94 CERROR("unknown obd type\n");
98 if ( type->typ_refcnt ) {
99 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
103 list_del(&type->typ_chain);
104 OBD_FREE(type, sizeof(*type));
107 } /* class_unregister_type */
109 int class_name2dev(char *name)
117 for (i=0; i < MAX_OBD_DEVICES; i++) {
118 struct obd_device *obd = &obd_dev[i];
119 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
128 int class_uuid2dev(char *name)
133 for (i=0; i < MAX_OBD_DEVICES; i++) {
134 struct obd_device *obd = &obd_dev[i];
135 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
145 struct obd_device *class_uuid2obd(char *name)
149 for (i=0; i < MAX_OBD_DEVICES; i++) {
150 struct obd_device *obd = &obd_dev[i];
151 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
159 void obd_cleanup_caches(void)
164 rc = kmem_cache_destroy(obdo_cachep);
166 CERROR("Cannot destory obdo_cachep\n");
170 rc = kmem_cache_destroy(import_cachep);
172 CERROR("Cannot destory import_cachep\n");
173 import_cachep = NULL;
176 rc = kmem_cache_destroy(export_cachep);
178 CERROR("Cannot destory import_cachep\n");
179 export_cachep = NULL;
184 int obd_init_caches(void)
187 if (obdo_cachep == NULL) {
188 obdo_cachep = kmem_cache_create("obdo_cache",
190 0, SLAB_HWCACHE_ALIGN,
192 if (obdo_cachep == NULL)
196 if (export_cachep == NULL) {
197 export_cachep = kmem_cache_create("export_cache",
198 sizeof(struct obd_export),
199 0, SLAB_HWCACHE_ALIGN,
201 if (export_cachep == NULL)
205 if (import_cachep == NULL) {
206 import_cachep = kmem_cache_create("import_cache",
207 sizeof(struct obd_import),
208 0, SLAB_HWCACHE_ALIGN,
210 if (import_cachep == NULL)
215 obd_cleanup_caches();
220 /* map connection to client */
221 struct obd_export *class_conn2export(struct lustre_handle *conn)
223 struct obd_export * export;
228 if (conn->addr == -1) /* this means assign a new connection */
236 export = (struct obd_export *) (unsigned long)conn->addr;
237 if (!kmem_cache_validate(export_cachep, (void *)export))
240 if (export->export_cookie != conn->cookie)
243 } /* class_conn2export */
245 struct obd_device *class_conn2obd(struct lustre_handle *conn)
247 struct obd_export *export;
248 export = class_conn2export(conn);
250 return export->export_obd;
255 /* a connection defines an export context in which preallocation can
257 int class_connect (struct lustre_handle *conn, struct obd_device *obd)
259 struct obd_export * export;
261 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
263 CERROR("no memory! (minor %d)\n", obd->obd_minor);
267 memset(export, 0, sizeof(*export));
268 get_random_bytes(&export->export_cookie, sizeof(__u64));
269 export->export_obd = obd;
270 export->export_import.addr = conn->addr;
271 export->export_import.cookie = conn->cookie;
273 list_add(&(export->export_chain), export->export_obd->obd_exports.prev);
274 conn->addr = (__u64) (unsigned long)export;
275 conn->cookie = export->export_cookie;
276 CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
277 (long long)conn->addr, (long long)conn->cookie);
281 int class_import2export(struct lustre_handle *conn, struct lustre_handle *imp)
283 struct obd_export *export = class_conn2export(conn);
288 export->export_import.addr = imp->addr;
289 export->export_import.cookie = imp->cookie;
294 int class_disconnect(struct lustre_handle *conn)
296 struct obd_export * export;
299 if (!(export = class_conn2export(conn))) {
301 CDEBUG(D_IOCTL, "disconnect: attempting to free "
302 "nonexistent client %Lx\n", conn->addr);
305 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
306 (long long)conn->addr, (long long)conn->cookie);
307 list_del(&export->export_chain);
308 kmem_cache_free(export_cachep, export);
315 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
317 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
323 for (p = data, count = 0; p < (char *)data + len; count++) {
325 int tmp = simple_strtoul(p, &end, 0);
328 CERROR("invalid device ID starting at: %s\n", p);
329 GOTO(err_disconnect, rc = -EINVAL);
332 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
333 CERROR("Trying to sub dev %d - dev no too large\n",
335 GOTO(err_disconnect, rc = -EINVAL);
338 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
340 CERROR("cannot connect to device %d: rc = %d\n", tmp,
342 GOTO(err_disconnect, rc);
345 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
346 obd_dev[tmp].obd_type->typ_name);
351 obddev->obd_multi_count = count;
356 for (count--; count >= 0; count--)
357 obd_disconnect(&obddev->obd_multi_conn[count]);
362 * remove all connections to this device
363 * close all connections to lower devices
364 * needed for forced unloads of OBD client drivers
366 int class_multi_cleanup(struct obd_device *obddev)
370 for (i = 0; i < obddev->obd_multi_count; i++) {
372 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
375 CERROR("no such device [i %d]\n", i);
379 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
381 CERROR("disconnect failure %d\n", obd->obd_minor);