Whamcloud - gitweb
tidy runfailure-ost a touch
[fs/lustre-release.git] / lustre / obdclass / genops.c
1 /*
2  *  linux/fs/ext2_obd/sim_obd.c
3  * Copyright (C) 2001  Cluster File Systems, Inc.
4  *
5  * This code is issued under the GNU General Public License.
6  * See the file COPYING in this distribution
7  *
8  * These are the only exported functions, they provide some generic
9  * infrastructure for managing object devices
10  *
11  */
12
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>
19
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;
25
26 /*
27  * support functions: we could use inter-module communication, but this
28  * is more portable to other OS's
29  */
30 static struct obd_type *class_search_type(char *nm)
31 {
32         struct list_head *tmp;
33         struct obd_type *type;
34         CDEBUG(D_INFO, "SEARCH %s\n", nm);
35
36         tmp = &obd_types;
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 ) {
42                         return type;
43                 }
44         }
45         return NULL;
46 }
47
48 struct obd_type *class_nm_to_type(char *nm)
49 {
50         struct obd_type *type = class_search_type(nm);
51
52 #ifdef CONFIG_KMOD
53         if ( !type ) {
54                 if ( !request_module(nm) ) {
55                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
56                         type = class_search_type(nm);
57                 } else {
58                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
59                 }
60         }
61 #endif
62         return type;
63 }
64
65 int class_register_type(struct obd_ops *ops, char *nm)
66 {
67         struct obd_type *type;
68
69         ENTRY;
70
71         if (class_nm_to_type(nm)) {
72                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
73                 RETURN(-EEXIST);
74         }
75
76         OBD_ALLOC(type, sizeof(*type));
77         if (!type)
78                 RETURN(-ENOMEM);
79         INIT_LIST_HEAD(&type->typ_chain);
80         MOD_INC_USE_COUNT;
81         list_add(&type->typ_chain, obd_types.next);
82         type->typ_ops = ops;
83         type->typ_name = nm;
84         RETURN(0);
85 }
86
87 int class_unregister_type(char *nm)
88 {
89         struct obd_type *type = class_nm_to_type(nm);
90
91         ENTRY;
92
93         if ( !type ) {
94                 MOD_DEC_USE_COUNT;
95                 CERROR("unknown obd type\n");
96                 RETURN(-EINVAL);
97         }
98
99         if ( type->typ_refcnt ) {
100                 MOD_DEC_USE_COUNT;
101                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
102                 RETURN(-EBUSY);
103         }
104
105         list_del(&type->typ_chain);
106         OBD_FREE(type, sizeof(*type));
107         MOD_DEC_USE_COUNT;
108         RETURN(0);
109 } /* class_unregister_type */
110
111 int class_name2dev(char *name)
112 {
113         int res = -1;
114         int i;
115
116         if (!name)
117             return -1;
118
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) {
122                         res = i;
123                         return res;
124                 }
125         }
126
127         return res;
128 }
129
130 int class_uuid2dev(char *name)
131 {
132         int res = -1;
133         int i;
134
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) {
138                         res = i;
139                         return res;
140                 }
141         }
142
143         return res;
144 }
145
146
147 struct obd_device *class_uuid2obd(char *name)
148 {
149         int i;
150
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) {
154                         return obd;
155                 }
156         }
157
158         return NULL;
159 }
160
161 void obd_cleanup_caches(void)
162 {
163         int rc;
164         ENTRY;
165         if (obdo_cachep) { 
166                 rc = kmem_cache_destroy(obdo_cachep);
167                 if (rc)
168                         CERROR("Cannot destory obdo_cachep\n");
169                 obdo_cachep = NULL;
170         }
171         if (import_cachep) { 
172                 rc = kmem_cache_destroy(import_cachep);
173                 if (rc)
174                         CERROR("Cannot destory import_cachep\n");
175                 import_cachep = NULL;
176         }
177         if (export_cachep) { 
178                 rc = kmem_cache_destroy(export_cachep);
179                 if (rc)
180                         CERROR("Cannot destory import_cachep\n");
181                 export_cachep = NULL;
182         }
183         EXIT;
184 }
185
186 int obd_init_caches(void)
187 {
188         ENTRY;
189         if (obdo_cachep == NULL) {
190                 obdo_cachep = kmem_cache_create("obdo_cache",
191                                                 sizeof(struct obdo),
192                                                 0, SLAB_HWCACHE_ALIGN,
193                                                 NULL, NULL);
194                 if (obdo_cachep == NULL)
195                         GOTO(out, -ENOMEM);
196         }
197
198         if (export_cachep == NULL) {
199                 export_cachep = kmem_cache_create("export_cache",
200                                                 sizeof(struct obd_export),
201                                                 0, SLAB_HWCACHE_ALIGN,
202                                                 NULL, NULL);
203                 if (export_cachep == NULL)
204                         GOTO(out, -ENOMEM);
205         }
206
207         if (import_cachep == NULL) {
208                 import_cachep = kmem_cache_create("import_cache",
209                                                 sizeof(struct obd_import),
210                                                 0, SLAB_HWCACHE_ALIGN,
211                                                 NULL, NULL);
212                 if (import_cachep == NULL)
213                         GOTO(out, -ENOMEM);
214         }
215         RETURN(0);
216  out:
217         obd_cleanup_caches();
218         RETURN(-ENOMEM);
219
220 }
221
222 /* map connection to client */
223 struct obd_export *class_conn2export(struct lustre_handle *conn)
224 {
225         struct obd_export * export;
226
227         if (!conn)
228                 RETURN(NULL);
229
230         if (!conn->addr || conn->addr == -1 ) { 
231                 fixme();
232                 RETURN(NULL);
233         }
234               
235         export = (struct obd_export *) (unsigned long)conn->addr;
236         if (!kmem_cache_validate(export_cachep, (void *)export))
237                 RETURN(NULL);
238
239         if (export->export_cookie != conn->cookie)
240                 return NULL;
241         return export;
242 } /* class_conn2export */
243
244 struct obd_device *class_conn2obd(struct lustre_handle *conn)
245 {
246         struct obd_export *export;
247         export = class_conn2export(conn); 
248         if (export) 
249                 return export->export_obd;
250         fixme();
251         return NULL;
252 }
253
254 /* a connection defines an export context in which preallocation can
255    be managed. */
256 int class_connect (struct lustre_handle *conn, struct obd_device *obd)
257 {
258         struct obd_export * export;
259
260         export = kmem_cache_alloc(export_cachep, GFP_KERNEL); 
261         if ( !export ) {
262                 CERROR("no memory! (minor %d)\n", obd->obd_minor);
263                 return -ENOMEM;
264         }
265
266         memset(export, 0, sizeof(*export));
267         get_random_bytes(&export->export_cookie, sizeof(__u64));
268         export->export_obd = obd; 
269         export->export_import.addr = conn->addr;
270         export->export_import.cookie = conn->cookie;
271         
272         list_add(&(export->export_chain), export->export_obd->obd_exports.prev);
273         conn->addr = (__u64) (unsigned long)export;
274         conn->cookie = export->export_cookie;
275         return 0;
276
277
278 int class_disconnect(struct lustre_handle *conn)
279 {
280         struct obd_export * export;
281         ENTRY;
282
283         if (!(export = class_conn2export(conn))) {
284                 fixme();
285                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
286                        "nonexistent client %Lx\n", conn->addr);
287                 RETURN(-EINVAL);
288         }
289         list_del(&export->export_chain);
290         kmem_cache_free(export_cachep, export);
291
292         RETURN(0);
293
294
295 #if 0
296
297 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
298  * have to change. */
299 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
300 {
301         int count, rc;
302         char *p;
303         ENTRY;
304
305         for (p = data, count = 0; p < (char *)data + len; count++) {
306                 char *end;
307                 int tmp = simple_strtoul(p, &end, 0);
308
309                 if (p == end) {
310                         CERROR("invalid device ID starting at: %s\n", p);
311                         GOTO(err_disconnect, rc = -EINVAL);
312                 }
313                 
314                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) { 
315                         CERROR("Trying to sub dev %d  - dev no too large\n", 
316                                tmp);
317                         GOTO(err_disconnect, rc  = -EINVAL); 
318                 }
319
320                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
321                 if (rc) {
322                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
323                                rc);
324                         GOTO(err_disconnect, rc);
325                 }
326
327                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
328                        obd_dev[tmp].obd_type->typ_name);
329
330                 p = end + 1;
331         }
332
333         obddev->obd_multi_count = count;
334
335         RETURN(0);
336
337  err_disconnect:
338         for (count--; count >= 0; count--)
339                 obd_disconnect(&obddev->obd_multi_conn[count]);
340         return rc;
341 }
342
343 /*
344  *    remove all connections to this device
345  *    close all connections to lower devices
346  *    needed for forced unloads of OBD client drivers
347  */
348 int class_multi_cleanup(struct obd_device *obddev)
349 {
350         int i;
351
352         for (i = 0; i < obddev->obd_multi_count; i++) {
353                 int rc;
354                 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
355
356                 if (!obd) { 
357                         CERROR("no such device [i %d]\n", i); 
358                         RETURN(-EINVAL);
359                 }
360
361                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
362                 if (rc)
363                         CERROR("disconnect failure %d\n", obd->obd_minor);
364         }
365         return 0;
366 }
367 #endif