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/module.h>
15 #include <linux/obd_class.h>
16 #include <linux/random.h>
17 #include <linux/slab.h>
19 extern struct list_head obd_types;
20 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
21 kmem_cache_t *obdo_cachep = NULL;
22 kmem_cache_t *export_cachep = NULL;
23 kmem_cache_t *import_cachep = NULL;
26 * support functions: we could use inter-module communication, but this
27 * is more portable to other OS's
29 static struct obd_type *class_search_type(char *nm)
31 struct list_head *tmp;
32 struct obd_type *type;
33 CDEBUG(D_INFO, "SEARCH %s\n", nm);
36 while ( (tmp = tmp->next) != &obd_types ) {
37 type = list_entry(tmp, struct obd_type, typ_chain);
38 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
39 if (strlen(type->typ_name) == strlen(nm) &&
40 strcmp(type->typ_name, nm) == 0 ) {
47 struct obd_type *class_nm_to_type(char *nm)
49 struct obd_type *type = class_search_type(nm);
53 if ( !request_module(nm) ) {
54 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
55 type = obd_search_type(nm);
57 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
64 int class_register_type(struct obd_ops *ops, char *nm)
66 struct obd_type *type;
70 if (class_nm_to_type(nm)) {
71 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
75 OBD_ALLOC(type, sizeof(*type));
78 INIT_LIST_HEAD(&type->typ_chain);
80 list_add(&type->typ_chain, obd_types.next);
86 int class_unregister_type(char *nm)
88 struct obd_type *type = class_nm_to_type(nm);
94 CERROR("unknown obd type\n");
98 if ( type->typ_refcnt ) {
100 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
104 list_del(&type->typ_chain);
105 OBD_FREE(type, sizeof(*type));
108 } /* class_unregister_type */
110 int class_name2dev(char *name)
115 for (i=0; i < MAX_OBD_DEVICES; i++) {
116 struct obd_device *obd = &obd_dev[i];
117 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
126 int class_uuid2dev(char *name)
131 for (i=0; i < MAX_OBD_DEVICES; i++) {
132 struct obd_device *obd = &obd_dev[i];
133 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
142 void obd_cleanup_caches(void)
147 rc = kmem_cache_destroy(obdo_cachep);
149 CERROR("Cannot destory obdo_cachep\n");
153 rc = kmem_cache_destroy(import_cachep);
155 CERROR("Cannot destory import_cachep\n");
156 import_cachep = NULL;
159 rc = kmem_cache_destroy(export_cachep);
161 CERROR("Cannot destory import_cachep\n");
162 export_cachep = NULL;
167 int obd_init_caches(void)
170 if (obdo_cachep == NULL) {
171 obdo_cachep = kmem_cache_create("obdo_cache",
173 0, SLAB_HWCACHE_ALIGN,
175 if (obdo_cachep == NULL)
179 if (export_cachep == NULL) {
180 export_cachep = kmem_cache_create("export_cache",
181 sizeof(struct obd_export),
182 0, SLAB_HWCACHE_ALIGN,
184 if (export_cachep == NULL)
188 if (import_cachep == NULL) {
189 import_cachep = kmem_cache_create("import_cache",
190 sizeof(struct obd_import),
191 0, SLAB_HWCACHE_ALIGN,
193 if (import_cachep == NULL)
198 obd_cleanup_caches();
203 /* map connection to client */
204 struct obd_export *class_conn2export(struct lustre_handle *conn)
206 struct obd_export * export;
211 if (!conn->addr || conn->addr == -1 ) {
216 export = (struct obd_export *) (unsigned long)conn->addr;
217 if (!kmem_cache_validate(export_cachep, (void *)export))
220 if (export->export_cookie != conn->cookie)
223 } /* class_conn2export */
225 struct obd_device *class_conn2obd(struct lustre_handle *conn)
227 struct obd_export *export;
228 export = class_conn2export(conn);
230 return export->export_obd;
235 /* a connection defines an export context in which preallocation can
237 int class_connect (struct lustre_handle *conn, struct obd_device *obd)
239 struct obd_export * export;
241 export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
243 CERROR("no memory! (minor %d)\n", obd->obd_minor);
247 memset(export, 0, sizeof(*export));
248 get_random_bytes(&export->export_cookie, sizeof(__u64));
249 /* XXX this should probably spinlocked? */
250 export->export_id = ++obd->obd_gen_last_id;
251 export->export_obd = obd;
252 export->export_import.addr = conn->addr;
253 export->export_import.cookie = conn->cookie;
255 list_add(&(export->export_chain), export->export_obd->obd_exports.prev);
257 CDEBUG(D_INFO, "connect: new ID %u\n", export->export_id);
258 conn->addr = (__u64) (unsigned long)export;
259 conn->cookie = export->export_cookie;
261 } /* class_connect */
263 int class_disconnect(struct lustre_handle *conn)
265 struct obd_export * export;
268 if (!(export = class_conn2export(conn))) {
270 CDEBUG(D_IOCTL, "disconnect: attempting to free "
271 "nonexistent client %Lx\n", conn->addr);
274 list_del(&export->export_chain);
275 kmem_cache_free(export_cachep, export);
278 } /* gen_obd_disconnect */
280 /* FIXME: Data is a space- or comma-separated list of device IDs. This will
282 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
288 for (p = data, count = 0; p < (char *)data + len; count++) {
290 int tmp = simple_strtoul(p, &end, 0);
293 CERROR("invalid device ID starting at: %s\n", p);
294 GOTO(err_disconnect, rc = -EINVAL);
297 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
298 CERROR("Trying to sub dev %d - dev no too large\n",
300 GOTO(err_disconnect, rc);
303 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
305 CERROR("cannot connect to device %d: rc = %d\n", tmp,
307 GOTO(err_disconnect, rc);
310 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
311 obd_dev[tmp].obd_type->typ_name);
316 obddev->obd_multi_count = count;
321 for (count--; count >= 0; count--)
322 obd_disconnect(&obddev->obd_multi_conn[count]);
327 * remove all connections to this device
328 * close all connections to lower devices
329 * needed for forced unloads of OBD client drivers
331 int class_multi_cleanup(struct obd_device *obddev)
335 for (i = 0; i < obddev->obd_multi_count; i++) {
337 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
340 CERROR("no such device [i %d]\n", i);
344 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
346 CERROR("disconnect failure %d\n", obd->obd_minor);