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 /* NOTE this must be larger than any of the ioctl data structs */
161 struct obd_conn conn;
163 long int cli_id; /* connect, disconnect */
168 dev = MINOR(inode->i_rdev);
169 if (dev > MAX_OBD_DEVICES)
171 obddev = &obd_dev[dev];
172 conn.oc_dev = obddev;
177 case OBD_IOC_ATTACH: {
178 struct obd_type *type;
179 struct oic_generic *input = karg;
182 /* have we attached a type to this device */
183 if ( obddev->obd_type ||
184 (obddev->obd_flags & OBD_ATTACHED) ){
186 "OBD Device %d already attached to type %s.\n",
187 dev, obddev->obd_type->typ_name);
192 /* get data structures */
193 err = copy_from_user(input, (void *)arg, sizeof(*input));
199 err = getdata(input->att_typelen + 1, &input->att_type);
206 type = obd_nm_to_type(input->att_type);
207 OBD_FREE(input->att_type, input->att_typelen + 1);
209 printk("Unknown obd type dev %d\n", dev);
213 obddev->obd_type = type;
215 /* get the attach data */
216 err = getdata(input->att_datalen, &input->att_data);
222 INIT_LIST_HEAD(&obddev->obd_gen_clients);
223 obddev->obd_multi_count = 0;
225 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
226 dev, input->att_datalen, obddev->obd_type->typ_name);
227 /* maybe we are done */
228 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
229 obddev->obd_flags |= OBD_ATTACHED;
231 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
239 err = OBP(obddev, attach)(obddev, input->att_datalen,
241 OBD_FREE(input->att_data, input->att_datalen);
244 obddev->obd_flags &= ~OBD_ATTACHED;
245 obddev->obd_type = NULL;
248 obddev->obd_flags |= OBD_ATTACHED;
250 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
258 case OBD_IOC_DETACH: {
261 if (obddev->obd_flags & OBD_SET_UP) {
265 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
266 CDEBUG(D_IOCTL, "Device not attached\n");
270 if ( !list_empty(&obddev->obd_gen_clients) ) {
271 CDEBUG(D_IOCTL, "Device has connected clients\n");
276 CDEBUG(D_IOCTL, "Detach %d, type %s\n", dev,
277 obddev->obd_type->typ_name);
278 obddev->obd_flags &= ~OBD_ATTACHED;
279 obddev->obd_type->typ_refcnt--;
280 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
281 obddev->obd_type->typ_refcnt);
282 obddev->obd_type = NULL;
288 case OBD_IOC_SETUP: {
296 /* have we attached a type to this device */
297 if (!(obddev->obd_flags & OBD_ATTACHED)) {
298 CDEBUG(D_IOCTL, "Device not attached\n");
303 /* has this been done already? */
304 if ( obddev->obd_flags & OBD_SET_UP ) {
305 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
306 dev, obddev->obd_type->typ_name);
311 /* get main structure */
312 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
318 err = getdata(setup->setup_datalen, &setup->setup_data);
325 CDEBUG(D_IOCTL, "Setup %d, type %s\n", dev,
326 obddev->obd_type->typ_name);
327 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
328 obddev->obd_type->typ_refcnt++;
329 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
330 dev, obddev->obd_type->typ_refcnt);
331 obddev->obd_flags |= OBD_SET_UP;
336 err = OBP(obddev, setup)(obddev, setup->setup_datalen,
340 obddev->obd_flags &= ~OBD_SET_UP;
343 obddev->obd_type->typ_refcnt++;
344 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
345 dev, obddev->obd_type->typ_refcnt);
346 obddev->obd_flags |= OBD_SET_UP;
351 case OBD_IOC_CLEANUP: {
353 /* has this minor been registered? */
354 if (!obddev->obd_type) {
355 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
360 if ( !obddev->obd_type->typ_refcnt ) {
361 printk("OBD_CLEANUP: Dev %d has refcount (%d)!\n",
362 dev, obddev->obd_type->typ_refcnt);
367 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
368 (!(obddev->obd_flags & OBD_ATTACHED))) {
369 CDEBUG(D_IOCTL, "Device not attached or set up\n");
374 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
377 /* cleanup has no argument */
378 err = OBP(obddev, cleanup)(obddev);
385 obddev->obd_flags &= ~OBD_SET_UP;
386 obddev->obd_type->typ_refcnt--;
387 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
388 obddev->obd_type->typ_refcnt);
392 case OBD_IOC_CONNECT:
394 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
395 (!(obddev->obd_flags & OBD_ATTACHED))) {
396 CDEBUG(D_IOCTL, "Device not attached or set up\n");
400 if ( !OBT(obddev) || !OBP(obddev, connect) )
403 err = OBP(obddev, connect)(&conn);
407 return copy_to_user((int *)arg, &conn.oc_id,
410 case OBD_IOC_DISCONNECT:
411 /* frees data structures */
412 /* has this minor been registered? */
413 if (!obddev->obd_type)
416 get_user(cli_id, (int *) arg);
419 if ( !OBT(obddev) || !OBP(obddev, disconnect))
422 OBP(obddev, disconnect)(&conn);
425 /* XXX sync needs to be done */
427 struct oic_range_s *range = karg;
429 if (!obddev->obd_type)
432 err = copy_from_user(range, (const void *)arg, sizeof(*range));
438 return put_user(err, (int *) arg);
440 case OBD_IOC_CREATE: {
441 struct oic_attr_s *attr = karg;
443 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
449 /* has this minor been registered? */
450 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
451 !(obddev->obd_flags & OBD_SET_UP)) {
452 CDEBUG(D_IOCTL, "Device not attached or set up\n");
455 conn.oc_id = attr->conn_id;
457 if ( !OBT(obddev) || !OBP(obddev, create) )
460 err = OBP(obddev, create)(&conn, &attr->obdo);
466 err = copy_to_user((int *)arg, attr, sizeof(*attr));
471 case OBD_IOC_DESTROY: {
472 struct oic_attr_s *attr = karg;
474 /* has this minor been registered? */
475 if (!obddev->obd_type)
478 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
484 if ( !OBT(obddev) || !OBP(obddev, destroy) )
487 conn.oc_id = attr->conn_id;
488 err = OBP(obddev, destroy)(&conn, &attr->obdo);
493 case OBD_IOC_SETATTR: {
494 struct oic_attr_s *attr = karg;
496 /* has this minor been registered? */
497 if (!obddev->obd_type)
500 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
504 if ( !OBT(obddev) || !OBP(obddev, setattr) )
507 conn.oc_id = attr->conn_id;
508 err = OBP(obddev, setattr)(&conn, &attr->obdo);
513 case OBD_IOC_GETATTR: {
514 struct oic_attr_s *attr = karg;
516 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
520 conn.oc_id = attr->conn_id;
521 err = OBP(obddev, getattr)(&conn, &attr->obdo);
527 err = copy_to_user((int *)arg, attr, sizeof(*attr));
534 struct oic_rw_s *rw_s = karg; /* read, write ioctl str */
536 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
542 conn.oc_id = rw_s->conn_id;
544 if ( !OBT(obddev) || !OBP(obddev, read) ) {
550 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
551 &rw_s->count, rw_s->offset);
557 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
562 case OBD_IOC_WRITE: {
563 struct oic_rw_s *rw_s = karg; /* read, write ioctl str */
565 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
571 conn.oc_id = rw_s->conn_id;
573 if ( !OBT(obddev) || !OBP(obddev, write) ) {
578 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
579 &rw_s->count, rw_s->offset);
585 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
589 case OBD_IOC_PREALLOCATE: {
590 struct oic_prealloc_s *prealloc = karg;
592 /* has this minor been registered? */
593 if (!obddev->obd_type)
596 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
600 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
601 !(obddev->obd_flags & OBD_SET_UP)) {
602 CDEBUG(D_IOCTL, "Device not attached or set up\n");
606 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
609 conn.oc_id = prealloc->cli_id;
610 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
617 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
621 case OBD_IOC_STATFS: {
623 unsigned int conn_id;
626 /* has this minor been registered? */
627 if (!obddev->obd_type)
630 tmp = (void *)arg + sizeof(unsigned int);
631 get_user(conn_id, (int *) arg);
633 if ( !OBT(obddev) || !OBP(obddev, statfs) )
636 conn.oc_id = conn_id;
637 err = OBP(obddev, statfs)(&conn, &buf);
642 err = copy_to_user(tmp, &buf, sizeof(buf));
648 struct ioc_mv_s *mvdata = karg;
650 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
651 (!(obddev->obd_flags & OBD_ATTACHED))) {
652 CDEBUG(D_IOCTL, "Device not attached or set up\n");
656 /* get main structure */
657 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
663 if ( !OBT(obddev) || !OBP(obddev, copy) )
666 /* do the partition */
667 CDEBUG(D_IOCTL, "Copy %d, type %s dst %Ld src %Ld\n", dev,
668 obddev->obd_type->typ_name, mvdata->dst.o_id,
671 conn.oc_id = mvdata->src_conn_id;
673 err = OBP(obddev, copy)(&conn, &mvdata->dst,
675 mvdata->src.o_size, 0);
680 struct ioc_mv_s *mvdata = karg;
682 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
683 (!(obddev->obd_flags & OBD_ATTACHED))) {
684 CDEBUG(D_IOCTL, "Device not attached or set up\n");
688 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
694 CDEBUG(D_IOCTL, "Migrate copying %d\n", sizeof(*mvdata));
696 if ( !OBT(obddev) || !OBP(obddev, migrate) )
699 /* do the partition */
700 CDEBUG(D_IOCTL, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
701 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
702 mvdata->src.o_id, mvdata->dst.o_id);
704 conn.oc_id = mvdata->src_conn_id;
705 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
706 mvdata->dst.o_size, 0);
712 struct obd_type *type;
713 struct oic_generic input;
716 /* get data structures */
717 err = copy_from_user(&input, (void *)arg, sizeof(input));
723 err = getdata(input.att_typelen + 1, &input.att_type);
730 type = obd_nm_to_type(input.att_type);
731 OBD_FREE(input.att_type, input.att_typelen + 1);
733 printk("Unknown obd type dev %d\n", dev);
738 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
742 conn.oc_id = input.att_connid;
744 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
745 cmd, type->typ_name, input.att_datalen);
747 /* get the generic data */
748 karg = input.att_data;
749 err = getdata(input.att_datalen, karg);
755 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
756 karg, input.att_data);
757 OBD_FREE(karg, input.att_datalen);
765 /* Driver interface done, utility functions follow */
767 int obd_register_type(struct obd_ops *ops, char *nm)
769 struct obd_type *type;
772 if (obd_init_magic != 0x11223344) {
777 if ( obd_nm_to_type(nm) ) {
778 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
783 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
788 memset(type, 0, sizeof(*type));
789 INIT_LIST_HEAD(&type->typ_chain);
791 list_add(&type->typ_chain, obd_types.next);
798 int obd_unregister_type(char *nm)
800 struct obd_type *type = obd_nm_to_type(nm);
804 printk("Unknown obd type\n");
809 if ( type->typ_refcnt ) {
811 printk("OBD: Type %s has refcount (%d)\n", nm,
817 list_del(&type->typ_chain);
818 OBD_FREE(type, sizeof(*type));
823 /* declare character device */
824 static struct file_operations obd_psdev_fops = {
828 NULL, /* presto_psdev_readdir */
830 obd_class_ioctl, /* ioctl */
831 NULL, /* presto_psdev_mmap */
832 obd_class_open, /* open */
834 obd_class_release, /* release */
837 NULL, /* check_media_change */
838 NULL, /* revalidate */
849 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
851 INIT_LIST_HEAD(&obd_types);
853 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
855 printk(KERN_ERR "obd_psdev: unable to get major %d\n",
860 for (i = 0; i < MAX_OBD_DEVICES; i++) {
861 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
862 obd_dev[i].obd_minor = i;
863 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
866 obd_init_obdo_cache();
868 obd_init_magic = 0x11223344;
872 EXPORT_SYMBOL(obd_register_type);
873 EXPORT_SYMBOL(obd_unregister_type);
875 EXPORT_SYMBOL(obd_print_entry);
876 EXPORT_SYMBOL(obd_debug_level);
877 EXPORT_SYMBOL(obd_dev);
879 EXPORT_SYMBOL(gen_connect);
880 EXPORT_SYMBOL(gen_client);
881 EXPORT_SYMBOL(gen_cleanup);
882 EXPORT_SYMBOL(gen_disconnect);
883 EXPORT_SYMBOL(gen_copy_data);
884 EXPORT_SYMBOL(obdo_cachep);
886 /* EXPORT_SYMBOL(gen_multi_attach); */
887 EXPORT_SYMBOL(gen_multi_setup);
888 EXPORT_SYMBOL(gen_multi_cleanup);
892 int init_module(void)
897 void cleanup_module(void)
902 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
903 for (i = 0; i < MAX_OBD_DEVICES; i++) {
904 struct obd_device *obddev = &obd_dev[i];
905 if ( obddev->obd_type &&
906 (obddev->obd_flags & OBD_SET_UP) &&
907 OBT(obddev) && OBP(obddev, detach) ) {
908 /* XXX should this call generic detach otherwise? */
909 OBP(obddev, detach)(obddev);