Whamcloud - gitweb
Added a ``modules [path]'' command to obdctl, which is complete modulo the
[fs/lustre-release.git] / lustre / obdclass / class_obd.c
1 /*
2  *              An implementation of a loadable kernel mode driver providing
3  *              multiple kernel/user space bidirectional communications links.
4  *
5  *              Author:         Alan Cox <alan@cymru.net>
6  *
7  *              This program is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              version 2 as published by the Free Software Foundation.
10  * 
11  *              Adapted to become the Linux 2.0 Coda pseudo device
12  *              Peter  Braam  <braam@maths.ox.ac.uk> 
13  *              Michael Callahan <mjc@emmy.smith.edu>           
14  *
15  *              Changes for Linux 2.1
16  *              Copyright (c) 1997 Carnegie-Mellon University
17  *
18  *              Redone again for Intermezzo
19  *              Copyright (c) 1998 Peter J. Braam
20  *
21  *              Hacked up again for simulated OBD
22  *              Copyright (c) 1999 Stelias Computing, Inc.
23  *                (authors {pschwan,braam}@stelias.com)
24  *              Copyright (C) 1999 Seagate Technology, Inc.
25  *              Copyright (C) 2001 Cluster File Systems, Inc.
26  *
27  * 
28  */
29
30 #define EXPORT_SYMTAB
31 #include <linux/config.h> /* for CONFIG_PROC_FS */
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/major.h>
36 #include <linux/kmod.h>   /* for request_module() */
37 #include <linux/sched.h>
38 #include <linux/lp.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/fcntl.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/proc_fs.h>
45 #include <linux/fs.h>
46 #include <linux/poll.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
49 #include <asm/io.h>
50 #include <asm/system.h>
51 #include <asm/poll.h>
52 #include <asm/uaccess.h>
53 #include <linux/miscdevice.h>
54
55 #include <linux/obd_support.h>
56 #include <linux/obd_class.h>
57
58 static int obd_init_magic;
59 int obd_print_entry = 1;
60 int obd_debug_level = ~0;
61 long obd_memory = 0;
62 struct obd_device obd_dev[MAX_OBD_DEVICES];
63 struct list_head obd_types;
64
65 /*  opening /dev/obd */
66 static int obd_class_open(struct inode * inode, struct file * file)
67 {
68         ENTRY;
69
70         file->private_data = NULL;
71         MOD_INC_USE_COUNT;
72         EXIT;
73         return 0;
74 }
75
76 /*  closing /dev/obd */
77 static int obd_class_release(struct inode * inode, struct file * file)
78 {
79         ENTRY;
80
81         if (file->private_data)
82                 file->private_data = NULL;
83
84         MOD_DEC_USE_COUNT;
85         EXIT;
86         return 0;
87 }
88
89 /* 
90  * support functions: we could use inter-module communication, but this 
91  * is more portable to other OS's
92  */
93 static struct obd_type *obd_search_type(char *nm)
94 {
95         struct list_head *tmp;
96         struct obd_type *type;
97         CDEBUG(D_INFO, "SEARCH %s\n", nm);
98         
99         tmp = &obd_types;
100         while ( (tmp = tmp->next) != &obd_types ) {
101                 type = list_entry(tmp, struct obd_type, typ_chain);
102                 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
103                 if (strlen(type->typ_name) == strlen(nm) &&
104                     strcmp(type->typ_name, nm) == 0 ) {
105                         return type;
106                 }
107         }
108         return NULL;
109 }
110
111 static struct obd_type *obd_nm_to_type(char *nm) 
112 {
113         struct obd_type *type = obd_search_type(nm);
114
115 #ifdef CONFIG_KMOD
116         if ( !type ) {
117                 if ( !request_module(nm) ) {
118                         CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
119                         type = obd_search_type(nm);
120                 } else {
121                         CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
122                 }
123         }
124 #endif
125         return type;
126 }
127
128 /* to control /dev/obd */
129 static int obd_class_ioctl (struct inode * inode, struct file * filp, 
130                             unsigned int cmd, unsigned long arg)
131 {
132         /* NOTE this must be larger than any of the ioctl data structs */
133         char buf[1024];
134         struct obd_ioctl_data *data;
135         struct obd_device *obd = filp->private_data;
136         struct obd_conn conn;
137         int err = 0;
138         ENTRY;
139
140         memset(buf, 0, sizeof(buf));
141
142         if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
143                 printk("OBD ioctl: No device\n");
144                 return -EINVAL;
145         } 
146         if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) { 
147                 printk("OBD ioctl: data error\n");
148                 return -EINVAL;
149         }
150         data = (struct obd_ioctl_data *)buf;
151
152         switch (cmd) {
153         case TCGETS: { 
154                 EXIT;
155                 return -EINVAL;
156         }
157         case OBD_IOC_DEVICE: { 
158                 CDEBUG(D_IOCTL, "\n");
159                 if (data->ioc_dev >= MAX_OBD_DEVICES ||
160                     data->ioc_dev < 0) { 
161                         printk("OBD ioctl: DEVICE insufficient devices\n");
162                         return -EINVAL;
163                 }
164                 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
165
166                 filp->private_data = &obd_dev[data->ioc_dev];
167                 EXIT;
168                 return 0;
169         }
170
171         case OBD_IOC_ATTACH: {
172                 struct obd_type *type;
173
174                 ENTRY;
175                 /* have we attached a type to this device */
176                 if ( obd->obd_flags & OBD_ATTACHED ) {
177                         printk("OBD: Device %d already typed as  %s.\n",
178                                obd->obd_minor, MKSTR(obd->obd_type->typ_name));
179                         return -EBUSY;
180                 }
181
182                 printk("-----> attach %s %s\n",  MKSTR(data->ioc_inlbuf1), 
183                        MKSTR(data->ioc_inlbuf2));
184
185                 /* find the type */
186                 type = obd_nm_to_type(data->ioc_inlbuf1);
187                 if ( !type ) {
188                         printk("OBD: unknown type dev %d\n", obd->obd_minor);
189                         return -EINVAL;
190                 }
191
192                 obd->obd_type = type;
193                 obd->obd_multi_count = 0;
194                 INIT_LIST_HEAD(&obd->obd_gen_clients);
195
196                 /* do the attach */
197                 if ( OBT(obd) && OBP(obd, attach) ) {
198                         err = OBP(obd, attach)(obd, sizeof(*data), data);
199                 }
200
201                 if ( err ) {
202                         obd->obd_type = NULL;
203                         EXIT;
204                 } else {
205                         obd->obd_flags |=  OBD_ATTACHED;
206                         type->typ_refcnt++;
207                         printk("OBD: dev %d attached type %s\n", 
208                                obd->obd_minor, data->ioc_inlbuf1);
209                         obd->obd_proc_entry = 
210                                 proc_lustre_register_obd_device(obd);
211                         MOD_INC_USE_COUNT;
212                         EXIT;
213                 }
214
215                 return err;
216         }
217
218         case OBD_IOC_DETACH: {
219                 ENTRY;
220                 if (obd->obd_flags & OBD_SET_UP) {
221                         printk("OBD device %d still set up\n", obd->obd_minor);
222                         return -EBUSY;
223                 }
224                 if (! (obd->obd_flags & OBD_ATTACHED) ) {
225                         printk("OBD device %d not attached\n", obd->obd_minor);
226                         return -ENODEV;
227                 }
228                 if ( !list_empty(&obd->obd_gen_clients) ) {
229                         printk("OBD device %d has connected clients\n", obd->obd_minor);
230                         return -EBUSY;
231                 }
232
233                 if (obd->obd_proc_entry)
234                         proc_lustre_release_obd_device(obd);
235
236                 obd->obd_flags &= ~OBD_ATTACHED;
237                 obd->obd_type->typ_refcnt--;
238                 obd->obd_type = NULL;
239                 MOD_DEC_USE_COUNT;
240                 EXIT;
241                 return 0;
242         }
243
244         case OBD_IOC_SETUP: {
245                 ENTRY;
246                 /* have we attached a type to this device? */
247                 if (!(obd->obd_flags & OBD_ATTACHED)) {
248                         printk("Device %d not attached\n", obd->obd_minor);
249                         return -ENODEV;
250                 }
251
252                 /* has this been done already? */
253                 if ( obd->obd_flags & OBD_SET_UP ) {
254                         printk("Device %d already setup (type %s)\n",
255                                obd->obd_minor, obd->obd_type->typ_name);
256                         return -EBUSY;
257                 }
258
259                 if ( OBT(obd) && OBP(obd, setup) )
260                         err = OBP(obd, setup)(obd, sizeof(*data), data);
261
262                 if (!err) { 
263                         obd->obd_type->typ_refcnt++;
264                         obd->obd_flags |= OBD_SET_UP;
265                         EXIT;
266                 }
267
268                 return err;
269         }
270         case OBD_IOC_CLEANUP: {
271                 ENTRY;
272
273                 if ( !(obd->obd_flags & OBD_SET_UP) ) {
274                         EXIT;
275                         return -EINVAL;
276                 }
277
278                 err = obd_cleanup(obd);
279                 if ( err ) {
280                         EXIT;
281                         return err;
282                 }
283
284                 obd->obd_flags &= ~OBD_SET_UP;
285                 obd->obd_type->typ_refcnt--;
286                 EXIT;
287                 return 0;
288         }
289
290         case OBD_IOC_CONNECT:
291         {
292                 conn.oc_id = data->ioc_conn1;
293                 conn.oc_dev = obd; 
294
295                 err = obd_connect(&conn);
296
297                 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
298                 data->ioc_conn1 = conn.oc_id;
299                 if ( err )
300                         return err;
301
302                 return copy_to_user((int *)arg, data, sizeof(*data));
303         }
304
305         case OBD_IOC_DISCONNECT: { 
306                 conn.oc_id = data->ioc_conn1;
307                 conn.oc_dev = obd;
308
309                 err = obd_disconnect(&conn);
310                 return err;
311         }               
312
313         case OBD_IOC_DEC_USE_COUNT: { 
314                 MOD_DEC_USE_COUNT;
315                 return 0;
316         }
317
318         case OBD_IOC_CREATE: {
319                 conn.oc_id = data->ioc_conn1;
320                 conn.oc_dev = obd;
321
322                 err = obd_create(&conn, &data->ioc_obdo1);
323                 if (err) {
324                         EXIT;
325                         return err;
326                 }
327
328                 err = copy_to_user((int *)arg, data, sizeof(*data));
329                 EXIT;
330                 return err;
331         }
332
333         case OBD_IOC_GETATTR: {
334                 conn.oc_id = data->ioc_conn1;
335                 conn.oc_dev = obd;
336
337                 err = obd_getattr(&conn, &data->ioc_obdo1);
338                 if (err) {
339                         EXIT;
340                         return err;
341                 }
342
343                 err = copy_to_user((int *)arg, data, sizeof(*data));
344                 EXIT;
345                 return err;
346         }
347
348         case OBD_IOC_SETATTR: {
349                 conn.oc_id = data->ioc_conn1;
350                 conn.oc_dev = obd;
351
352                 err = obd_setattr(&conn, &data->ioc_obdo1);
353                 if (err) {
354                         EXIT;
355                         return err;
356                 }
357
358                 err = copy_to_user((int *)arg, data, sizeof(*data));
359                 EXIT;
360                 return err;
361         }
362
363         case OBD_IOC_DESTROY: {
364                 conn.oc_id = data->ioc_conn1;
365                 conn.oc_dev = obd;
366
367                 err = obd_destroy(&conn, &data->ioc_obdo1);
368                 if (err) {
369                         EXIT;
370                         return err;
371                 }
372
373                 err = copy_to_user((int *)arg, data, sizeof(*data));
374                 EXIT;
375                 return err;
376         }
377
378 #if 0
379         case OBD_IOC_SYNC: {
380                 struct oic_range_s *range = tmp_buf;
381
382                 if (!obd->obd_type)
383                         return -ENODEV;
384
385                 err = copy_from_user(range, (const void *)arg,  sizeof(*range));
386
387                 if ( err ) {
388                         EXIT;
389                         return err;
390                 }
391                         
392                 if ( !OBT(obd) || !OBP(obd, sync) ) {
393                         err = -EOPNOTSUPP;
394                         EXIT;
395                         return err;
396                 }
397
398                 /* XXX sync needs to be tested/verified */
399                 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
400                                         range->offset);
401
402                 if ( err ) {
403                         EXIT;
404                         return err;
405                 }
406                         
407                 return put_user(err, (int *) arg);
408         }
409
410         case OBD_IOC_READ: {
411                 int err;
412                 struct oic_rw_s *rw_s = tmp_buf;  /* read, write ioctl str */
413
414                 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
415                 if ( err ) {
416                         EXIT;
417                         return err;
418                 }
419
420                 conn.oc_id = rw_s->conn_id;
421
422                 if ( !OBT(obd) || !OBP(obd, read) ) {
423                         err = -EOPNOTSUPP;
424                         EXIT;
425                         return err;
426                 }
427
428
429                 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf, 
430                                         &rw_s->count, rw_s->offset);
431                 
432                 ODEBUG(&rw_s->obdo);
433                 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
434                        rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
435                 if ( err ) {
436                         EXIT;
437                         return err;
438                 }
439                         
440                 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
441                 EXIT;
442                 return err;
443         }
444
445         case OBD_IOC_WRITE: {
446                 struct oic_rw_s *rw_s = tmp_buf;  /* read, write ioctl str */
447
448                 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
449                 if ( err ) {
450                         EXIT;
451                         return err;
452                 }
453
454                 conn.oc_id = rw_s->conn_id;
455
456                 if ( !OBT(obd) || !OBP(obd, write) ) {
457                         err = -EOPNOTSUPP;
458                         return err;
459                 }
460
461                 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
462                        rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
463
464                 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf, 
465                                          &rw_s->count, rw_s->offset);
466                 ODEBUG(&rw_s->obdo);
467                 if ( err ) {
468                         EXIT;
469                         return err;
470                 }
471
472                 err = copy_to_user((int *)arg, &rw_s->count,
473                                    sizeof(rw_s->count));
474                 EXIT;
475                 return err;
476         }
477         case OBD_IOC_PREALLOCATE: {
478                 struct oic_prealloc_s *prealloc = tmp_buf;
479
480                 /* has this minor been registered? */
481                 if (!obd->obd_type)
482                         return -ENODEV;
483
484                 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
485                 if (err) 
486                         return -EFAULT;
487
488                 if ( !(obd->obd_flags & OBD_ATTACHED) ||
489                      !(obd->obd_flags & OBD_SET_UP)) {
490                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
491                         return -ENODEV;
492                 }
493
494                 if ( !OBT(obd) || !OBP(obd, preallocate) )
495                         return -EOPNOTSUPP;
496
497                 conn.oc_id = prealloc->conn_id;
498                 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
499                                                prealloc->ids);
500                 if ( err ) {
501                         EXIT;
502                         return err;
503                 }
504
505                 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
506                 EXIT;
507                 return err;
508         }
509         case OBD_IOC_STATFS: {
510                 struct statfs *tmp;
511                 unsigned int conn_id;
512                 struct statfs buf;
513
514                 /* has this minor been registered? */
515                 if (!obd->obd_type)
516                         return -ENODEV;
517
518                 tmp = (void *)arg + sizeof(unsigned int);
519                 get_user(conn_id, (int *) arg);
520
521                 if ( !OBT(obd) || !OBP(obd, statfs) )
522                         return -EOPNOTSUPP;
523
524                 conn.oc_id = conn_id;
525                 err = OBP(obd, statfs)(&conn, &buf);
526                 if ( err ) {
527                         EXIT;
528                         return err;
529                 }
530                 err = copy_to_user(tmp, &buf, sizeof(buf));
531                 EXIT;
532                 return err;
533                 
534         }
535         case OBD_IOC_COPY: {
536                 struct ioc_mv_s *mvdata = tmp_buf;
537
538                 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
539                      (!(obd->obd_flags & OBD_ATTACHED))) {
540                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
541                         return -ENODEV;
542                 }
543
544                 /* get main structure */
545                 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
546                 if (err) {
547                         EXIT;
548                         return err;
549                 }
550
551                 if ( !OBT(obd) || !OBP(obd, copy) )
552                         return -EOPNOTSUPP;
553
554                 /* do the partition */
555                 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev, 
556                        obd->obd_type->typ_name, mvdata->dst.o_id, 
557                        mvdata->src.o_id);
558
559                 conn.oc_id = mvdata->src_conn_id;
560
561                 err = OBP(obd, copy)(&conn, &mvdata->dst, 
562                                         &conn, &mvdata->src, 
563                                         mvdata->src.o_size, 0);
564                 return err;
565         }
566
567         case OBD_IOC_MIGR: {
568                 struct ioc_mv_s *mvdata = tmp_buf;
569
570                 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
571                      (!(obd->obd_flags & OBD_ATTACHED))) {
572                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
573                         return -ENODEV;
574                 }
575
576                 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
577                 if (err) {
578                         EXIT;
579                         return err;
580                 }
581
582                 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
583
584                 if ( !OBT(obd) || !OBP(obd, migrate) )
585                         return -EOPNOTSUPP;
586
587                 /* do the partition */
588                 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
589                        dev, obd->obd_type->typ_name, mvdata->src_conn_id,
590                        mvdata->src.o_id, mvdata->dst.o_id);
591
592                 conn.oc_id = mvdata->src_conn_id;
593                 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src, 
594                                            mvdata->src.o_size, 0);
595
596                 return err;
597         }
598         case OBD_IOC_PUNCH: {
599                 struct oic_rw_s *rw_s = tmp_buf;  /* read, write ioctl str */
600
601                 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
602                 if ( err ) {
603                         EXIT;
604                         return err;
605                 }
606
607                 conn.oc_id = rw_s->conn_id;
608
609                 if ( !OBT(obd) || !OBP(obd, punch) ) {
610                         err = -EOPNOTSUPP;
611                         return err;
612                 }
613
614                 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
615                        rw_s->conn_id, rw_s->count, rw_s->offset);
616                 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
617                                          rw_s->offset);
618                 ODEBUG(&rw_s->obdo);
619                 if ( err ) {
620                         EXIT;
621                         return err;
622                 }
623                 EXIT;
624                 return err;
625         }
626
627         default: {
628                 struct obd_type *type;
629                 struct oic_generic input;
630                 char *nm;
631                 void *karg;
632
633                 /* get data structures */
634                 err = copy_from_user(&input, (void *)arg, sizeof(input));
635                 if ( err ) {
636                         EXIT;
637                         return err;
638                 }
639
640                 err = getdata(input.att_typelen + 1, &input.att_type);
641                 if ( err ) {
642                         EXIT;
643                         return err;
644                 }
645
646                 /* find the type */
647                 nm = input.att_type;
648                 type = obd_nm_to_type(nm);
649 #ifdef CONFIG_KMOD
650                 if ( !type ) {
651                         if ( !request_module(nm) ) {
652                                 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
653                                 type = obd_nm_to_type(nm);
654                         } else {
655                                 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
656                         }
657                 }
658 #endif
659                 OBD_FREE(input.att_type, input.att_typelen + 1);
660                 if ( !type ) {
661                         printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
662                         EXIT;
663                         return -EINVAL;
664                 }
665                 
666                 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
667                         EXIT;
668                         return -EOPNOTSUPP;
669                 }
670                 conn.oc_id = input.att_connid;
671                 
672                 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
673                        cmd, type->typ_name, input.att_datalen);
674
675                 /* get the generic data */
676                 karg = input.att_data;
677                 err = getdata(input.att_datalen, &karg);
678                 if ( err ) {
679                         EXIT;
680                         return err;
681                 }
682
683                 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen, 
684                                                  karg, input.att_data);
685                 OBD_FREE(karg, input.att_datalen);
686
687                 EXIT;
688                 return err;
689         }
690 #endif 
691         default:
692                 return -EINVAL;
693
694         }
695 } /* obd_class_ioctl */
696
697
698 /* Driver interface done, utility functions follow */
699 int obd_register_type(struct obd_ops *ops, char *nm)
700 {
701         struct obd_type *type;
702
703         if (obd_init_magic != 0x11223344) {
704                 printk(__FUNCTION__ ": bad magic for type\n");
705                 EXIT;
706                 return -EINVAL;
707         }
708
709         if  ( obd_nm_to_type(nm) ) {
710                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
711                 EXIT;
712                 return -EEXIST;
713         }
714         
715         OBD_ALLOC(type, sizeof(*type));
716         if ( !type ) {
717                 EXIT;
718                 return -ENOMEM;
719         }
720         memset(type, 0, sizeof(*type));
721         INIT_LIST_HEAD(&type->typ_chain);
722         MOD_INC_USE_COUNT;
723         list_add(&type->typ_chain, obd_types.next);
724         type->typ_ops = ops;
725         type->typ_name = nm;
726         EXIT;
727         return 0;
728 }
729         
730 int obd_unregister_type(char *nm)
731 {
732         struct obd_type *type = obd_nm_to_type(nm);
733
734         if ( !type ) {
735                 MOD_DEC_USE_COUNT;
736                 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
737                 EXIT;
738                 return -EINVAL;
739         }
740
741         if ( type->typ_refcnt ) {
742                 MOD_DEC_USE_COUNT;
743                 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
744                        "(%d)\n", nm, type->typ_refcnt);
745                 EXIT;
746                 return -EBUSY;
747         }
748
749         list_del(&type->typ_chain);
750         OBD_FREE(type, sizeof(*type));
751         MOD_DEC_USE_COUNT;
752         return 0;
753 } /* obd_unregister_type */
754
755 /* declare character device */
756 static struct file_operations obd_psdev_fops = {
757         ioctl: obd_class_ioctl,       /* ioctl */
758         open: obd_class_open,        /* open */
759         release: obd_class_release,     /* release */
760 };
761
762 /* modules setup */
763 #define OBD_MINOR 241
764 static struct miscdevice obd_psdev = {
765         OBD_MINOR,
766         "obd_psdev",
767         &obd_psdev_fops
768 };
769
770 int init_obd(void)
771 {
772         int err;
773         int i;
774
775         printk(KERN_INFO "OBD class driver  v0.01, braam@stelias.com\n");
776         
777         INIT_LIST_HEAD(&obd_types);
778         
779         if ( (err = misc_register(&obd_psdev)) ) { 
780                 printk(KERN_ERR __FUNCTION__ ": cannot register %d err %d\n", 
781                        OBD_MINOR, err);
782                 return -EIO;
783         }
784
785         for (i = 0; i < MAX_OBD_DEVICES; i++) {
786                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
787                 obd_dev[i].obd_minor = i;
788                 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
789         }
790
791         err = obd_init_obdo_cache();
792         if (err)
793                 return err;
794         obd_sysctl_init();
795         obd_init_magic = 0x11223344;
796         return 0;
797 }
798
799 EXPORT_SYMBOL(obd_register_type);
800 EXPORT_SYMBOL(obd_unregister_type);
801
802 EXPORT_SYMBOL(obd_print_entry);
803 EXPORT_SYMBOL(obd_debug_level);
804 EXPORT_SYMBOL(obd_dev);
805
806 EXPORT_SYMBOL(gen_connect);
807 EXPORT_SYMBOL(gen_client);
808 EXPORT_SYMBOL(gen_cleanup);
809 EXPORT_SYMBOL(gen_disconnect);
810 EXPORT_SYMBOL(gen_copy_data); 
811 EXPORT_SYMBOL(obdo_cachep);
812
813 /* EXPORT_SYMBOL(gen_multi_attach); */
814 EXPORT_SYMBOL(gen_multi_setup);
815 EXPORT_SYMBOL(gen_multi_cleanup);
816 EXPORT_SYMBOL(obd_memory);
817
818 #ifdef MODULE
819 int init_module(void)
820 {
821         return init_obd();
822 }
823
824 void cleanup_module(void)
825 {
826         int i;
827         ENTRY;
828
829         misc_deregister(&obd_psdev);
830         for (i = 0; i < MAX_OBD_DEVICES; i++) {
831                 struct obd_device *obd = &obd_dev[i];
832                 if ( obd->obd_type && 
833                      (obd->obd_flags & OBD_SET_UP) &&
834                      OBT(obd) && OBP(obd, detach) ) {
835                         /* XXX should this call generic detach otherwise? */
836                         OBP(obd, detach)(obd);
837                 } 
838         }
839
840         obd_cleanup_obdo_cache();
841         obd_sysctl_clean();
842         CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);
843         obd_init_magic = 0;
844         EXIT;
845 }
846 #endif