2 * An implementation of a loadable kernel mode driver providing
3 * multiple kernel/user space bidirectional communications links.
5 * Author: Alan Cox <alan@cymru.net>
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.
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>
16 * Changes for Linux 2.1
17 * Copyright (c) 1997 Carnegie-Mellon University
19 * Redone again for Intermezzo
20 * Copyright (c) 1998 Peter J. Braam
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.
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>
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>
47 #include <linux/poll.h>
48 #include <linux/init.h>
49 #include <linux/list.h>
51 #include <asm/segment.h>
52 #include <asm/system.h>
54 #include <asm/uaccess.h>
56 #include <linux/obd_support.h>
57 #include <linux/obd_class.h>
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;
65 /* called when opening /dev/obdNNN */
66 static int obd_class_open(struct inode * inode, struct file * file)
73 dev = MINOR(inode->i_rdev);
74 if (dev >= MAX_OBD_DEVICES)
76 obd_dev[dev].obd_refcnt++;
77 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
78 obd_dev[dev].obd_refcnt);
85 /* called when closing /dev/obdNNN */
86 static int obd_class_release(struct inode * inode, struct file * file)
93 dev = MINOR(inode->i_rdev);
94 if (dev >= MAX_OBD_DEVICES)
96 fsync_dev(inode->i_rdev);
97 if (obd_dev[dev].obd_refcnt <= 0)
98 printk(KERN_ALERT "obd_class_release: refcount(%d) <= 0\n",
99 obd_dev[dev].obd_refcnt);
100 obd_dev[dev].obd_refcnt--;
102 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
103 obd_dev[dev].obd_refcnt);
110 /* support function */
111 static struct obd_type *obd_nm_to_type(char *nm)
113 struct list_head *tmp;
114 struct obd_type *type;
115 CDEBUG(D_IOCTL, "SEARCH %s\n", nm);
118 while ( (tmp = tmp->next) != &obd_types ) {
119 type = list_entry(tmp, struct obd_type, typ_chain);
120 CDEBUG(D_IOCTL, "TYP %s\n", type->typ_name);
121 if (strlen(type->typ_name) == strlen(nm) &&
122 strcmp(type->typ_name, nm) == 0 ) {
130 static int getdata(int len, void **data)
137 CDEBUG(D_IOCTL, "getdata: len %d, add %p\n", len, *data);
139 OBD_ALLOC(tmp, void *, len);
144 if ( copy_from_user(tmp, *data, len)) {
153 /* to control /dev/obdNNN */
154 static int obd_class_ioctl (struct inode * inode, struct file * filp,
155 unsigned int cmd, unsigned long arg)
157 struct obd_device *obddev;
158 struct obd_conn conn;
160 long int cli_id; /* connect, disconnect */
165 dev = MINOR(inode->i_rdev);
166 if (dev > MAX_OBD_DEVICES)
168 obddev = &obd_dev[dev];
169 conn.oc_dev = obddev;
174 case OBD_IOC_ATTACH: {
175 struct obd_type *type;
176 struct oic_generic input;
179 /* have we attached a type to this device */
180 if ( obddev->obd_type || (obddev->obd_flags & OBD_ATTACHED) ){
182 "OBD Device %d already attached to type %s.\n",
183 dev, obddev->obd_type->typ_name);
188 /* get data structures */
189 err = copy_from_user(&input, (void *)arg, sizeof(input));
195 err = getdata(input.att_typelen + 1, &input.att_type);
202 type = obd_nm_to_type(input.att_type);
203 OBD_FREE(input.att_type, input.att_typelen + 1);
205 printk("Unknown obd type dev %d\n", dev);
209 obddev->obd_type = type;
211 /* get the attach data */
212 err = getdata(input.att_datalen, &input.att_data);
218 INIT_LIST_HEAD(&obddev->obd_gen_clients);
219 obddev->obd_multi_count = 0;
221 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
222 dev, input.att_datalen, obddev->obd_type->typ_name);
223 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
224 obddev->obd_flags |= OBD_ATTACHED;
226 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
234 err = OBP(obddev, attach)(obddev, input.att_datalen,
236 OBD_FREE(input.att_data, input.att_datalen);
239 obddev->obd_flags &= ~OBD_ATTACHED;
240 obddev->obd_type = NULL;
243 obddev->obd_flags |= OBD_ATTACHED;
245 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
253 case OBD_IOC_DETACH: {
256 if (obddev->obd_flags & OBD_SET_UP) {
260 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
261 CDEBUG(D_IOCTL, "Device not attached\n");
265 if ( !list_empty(&obddev->obd_gen_clients) ) {
266 CDEBUG(D_IOCTL, "Device has connected clients\n");
271 CDEBUG(D_IOCTL, "Detach %d, type %s\n", dev,
272 obddev->obd_type->typ_name);
273 obddev->obd_flags &= ~OBD_ATTACHED;
274 obddev->obd_type->typ_refcnt--;
275 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
276 obddev->obd_type->typ_refcnt);
277 obddev->obd_type = NULL;
283 case OBD_IOC_FORMAT: {
289 /* have we attached a type to this device */
290 if ( !obddev->obd_type ) {
291 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
295 /* get main structure */
296 err = copy_from_user(&input, (void *) arg, sizeof(input));
300 err = getdata(input.format_datalen, &input.format_data);
304 if ( !OBT(obddev) || !OBP(obddev, format) )
308 CDEBUG(D_IOCTL, "Format %d, type %s\n", dev,
309 obddev->obd_type->typ_name);
310 err = OBP(obddev, format)(obddev, input.format_datalen,
313 OBD_FREE(input.format_data, input.format_datalen);
316 case OBD_IOC_PARTITION: {
322 /* have we attached a type to this device */
323 if ( !obddev->obd_type ) {
324 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
328 /* get main structure */
329 err = copy_from_user(&input, (void *) arg, sizeof(input));
333 err = getdata(input.part_datalen, &input.part_data);
337 if ( !OBT(obddev) || !OBP(obddev, partition) )
340 /* do the partition */
341 CDEBUG(D_IOCTL, "Partition %d, type %s\n", dev,
342 obddev->obd_type->typ_name);
343 err = OBP(obddev, partition)(obddev, input.part_datalen,
346 OBD_FREE(input.part_data, input.part_datalen);
351 struct ioc_mv_s mvdata;
358 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
359 (!(obddev->obd_flags & OBD_ATTACHED))) {
360 CDEBUG(D_IOCTL, "Device not attached or set up\n");
364 /* get main structure */
365 err = copy_from_user(&input, (void *) arg, sizeof(input));
369 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
373 if ( !OBT(obddev) || !OBP(obddev, copy) )
376 /* do the partition */
377 CDEBUG(D_IOCTL, "Copy %d, type %s dst %ld src %ld\n", dev,
378 obddev->obd_type->typ_name, mvdata.dst, mvdata.src);
380 conn.oc_id = mvdata.conn_id;
381 src = obd_oa_fromid(&conn, mvdata.src);
384 dst = obd_oa_fromid(&conn, mvdata.dst);
390 err = OBP(obddev, copy)(&conn, dst, src);
397 struct ioc_mv_s mvdata;
404 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
405 (!(obddev->obd_flags & OBD_ATTACHED))) {
406 CDEBUG(D_IOCTL, "Device not attached or set up\n");
410 /* get main structure */
411 err = copy_from_user(&input, (void *) arg, sizeof(input));
416 CDEBUG(D_IOCTL, "Migrate copying %d\n", sizeof(mvdata));
417 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
421 if ( !OBT(obddev) || !OBP(obddev, migrate) )
424 /* do the partition */
425 CDEBUG(D_IOCTL, "Migrate %d, type %s conn %d src %ld dst %ld\n",
426 dev, obddev->obd_type->typ_name, mvdata.conn_id,
427 mvdata.src, mvdata.dst);
430 if ( ! (src = obd_empty_oa()) )
432 if ( ! (dst = obd_empty_oa()) ) {
437 src->i_ino = mvdata.src;
438 dst->i_ino = mvdata.dst;
440 conn.oc_id = mvdata.conn_id;
442 err = OBP(obddev, migrate)(&conn, dst, src);
449 case OBD_IOC_SETUP: {
456 /* have we attached a type to this device */
457 if (!(obddev->obd_flags & OBD_ATTACHED)) {
458 CDEBUG(D_IOCTL, "Device not attached\n");
463 /* has this been done already? */
464 if ( obddev->obd_flags & OBD_SET_UP ) {
465 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
466 dev, obddev->obd_type->typ_name);
471 /* get main structure */
472 err = copy_from_user(&input, (void *) arg, sizeof(input));
478 err = getdata(input.setup_datalen, &input.setup_data);
485 CDEBUG(D_IOCTL, "Setup %d, type %s\n", dev,
486 obddev->obd_type->typ_name);
487 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
488 obddev->obd_type->typ_refcnt++;
489 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
490 dev, obddev->obd_type->typ_refcnt);
491 obddev->obd_flags |= OBD_SET_UP;
496 err = OBP(obddev, setup)(obddev, input.setup_datalen,
500 obddev->obd_flags &= ~OBD_SET_UP;
503 obddev->obd_type->typ_refcnt++;
504 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
505 dev, obddev->obd_type->typ_refcnt);
506 obddev->obd_flags |= OBD_SET_UP;
511 case OBD_IOC_CLEANUP: {
513 /* has this minor been registered? */
514 if (!obddev->obd_type) {
515 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
520 if ( !obddev->obd_type->typ_refcnt ) {
521 printk("OBD_CLEANUP: Dev %d has refcount (%d)!\n",
522 dev, obddev->obd_type->typ_refcnt);
527 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
528 (!(obddev->obd_flags & OBD_ATTACHED))) {
529 CDEBUG(D_IOCTL, "Device not attached or set up\n");
534 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
537 /* cleanup has no argument */
538 err = OBP(obddev, cleanup)(obddev);
545 obddev->obd_flags &= ~OBD_SET_UP;
546 obddev->obd_type->typ_refcnt--;
547 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
548 obddev->obd_type->typ_refcnt);
552 case OBD_IOC_CONNECT:
554 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
555 (!(obddev->obd_flags & OBD_ATTACHED))) {
556 CDEBUG(D_IOCTL, "Device not attached or set up\n");
560 if ( !OBT(obddev) || !OBP(obddev, connect) )
563 err = OBP(obddev, connect)(&conn);
567 return copy_to_user((int *)arg, &conn.oc_id,
570 case OBD_IOC_DISCONNECT:
571 /* frees data structures */
572 /* has this minor been registered? */
573 if (!obddev->obd_type)
576 get_user(cli_id, (int *) arg);
579 if ( !OBT(obddev) || !OBP(obddev, disconnect))
582 OBP(obddev, disconnect)(&conn);
586 /* sync doesn't need a connection ID, because it knows
587 * what device it was called on, and can thus get the
588 * superblock that it needs. */
589 /* has this minor been registered? */
590 if (!obddev->obd_type)
593 if (!obddev->u.ext2.ext2_sb || !obddev->u.ext2.ext2_sb->s_dev) {
594 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
597 err = fsync_dev(obddev->u.ext2.ext2_sb->s_dev);
599 CDEBUG(D_IOCTL, "sync: fsync_dev failure\n");
601 CDEBUG(D_IOCTL, "sync: success\n");
604 return put_user(err, (int *) arg);
606 case OBD_IOC_CREATE: {
607 struct oic_create_s foo;
610 if ( copy_from_user(&foo, (const void *)arg, sizeof(foo)) )
613 /* has this minor been registered? */
614 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
615 !(obddev->obd_flags & OBD_SET_UP)) {
616 CDEBUG(D_IOCTL, "Device not attached or set up\n");
619 conn.oc_id = foo.conn_id;
621 if ( !OBT(obddev) || !OBP(obddev, create) )
624 err = OBP(obddev, create)(&conn, foo.prealloc, &id);
626 CDEBUG(D_IOCTL, "create: obd_inode_new failure\n");
627 /* 0 is the only error value */
628 put_user(0, (int *) arg);
632 return put_user(id, (int *) arg);
634 case OBD_IOC_DESTROY: {
636 unsigned int conn_id;
641 if ( ! (oa = obd_empty_oa()) )
644 /* has this minor been registered? */
645 if (!obddev->obd_type)
648 copy_from_user(&destroy, (int *)arg, sizeof(struct destroy_s));
649 if ( !OBT(obddev) || !OBP(obddev, destroy) )
652 oa->i_ino = destroy.ino;
653 conn.oc_id = destroy.conn_id;
654 err = OBP(obddev, destroy)(&conn, oa);
655 OBD_FREE(oa, sizeof(*oa));
658 case OBD_IOC_SETATTR: {
659 struct oic_attr_s foo;
662 if ( ! (oa = obd_empty_oa()) )
665 /* has this minor been registered? */
666 if (!obddev->obd_type)
669 err = copy_from_user(&foo, (int *)arg, sizeof(foo));
673 if ( !OBT(obddev) || !OBP(obddev, setattr) )
677 inode_setattr(oa, &foo.iattr);
678 conn.oc_id = foo.conn_id;
679 err = OBP(obddev, setattr)(&conn, oa);
680 OBD_FREE(oa, sizeof(*oa));
684 case OBD_IOC_GETATTR: {
686 unsigned int conn_id;
692 err = copy_from_user(&foo, (int *)arg, sizeof(foo));
696 conn.oc_id = foo.conn_id;
697 oa = obd_oa_fromid(&conn, foo.ino);
701 inode_to_iattr(oa, &iattr);
702 err = copy_to_user((int *)arg, &iattr, sizeof(iattr));
709 struct oic_rw_s rw_s; /* read, write ioctl str */
711 err = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
715 conn.oc_id = rw_s.conn_id;
716 if ( ! (oa = obd_oa_fromid(&conn, rw_s.id)) )
719 if ( !OBT(obddev) || !OBP(obddev, read) ) {
724 err = OBP(obddev, read)(&conn, oa, rw_s.buf, &rw_s.count,
729 err = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
733 OBD_FREE(oa, sizeof(*oa));
737 case OBD_IOC_WRITE: {
739 struct oic_rw_s rw_s; /* read, write ioctl str */
741 err = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
745 conn.oc_id = rw_s.conn_id;
746 oa = obd_oa_fromid(&conn, rw_s.id);
750 if ( !OBT(obddev) || !OBP(obddev, write) ) {
755 err = OBP(obddev, write)(&conn, oa, rw_s.buf, &rw_s.count,
760 err = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
763 OBD_FREE(oa, sizeof(*oa));
766 case OBD_IOC_PREALLOCATE: {
767 struct oic_prealloc_s prealloc;
769 /* has this minor been registered? */
770 if (!obddev->obd_type)
773 err = copy_from_user(&prealloc, (int *)arg, sizeof(prealloc));
777 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
778 !(obddev->obd_flags & OBD_SET_UP)) {
779 CDEBUG(D_IOCTL, "Device not attached or set up\n");
783 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
786 conn.oc_id = prealloc.cli_id;
787 err = OBP(obddev, preallocate)(&conn, &prealloc.alloc,
792 return copy_to_user((int *)arg, &prealloc, sizeof(prealloc));
794 case OBD_IOC_STATFS: {
796 unsigned int conn_id;
799 /* has this minor been registered? */
800 if (!obddev->obd_type)
803 tmp = (void *)arg + sizeof(unsigned int);
804 get_user(conn_id, (int *) arg);
806 if ( !OBT(obddev) || !OBP(obddev, statfs) )
809 conn.oc_id = conn_id;
810 err = OBP(obddev, statfs)(&conn, &buf);
813 err = copy_to_user(tmp, &buf, sizeof(buf));
818 struct obd_type *type;
819 struct oic_generic input;
822 /* get data structures */
823 err = copy_from_user(&input, (void *)arg, sizeof(input));
829 err = getdata(input.att_typelen + 1, &input.att_type);
836 type = obd_nm_to_type(input.att_type);
837 OBD_FREE(input.att_type, input.att_typelen + 1);
839 printk("Unknown obd type dev %d\n", dev);
844 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
848 conn.oc_id = input.att_connid;
850 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
851 cmd, type->typ_name, input.att_datalen);
853 /* get the generic data */
854 karg = input.att_data;
855 err = getdata(input.att_datalen, &karg);
861 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
862 karg, input.att_data);
863 OBD_FREE(karg, input.att_datalen);
871 /* Driver interface done, utility functions follow */
873 int obd_register_type(struct obd_ops *ops, char *nm)
875 struct obd_type *type;
878 if (obd_init_magic != 0x11223344) {
883 if ( obd_nm_to_type(nm) ) {
884 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
889 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
894 memset(type, 0, sizeof(*type));
895 INIT_LIST_HEAD(&type->typ_chain);
897 list_add(&type->typ_chain, obd_types.next);
904 int obd_unregister_type(char *nm)
906 struct obd_type *type = obd_nm_to_type(nm);
910 printk("Unknown obd type\n");
915 if ( type->typ_refcnt ) {
917 printk("OBD: Type %s has refcount (%d)\n", nm,
923 list_del(&type->typ_chain);
924 OBD_FREE(type, sizeof(*type));
929 /* declare character device */
930 static struct file_operations obd_psdev_fops = {
934 NULL, /* presto_psdev_readdir */
936 obd_class_ioctl, /* ioctl */
937 NULL, /* presto_psdev_mmap */
938 obd_class_open, /* open */
940 obd_class_release, /* release */
943 NULL, /* check_media_change */
944 NULL, /* revalidate */
955 printk(KERN_INFO "OBD class driver v0.002, braam@stelias.com\n");
957 INIT_LIST_HEAD(&obd_types);
959 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
961 printk(KERN_ERR "obd_psdev: unable to get major %d\n",
966 for (i = 0; i < MAX_OBD_DEVICES; i++) {
967 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
968 obd_dev[i].obd_minor = i;
969 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
973 obd_init_magic = 0x11223344;
977 EXPORT_SYMBOL(obd_register_type);
978 EXPORT_SYMBOL(obd_unregister_type);
980 EXPORT_SYMBOL(obd_print_entry);
981 EXPORT_SYMBOL(obd_debug_level);
982 EXPORT_SYMBOL(obd_dev);
984 EXPORT_SYMBOL(gen_connect);
985 EXPORT_SYMBOL(gen_client);
986 EXPORT_SYMBOL(gen_cleanup);
987 EXPORT_SYMBOL(gen_disconnect);
988 EXPORT_SYMBOL(gen_copy_data);
990 /* EXPORT_SYMBOL(gen_multi_attach); */
991 EXPORT_SYMBOL(gen_multi_setup);
992 EXPORT_SYMBOL(gen_multi_cleanup);
996 int init_module(void)
1001 void cleanup_module(void)
1006 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
1007 for (i = 0; i < MAX_OBD_DEVICES; i++) {
1008 struct obd_device *obddev = &obd_dev[i];
1009 if ( obddev->obd_type &&
1010 (obddev->obd_flags & OBD_SET_UP) &&
1011 OBT(obddev) && OBP(obddev, detach) ) {
1012 /* XXX should this call generic detach otherwise? */
1013 OBP(obddev, detach)(obddev);