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/kmod.h> /* for request_module() */
38 #include <linux/sched.h>
40 #include <linux/malloc.h>
41 #include <linux/ioport.h>
42 #include <linux/fcntl.h>
43 #include <linux/delay.h>
44 #include <linux/skbuff.h>
45 #include <linux/proc_fs.h>
46 #include <linux/vmalloc.h>
48 #include <linux/poll.h>
49 #include <linux/init.h>
50 #include <linux/list.h>
52 #include <asm/segment.h>
53 #include <asm/system.h>
55 #include <asm/uaccess.h>
57 #include <linux/obd_support.h>
58 #include <linux/obd_class.h>
60 static int obd_init_magic;
61 int obd_print_entry = 1;
62 int obd_debug_level = 0;
63 struct obd_device obd_dev[MAX_OBD_DEVICES];
64 struct list_head obd_types;
66 /* called when opening /dev/obdNNN */
67 static int obd_class_open(struct inode * inode, struct file * file)
74 dev = MINOR(inode->i_rdev);
75 if (dev >= MAX_OBD_DEVICES)
77 obd_dev[dev].obd_refcnt++;
78 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
79 obd_dev[dev].obd_refcnt);
86 /* called when closing /dev/obdNNN */
87 static int obd_class_release(struct inode * inode, struct file * file)
94 dev = MINOR(inode->i_rdev);
95 if (dev >= MAX_OBD_DEVICES)
97 fsync_dev(inode->i_rdev);
98 if (obd_dev[dev].obd_refcnt <= 0)
99 printk(KERN_ALERT __FUNCTION__ ": refcount(%d) <= 0\n",
100 obd_dev[dev].obd_refcnt);
101 obd_dev[dev].obd_refcnt--;
103 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
104 obd_dev[dev].obd_refcnt);
111 /* support function */
112 static struct obd_type *obd_nm_to_type(char *nm)
114 struct list_head *tmp;
115 struct obd_type *type;
116 CDEBUG(D_IOCTL, "SEARCH %s\n", nm);
119 while ( (tmp = tmp->next) != &obd_types ) {
120 type = list_entry(tmp, struct obd_type, typ_chain);
121 CDEBUG(D_IOCTL, "TYP %s\n", type->typ_name);
122 if (strlen(type->typ_name) == strlen(nm) &&
123 strcmp(type->typ_name, nm) == 0 ) {
131 static int getdata(int len, void **data)
138 CDEBUG(D_IOCTL, "getdata: len %d, add %p\n", len, *data);
140 OBD_ALLOC(tmp, void *, len);
145 if ( copy_from_user(tmp, *data, len)) {
154 /* to control /dev/obdNNN */
155 static int obd_class_ioctl (struct inode * inode, struct file * filp,
156 unsigned int cmd, unsigned long arg)
158 struct obd_device *obddev;
159 /* NOTE this must be larger than any of the ioctl data structs */
162 struct obd_conn conn;
164 long int cli_id; /* connect, disconnect */
169 dev = MINOR(inode->i_rdev);
170 if (dev > MAX_OBD_DEVICES)
172 obddev = &obd_dev[dev];
173 conn.oc_dev = obddev;
178 case OBD_IOC_ATTACH: {
179 struct obd_type *type;
180 struct oic_generic *input = tmp_buf;
184 /* have we attached a type to this device */
185 if ( obddev->obd_type ||
186 (obddev->obd_flags & OBD_ATTACHED) ){
188 "OBD Device %d already attached to type %s.\n",
189 dev, obddev->obd_type->typ_name);
194 /* get data structures */
195 err = copy_from_user(input, (void *)arg, sizeof(*input));
201 err = getdata(input->att_typelen + 1, &input->att_type);
208 nm = input->att_type;
209 type = obd_nm_to_type(nm);
212 if ( !request_module(nm) ) {
213 CDEBUG(D_IOCTL, "Loaded module '%s'\n", nm);
214 type = obd_nm_to_type(nm);
216 CDEBUG(D_IOCTL, "Can't load module '%s'\n", nm);
221 OBD_FREE(input->att_type, input->att_typelen + 1);
223 printk(__FUNCTION__ ": unknown obd type dev %d\n",
228 obddev->obd_type = type;
230 /* get the attach data */
231 err = getdata(input->att_datalen, &input->att_data);
237 INIT_LIST_HEAD(&obddev->obd_gen_clients);
238 obddev->obd_multi_count = 0;
240 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
241 dev, input->att_datalen, obddev->obd_type->typ_name);
242 /* maybe we are done */
243 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
244 obddev->obd_flags |= OBD_ATTACHED;
246 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
254 err = OBP(obddev, attach)(obddev, input->att_datalen,
256 OBD_FREE(input->att_data, input->att_datalen);
259 obddev->obd_flags &= ~OBD_ATTACHED;
260 obddev->obd_type = NULL;
263 obddev->obd_flags |= OBD_ATTACHED;
265 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
273 case OBD_IOC_DETACH: {
276 if (obddev->obd_flags & OBD_SET_UP) {
280 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
281 CDEBUG(D_IOCTL, "Device not attached\n");
285 if ( !list_empty(&obddev->obd_gen_clients) ) {
286 CDEBUG(D_IOCTL, "Device has connected clients\n");
291 CDEBUG(D_IOCTL, "Detach %d, type %s\n", dev,
292 obddev->obd_type->typ_name);
293 obddev->obd_flags &= ~OBD_ATTACHED;
294 obddev->obd_type->typ_refcnt--;
295 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
296 obddev->obd_type->typ_refcnt);
297 obddev->obd_type = NULL;
303 case OBD_IOC_SETUP: {
311 /* have we attached a type to this device */
312 if (!(obddev->obd_flags & OBD_ATTACHED)) {
313 CDEBUG(D_IOCTL, "Device not attached\n");
318 /* has this been done already? */
319 if ( obddev->obd_flags & OBD_SET_UP ) {
320 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
321 dev, obddev->obd_type->typ_name);
326 /* get main structure */
327 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
333 err = getdata(setup->setup_datalen, &setup->setup_data);
340 CDEBUG(D_IOCTL, "Setup %d, type %s\n", dev,
341 obddev->obd_type->typ_name);
342 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
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 err = OBP(obddev, setup)(obddev, setup->setup_datalen,
355 obddev->obd_flags &= ~OBD_SET_UP;
358 obddev->obd_type->typ_refcnt++;
359 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n",
360 dev, obddev->obd_type->typ_refcnt);
361 obddev->obd_flags |= OBD_SET_UP;
366 case OBD_IOC_CLEANUP: {
368 /* has this minor been registered? */
369 if (!obddev->obd_type) {
370 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
375 if ( !obddev->obd_type->typ_refcnt ) {
376 CDEBUG(D_IOCTL, "dev %d has refcount (%d)!\n",
377 dev, obddev->obd_type->typ_refcnt);
382 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
383 (!(obddev->obd_flags & OBD_ATTACHED))) {
384 CDEBUG(D_IOCTL, "device not attached or set up\n");
389 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
392 /* cleanup has no argument */
393 err = OBP(obddev, cleanup)(obddev);
400 obddev->obd_flags &= ~OBD_SET_UP;
401 obddev->obd_type->typ_refcnt--;
402 CDEBUG(D_IOCTL, "Dev %d refcount now %d\n", dev,
403 obddev->obd_type->typ_refcnt);
407 case OBD_IOC_CONNECT:
409 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
410 (!(obddev->obd_flags & OBD_ATTACHED))) {
411 CDEBUG(D_IOCTL, "Device not attached or set up\n");
415 if ( !OBT(obddev) || !OBP(obddev, connect) )
418 err = OBP(obddev, connect)(&conn);
422 return copy_to_user((int *)arg, &conn.oc_id,
425 case OBD_IOC_DISCONNECT:
426 /* frees data structures */
427 /* has this minor been registered? */
428 if (!obddev->obd_type)
431 get_user(cli_id, (int *) arg);
434 if ( !OBT(obddev) || !OBP(obddev, disconnect))
437 OBP(obddev, disconnect)(&conn);
441 struct oic_range_s *range = tmp_buf;
443 if (!obddev->obd_type)
446 err = copy_from_user(range, (const void *)arg, sizeof(*range));
453 if ( !OBT(obddev) || !OBP(obddev, sync) ) {
459 /* XXX sync needs to be tested/verified */
460 err = OBP(obddev, sync)(&conn, &range->obdo, range->count,
468 return put_user(err, (int *) arg);
470 case OBD_IOC_CREATE: {
471 struct oic_attr_s *attr = tmp_buf;
473 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
479 /* has this minor been registered? */
480 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
481 !(obddev->obd_flags & OBD_SET_UP)) {
482 CDEBUG(D_IOCTL, "Device not attached or set up\n");
485 conn.oc_id = attr->conn_id;
487 if ( !OBT(obddev) || !OBP(obddev, create) )
490 err = OBP(obddev, create)(&conn, &attr->obdo);
496 err = copy_to_user((int *)arg, attr, sizeof(*attr));
501 case OBD_IOC_DESTROY: {
502 struct oic_attr_s *attr = tmp_buf;
504 /* has this minor been registered? */
505 if (!obddev->obd_type)
508 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
514 if ( !OBT(obddev) || !OBP(obddev, destroy) )
517 conn.oc_id = attr->conn_id;
518 err = OBP(obddev, destroy)(&conn, &attr->obdo);
523 case OBD_IOC_SETATTR: {
524 struct oic_attr_s *attr = tmp_buf;
526 /* has this minor been registered? */
527 if (!obddev->obd_type)
530 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
534 if ( !OBT(obddev) || !OBP(obddev, setattr) )
537 conn.oc_id = attr->conn_id;
538 err = OBP(obddev, setattr)(&conn, &attr->obdo);
543 case OBD_IOC_GETATTR: {
544 struct oic_attr_s *attr = tmp_buf;
546 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
550 conn.oc_id = attr->conn_id;
552 err = OBP(obddev, getattr)(&conn, &attr->obdo);
558 err = copy_to_user((int *)arg, attr, sizeof(*attr));
565 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
567 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
573 conn.oc_id = rw_s->conn_id;
575 if ( !OBT(obddev) || !OBP(obddev, read) ) {
582 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
583 &rw_s->count, rw_s->offset);
586 CDEBUG(D_INODE, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
587 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
593 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
598 case OBD_IOC_WRITE: {
599 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
601 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
607 conn.oc_id = rw_s->conn_id;
609 if ( !OBT(obddev) || !OBP(obddev, write) ) {
614 CDEBUG(D_INODE, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
615 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
616 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
617 &rw_s->count, rw_s->offset);
624 err = copy_to_user((int *)arg, &rw_s->count,
625 sizeof(rw_s->count));
629 case OBD_IOC_PREALLOCATE: {
630 struct oic_prealloc_s *prealloc = tmp_buf;
632 /* has this minor been registered? */
633 if (!obddev->obd_type)
636 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
640 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
641 !(obddev->obd_flags & OBD_SET_UP)) {
642 CDEBUG(D_IOCTL, "Device not attached or set up\n");
646 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
649 conn.oc_id = prealloc->conn_id;
650 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
657 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
661 case OBD_IOC_STATFS: {
663 unsigned int conn_id;
666 /* has this minor been registered? */
667 if (!obddev->obd_type)
670 tmp = (void *)arg + sizeof(unsigned int);
671 get_user(conn_id, (int *) arg);
673 if ( !OBT(obddev) || !OBP(obddev, statfs) )
676 conn.oc_id = conn_id;
677 err = OBP(obddev, statfs)(&conn, &buf);
682 err = copy_to_user(tmp, &buf, sizeof(buf));
688 struct ioc_mv_s *mvdata = tmp_buf;
690 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
691 (!(obddev->obd_flags & OBD_ATTACHED))) {
692 CDEBUG(D_IOCTL, "Device not attached or set up\n");
696 /* get main structure */
697 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
703 if ( !OBT(obddev) || !OBP(obddev, copy) )
706 /* do the partition */
707 CDEBUG(D_IOCTL, "Copy %d, type %s dst %Ld src %Ld\n", dev,
708 obddev->obd_type->typ_name, mvdata->dst.o_id,
711 conn.oc_id = mvdata->src_conn_id;
713 err = OBP(obddev, copy)(&conn, &mvdata->dst,
715 mvdata->src.o_size, 0);
720 struct ioc_mv_s *mvdata = tmp_buf;
722 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
723 (!(obddev->obd_flags & OBD_ATTACHED))) {
724 CDEBUG(D_IOCTL, "Device not attached or set up\n");
728 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
734 CDEBUG(D_IOCTL, "Migrate copying %d bytes\n", sizeof(*mvdata));
736 if ( !OBT(obddev) || !OBP(obddev, migrate) )
739 /* do the partition */
740 CDEBUG(D_IOCTL, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
741 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
742 mvdata->src.o_id, mvdata->dst.o_id);
744 conn.oc_id = mvdata->src_conn_id;
745 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
746 mvdata->src.o_size, 0);
752 struct obd_type *type;
753 struct oic_generic input;
757 /* get data structures */
758 err = copy_from_user(&input, (void *)arg, sizeof(input));
764 err = getdata(input.att_typelen + 1, &input.att_type);
772 type = obd_nm_to_type(nm);
775 if ( !request_module(nm) ) {
776 CDEBUG(D_IOCTL, "Loaded module '%s'\n", nm);
777 type = obd_nm_to_type(nm);
779 CDEBUG(D_IOCTL, "Can't load module '%s'\n", nm);
783 OBD_FREE(input.att_type, input.att_typelen + 1);
785 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
790 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
794 conn.oc_id = input.att_connid;
796 CDEBUG(D_IOCTL, "Calling ioctl %x for type %s, len %d\n",
797 cmd, type->typ_name, input.att_datalen);
799 /* get the generic data */
800 karg = input.att_data;
801 err = getdata(input.att_datalen, &karg);
807 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
808 karg, input.att_data);
809 OBD_FREE(karg, input.att_datalen);
815 } /* obd_class_ioctl */
817 /* Driver interface done, utility functions follow */
819 int obd_register_type(struct obd_ops *ops, char *nm)
821 struct obd_type *type;
824 if (obd_init_magic != 0x11223344) {
829 if ( obd_nm_to_type(nm) ) {
830 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
835 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
840 memset(type, 0, sizeof(*type));
841 INIT_LIST_HEAD(&type->typ_chain);
843 list_add(&type->typ_chain, obd_types.next);
850 int obd_unregister_type(char *nm)
852 struct obd_type *type = obd_nm_to_type(nm);
856 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
861 if ( type->typ_refcnt ) {
863 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
864 "(%d)\n", nm, type->typ_refcnt);
869 list_del(&type->typ_chain);
870 OBD_FREE(type, sizeof(*type));
873 } /* obd_unregister_type */
875 /* declare character device */
876 static struct file_operations obd_psdev_fops = {
880 NULL, /* presto_psdev_readdir */
882 obd_class_ioctl, /* ioctl */
883 NULL, /* presto_psdev_mmap */
884 obd_class_open, /* open */
886 obd_class_release, /* release */
900 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
902 INIT_LIST_HEAD(&obd_types);
904 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
906 printk(KERN_ERR __FUNCTION__ ": unable to get major %d\n",
911 for (i = 0; i < MAX_OBD_DEVICES; i++) {
912 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
913 obd_dev[i].obd_minor = i;
914 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
917 err = obd_init_obdo_cache();
921 obd_init_magic = 0x11223344;
925 EXPORT_SYMBOL(obd_register_type);
926 EXPORT_SYMBOL(obd_unregister_type);
928 EXPORT_SYMBOL(obd_print_entry);
929 EXPORT_SYMBOL(obd_debug_level);
930 EXPORT_SYMBOL(obd_dev);
932 EXPORT_SYMBOL(gen_connect);
933 EXPORT_SYMBOL(gen_client);
934 EXPORT_SYMBOL(gen_cleanup);
935 EXPORT_SYMBOL(gen_disconnect);
936 EXPORT_SYMBOL(gen_copy_data);
937 EXPORT_SYMBOL(obdo_cachep);
939 /* EXPORT_SYMBOL(gen_multi_attach); */
940 EXPORT_SYMBOL(gen_multi_setup);
941 EXPORT_SYMBOL(gen_multi_cleanup);
945 int init_module(void)
950 void cleanup_module(void)
955 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
956 for (i = 0; i < MAX_OBD_DEVICES; i++) {
957 struct obd_device *obddev = &obd_dev[i];
958 if ( obddev->obd_type &&
959 (obddev->obd_flags & OBD_SET_UP) &&
960 OBT(obddev) && OBP(obddev, detach) ) {
961 /* XXX should this call generic detach otherwise? */
962 OBP(obddev, detach)(obddev);
966 obd_cleanup_obdo_cache();