Whamcloud - gitweb
Mike's fix for the missing exp_connection oops caused us to not remove
[fs/lustre-release.git] / lustre / obdclass / genops.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lustre/obdclass/genops.c
5  * Copyright (C) 2001-2002  Cluster File Systems, Inc.
6  *
7  * This code is issued under the GNU General Public License.
8  * See the file COPYING in this distribution
9  *
10  * These are the only exported functions, they provide some generic
11  * infrastructure for managing object devices
12  *
13  */
14
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>
21
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;
27
28 /*
29  * support functions: we could use inter-module communication, but this
30  * is more portable to other OS's
31  */
32 static struct obd_type *class_search_type(char *nm)
33 {
34         struct list_head *tmp;
35         struct obd_type *type;
36         CDEBUG(D_INFO, "SEARCH %s\n", nm);
37
38         tmp = &obd_types;
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 ) {
44                         return type;
45                 }
46         }
47         return NULL;
48 }
49
50 struct obd_type *class_nm_to_type(char *nm)
51 {
52         struct obd_type *type = class_search_type(nm);
53
54 #ifdef CONFIG_KMOD
55         if ( !type ) {
56                 if ( !request_module(nm) ) {
57                         CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
58                         type = class_search_type(nm);
59                 } else {
60                         CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
61                 }
62         }
63 #endif
64         return type;
65 }
66
67 int class_register_type(struct obd_ops *ops, char *nm)
68 {
69         struct obd_type *type;
70
71         ENTRY;
72
73         if (class_search_type(nm)) {
74                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
75                 RETURN(-EEXIST);
76         }
77
78         OBD_ALLOC(type, sizeof(*type));
79         OBD_ALLOC(type->typ_ops, sizeof(*type->typ_ops));
80         OBD_ALLOC(type->typ_name, strlen(nm) + 1);
81         if (!type)
82                 RETURN(-ENOMEM);
83         INIT_LIST_HEAD(&type->typ_chain);
84         MOD_INC_USE_COUNT;
85         list_add(&type->typ_chain, &obd_types);
86         memcpy(type->typ_ops, ops, sizeof(*type->typ_ops));
87         strcpy(type->typ_name, nm);
88         RETURN(0);
89 }
90
91 int class_unregister_type(char *nm)
92 {
93         struct obd_type *type = class_nm_to_type(nm);
94
95         ENTRY;
96
97         if (!type) {
98                 CERROR("unknown obd type\n");
99                 RETURN(-EINVAL);
100         }
101
102         if (type->typ_refcnt) {
103                 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
104                 /* This is a bad situation, let's make the best of it */
105                 /* Remove ops, but leave the name for debugging */
106                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
107                 RETURN(-EBUSY);
108         }
109
110         list_del(&type->typ_chain);
111         OBD_FREE(type->typ_name, strlen(nm) + 1);
112         if (type->typ_ops != NULL)
113                 OBD_FREE(type->typ_ops, sizeof(*type->typ_ops));
114         OBD_FREE(type, sizeof(*type));
115         MOD_DEC_USE_COUNT;
116         RETURN(0);
117 } /* class_unregister_type */
118
119 int class_name2dev(char *name)
120 {
121         int res = -1;
122         int i;
123
124         if (!name)
125                 return -1;
126
127         for (i=0; i < MAX_OBD_DEVICES; i++) {
128                 struct obd_device *obd = &obd_dev[i];
129                 if (obd->obd_name && strcmp(name, obd->obd_name) == 0) {
130                         res = i;
131                         return res;
132                 }
133         }
134
135         return res;
136 }
137
138 int class_uuid2dev(char *name)
139 {
140         int res = -1;
141         int i;
142
143         for (i=0; i < MAX_OBD_DEVICES; i++) {
144                 struct obd_device *obd = &obd_dev[i];
145                 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
146                         res = i;
147                         return res;
148                 }
149         }
150
151         return res;
152 }
153
154
155 struct obd_device *class_uuid2obd(char *name)
156 {
157         int i;
158
159         for (i=0; i < MAX_OBD_DEVICES; i++) {
160                 struct obd_device *obd = &obd_dev[i];
161                 if (obd->obd_name && strncmp(name, obd->obd_uuid, 37) == 0) {
162                         return obd;
163                 }
164         }
165
166         return NULL;
167 }
168
169 void obd_cleanup_caches(void)
170 {
171         int rc;
172         ENTRY;
173         if (obdo_cachep) {
174                 rc = kmem_cache_destroy(obdo_cachep);
175                 if (rc)
176                         CERROR("Cannot destory obdo_cachep\n");
177                 obdo_cachep = NULL;
178         }
179         if (import_cachep) {
180                 rc = kmem_cache_destroy(import_cachep);
181                 if (rc)
182                         CERROR("Cannot destory import_cachep\n");
183                 import_cachep = NULL;
184         }
185         if (export_cachep) {
186                 rc = kmem_cache_destroy(export_cachep);
187                 if (rc)
188                         CERROR("Cannot destory import_cachep\n");
189                 export_cachep = NULL;
190         }
191         EXIT;
192 }
193
194 int obd_init_caches(void)
195 {
196         ENTRY;
197         if (obdo_cachep == NULL) {
198                 obdo_cachep = kmem_cache_create("obdo_cache",
199                                                 sizeof(struct obdo),
200                                                 0, SLAB_HWCACHE_ALIGN,
201                                                 NULL, NULL);
202                 if (obdo_cachep == NULL)
203                         GOTO(out, -ENOMEM);
204         }
205
206         if (export_cachep == NULL) {
207                 export_cachep = kmem_cache_create("export_cache",
208                                                 sizeof(struct obd_export),
209                                                 0, SLAB_HWCACHE_ALIGN,
210                                                 NULL, NULL);
211                 if (export_cachep == NULL)
212                         GOTO(out, -ENOMEM);
213         }
214
215         if (import_cachep == NULL) {
216                 import_cachep = kmem_cache_create("import_cache",
217                                                 sizeof(struct obd_import),
218                                                 0, SLAB_HWCACHE_ALIGN,
219                                                 NULL, NULL);
220                 if (import_cachep == NULL)
221                         GOTO(out, -ENOMEM);
222         }
223         RETURN(0);
224  out:
225         obd_cleanup_caches();
226         RETURN(-ENOMEM);
227
228 }
229
230 /* map connection to client */
231 struct obd_export *class_conn2export(struct lustre_handle *conn)
232 {
233         struct obd_export *export;
234
235         if (!conn) {
236                 CDEBUG(D_CACHE, "looking for null handle\n");
237                 RETURN(NULL);
238         }
239
240         if (conn->addr == -1) {  /* this means assign a new connection */
241                 CDEBUG(D_CACHE, "want a new connection\n");
242                 RETURN(NULL);
243         }
244
245         if (!conn->addr) {
246                 CDEBUG(D_CACHE, "looking for null addr\n");
247                 fixme();
248                 RETURN(NULL);
249         }
250
251         CDEBUG(D_IOCTL, "looking for export addr %Lx cookie %Lx\n",
252                conn->addr, conn->cookie);
253         export = (struct obd_export *) (unsigned long)conn->addr;
254         if (!kmem_cache_validate(export_cachep, (void *)export))
255                 RETURN(NULL);
256
257         if (export->exp_cookie != conn->cookie)
258                 return NULL;
259         return export;
260 } /* class_conn2export */
261
262 struct obd_device *class_conn2obd(struct lustre_handle *conn)
263 {
264         struct obd_export *export;
265         export = class_conn2export(conn);
266         if (export)
267                 return export->exp_obd;
268         fixme();
269         return NULL;
270 }
271
272 struct obd_export *class_new_export(struct obd_device *obddev)
273 {
274         struct obd_export * export;
275
276         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
277         if ( !export ) {
278                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
279                 return NULL;
280         }
281
282         memset(export, 0, sizeof(*export));
283         get_random_bytes(&export->exp_cookie, sizeof(__u64));
284         export->exp_obd = obddev;
285         /* XXX should these be in MDS and LDLM init functions? */
286         INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
287         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
288         list_add(&export->exp_chain, &export->exp_obd->obd_exports);
289         return export;
290 }
291
292 void class_destroy_export(struct obd_export *exp)
293 {
294         int rc;
295         ENTRY;
296
297         //spin_lock(&exp->exp_connection->c_lock);
298         list_del(&exp->exp_chain);
299         //spin_unlock(&exp->exp_connection->c_lock);
300
301         /* XXXshaver these bits want to be hung off the export, instead of
302          * XXXshaver hard-coded here.
303          */
304         if (mds_destroy_export) {
305                 rc = mds_destroy_export(exp);
306                 if (rc)
307                         CERROR("error freeing mds client data: rc = %d\n", rc);
308         }
309         if (ldlm_destroy_export) {
310                 rc = ldlm_destroy_export(exp);
311                 if (rc)
312                         CERROR("error freeing dlm client data: rc = %d\n", rc);
313         }
314         kmem_cache_free(export_cachep, exp);
315
316         EXIT;
317 }
318
319 /* a connection defines an export context in which preallocation can
320    be managed. */
321 int class_connect (struct lustre_handle *conn, struct obd_device *obd,
322                    char *cluuid)
323 {
324         struct obd_export * export;
325         if (conn == NULL) {
326                 LBUG();
327                 return -EINVAL;
328         }
329
330         if (obd == NULL) {
331                 LBUG();
332                 return -EINVAL;
333         }
334
335         export = class_new_export(obd);
336         if (!export)
337                 return -ENOMEM;
338
339         export->exp_rconnh.addr = conn->addr;
340         export->exp_rconnh.cookie = conn->cookie;
341
342         conn->addr = (__u64) (unsigned long)export;
343         conn->cookie = export->exp_cookie;
344         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
345                (long long)conn->addr, (long long)conn->cookie);
346         return 0;
347 }
348
349 int class_rconn2export(struct lustre_handle *conn, struct lustre_handle *rconn)
350 {
351         struct obd_export *export = class_conn2export(conn);
352
353         if (!export)
354                 return -EINVAL;
355
356         export->exp_rconnh.addr = rconn->addr;
357         export->exp_rconnh.cookie = rconn->cookie;
358
359         return 0;
360 }
361
362 int class_disconnect(struct lustre_handle *conn)
363 {
364         struct obd_export *export;
365         ENTRY;
366
367         if (!(export = class_conn2export(conn))) {
368                 fixme();
369                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
370                        "nonexistent client %Lx\n", conn->addr);
371                 RETURN(-EINVAL);
372         } else
373                 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
374                        (long long)conn->addr, (long long)conn->cookie);
375
376         class_destroy_export(export);
377
378         RETURN(0);
379 }
380
381 void class_disconnect_all(struct obd_device *obddev)
382 {
383         struct list_head *tmp, *next;
384
385         list_for_each_safe(tmp, next, &obddev->obd_exports) {
386                 struct obd_export *export;
387                 struct lustre_handle conn;
388
389                 export = list_entry(tmp, struct obd_export, exp_chain);
390                 conn.addr = (__u64) (unsigned long)export;
391                 conn.cookie = export->exp_cookie;
392                 obd_disconnect(&conn);
393         }
394 }
395
396 #if 0
397
398 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
399  * have to change. */
400 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
401 {
402         int count, rc;
403         char *p;
404         ENTRY;
405
406         for (p = data, count = 0; p < (char *)data + len; count++) {
407                 char *end;
408                 int tmp = simple_strtoul(p, &end, 0);
409
410                 if (p == end) {
411                         CERROR("invalid device ID starting at: %s\n", p);
412                         GOTO(err_disconnect, rc = -EINVAL);
413                 }
414
415                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
416                         CERROR("Trying to sub dev %d  - dev no too large\n",
417                                tmp);
418                         GOTO(err_disconnect, rc  = -EINVAL);
419                 }
420
421                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
422                 if (rc) {
423                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
424                                rc);
425                         GOTO(err_disconnect, rc);
426                 }
427
428                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
429                        obd_dev[tmp].obd_type->typ_name);
430
431                 p = end + 1;
432         }
433
434         obddev->obd_multi_count = count;
435
436         RETURN(0);
437
438  err_disconnect:
439         for (count--; count >= 0; count--)
440                 obd_disconnect(&obddev->obd_multi_conn[count]);
441         return rc;
442 }
443
444 /*
445  *    remove all connections to this device
446  *    close all connections to lower devices
447  *    needed for forced unloads of OBD client drivers
448  */
449 int class_multi_cleanup(struct obd_device *obddev)
450 {
451         int i;
452
453         for (i = 0; i < obddev->obd_multi_count; i++) {
454                 int rc;
455                 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
456
457                 if (!obd) {
458                         CERROR("no such device [i %d]\n", i);
459                         RETURN(-EINVAL);
460                 }
461
462                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
463                 if (rc)
464                         CERROR("disconnect failure %d\n", obd->obd_minor);
465         }
466         return 0;
467 }
468 #endif