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 = 0;
62 int obd_debug_level = D_IOCTL|D_INODE|D_SUPER|D_WARNING;
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_INFO, "SEARCH %s\n", nm);
119 while ( (tmp = tmp->next) != &obd_types ) {
120 type = list_entry(tmp, struct obd_type, typ_chain);
121 CDEBUG(D_INFO, "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_INFO, "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 */
167 CDEBUG(D_IOCTL, "invalid inode\n");
171 dev = MINOR(inode->i_rdev);
172 if (dev > MAX_OBD_DEVICES)
174 obddev = &obd_dev[dev];
175 conn.oc_dev = obddev;
180 case OBD_IOC_ATTACH: {
181 struct obd_type *type;
182 struct oic_generic *input = tmp_buf;
186 /* have we attached a type to this device */
187 if ( obddev->obd_type ||
188 (obddev->obd_flags & OBD_ATTACHED) ){
190 "OBD Device %d already attached to type %s.\n",
191 dev, obddev->obd_type->typ_name);
196 /* get data structures */
197 err = copy_from_user(input, (void *)arg, sizeof(*input));
203 err = getdata(input->att_typelen + 1, &input->att_type);
210 nm = input->att_type;
211 type = obd_nm_to_type(nm);
214 if ( !request_module(nm) ) {
215 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
216 type = obd_nm_to_type(nm);
218 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
223 OBD_FREE(input->att_type, input->att_typelen + 1);
225 printk(__FUNCTION__ ": unknown obd type dev %d\n",
230 obddev->obd_type = type;
232 /* get the attach data */
233 err = getdata(input->att_datalen, &input->att_data);
239 INIT_LIST_HEAD(&obddev->obd_gen_clients);
240 obddev->obd_multi_count = 0;
242 CDEBUG(D_INFO, "Attach %d, datalen %d, type %s\n",
243 dev, input->att_datalen, obddev->obd_type->typ_name);
244 /* maybe we are done */
245 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
246 obddev->obd_flags |= OBD_ATTACHED;
248 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
256 err = OBP(obddev, attach)(obddev, input->att_datalen,
258 OBD_FREE(input->att_data, input->att_datalen);
261 obddev->obd_flags &= ~OBD_ATTACHED;
262 obddev->obd_type = NULL;
265 obddev->obd_flags |= OBD_ATTACHED;
267 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
275 case OBD_IOC_DETACH: {
278 if (obddev->obd_flags & OBD_SET_UP) {
282 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
283 CDEBUG(D_IOCTL, "Device not attached\n");
287 if ( !list_empty(&obddev->obd_gen_clients) ) {
288 CDEBUG(D_IOCTL, "Device has connected clients\n");
293 CDEBUG(D_PSDEV, "Detach %d, type %s\n", dev,
294 obddev->obd_type->typ_name);
295 obddev->obd_flags &= ~OBD_ATTACHED;
296 obddev->obd_type->typ_refcnt--;
297 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
298 obddev->obd_type->typ_refcnt);
299 obddev->obd_type = NULL;
305 case OBD_IOC_SETUP: {
313 /* have we attached a type to this device */
314 if (!(obddev->obd_flags & OBD_ATTACHED)) {
315 CDEBUG(D_IOCTL, "Device not attached\n");
320 /* has this been done already? */
321 if ( obddev->obd_flags & OBD_SET_UP ) {
322 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
323 dev, obddev->obd_type->typ_name);
328 /* get main structure */
329 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
335 err = getdata(setup->setup_datalen, &setup->setup_data);
342 CDEBUG(D_PSDEV, "Setup %d, type %s\n", dev,
343 obddev->obd_type->typ_name);
344 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
345 obddev->obd_type->typ_refcnt++;
346 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
347 dev, obddev->obd_type->typ_refcnt);
348 obddev->obd_flags |= OBD_SET_UP;
353 err = OBP(obddev, setup)(obddev, setup->setup_datalen,
357 obddev->obd_flags &= ~OBD_SET_UP;
360 obddev->obd_type->typ_refcnt++;
361 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
362 dev, obddev->obd_type->typ_refcnt);
363 obddev->obd_flags |= OBD_SET_UP;
368 case OBD_IOC_CLEANUP: {
370 /* has this minor been registered? */
371 if (!obddev->obd_type) {
372 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
377 if ( !obddev->obd_type->typ_refcnt ) {
378 CDEBUG(D_IOCTL, "dev %d has refcount (%d)!\n",
379 dev, obddev->obd_type->typ_refcnt);
384 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
385 (!(obddev->obd_flags & OBD_ATTACHED))) {
386 CDEBUG(D_IOCTL, "device not attached or set up\n");
391 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
394 /* cleanup has no argument */
395 err = OBP(obddev, cleanup)(obddev);
402 obddev->obd_flags &= ~OBD_SET_UP;
403 obddev->obd_type->typ_refcnt--;
404 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
405 obddev->obd_type->typ_refcnt);
409 case OBD_IOC_CONNECT:
411 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
412 (!(obddev->obd_flags & OBD_ATTACHED))) {
413 CDEBUG(D_IOCTL, "Device not attached or set up\n");
417 if ( !OBT(obddev) || !OBP(obddev, connect) )
420 err = OBP(obddev, connect)(&conn);
424 return copy_to_user((int *)arg, &conn.oc_id,
427 case OBD_IOC_DISCONNECT:
428 /* frees data structures */
429 /* has this minor been registered? */
430 if (!obddev->obd_type)
433 get_user(cli_id, (int *) arg);
436 if ( !OBT(obddev) || !OBP(obddev, disconnect))
439 OBP(obddev, disconnect)(&conn);
443 struct oic_range_s *range = tmp_buf;
445 if (!obddev->obd_type)
448 err = copy_from_user(range, (const void *)arg, sizeof(*range));
455 if ( !OBT(obddev) || !OBP(obddev, sync) ) {
461 /* XXX sync needs to be tested/verified */
462 err = OBP(obddev, sync)(&conn, &range->obdo, range->count,
470 return put_user(err, (int *) arg);
472 case OBD_IOC_CREATE: {
473 struct oic_attr_s *attr = tmp_buf;
475 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
481 /* has this minor been registered? */
482 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
483 !(obddev->obd_flags & OBD_SET_UP)) {
484 CDEBUG(D_IOCTL, "Device not attached or set up\n");
487 conn.oc_id = attr->conn_id;
489 if ( !OBT(obddev) || !OBP(obddev, create) )
492 err = OBP(obddev, create)(&conn, &attr->obdo);
498 err = copy_to_user((int *)arg, attr, sizeof(*attr));
503 case OBD_IOC_DESTROY: {
504 struct oic_attr_s *attr = tmp_buf;
506 /* has this minor been registered? */
507 if (!obddev->obd_type)
510 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
516 if ( !OBT(obddev) || !OBP(obddev, destroy) )
519 conn.oc_id = attr->conn_id;
520 err = OBP(obddev, destroy)(&conn, &attr->obdo);
525 case OBD_IOC_SETATTR: {
526 struct oic_attr_s *attr = tmp_buf;
528 /* has this minor been registered? */
529 if (!obddev->obd_type)
532 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
536 if ( !OBT(obddev) || !OBP(obddev, setattr) )
539 conn.oc_id = attr->conn_id;
540 err = OBP(obddev, setattr)(&conn, &attr->obdo);
545 case OBD_IOC_GETATTR: {
546 struct oic_attr_s *attr = tmp_buf;
548 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
552 conn.oc_id = attr->conn_id;
554 err = OBP(obddev, getattr)(&conn, &attr->obdo);
560 err = copy_to_user((int *)arg, attr, sizeof(*attr));
567 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
569 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
575 conn.oc_id = rw_s->conn_id;
577 if ( !OBT(obddev) || !OBP(obddev, read) ) {
584 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
585 &rw_s->count, rw_s->offset);
588 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
589 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
595 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
600 case OBD_IOC_WRITE: {
601 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
603 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
609 conn.oc_id = rw_s->conn_id;
611 if ( !OBT(obddev) || !OBP(obddev, write) ) {
616 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
617 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
618 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
619 &rw_s->count, rw_s->offset);
626 err = copy_to_user((int *)arg, &rw_s->count,
627 sizeof(rw_s->count));
631 case OBD_IOC_PREALLOCATE: {
632 struct oic_prealloc_s *prealloc = tmp_buf;
634 /* has this minor been registered? */
635 if (!obddev->obd_type)
638 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
642 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
643 !(obddev->obd_flags & OBD_SET_UP)) {
644 CDEBUG(D_IOCTL, "Device not attached or set up\n");
648 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
651 conn.oc_id = prealloc->conn_id;
652 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
659 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
663 case OBD_IOC_STATFS: {
665 unsigned int conn_id;
668 /* has this minor been registered? */
669 if (!obddev->obd_type)
672 tmp = (void *)arg + sizeof(unsigned int);
673 get_user(conn_id, (int *) arg);
675 if ( !OBT(obddev) || !OBP(obddev, statfs) )
678 conn.oc_id = conn_id;
679 err = OBP(obddev, statfs)(&conn, &buf);
684 err = copy_to_user(tmp, &buf, sizeof(buf));
690 struct ioc_mv_s *mvdata = tmp_buf;
692 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
693 (!(obddev->obd_flags & OBD_ATTACHED))) {
694 CDEBUG(D_IOCTL, "Device not attached or set up\n");
698 /* get main structure */
699 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
705 if ( !OBT(obddev) || !OBP(obddev, copy) )
708 /* do the partition */
709 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
710 obddev->obd_type->typ_name, mvdata->dst.o_id,
713 conn.oc_id = mvdata->src_conn_id;
715 err = OBP(obddev, copy)(&conn, &mvdata->dst,
717 mvdata->src.o_size, 0);
722 struct ioc_mv_s *mvdata = tmp_buf;
724 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
725 (!(obddev->obd_flags & OBD_ATTACHED))) {
726 CDEBUG(D_IOCTL, "Device not attached or set up\n");
730 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
736 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
738 if ( !OBT(obddev) || !OBP(obddev, migrate) )
741 /* do the partition */
742 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
743 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
744 mvdata->src.o_id, mvdata->dst.o_id);
746 conn.oc_id = mvdata->src_conn_id;
747 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
748 mvdata->src.o_size, 0);
754 struct obd_type *type;
755 struct oic_generic input;
759 /* get data structures */
760 err = copy_from_user(&input, (void *)arg, sizeof(input));
766 err = getdata(input.att_typelen + 1, &input.att_type);
774 type = obd_nm_to_type(nm);
777 if ( !request_module(nm) ) {
778 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
779 type = obd_nm_to_type(nm);
781 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
785 OBD_FREE(input.att_type, input.att_typelen + 1);
787 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
792 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
796 conn.oc_id = input.att_connid;
798 CDEBUG(D_INFO, "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 err = getdata(input.att_datalen, &karg);
809 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
810 karg, input.att_data);
811 OBD_FREE(karg, input.att_datalen);
817 } /* obd_class_ioctl */
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) {
827 printk(__FUNCTION__ ": bad magic for type\n");
832 if ( obd_nm_to_type(nm) ) {
833 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
838 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
843 memset(type, 0, sizeof(*type));
844 INIT_LIST_HEAD(&type->typ_chain);
846 list_add(&type->typ_chain, obd_types.next);
853 int obd_unregister_type(char *nm)
855 struct obd_type *type = obd_nm_to_type(nm);
859 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
864 if ( type->typ_refcnt ) {
866 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
867 "(%d)\n", nm, type->typ_refcnt);
872 list_del(&type->typ_chain);
873 OBD_FREE(type, sizeof(*type));
876 } /* obd_unregister_type */
878 /* declare character device */
879 static struct file_operations obd_psdev_fops = {
883 NULL, /* presto_psdev_readdir */
885 obd_class_ioctl, /* ioctl */
886 NULL, /* presto_psdev_mmap */
887 obd_class_open, /* open */
889 obd_class_release, /* release */
903 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
905 INIT_LIST_HEAD(&obd_types);
907 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
909 printk(KERN_ERR __FUNCTION__ ": unable to get major %d\n",
914 for (i = 0; i < MAX_OBD_DEVICES; i++) {
915 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
916 obd_dev[i].obd_minor = i;
917 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
920 err = obd_init_obdo_cache();
924 obd_init_magic = 0x11223344;
928 EXPORT_SYMBOL(obd_register_type);
929 EXPORT_SYMBOL(obd_unregister_type);
931 EXPORT_SYMBOL(obd_print_entry);
932 EXPORT_SYMBOL(obd_debug_level);
933 EXPORT_SYMBOL(obd_dev);
935 EXPORT_SYMBOL(gen_connect);
936 EXPORT_SYMBOL(gen_client);
937 EXPORT_SYMBOL(gen_cleanup);
938 EXPORT_SYMBOL(gen_disconnect);
939 EXPORT_SYMBOL(gen_copy_data);
940 EXPORT_SYMBOL(obdo_cachep);
942 /* EXPORT_SYMBOL(gen_multi_attach); */
943 EXPORT_SYMBOL(gen_multi_setup);
944 EXPORT_SYMBOL(gen_multi_cleanup);
948 int init_module(void)
953 void cleanup_module(void)
958 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
959 for (i = 0; i < MAX_OBD_DEVICES; i++) {
960 struct obd_device *obddev = &obd_dev[i];
961 if ( obddev->obd_type &&
962 (obddev->obd_flags & OBD_SET_UP) &&
963 OBT(obddev) && OBP(obddev, detach) ) {
964 /* XXX should this call generic detach otherwise? */
965 OBP(obddev, detach)(obddev);
969 obd_cleanup_obdo_cache();