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)
158 struct obd_device *obddev;
159 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 (!obddev->obd_type->typ_ops || !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,
235 input.att_datalen, input.att_data);
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) ) {
264 if ( !list_empty(&obddev->obd_gen_clients) ) {
269 CDEBUG(D_IOCTL, "Detach %d, type %s\n", dev,
270 obddev->obd_type->typ_name);
271 obddev->obd_flags &= ~OBD_ATTACHED;
272 obddev->obd_type->typ_refcnt--;
273 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
274 obddev->obd_type->typ_refcnt);
275 obddev->obd_type = NULL;
281 case OBD_IOC_FORMAT: {
287 /* have we attached a type to this device */
288 if ( !obddev->obd_type ) {
289 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
293 /* get main structure */
294 err = copy_from_user(&input, (void *) arg, sizeof(input));
298 err = getdata(input.format_datalen, &input.format_data);
302 if (!obddev->obd_type->typ_ops ||
303 !obddev->obd_type->typ_ops->o_format )
307 CDEBUG(D_IOCTL, "Format %d, type %s\n", dev,
308 obddev->obd_type->typ_name);
309 err = obddev->obd_type->typ_ops->o_format(obddev,
310 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 (!obddev->obd_type->typ_ops ||
338 !obddev->obd_type->typ_ops->o_partition )
341 /* do the partition */
342 CDEBUG(D_IOCTL, "Partition %d, type %s\n", dev,
343 obddev->obd_type->typ_name);
344 err = obddev->obd_type->typ_ops->o_partition(obddev,
348 OBD_FREE(input.part_data, input.part_datalen);
353 struct ioc_mv_s mvdata;
358 obdattr *srcoa, *tgtoa;
360 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
361 (!(obddev->obd_flags & OBD_ATTACHED))) {
362 CDEBUG(D_IOCTL, "Device not attached or set up\n");
366 /* get main structure */
367 err = copy_from_user(&input, (void *) arg, sizeof(input));
372 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
376 if (!obddev->obd_type->typ_ops ||
377 !obddev->obd_type->typ_ops->o_copy )
380 /* do the partition */
381 CDEBUG(D_IOCTL, "Copy %d, type %s src %ld tgt %ld\n", dev,
382 obddev->obd_type->typ_name, mvdata.src, mvdata.tgt);
384 conn.oc_id = mvdata.conn_id;
385 srcoa = obd_oa_fromid(&conn, mvdata.src);
388 tgtoa = obd_oa_fromid(&conn, mvdata.tgt);
394 err = obddev->obd_type->typ_ops->o_copy(&conn,srcoa, tgtoa);
401 struct ioc_mv_s mvdata;
406 obdattr *srcoa, *tgtoa;
408 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
409 (!(obddev->obd_flags & OBD_ATTACHED))) {
410 CDEBUG(D_IOCTL, "Device not attached or set up\n");
414 /* get main structure */
415 err = copy_from_user(&input, (void *) arg, sizeof(input));
420 CDEBUG(D_IOCTL, "Migrate copying %d\n", sizeof(mvdata));
421 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
425 if (!obddev->obd_type->typ_ops ||
426 !obddev->obd_type->typ_ops->o_copy )
429 /* do the partition */
430 CDEBUG(D_IOCTL, "Migrate %d, type %s conn %d src %ld tgt %ld\n",
431 dev, obddev->obd_type->typ_name, mvdata.conn_id,
432 mvdata.src, mvdata.tgt);
435 if ( ! (srcoa = obd_empty_oa()) )
437 if ( ! (tgtoa = obd_empty_oa()) ) {
442 srcoa->i_ino = mvdata.src;
443 tgtoa->i_ino = mvdata.tgt;
445 conn.oc_id = mvdata.conn_id;
447 err = obddev->obd_type->typ_ops->o_migrate(&conn, tgtoa, srcoa);
454 case OBD_IOC_SETUP: {
461 /* have we attached a type to this device */
462 if (!(obddev->obd_flags & OBD_ATTACHED)) {
463 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
468 /* has this been done already? */
469 if ( obddev->obd_flags & OBD_SET_UP ) {
470 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
471 dev, obddev->obd_type->typ_name);
476 /* get main structure */
477 err = copy_from_user(&input, (void *) arg, sizeof(input));
483 err = getdata(input.setup_datalen, &input.setup_data);
490 CDEBUG(D_IOCTL, "Setup %d, type %s\n", dev,
491 obddev->obd_type->typ_name);
492 if ( !obddev->obd_type->typ_ops ||
493 !obddev->obd_type->typ_ops->o_setup ) {
494 obddev->obd_type->typ_refcnt++;
495 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
496 dev, obddev->obd_type->typ_refcnt);
497 obddev->obd_flags |= OBD_SET_UP;
502 err = obddev->obd_type->typ_ops->o_setup(obddev,
507 obddev->obd_flags &= ~OBD_SET_UP;
510 obddev->obd_type->typ_refcnt++;
511 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
512 dev, obddev->obd_type->typ_refcnt);
513 obddev->obd_flags |= OBD_SET_UP;
518 case OBD_IOC_CLEANUP: {
520 /* has this minor been registered? */
521 if (!obddev->obd_type) {
526 if ( !obddev->obd_type->typ_refcnt )
527 printk("OBD_CLEANUP: Dev %d refcount wrap (%d)!\n",
528 dev, obddev->obd_type->typ_refcnt);
530 if ( !obddev->obd_flags & OBD_SET_UP ) {
535 if ( !obddev->obd_type->typ_ops->o_cleanup )
538 /* cleanup has no argument */
539 err = OBP(obddev, cleanup)(obddev);
546 obddev->obd_flags &= ~OBD_SET_UP;
547 obddev->obd_type->typ_refcnt--;
548 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
549 obddev->obd_type->typ_refcnt);
553 case OBD_IOC_CONNECT:
555 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
556 (!(obddev->obd_flags & OBD_ATTACHED))) {
557 CDEBUG(D_IOCTL, "Device not attached or set up\n");
562 if (obddev->obd_type->typ_ops->o_connect(&conn))
565 return copy_to_user((int *)arg, &conn.oc_id,
568 case OBD_IOC_DISCONNECT:
569 /* frees data structures */
570 /* has this minor been registered? */
571 if (!obddev->obd_type)
574 get_user(cli_id, (int *) arg);
577 OBP(obddev, disconnect)(&conn);
581 /* sync doesn't need a connection ID, because it knows
582 * what device it was called on, and can thus get the
583 * superblock that it needs. */
584 /* has this minor been registered? */
585 if (!obddev->obd_type)
588 if (!obddev->u.ext2.ext2_sb || !obddev->u.ext2.ext2_sb->s_dev) {
589 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
592 err = fsync_dev(obddev->u.ext2.ext2_sb->s_dev);
594 CDEBUG(D_IOCTL, "sync: fsync_dev failure\n");
596 CDEBUG(D_IOCTL, "sync: success\n");
599 return put_user(err, (int *) arg);
601 case OBD_IOC_CREATE: {
602 struct oic_create_s foo;
604 if ( copy_from_user(&foo, (const void *)arg, sizeof(foo)) )
607 /* has this minor been registered? */
608 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
609 !(obddev->obd_flags & OBD_SET_UP))
611 conn.oc_id = foo.conn_id;
613 i_ino = OBP(obddev, create)(&conn, foo.prealloc, &err);
615 CDEBUG(D_IOCTL, "create: obd_inode_new failure\n");
616 /* 0 is the only error value */
617 return put_user(0, (int *) arg);
620 return put_user(i_ino, (int *) arg);
622 case OBD_IOC_DESTROY: {
624 unsigned int conn_id;
629 if ( ! (oa = obd_empty_oa()) )
632 /* has this minor been registered? */
633 if (!obddev->obd_type)
637 copy_from_user(&destroy, (int *)arg, sizeof(struct destroy_s));
638 if ( !obddev->obd_type ||
639 !obddev->obd_type->typ_ops->o_destroy)
642 oa->i_ino = destroy.ino;
643 conn.oc_id = destroy.conn_id;
644 err = obddev->obd_type->typ_ops->o_destroy(&conn, oa);
645 OBD_FREE(oa, sizeof(*oa));
648 case OBD_IOC_SETATTR: {
649 struct oic_attr_s foo;
652 if ( ! (oa = obd_empty_oa()) )
655 /* has this minor been registered? */
656 if (!obddev->obd_type)
659 err = copy_from_user(&foo, (int *)arg, sizeof(foo));
663 if ( !obddev->obd_type ||
664 !obddev->obd_type->typ_ops->o_setattr)
668 inode_setattr(oa, &foo.iattr);
669 conn.oc_id = foo.conn_id;
670 err = obddev->obd_type->typ_ops->o_setattr(&conn, oa);
671 OBD_FREE(oa, sizeof(*oa));
675 case OBD_IOC_GETATTR: {
677 unsigned int conn_id;
683 err = copy_from_user(&foo, (int *)arg, sizeof(foo));
687 conn.oc_id = foo.conn_id;
688 oa = obd_oa_fromid(&conn, foo.ino);
692 inode_to_iattr(oa, &iattr);
693 err = copy_to_user((int *)arg, &iattr, sizeof(iattr));
700 struct oic_rw_s rw_s; /* read, write ioctl str */
702 err = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
707 conn.oc_id = rw_s.conn_id;
708 if ( ! (oa = obd_oa_fromid(&conn, rw_s.id)) )
712 if ( !obddev->obd_type->typ_ops ||
713 !obddev->obd_type->typ_ops->o_read )
716 err = obddev->obd_type->typ_ops->o_read(&conn, oa, rw_s.buf,
722 err = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
726 OBD_FREE(oa, sizeof(*oa));
730 case OBD_IOC_WRITE: {
732 struct oic_rw_s rw_s; /* read, write ioctl str */
734 err = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
738 conn.oc_id = rw_s.conn_id;
739 oa = obd_oa_fromid(&conn, rw_s.id);
744 if ( !obddev->obd_type->typ_ops ||
745 !obddev->obd_type->typ_ops->o_write )
748 err = obddev->obd_type->typ_ops->o_write(&conn, oa, rw_s.buf,
754 err = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
757 OBD_FREE(oa, sizeof(*oa));
760 case OBD_IOC_PREALLOCATE: {
761 struct oic_prealloc_s prealloc;
763 /* has this minor been registered? */
764 if (!obddev->obd_type)
767 err = copy_from_user(&prealloc, (int *)arg, sizeof(prealloc));
771 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
772 !(obddev->obd_flags & OBD_SET_UP)) {
773 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
777 if (!obddev->obd_type ||
778 !obddev->obd_type->typ_ops->o_preallocate)
780 conn.oc_id = prealloc.cli_id;
781 err = obddev->obd_type->typ_ops->o_preallocate(&conn,
787 return copy_to_user((int *)arg, &prealloc, sizeof(prealloc));
789 case OBD_IOC_STATFS: {
791 unsigned int conn_id;
794 /* has this minor been registered? */
795 if (!obddev->obd_type)
798 tmp = (void *)arg + sizeof(unsigned int);
799 get_user(conn_id, (int *) arg);
800 if ( !obddev->obd_type ||
801 !obddev->obd_type->typ_ops->o_statfs)
804 conn.oc_id = conn_id;
805 err = obddev->obd_type->typ_ops->o_statfs(&conn, &buf);
808 err = copy_to_user(tmp, &buf, sizeof(buf));
813 struct obd_type *type;
814 struct oic_generic input;
817 /* get data structures */
818 err = copy_from_user(&input, (void *)arg, sizeof(input));
824 err = getdata(input.att_typelen + 1, &input.att_type);
831 type = obd_nm_to_type(input.att_type);
832 OBD_FREE(input.att_type, input.att_typelen + 1);
834 printk("Unknown obd type dev %d\n", dev);
839 if ( !type->typ_ops->o_iocontrol ) {
843 conn.oc_id = input.att_connid;
845 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
846 cmd, type->typ_name, input.att_datalen);
848 /* get the generic data */
849 karg = input.att_data;
850 err = getdata(input.att_datalen, &karg);
856 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
857 karg, input.att_data);
858 OBD_FREE(karg, input.att_datalen);
866 /* Driver interface done, utility functions follow */
868 int obd_register_type(struct obd_ops *ops, char *nm)
870 struct obd_type *type;
873 if (obd_init_magic != 0x11223344) {
878 if ( obd_nm_to_type(nm) ) {
879 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
884 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
889 memset(type, 0, sizeof(*type));
890 INIT_LIST_HEAD(&type->typ_chain);
892 list_add(&type->typ_chain, obd_types.next);
899 int obd_unregister_type(char *nm)
901 struct obd_type *type = obd_nm_to_type(nm);
905 printk("OBD: NO TYPE\n");
910 if ( type->typ_refcnt ) {
912 printk("OBD: refcount wrap (%d)\n", type->typ_refcnt);
917 list_del(&type->typ_chain);
918 OBD_FREE(type, sizeof(*type));
923 /* declare character device */
924 static struct file_operations obd_psdev_fops = {
928 NULL, /* presto_psdev_readdir */
930 obd_class_ioctl, /* ioctl */
931 NULL, /* presto_psdev_mmap */
932 obd_class_open, /* open */
934 obd_class_release, /* release */
937 NULL, /* check_media_change */
938 NULL, /* revalidate */
949 printk(KERN_INFO "OBD class driver v0.002, braam@stelias.com\n");
951 INIT_LIST_HEAD(&obd_types);
953 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
955 printk(KERN_ERR "obd_psdev: unable to get major %d\n",
960 for (i = 0; i < MAX_OBD_DEVICES; i++) {
961 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
962 obd_dev[i].obd_minor = i;
963 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
967 obd_init_magic = 0x11223344;
971 EXPORT_SYMBOL(obd_register_type);
972 EXPORT_SYMBOL(obd_unregister_type);
974 EXPORT_SYMBOL(obd_print_entry);
975 EXPORT_SYMBOL(obd_debug_level);
976 EXPORT_SYMBOL(obd_dev);
978 EXPORT_SYMBOL(gen_connect);
979 EXPORT_SYMBOL(gen_client);
980 EXPORT_SYMBOL(gen_cleanup);
981 EXPORT_SYMBOL(gen_disconnect);
982 EXPORT_SYMBOL(gen_copy_data);
984 /* EXPORT_SYMBOL(gen_multi_attach); */
985 EXPORT_SYMBOL(gen_multi_setup);
986 EXPORT_SYMBOL(gen_multi_cleanup);
990 int init_module(void)
995 void cleanup_module(void)
1000 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
1001 for (i = 0; i < MAX_OBD_DEVICES; i++) {
1002 struct obd_device *obddev = &obd_dev[i];
1003 if ( obddev->obd_type &&
1004 (obddev->obd_flags & OBD_SET_UP) &&
1005 obddev->obd_type->typ_ops->o_detach ) {
1006 OBP(obddev, detach)(obddev);