Whamcloud - gitweb
bb97aa56ae5687cf44f6a70ca859d2dc8bbad932
[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  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  * 
12  *              Adapted to become the Linux 2.0 Coda pseudo device
13  *              Peter  Braam  <braam@maths.ox.ac.uk> 
14  *              Michael Callahan <mjc@emmy.smith.edu>           
15  *
16  *              Changes for Linux 2.1
17  *              Copyright (c) 1997 Carnegie-Mellon University
18  *
19  *              Redone again for Intermezzo
20  *              Copyright (c) 1998 Peter J. Braam
21  *
22  *              Hacked up again for simulated OBD
23  *              Copyright (c) 1999 Stelias Computing, Inc.
24  *                (authors {pschwan,braam}@stelias.com)
25  *              Copyright (C) 1999 Seagate Technology, Inc.
26  *
27  * 
28  */
29
30 #define EXPORT_SYMTAB
31
32 #include <linux/config.h> /* for CONFIG_PROC_FS */
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/sched.h>
38 #include <linux/lp.h>
39 #include <linux/malloc.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/vmalloc.h>
46 #include <linux/fs.h>
47 #include <linux/poll.h>
48 #include <linux/init.h>
49 #include <linux/list.h>
50 #include <asm/io.h>
51 #include <asm/segment.h>
52 #include <asm/system.h>
53 #include <asm/poll.h>
54 #include <asm/uaccess.h>
55
56 #include <linux/obd_support.h>
57 #include <linux/obd_class.h>
58
59 static int obd_init_magic;
60 int           obd_print_entry = 1;
61 int           obd_debug_level = 4095;
62 struct obd_device obd_dev[MAX_OBD_DEVICES];
63 struct list_head obd_types;
64
65 /* called when opening /dev/obdNNN */
66 static int obd_class_open(struct inode * inode, struct file * file)
67 {
68         int dev;
69         ENTRY;
70
71         if (!inode)
72                 return -EINVAL;
73         dev = MINOR(inode->i_rdev);
74         if (dev >= MAX_OBD_DEVICES)
75                 return -ENODEV;
76         obd_dev[dev].obd_refcnt++;
77         CDEBUG(D_PSDEV, "Refcount now %d\n", obd_dev[dev].obd_refcnt);
78
79         MOD_INC_USE_COUNT;
80         EXIT;
81         return 0;
82 }
83
84 /* called when closing /dev/obdNNN */
85 static int obd_class_release(struct inode * inode, struct file * file)
86 {
87         int dev;
88         ENTRY;
89
90         if (!inode)
91                 return -EINVAL;
92         dev = MINOR(inode->i_rdev);
93         if (dev >= MAX_OBD_DEVICES)
94                 return -ENODEV;
95         fsync_dev(inode->i_rdev);
96         if (obd_dev[dev].obd_refcnt <= 0)
97                 printk(KERN_ALERT "presto_psdev_release: refcount(%d) <= 0\n",
98                        obd_dev[dev].obd_refcnt);
99         obd_dev[dev].obd_refcnt--;
100
101         CDEBUG(D_PSDEV, "Refcount now %d\n", obd_dev[dev].obd_refcnt);
102         MOD_DEC_USE_COUNT;
103
104         EXIT;
105         return 0;
106 }
107
108 /* support function */
109 static struct obd_type *obd_nm_to_type(char *nm) 
110 {
111         struct list_head *tmp;
112         struct obd_type *type;
113         CDEBUG(D_IOCTL, "SEARCH %s\n", nm);
114         
115         tmp = &obd_types;
116         while ( (tmp = tmp->next) != &obd_types ) {
117                 type = list_entry(tmp, struct obd_type, typ_chain);
118                 CDEBUG(D_IOCTL, "TYP %s\n", type->typ_name);
119                 if (strlen(type->typ_name) == strlen(nm) &&
120                     strcmp(type->typ_name, nm) == 0 ) {
121                         return type;
122                 }
123         }
124         return NULL;
125 }
126
127
128 static int getdata(int len, void **data)
129 {
130         void *tmp = NULL;
131
132         if (!len) 
133                 return 0;
134
135         CDEBUG(D_IOCTL, "getdata: len %d, add %p\n", len, *data);
136
137         OBD_ALLOC(tmp, void *, len);
138         if ( !tmp )
139                 return -ENOMEM;
140         
141         memset(tmp, 0, len);
142         if ( copy_from_user(tmp, *data, len)) {
143                 OBD_FREE(tmp,len);
144                 return -EFAULT;
145         }
146         *data = tmp;
147
148         return 0;
149 }
150
151 /* to control /dev/obdNNN */
152 static int obd_class_ioctl (struct inode * inode, struct file * filp, 
153                             unsigned int cmd, unsigned long arg)
154 {
155         int err, i_ino, dev;
156         struct obd_device *obddev;
157         struct obd_conn conn;
158         long int cli_id; /* connect, disconnect */
159
160         if (!inode)
161                 return -EINVAL;
162
163         dev = MINOR(inode->i_rdev);
164         if (dev > MAX_OBD_DEVICES)
165                 return -ENODEV;
166         obddev = &obd_dev[dev];
167         conn.oc_dev = obddev;
168
169         switch (cmd) {
170         case TCGETS:
171                 return -EINVAL;
172         case OBD_IOC_ATTACH: {
173                 struct obd_type *type;
174                 struct oic_generic input;
175
176                 /* have we attached a type to this device */
177                 if ( obddev->obd_type || (obddev->obd_flags & OBD_ATTACHED) ){
178                         CDEBUG(D_IOCTL, "OBD Device %d already attached to type %s.\n", dev, obddev->obd_type->typ_name);
179                         return -EINVAL;
180                 }
181
182                 /* get data structures */
183                 err = copy_from_user(&input, (void *) arg, sizeof(input));
184                 if (err)
185                         return err;
186
187                 if ( (err = getdata(input.att_typelen + 1, &input.att_type)) )
188                         return err;
189
190                 /* find the type */
191                 err = -EINVAL;
192                 type = obd_nm_to_type(input.att_type);
193                 OBD_FREE(input.att_type, input.att_typelen + 1);
194                 if ( !type ) {
195                         printk("Unknown obd type dev %d\n", dev);
196                         return err;
197                 }
198                 obddev->obd_type = type;
199                 
200                 /* get the attach data */
201                 if ( (err = getdata(input.att_datalen, &input.att_data)) ) {
202                         return err;
203                 }
204
205                 INIT_LIST_HEAD(&obddev->obd_gen_clients);
206                 obddev->obd_multi_count = 0;
207
208                 CDEBUG(D_IOCTL, "Attach %d,  datalen %d, type %s\n", 
209                        dev, input.att_datalen, obddev->obd_type->typ_name);
210                 if (!obddev->obd_type->typ_ops || !OBP(obddev,attach)) {
211                         obddev->obd_flags |=  OBD_ATTACHED;
212                         type->typ_refcnt++;
213                         MOD_INC_USE_COUNT;
214                         return 0;
215                 }
216
217                 /* do the attach */
218                 err = OBP(obddev,attach)(obddev,  
219                                          input.att_datalen, input.att_data);
220                 OBD_FREE(input.att_data, input.att_datalen);
221
222                 if ( err ) {
223                         obddev->obd_flags &= ~OBD_ATTACHED;
224                         obddev->obd_type = NULL;
225                 } else {
226                         obddev->obd_flags |=  OBD_ATTACHED;
227                         type->typ_refcnt++;
228                         MOD_INC_USE_COUNT;
229                 }
230                 return err;
231         }
232
233         case OBD_IOC_DETACH: {
234
235                 if (obddev->obd_flags & OBD_SET_UP)
236                         return -EINVAL;
237                 if (! (obddev->obd_flags & OBD_ATTACHED) )
238                         return -EINVAL;
239                 if ( !list_empty(&obddev->obd_gen_clients) ) 
240                         return -EINVAL;
241
242                 obddev->obd_flags &= ~OBD_ATTACHED;
243                 obddev->obd_type->typ_refcnt--;
244                 obddev->obd_type = NULL;
245                 MOD_DEC_USE_COUNT;
246                 return 0;
247         }
248
249
250         case OBD_IOC_FORMAT: {
251                 struct ioc_format {
252                         int format_datalen;
253                         void *format_data;
254                 } input;
255
256                 /* have we attached a type to this device */
257                 if ( !obddev->obd_type ) {
258                         CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
259                         return -EINVAL;
260                 }
261
262                 /* get main structure */
263                 err = copy_from_user(&input, (void *) arg, sizeof(input));
264                 if (err) 
265                         return err;
266
267                 err = getdata(input.format_datalen, &input.format_data);
268                 if (err) 
269                         return err;
270
271                 if (!obddev->obd_type->typ_ops || 
272                     !obddev->obd_type->typ_ops->o_format )
273                         return -EOPNOTSUPP;
274
275                 /* do the format */
276                 CDEBUG(D_IOCTL, "Format %d, type %s\n", dev, 
277                        obddev->obd_type->typ_name);
278                 err = obddev->obd_type->typ_ops->o_format
279                         (obddev, input.format_datalen, input.format_data);
280
281                 OBD_FREE(input.format_data, input.format_datalen);
282                 return err;
283         }
284         case OBD_IOC_PARTITION: {
285                 struct ioc_part {
286                         int part_datalen;
287                         void *part_data;
288                 } input;
289
290                 /* have we attached a type to this device */
291                 if ( !obddev->obd_type ) {
292                         CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
293                         return -EINVAL;
294                 }
295
296                 /* get main structure */
297                 err = copy_from_user(&input, (void *) arg, sizeof(input));
298                 if (err) 
299                         return err;
300
301                 err = getdata(input.part_datalen, &input.part_data);
302                 if (err) 
303                         return err;
304
305                 if (!obddev->obd_type->typ_ops || 
306                     !obddev->obd_type->typ_ops->o_partition )
307                         return -EOPNOTSUPP;
308
309                 /* do the partition */
310                 CDEBUG(D_IOCTL, "Partition %d, type %s\n", dev, 
311                        obddev->obd_type->typ_name);
312                 err = obddev->obd_type->typ_ops->o_partition
313                         (obddev, input.part_datalen, input.part_data);
314
315                 OBD_FREE(input.part_data, input.part_datalen);
316                 return err;
317         }
318
319         case OBD_IOC_COPY: {
320                 struct ioc_mv_s mvdata;
321                 struct ioc_part {
322                         int part_datalen;
323                         void *part_data;
324                 } input;
325                 obdattr *srcoa, *tgtoa;
326
327                 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
328                      (!(obddev->obd_flags & OBD_ATTACHED))) {
329                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
330                         return -EINVAL;
331                 }
332
333                 /* get main structure */
334                 err = copy_from_user(&input, (void *) arg, sizeof(input));
335                 if (err) 
336                         return err;
337
338
339                 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
340                 if (err) 
341                         return err;
342
343                 if (!obddev->obd_type->typ_ops || 
344                     !obddev->obd_type->typ_ops->o_copy )
345                         return -EOPNOTSUPP;
346
347                 /* do the partition */
348                 CDEBUG(D_IOCTL, "Copy %d, type %s src %ld tgt %ld\n", dev, 
349                        obddev->obd_type->typ_name, mvdata.src, mvdata.tgt);
350
351                 conn.oc_id = mvdata.conn_id;
352                 srcoa = obd_oa_fromid(&conn, mvdata.src);
353                 if ( !srcoa ) 
354                         return -ENOENT;
355                 tgtoa = obd_oa_fromid(&conn, mvdata.tgt);
356                 if ( ! tgtoa ) {
357                         obd_free_oa(srcoa);
358                         return -ENOMEM;
359                 }
360
361                 err = obddev->obd_type->typ_ops->o_copy(&conn,srcoa, tgtoa);
362
363                 obd_free_oa(srcoa);
364                 obd_free_oa(tgtoa);
365                 return err;
366         }
367         case OBD_IOC_MIGR: {
368                 struct ioc_mv_s mvdata;
369                 struct ioc_part {
370                         int part_datalen;
371                         void *part_data;
372                 } input;
373                 obdattr *srcoa, *tgtoa;
374
375                 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
376                      (!(obddev->obd_flags & OBD_ATTACHED))) {
377                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
378                         return -EINVAL;
379                 }
380
381                 /* get main structure */
382                 err = copy_from_user(&input, (void *) arg, sizeof(input));
383                 if (err) 
384                         return err;
385
386
387                 CDEBUG(D_IOCTL, "Migrate copying %d\n", sizeof(mvdata));
388                 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
389                 if (err) 
390                         return err;
391
392                 if (!obddev->obd_type->typ_ops || 
393                     !obddev->obd_type->typ_ops->o_copy )
394                         return -EOPNOTSUPP;
395
396                 /* do the partition */
397                 CDEBUG(D_IOCTL, "Migrate %d, type %s conn %d src %ld tgt %ld\n", dev, 
398                        obddev->obd_type->typ_name, mvdata.conn_id, mvdata.src, mvdata.tgt);
399
400
401                 if ( ! (srcoa = obd_empty_oa()) ) 
402                         return -ENOMEM;
403                 if ( ! (tgtoa = obd_empty_oa()) ) {
404                         obd_free_oa(srcoa);
405                         return -ENOMEM;
406                 }
407
408                 srcoa->i_ino = mvdata.src;
409                 tgtoa->i_ino = mvdata.tgt;
410
411                 conn.oc_id = mvdata.conn_id;
412
413                 err = obddev->obd_type->typ_ops->o_migrate(&conn, tgtoa, srcoa);
414
415                 obd_free_oa(srcoa);
416                 obd_free_oa(tgtoa);
417                 return err;
418         }
419
420         case OBD_IOC_SETUP: {
421                 struct ioc_setup {
422                         int setup_datalen;
423                         void *setup_data;
424                 } input;
425
426                 /* have we attached a type to this device */
427                 if (!(obddev->obd_flags & OBD_ATTACHED)) {
428                         CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
429                         return -EINVAL;
430                 }
431
432                 /* has this been done already? */
433                 if ( obddev->obd_flags & OBD_SET_UP ) {
434                         CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
435                                dev, obddev->obd_type->typ_name);
436                         return -EINVAL;
437                 }
438
439                 /* get main structure */
440                 err = copy_from_user(&input, (void *) arg, sizeof(input));
441                 if (err) 
442                         return err;
443
444                 err = getdata(input.setup_datalen, &input.setup_data);
445                 if (err) 
446                         return err;
447
448
449                 /* do the setup */
450                 CDEBUG(D_IOCTL, "Setup %d, type %s\n", dev, 
451                        obddev->obd_type->typ_name);
452                 if ( !obddev->obd_type->typ_ops || 
453                      !obddev->obd_type->typ_ops->o_setup ) {
454                         obddev->obd_flags |= OBD_SET_UP;
455                         return 0;
456                 }
457
458                 err = obddev->obd_type->typ_ops->o_setup
459                         (obddev, input.setup_datalen, input.setup_data);
460
461                 if ( err ) 
462                         obddev->obd_flags &= ~OBD_SET_UP;
463                 else {
464                         obddev->obd_type->typ_refcnt++;
465                         obddev->obd_flags |= OBD_SET_UP;
466                 }
467                 return err;
468         }
469         case OBD_IOC_CLEANUP: {
470                 int rc;
471
472                 /* has this minor been registered? */
473                 if (!obddev->obd_type)
474                         return -ENODEV;
475
476                 if ( !obddev->obd_type->typ_refcnt ) 
477                         printk("OBD_CLEANUP: refcount wrap!\n");
478
479                 if ( !obddev->obd_flags & OBD_SET_UP ) 
480                         return -EINVAL;
481
482                 if ( !obddev->obd_type->typ_ops->o_cleanup )
483                         goto cleanup_out;
484
485                 /* cleanup has no argument */
486                 rc = OBP(obddev, cleanup)(obddev);
487                 if ( rc )
488                         return rc;
489
490         cleanup_out: 
491                 obddev->obd_flags &= ~OBD_SET_UP;
492                 obddev->obd_type->typ_refcnt--;
493                 return 0;
494         }
495         case OBD_IOC_CONNECT:
496         {
497
498                 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
499                      (!(obddev->obd_flags & OBD_ATTACHED))) {
500                         CDEBUG(D_IOCTL, "Device not attached or set up\n");
501                         return -EINVAL;
502                 }
503
504                 
505                 if (obddev->obd_type->typ_ops->o_connect(&conn))
506                         return -EINVAL;
507
508                 return copy_to_user((int *)arg, &conn.oc_id,
509                                     sizeof(int));
510         }
511         case OBD_IOC_DISCONNECT:
512                 /* frees data structures */
513                 /* has this minor been registered? */
514                 if (!obddev->obd_type)
515                         return -ENODEV;
516
517                 get_user(cli_id, (int *) arg);
518                 conn.oc_id = cli_id;
519
520                 OBP(obddev, disconnect)(&conn);
521                 return 0;
522
523         case OBD_IOC_SYNC: {
524                 /* sync doesn't need a connection ID, because it knows
525                  * what device it was called on, and can thus get the
526                  * superblock that it needs. */
527                 /* has this minor been registered? */
528                 if (!obddev->obd_type)
529                         return -ENODEV;
530
531                 if (!obddev->u.ext2.ext2_sb || !obddev->u.ext2.ext2_sb->s_dev) {
532                         CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
533                         err = -EINVAL;
534                 } else {
535                         if ((err = fsync_dev(obddev->u.ext2.ext2_sb->s_dev)))
536                                 CDEBUG(D_IOCTL, "sync: fsync_dev failure\n");
537                         else
538                                 CDEBUG(D_IOCTL, "sync: success\n");
539                 }
540
541                 return put_user(err, (int *) arg);
542         }
543         case OBD_IOC_CREATE: {
544                 int err;
545                 struct oic_create_s foo;
546
547                 if ( copy_from_user(&foo, (const void *)arg, sizeof(foo)) )
548                         return -EFAULT;
549
550                 /* has this minor been registered? */
551                 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
552                      !(obddev->obd_flags & OBD_SET_UP))
553                         return -ENODEV;
554                 conn.oc_id = foo.conn_id;
555
556                 i_ino = OBP(obddev, create)(&conn, foo.prealloc, &err);
557                 if (err) {
558                         CDEBUG(D_IOCTL, "create: obd_inode_new failure\n");
559                         /* 0 is the only error value */
560                         return put_user(0, (int *) arg);
561                 }
562
563                 return put_user(i_ino, (int *) arg);
564         }
565         case OBD_IOC_DESTROY:
566         {
567                 struct destroy_s {
568                         unsigned int conn_id;
569                         unsigned int ino;
570                 } destroy;
571                 obdattr *oa;
572                 int rc;
573                 
574                 if ( ! (oa = obd_empty_oa()) ) 
575                         return -ENOMEM;
576
577                 /* has this minor been registered? */
578                 if (!obddev->obd_type)
579                         return -ENODEV;
580
581
582                 copy_from_user(&destroy, (int *)arg, sizeof(struct destroy_s));
583                 if ( !obddev->obd_type ||
584                      !obddev->obd_type->typ_ops->o_destroy)
585                         return -EINVAL;
586
587                 oa->i_ino = destroy.ino;
588                 conn.oc_id = destroy.conn_id;
589                 rc = obddev->obd_type->typ_ops->o_destroy(&conn, oa);
590                 OBD_FREE(oa, sizeof(*oa));
591                 return rc;
592         }
593         case OBD_IOC_SETATTR:
594         {
595                 struct oic_attr_s foo;
596                 obdattr *oa;
597                 int rc;
598
599                 if ( ! (oa = obd_empty_oa()) ) 
600                         return -ENOMEM;
601
602                 /* has this minor been registered? */
603                 if (!obddev->obd_type)
604                         return -ENODEV;
605
606                 rc = copy_from_user(&foo, (int *)arg, sizeof(foo));
607                 if (rc)
608                         return rc;
609
610                 if ( !obddev->obd_type ||
611                      !obddev->obd_type->typ_ops->o_setattr)
612                         return -EINVAL;
613                 
614                 oa->i_ino = foo.ino;
615                 inode_setattr(oa, &foo.iattr);
616                 conn.oc_id = foo.conn_id;
617                 rc = obddev->obd_type->typ_ops->o_setattr(&conn, oa);
618                 OBD_FREE(oa, sizeof(*oa));
619                 return rc;
620         }
621
622         case OBD_IOC_GETATTR:
623         {
624                 int rc;
625                 struct oic_getattr {
626                         unsigned int conn_id;
627                         unsigned long ino;
628                 } foo;
629                 struct iattr iattr;
630                 obdattr *oa;
631
632                 rc = copy_from_user(&foo, (int *)arg, sizeof(foo));
633                 if (rc)
634                         return rc;
635
636                 conn.oc_id = foo.conn_id;
637                 oa = obd_oa_fromid(&conn, foo.ino);
638                 if ( !oa ) 
639                         return -ENOENT;
640
641                 inode_to_iattr(oa, &iattr);
642                 rc = copy_to_user((int *)arg, &iattr, sizeof(iattr));
643                 return rc;
644         }
645
646         case OBD_IOC_READ:
647         {
648                 obdattr *oa = NULL;
649                 int rc;
650                 struct oic_rw_s rw_s;  /* read, write ioctl str */
651
652                 rc = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
653                 if ( rc ) 
654                         goto READ_OUT;
655
656                 
657                 conn.oc_id = rw_s.conn_id;
658                 if ( ! (oa = obd_oa_fromid(&conn, rw_s.id)) ) 
659                         return -ENOENT;
660
661                 rc = -EINVAL;
662                 if ( !obddev->obd_type->typ_ops || 
663                      !obddev->obd_type->typ_ops->o_read ) 
664                         goto READ_OUT;
665
666                 rc = obddev->obd_type->typ_ops->o_read
667                         (&conn, oa, rw_s.buf, &rw_s.count, rw_s.offset);
668                 if ( rc ) 
669                         goto READ_OUT;
670
671                 rc = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
672
673         READ_OUT:
674                 if ( oa ) 
675                         OBD_FREE(oa, sizeof(*oa));
676                 return rc;
677         }
678
679         case OBD_IOC_WRITE: {
680                 obdattr *oa = NULL;
681                 int rc;
682                 struct oic_rw_s rw_s;  /* read, write ioctl str */
683
684                 rc = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
685                 if ( rc ) 
686                         goto WRITE_OUT;
687
688                 conn.oc_id = rw_s.conn_id;
689                 oa = obd_oa_fromid(&conn, rw_s.id);
690                 if ( !oa ) 
691                         return -ENOENT;
692
693                 rc = -EINVAL;
694                 if ( !obddev->obd_type->typ_ops || 
695                      !obddev->obd_type->typ_ops->o_write ) 
696                         goto WRITE_OUT;
697
698                 rc = obddev->obd_type->typ_ops->o_write
699                         (&conn, oa, rw_s.buf, &rw_s.count, rw_s.offset);
700                 if ( rc ) 
701                         goto WRITE_OUT;
702
703                 rc = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
704
705         WRITE_OUT:
706                 OBD_FREE(oa, sizeof(*oa));
707                 return rc;
708         }
709         case OBD_IOC_PREALLOCATE: {
710                 struct oic_prealloc_s prealloc;
711                 int rc;
712
713                 /* has this minor been registered? */
714                 if (!obddev->obd_type)
715                         return -ENODEV;
716
717
718                 rc = copy_from_user(&prealloc, (int *)arg, sizeof(prealloc));
719                 if (rc) 
720                         return -ENOMEM;
721
722                 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
723                      !(obddev->obd_flags & OBD_SET_UP)) {
724                         CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
725                         return -EINVAL;
726                 }
727
728                 if (!obddev->obd_type || 
729                     !obddev->obd_type->typ_ops->o_preallocate)
730                         return -EOPNOTSUPP;
731                 conn.oc_id = prealloc.cli_id;
732                 rc = obddev->obd_type->typ_ops->o_preallocate
733                         (&conn, &prealloc.alloc, prealloc.inodes);
734                 if ( rc ) 
735                         return rc;
736
737                 return copy_to_user((int *)arg, &prealloc, sizeof(prealloc));
738         }
739         case OBD_IOC_STATFS:
740         {
741                 struct statfs *tmp;
742                 unsigned int conn_id;
743                 struct statfs buf;
744                 int rc;
745
746                 /* has this minor been registered? */
747                 if (!obddev->obd_type)
748                         return -ENODEV;
749
750                 tmp = (void *)arg + sizeof(unsigned int);
751                 get_user(conn_id, (int *) arg);
752                 if ( !obddev->obd_type ||
753                      !obddev->obd_type->typ_ops->o_statfs)
754                         return -EINVAL;
755
756                 conn.oc_id = conn_id;
757                 rc = obddev->obd_type->typ_ops->o_statfs(&conn, &buf);
758                 if ( rc ) 
759                         return rc;
760                 rc = copy_to_user(tmp, &buf, sizeof(buf));
761                 return rc;
762                 
763         }
764         default: {
765                 struct obd_type *type;
766                 struct oic_generic input;
767                 void *karg;
768
769                 /* get data structures */
770                 err = copy_from_user(&input, (void *) arg, sizeof(input));
771                 if (err) {
772                         EXIT;
773                         return err;
774                 }
775
776                 if ( (err = getdata(input.att_typelen + 1, &input.att_type))){
777                         EXIT;
778                         return err;
779                 }
780
781                 /* find the type */
782                 err = -EINVAL;
783                 type = obd_nm_to_type(input.att_type);
784                 OBD_FREE(input.att_type, input.att_typelen + 1);
785                 if ( !type ) {
786                         printk("Unknown obd type dev %d\n", dev);
787                         EXIT;
788                         return err;
789                 }
790                 
791                 if ( !type->typ_ops->o_iocontrol ) {
792                         EXIT;
793                         return -EINVAL;
794                 }
795                 conn.oc_id = input.att_connid;
796                 
797                 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
798                        cmd, type->typ_name, input.att_datalen);
799
800                 /* get the generic data */
801                 karg = input.att_data;
802                 if ( (err = getdata(input.att_datalen, &karg)) ) {
803                         EXIT;
804                         return err;
805                 }
806
807                 err = type->typ_ops->o_iocontrol
808                         (cmd, &conn, input.att_datalen, 
809                          karg, input.att_data);
810                 OBD_FREE(karg, input.att_datalen);
811
812                 EXIT;
813                 return err;
814         }
815         }
816 }
817
818 /* Driver interface done, utility functions follow */
819
820 int obd_register_type(struct obd_ops *ops, char *nm)
821 {
822         struct obd_type *type;
823
824
825         if (obd_init_magic != 0x11223344) {
826                 EXIT;
827                 return -EINVAL;
828         }
829
830         if  ( obd_nm_to_type(nm) ) {
831                 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
832                 EXIT;
833                 return -1;
834         }
835         
836         OBD_ALLOC(type, struct obd_type * , sizeof(*type));
837         if ( !type ) {
838                 EXIT;
839                 return -ENOMEM;
840         }
841         memset(type, 0, sizeof(*type));
842         INIT_LIST_HEAD(&type->typ_chain);
843         MOD_INC_USE_COUNT;
844         list_add(&type->typ_chain, obd_types.next);
845         type->typ_ops = ops;
846         type->typ_name = nm;
847         EXIT;
848         return 0;
849 }
850         
851 int obd_unregister_type(char *nm)
852 {
853         struct obd_type *type = obd_nm_to_type(nm);
854
855         if ( !type ) {
856                 MOD_DEC_USE_COUNT;
857                 printk("OBD: NO TYPE\n");
858                 EXIT;
859                 return -1;
860         }
861
862         if ( type->typ_refcnt ) {
863                 MOD_DEC_USE_COUNT;
864                 printk("OBD: refcount wrap\n");
865                 EXIT;
866                 return -1;
867         }
868
869         list_del(&type->typ_chain);
870         OBD_FREE(type, sizeof(*type));
871         MOD_DEC_USE_COUNT;
872         return 0;
873 }
874
875 /* declare character device */
876 static struct file_operations obd_psdev_fops = {
877         NULL,                  /* llseek */
878         NULL,                  /* read */
879         NULL,                  /* write */
880         NULL,                  /* presto_psdev_readdir */
881         NULL,                  /* poll */
882         obd_class_ioctl,       /* ioctl */
883         NULL,                  /* presto_psdev_mmap */
884         obd_class_open,        /* open */
885         NULL,
886         obd_class_release,     /* release */
887         NULL,                  /* fsync */
888         NULL,                  /* fasync */
889         NULL,                  /* check_media_change */
890         NULL,                  /* revalidate */
891         NULL                   /* lock */
892 };
893
894
895 /* modules setup */
896
897 int init_obd(void)
898 {
899         int i;
900
901         printk(KERN_INFO "OBD class driver  v0.002, braam@stelias.com\n");
902         
903         INIT_LIST_HEAD(&obd_types);
904         
905         if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev", 
906                             &obd_psdev_fops)) {
907                 printk(KERN_ERR "obd_psdev: unable to get major %d\n", 
908                        OBD_PSDEV_MAJOR);
909                 return -EIO;
910         }
911
912         for (i = 0; i < MAX_OBD_DEVICES; i++) {
913                 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
914                 obd_dev[i].obd_minor = i;
915                 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
916         }
917
918         obd_sysctl_init();
919         obd_init_magic = 0x11223344;
920         return 0;
921 }
922
923 EXPORT_SYMBOL(obd_register_type);
924 EXPORT_SYMBOL(obd_unregister_type);
925
926 EXPORT_SYMBOL(obd_print_entry);
927 EXPORT_SYMBOL(obd_debug_level);
928 EXPORT_SYMBOL(obd_dev);
929
930 EXPORT_SYMBOL(gen_connect);
931 EXPORT_SYMBOL(gen_client);
932 EXPORT_SYMBOL(gen_cleanup);
933 EXPORT_SYMBOL(gen_disconnect);
934 EXPORT_SYMBOL(gen_copy_data); 
935
936 /* EXPORT_SYMBOL(gen_multi_attach); */
937 EXPORT_SYMBOL(gen_multi_setup);
938 EXPORT_SYMBOL(gen_multi_cleanup);
939
940
941 #ifdef MODULE
942 int init_module(void)
943 {
944         return init_obd();
945 }
946
947 void cleanup_module(void)
948 {
949         int i;
950         ENTRY;
951
952         unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
953         for (i = 0; i < MAX_OBD_DEVICES; i++) {
954                 struct obd_device *obddev = &obd_dev[i];
955                 if ( obddev->obd_type && 
956                      (obddev->obd_flags & OBD_SET_UP) &&
957                      obddev->obd_type->typ_ops->o_detach ) {
958                         OBP(obddev, detach)(obddev);
959                 } 
960         }
961
962
963         obd_sysctl_clean();
964         obd_init_magic = 0;
965 }
966 #endif