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