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