Whamcloud - gitweb
WARNING: we currently crash on unmount after the last phase of runtests.
[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 *uuid)
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 (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0) {
146                         res = i;
147                         return res;
148                 }
149         }
150
151         return res;
152 }
153
154
155 struct obd_device *class_uuid2obd(char *uuid)
156 {
157         int i;
158
159         for (i=0; i < MAX_OBD_DEVICES; i++) {
160                 struct obd_device *obd = &obd_dev[i];
161                 if (strncmp(uuid, obd->obd_uuid, sizeof(obd->obd_uuid)) == 0)
162                         return obd;
163         }
164
165         return NULL;
166 }
167
168 void obd_cleanup_caches(void)
169 {
170         int rc;
171         ENTRY;
172         if (obdo_cachep) {
173                 rc = kmem_cache_destroy(obdo_cachep);
174                 if (rc)
175                         CERROR("Cannot destory obdo_cachep\n");
176                 obdo_cachep = NULL;
177         }
178         if (import_cachep) {
179                 rc = kmem_cache_destroy(import_cachep);
180                 if (rc)
181                         CERROR("Cannot destory import_cachep\n");
182                 import_cachep = NULL;
183         }
184         if (export_cachep) {
185                 rc = kmem_cache_destroy(export_cachep);
186                 if (rc)
187                         CERROR("Cannot destory import_cachep\n");
188                 export_cachep = NULL;
189         }
190         EXIT;
191 }
192
193 int obd_init_caches(void)
194 {
195         ENTRY;
196         if (obdo_cachep == NULL) {
197                 obdo_cachep = kmem_cache_create("obdo_cache",
198                                                 sizeof(struct obdo),
199                                                 0, SLAB_HWCACHE_ALIGN,
200                                                 NULL, NULL);
201                 if (obdo_cachep == NULL)
202                         GOTO(out, -ENOMEM);
203         }
204
205         if (export_cachep == NULL) {
206                 export_cachep = kmem_cache_create("export_cache",
207                                                 sizeof(struct obd_export),
208                                                 0, SLAB_HWCACHE_ALIGN,
209                                                 NULL, NULL);
210                 if (export_cachep == NULL)
211                         GOTO(out, -ENOMEM);
212         }
213
214         if (import_cachep == NULL) {
215                 import_cachep = kmem_cache_create("import_cache",
216                                                 sizeof(struct obd_import),
217                                                 0, SLAB_HWCACHE_ALIGN,
218                                                 NULL, NULL);
219                 if (import_cachep == NULL)
220                         GOTO(out, -ENOMEM);
221         }
222         RETURN(0);
223  out:
224         obd_cleanup_caches();
225         RETURN(-ENOMEM);
226
227 }
228
229 /* map connection to client */
230 struct obd_export *class_conn2export(struct lustre_handle *conn)
231 {
232         struct obd_export *export;
233
234         if (!conn) {
235                 CDEBUG(D_CACHE, "looking for null handle\n");
236                 RETURN(NULL);
237         }
238
239         if (conn->addr == -1) {  /* this means assign a new connection */
240                 CDEBUG(D_CACHE, "want a new connection\n");
241                 RETURN(NULL);
242         }
243
244         if (!conn->addr) {
245                 CDEBUG(D_CACHE, "looking for null addr\n");
246                 fixme();
247                 RETURN(NULL);
248         }
249
250         CDEBUG(D_IOCTL, "looking for export addr %Lx cookie %Lx\n",
251                conn->addr, conn->cookie);
252         export = (struct obd_export *) (unsigned long)conn->addr;
253         if (!kmem_cache_validate(export_cachep, (void *)export))
254                 RETURN(NULL);
255
256         if (export->exp_cookie != conn->cookie)
257                 return NULL;
258         return export;
259 } /* class_conn2export */
260
261 struct obd_device *class_conn2obd(struct lustre_handle *conn)
262 {
263         struct obd_export *export;
264         export = class_conn2export(conn);
265         if (export)
266                 return export->exp_obd;
267         fixme();
268         return NULL;
269 }
270
271 struct obd_import *class_conn2cliimp(struct lustre_handle *conn)
272 {
273         return &class_conn2obd(conn)->u.cli.cl_import;
274 }
275
276 struct obd_import *class_conn2ldlmimp(struct lustre_handle *conn)
277 {
278         return &class_conn2export(conn)->exp_ldlm_data.led_import;
279 }
280
281 struct obd_export *class_new_export(struct obd_device *obddev)
282 {
283         struct obd_export * export;
284
285         export = kmem_cache_alloc(export_cachep, GFP_KERNEL);
286         if ( !export ) {
287                 CERROR("no memory! (minor %d)\n", obddev->obd_minor);
288                 return NULL;
289         }
290
291         memset(export, 0, sizeof(*export));
292         get_random_bytes(&export->exp_cookie, sizeof(__u64));
293         export->exp_obd = obddev;
294         /* XXX should these be in MDS and LDLM init functions? */
295         INIT_LIST_HEAD(&export->exp_mds_data.med_open_head);
296         INIT_LIST_HEAD(&export->exp_ldlm_data.led_held_locks);
297         INIT_LIST_HEAD(&export->exp_conn_chain);
298         spin_lock(&obddev->obd_dev_lock);
299         list_add(&export->exp_obd_chain, &export->exp_obd->obd_exports);
300         spin_unlock(&obddev->obd_dev_lock);
301         return export;
302 }
303
304 void class_destroy_export(struct obd_export *exp)
305 {
306         int rc;
307         ENTRY;
308
309         spin_lock(&exp->exp_obd->obd_dev_lock);
310         list_del(&exp->exp_obd_chain);
311         spin_unlock(&exp->exp_obd->obd_dev_lock);
312
313         spin_lock(&exp->exp_connection->c_lock);
314         list_del(&exp->exp_conn_chain);
315         spin_unlock(&exp->exp_connection->c_lock);
316         
317         /* XXXshaver these bits want to be hung off the export, instead of
318          * XXXshaver hard-coded here.
319          */
320         if (mds_destroy_export) {
321                 rc = mds_destroy_export(exp);
322                 if (rc)
323                         CERROR("error freeing mds client data: rc = %d\n", rc);
324         }
325         if (ldlm_destroy_export) {
326                 rc = ldlm_destroy_export(exp);
327                 if (rc)
328                         CERROR("error freeing dlm client data: rc = %d\n", rc);
329         }
330         kmem_cache_free(export_cachep, exp);
331
332         EXIT;
333 }
334
335 /* a connection defines an export context in which preallocation can
336    be managed. */
337 int class_connect (struct lustre_handle *conn, struct obd_device *obd,
338                    char *cluuid)
339 {
340         struct obd_export * export;
341         if (conn == NULL) {
342                 LBUG();
343                 return -EINVAL;
344         }
345
346         if (obd == NULL) {
347                 LBUG();
348                 return -EINVAL;
349         }
350
351         export = class_new_export(obd);
352         if (!export)
353                 return -ENOMEM;
354
355
356         conn->addr = (__u64) (unsigned long)export;
357         conn->cookie = export->exp_cookie;
358         
359         CDEBUG(D_IOCTL, "connect: addr %Lx cookie %Lx\n",
360                (long long)conn->addr, (long long)conn->cookie);
361         return 0;
362 }
363
364 int class_disconnect(struct lustre_handle *conn)
365 {
366         struct obd_export *export;
367         ENTRY;
368
369         if (!(export = class_conn2export(conn))) {
370                 fixme();
371                 CDEBUG(D_IOCTL, "disconnect: attempting to free "
372                        "nonexistent client %Lx\n", conn->addr);
373                 RETURN(-EINVAL);
374         } else
375                 CDEBUG(D_IOCTL, "disconnect: addr %Lx cookie %Lx\n",
376                        (long long)conn->addr, (long long)conn->cookie);
377
378         class_destroy_export(export);
379
380         RETURN(0);
381 }
382
383 void class_disconnect_all(struct obd_device *obddev)
384 {
385         struct list_head *tmp;
386         int again = 1;
387
388         while (again) {
389                 spin_lock(&obddev->obd_dev_lock);
390                 if (!list_empty(&obddev->obd_exports)) {
391                         struct obd_export *export;
392                         struct lustre_handle conn;
393                         int rc;
394
395                         export = list_entry(tmp, struct obd_export, 
396                                             exp_obd_chain);
397                         conn.addr = (__u64)(unsigned long)export;
398                         conn.cookie = export->exp_cookie;
399                         spin_unlock(&obddev->obd_dev_lock);
400                         CERROR("force disconnecting export %p\n", export);
401                         rc = obd_disconnect(&conn);
402                         if (rc < 0) {
403                                 /* AED: not so sure about this...  We can't
404                                  * loop here forever, yet we shouldn't leak
405                                  * exports on a struct we will soon destroy.
406                                  */
407                                 CERROR("destroy export %p with err: rc = %d\n",
408                                        export, rc);
409                                 class_destroy_export(export);
410                         }
411                 } else {
412                         spin_unlock(&obddev->obd_dev_lock);
413                         again = 0;
414                 }
415         }
416 }
417
418 #if 0
419
420 /* FIXME: Data is a space- or comma-separated list of device IDs.  This will
421  * have to change. */
422 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data)
423 {
424         int count, rc;
425         char *p;
426         ENTRY;
427
428         for (p = data, count = 0; p < (char *)data + len; count++) {
429                 char *end;
430                 int tmp = simple_strtoul(p, &end, 0);
431
432                 if (p == end) {
433                         CERROR("invalid device ID starting at: %s\n", p);
434                         GOTO(err_disconnect, rc = -EINVAL);
435                 }
436
437                 if (tmp < 0 || tmp >= MAX_OBD_DEVICES) {
438                         CERROR("Trying to sub dev %d  - dev no too large\n",
439                                tmp);
440                         GOTO(err_disconnect, rc  = -EINVAL);
441                 }
442
443                 rc = obd_connect(&obddev->obd_multi_conn[count], &obd_dev[tmp]);
444                 if (rc) {
445                         CERROR("cannot connect to device %d: rc = %d\n", tmp,
446                                rc);
447                         GOTO(err_disconnect, rc);
448                 }
449
450                 CDEBUG(D_INFO, "target OBD %d is of type %s\n", count,
451                        obd_dev[tmp].obd_type->typ_name);
452
453                 p = end + 1;
454         }
455
456         obddev->obd_multi_count = count;
457
458         RETURN(0);
459
460  err_disconnect:
461         for (count--; count >= 0; count--)
462                 obd_disconnect(&obddev->obd_multi_conn[count]);
463         return rc;
464 }
465
466 /*
467  *    remove all connections to this device
468  *    close all connections to lower devices
469  *    needed for forced unloads of OBD client drivers
470  */
471 int class_multi_cleanup(struct obd_device *obddev)
472 {
473         int i;
474
475         for (i = 0; i < obddev->obd_multi_count; i++) {
476                 int rc;
477                 struct obd_device *obd = class_conn2obd(&obddev->obd_multi_conn[i]);
478
479                 if (!obd) {
480                         CERROR("no such device [i %d]\n", i);
481                         RETURN(-EINVAL);
482                 }
483
484                 rc = obd_disconnect(&obddev->obd_multi_conn[i]);
485                 if (rc)
486                         CERROR("disconnect failure %d\n", obd->obd_minor);
487         }
488         return 0;
489 }
490 #endif