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