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, "Refcount now %d\n", obd_dev[dev].obd_refcnt++);
84 /* called when closing /dev/obdNNN */
85 static int obd_class_release(struct inode * inode, struct file * file)
92 dev = MINOR(inode->i_rdev);
93 if (dev >= MAX_OBD_DEVICES)
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--;
101 CDEBUG(D_PSDEV, "Refcount now %d\n", obd_dev[dev].obd_refcnt++);
109 /* support function */
110 static struct obd_type *obd_nm_to_type(char *nm)
112 struct list_head *tmp;
113 struct obd_type *type;
114 CDEBUG(D_IOCTL, "SEARCH %s\n", nm);
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 ) {
129 static int getdata(int len, void **data)
136 CDEBUG(D_IOCTL, "getdata: len %d, add %p\n", len, *data);
138 OBD_ALLOC(tmp, void *, len);
143 if ( copy_from_user(tmp, *data, len)) {
152 /* to control /dev/obdNNN */
153 static int obd_class_ioctl (struct inode * inode, struct file * filp,
154 unsigned int cmd, unsigned long arg)
157 struct obd_device *obddev;
158 struct obd_conn conn;
159 long int cli_id; /* connect, disconnect */
164 dev = MINOR(inode->i_rdev);
165 if (dev > MAX_OBD_DEVICES)
167 obddev = &obd_dev[dev];
168 conn.oc_dev = obddev;
173 case OBD_IOC_ATTACH: {
174 struct obd_type *type;
175 struct oic_generic input;
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);
183 /* get data structures */
184 err = copy_from_user(&input, (void *) arg, sizeof(input));
188 if ( (err = getdata(input.att_typelen + 1, &input.att_type)) )
193 type = obd_nm_to_type(input.att_type);
194 OBD_FREE(input.att_type, input.att_typelen + 1);
196 printk("Unknown obd type dev %d\n", dev);
199 obddev->obd_type = type;
201 /* get the attach data */
202 if ( (err = getdata(input.att_datalen, &input.att_data)) ) {
206 INIT_LIST_HEAD(&obddev->obd_gen_clients);
207 obddev->obd_multi_count = 0;
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;
219 err = OBP(obddev,attach)(obddev,
220 input.att_datalen, input.att_data);
221 OBD_FREE(input.att_data, input.att_datalen);
224 obddev->obd_flags &= ~OBD_ATTACHED;
225 obddev->obd_type = NULL;
227 obddev->obd_flags |= OBD_ATTACHED;
234 case OBD_IOC_DETACH: {
236 if (obddev->obd_flags & OBD_SET_UP)
238 if (! (obddev->obd_flags & OBD_ATTACHED) )
240 if ( !list_empty(&obddev->obd_gen_clients) )
243 obddev->obd_flags &= ~OBD_ATTACHED;
244 obddev->obd_type->typ_refcnt--;
245 obddev->obd_type = NULL;
251 case OBD_IOC_FORMAT: {
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);
263 /* get main structure */
264 err = copy_from_user(&input, (void *) arg, sizeof(input));
268 err = getdata(input.format_datalen, &input.format_data);
272 if (!obddev->obd_type->typ_ops ||
273 !obddev->obd_type->typ_ops->o_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);
282 OBD_FREE(input.format_data, input.format_datalen);
285 case OBD_IOC_PARTITION: {
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);
297 /* get main structure */
298 err = copy_from_user(&input, (void *) arg, sizeof(input));
302 err = getdata(input.part_datalen, &input.part_data);
306 if (!obddev->obd_type->typ_ops ||
307 !obddev->obd_type->typ_ops->o_partition )
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);
316 OBD_FREE(input.part_data, input.part_datalen);
321 struct ioc_mv_s mvdata;
326 obdattr *srcoa, *tgtoa;
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");
334 /* get main structure */
335 err = copy_from_user(&input, (void *) arg, sizeof(input));
340 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
344 if (!obddev->obd_type->typ_ops ||
345 !obddev->obd_type->typ_ops->o_copy )
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);
352 conn.oc_id = mvdata.conn_id;
353 srcoa = obd_oa_fromid(&conn, mvdata.src);
356 tgtoa = obd_oa_fromid(&conn, mvdata.tgt);
362 err = obddev->obd_type->typ_ops->o_copy(&conn,srcoa, tgtoa);
369 struct ioc_mv_s mvdata;
374 obdattr *srcoa, *tgtoa;
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");
382 /* get main structure */
383 err = copy_from_user(&input, (void *) arg, sizeof(input));
388 CDEBUG(D_IOCTL, "Migrate copying %d\n", sizeof(mvdata));
389 err = copy_from_user(&mvdata, input.part_data, sizeof(mvdata));
393 if (!obddev->obd_type->typ_ops ||
394 !obddev->obd_type->typ_ops->o_copy )
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);
402 if ( ! (srcoa = obd_empty_oa()) )
404 if ( ! (tgtoa = obd_empty_oa()) ) {
409 srcoa->i_ino = mvdata.src;
410 tgtoa->i_ino = mvdata.tgt;
412 conn.oc_id = mvdata.conn_id;
414 err = obddev->obd_type->typ_ops->o_migrate(&conn, tgtoa, srcoa);
421 case OBD_IOC_SETUP: {
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);
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);
440 /* get main structure */
441 err = copy_from_user(&input, (void *) arg, sizeof(input));
445 err = getdata(input.setup_datalen, &input.setup_data);
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;
459 err = obddev->obd_type->typ_ops->o_setup
460 (obddev, input.setup_datalen, input.setup_data);
463 obddev->obd_flags &= ~OBD_SET_UP;
465 obddev->obd_type->typ_refcnt++;
466 obddev->obd_flags |= OBD_SET_UP;
470 case OBD_IOC_CLEANUP: {
473 /* has this minor been registered? */
474 if (!obddev->obd_type)
477 if ( !obddev->obd_type->typ_refcnt )
478 printk("OBD_CLEANUP: refcount wrap!\n");
480 if ( !obddev->obd_flags & OBD_SET_UP )
483 if ( !obddev->obd_type->typ_ops->o_cleanup )
486 /* cleanup has no argument */
487 rc = OBP(obddev, cleanup)(obddev);
492 obddev->obd_flags &= ~OBD_SET_UP;
493 obddev->obd_type->typ_refcnt--;
496 case OBD_IOC_CONNECT:
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");
506 if (obddev->obd_type->typ_ops->o_connect(&conn))
509 return copy_to_user((int *)arg, &conn.oc_id,
512 case OBD_IOC_DISCONNECT:
513 /* frees data structures */
514 /* has this minor been registered? */
515 if (!obddev->obd_type)
518 get_user(cli_id, (int *) arg);
521 OBP(obddev, disconnect)(&conn);
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)
532 if (!obddev->u.ext2.ext2_sb || !obddev->u.ext2.ext2_sb->s_dev) {
533 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
536 if ((err = fsync_dev(obddev->u.ext2.ext2_sb->s_dev)))
537 CDEBUG(D_IOCTL, "sync: fsync_dev failure\n");
539 CDEBUG(D_IOCTL, "sync: success\n");
542 return put_user(err, (int *) arg);
544 case OBD_IOC_CREATE: {
546 struct oic_create_s foo;
548 if ( copy_from_user(&foo, (const void *)arg, sizeof(foo)) )
551 /* has this minor been registered? */
552 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
553 !(obddev->obd_flags & OBD_SET_UP))
555 conn.oc_id = foo.conn_id;
557 i_ino = OBP(obddev, create)(&conn, foo.prealloc, &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);
564 return put_user(i_ino, (int *) arg);
566 case OBD_IOC_DESTROY:
569 unsigned int conn_id;
575 if ( ! (oa = obd_empty_oa()) )
578 /* has this minor been registered? */
579 if (!obddev->obd_type)
583 copy_from_user(&destroy, (int *)arg, sizeof(struct destroy_s));
584 if ( !obddev->obd_type ||
585 !obddev->obd_type->typ_ops->o_destroy)
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));
594 case OBD_IOC_SETATTR:
596 struct oic_attr_s foo;
600 if ( ! (oa = obd_empty_oa()) )
603 /* has this minor been registered? */
604 if (!obddev->obd_type)
607 rc = copy_from_user(&foo, (int *)arg, sizeof(foo));
611 if ( !obddev->obd_type ||
612 !obddev->obd_type->typ_ops->o_setattr)
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));
623 case OBD_IOC_GETATTR:
627 unsigned int conn_id;
633 rc = copy_from_user(&foo, (int *)arg, sizeof(foo));
637 conn.oc_id = foo.conn_id;
638 oa = obd_oa_fromid(&conn, foo.ino);
642 inode_to_iattr(oa, &iattr);
643 rc = copy_to_user((int *)arg, &iattr, sizeof(iattr));
651 struct oic_rw_s rw_s; /* read, write ioctl str */
653 rc = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
658 conn.oc_id = rw_s.conn_id;
659 if ( ! (oa = obd_oa_fromid(&conn, rw_s.id)) )
663 if ( !obddev->obd_type->typ_ops ||
664 !obddev->obd_type->typ_ops->o_read )
667 rc = obddev->obd_type->typ_ops->o_read
668 (&conn, oa, rw_s.buf, &rw_s.count, rw_s.offset);
672 rc = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
676 OBD_FREE(oa, sizeof(*oa));
680 case OBD_IOC_WRITE: {
683 struct oic_rw_s rw_s; /* read, write ioctl str */
685 rc = copy_from_user(&rw_s, (int *)arg, sizeof(rw_s));
689 conn.oc_id = rw_s.conn_id;
690 oa = obd_oa_fromid(&conn, rw_s.id);
695 if ( !obddev->obd_type->typ_ops ||
696 !obddev->obd_type->typ_ops->o_write )
699 rc = obddev->obd_type->typ_ops->o_write
700 (&conn, oa, rw_s.buf, &rw_s.count, rw_s.offset);
704 rc = copy_to_user((int*)arg, &rw_s.count, sizeof(rw_s.count));
707 OBD_FREE(oa, sizeof(*oa));
710 case OBD_IOC_PREALLOCATE: {
711 struct oic_prealloc_s prealloc;
714 /* has this minor been registered? */
715 if (!obddev->obd_type)
719 rc = copy_from_user(&prealloc, (int *)arg, sizeof(prealloc));
723 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
724 !(obddev->obd_flags & OBD_SET_UP)) {
725 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
729 if (!obddev->obd_type ||
730 !obddev->obd_type->typ_ops->o_preallocate)
732 conn.oc_id = prealloc.cli_id;
733 rc = obddev->obd_type->typ_ops->o_preallocate
734 (&conn, &prealloc.alloc, prealloc.inodes);
738 return copy_to_user((int *)arg, &prealloc, sizeof(prealloc));
743 unsigned int conn_id;
747 /* has this minor been registered? */
748 if (!obddev->obd_type)
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)
757 conn.oc_id = conn_id;
758 rc = obddev->obd_type->typ_ops->o_statfs(&conn, &buf);
761 rc = copy_to_user(tmp, &buf, sizeof(buf));
766 struct obd_type *type;
767 struct oic_generic input;
770 /* get data structures */
771 err = copy_from_user(&input, (void *) arg, sizeof(input));
777 if ( (err = getdata(input.att_typelen + 1, &input.att_type))){
784 type = obd_nm_to_type(input.att_type);
785 OBD_FREE(input.att_type, input.att_typelen + 1);
787 printk("Unknown obd type dev %d\n", dev);
792 if ( !type->typ_ops->o_iocontrol ) {
796 conn.oc_id = input.att_connid;
798 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
799 cmd, type->typ_name, input.att_datalen);
801 /* get the generic data */
802 karg = input.att_data;
803 if ( (err = getdata(input.att_datalen, &karg)) ) {
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);
819 /* Driver interface done, utility functions follow */
821 int obd_register_type(struct obd_ops *ops, char *nm)
823 struct obd_type *type;
826 if (obd_init_magic != 0x11223344) {
831 if ( obd_nm_to_type(nm) ) {
832 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
837 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
842 memset(type, 0, sizeof(*type));
843 INIT_LIST_HEAD(&type->typ_chain);
845 list_add(&type->typ_chain, obd_types.next);
852 int obd_unregister_type(char *nm)
854 struct obd_type *type = obd_nm_to_type(nm);
858 printk("OBD: NO TYPE\n");
863 if ( type->typ_refcnt ) {
865 printk("OBD: refcount wrap\n");
870 list_del(&type->typ_chain);
871 OBD_FREE(type, sizeof(*type));
876 /* declare character device */
877 static struct file_operations obd_psdev_fops = {
881 NULL, /* presto_psdev_readdir */
883 obd_class_ioctl, /* ioctl */
884 NULL, /* presto_psdev_mmap */
885 obd_class_open, /* open */
887 obd_class_release, /* release */
890 NULL, /* check_media_change */
891 NULL, /* revalidate */
902 printk(KERN_INFO "OBD class driver v0.002, braam@stelias.com\n");
904 INIT_LIST_HEAD(&obd_types);
906 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
908 printk(KERN_ERR "obd_psdev: unable to get major %d\n",
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);
920 obd_init_magic = 0x11223344;
924 EXPORT_SYMBOL(obd_register_type);
925 EXPORT_SYMBOL(obd_unregister_type);
927 EXPORT_SYMBOL(obd_print_entry);
928 EXPORT_SYMBOL(obd_debug_level);
929 EXPORT_SYMBOL(obd_dev);
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);
937 /* EXPORT_SYMBOL(gen_multi_attach); */
938 EXPORT_SYMBOL(gen_multi_setup);
939 EXPORT_SYMBOL(gen_multi_cleanup);
943 int init_module(void)
948 void cleanup_module(void)
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);