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|D_MALLOC|D_CACHE;
64 struct obd_device obd_dev[MAX_OBD_DEVICES];
65 struct list_head obd_types;
67 /* called when opening /dev/obdNNN */
68 static int obd_class_open(struct inode * inode, struct file * file)
75 dev = MINOR(inode->i_rdev);
76 if (dev >= MAX_OBD_DEVICES)
78 obd_dev[dev].obd_refcnt++;
79 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
80 obd_dev[dev].obd_refcnt);
87 /* called when closing /dev/obdNNN */
88 static int obd_class_release(struct inode * inode, struct file * file)
95 dev = MINOR(inode->i_rdev);
96 if (dev >= MAX_OBD_DEVICES)
98 fsync_dev(inode->i_rdev);
99 if (obd_dev[dev].obd_refcnt <= 0)
100 printk(KERN_ALERT __FUNCTION__ ": refcount(%d) <= 0\n",
101 obd_dev[dev].obd_refcnt);
102 obd_dev[dev].obd_refcnt--;
104 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
105 obd_dev[dev].obd_refcnt);
112 /* support function */
113 static struct obd_type *obd_nm_to_type(char *nm)
115 struct list_head *tmp;
116 struct obd_type *type;
117 CDEBUG(D_INFO, "SEARCH %s\n", nm);
120 while ( (tmp = tmp->next) != &obd_types ) {
121 type = list_entry(tmp, struct obd_type, typ_chain);
122 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
123 if (strlen(type->typ_name) == strlen(nm) &&
124 strcmp(type->typ_name, nm) == 0 ) {
132 static int getdata(int len, void **data)
141 CDEBUG(D_MALLOC, "len %d, add %p\n", len, *data);
143 OBD_ALLOC(tmp, void *, len);
148 if ( copy_from_user(tmp, *data, len)) {
157 /* to control /dev/obdNNN */
158 static int obd_class_ioctl (struct inode * inode, struct file * filp,
159 unsigned int cmd, unsigned long arg)
161 struct obd_device *obddev;
162 /* NOTE this must be larger than any of the ioctl data structs */
165 struct obd_conn conn;
167 long int cli_id; /* connect, disconnect */
170 CDEBUG(D_IOCTL, "invalid inode\n");
174 dev = MINOR(inode->i_rdev);
175 if (dev > MAX_OBD_DEVICES)
177 obddev = &obd_dev[dev];
178 conn.oc_dev = obddev;
183 case OBD_IOC_ATTACH: {
184 struct obd_type *type;
185 struct oic_generic *input = tmp_buf;
189 /* have we attached a type to this device */
190 if ( obddev->obd_type ||
191 (obddev->obd_flags & OBD_ATTACHED) ){
193 "OBD Device %d already attached to type %s.\n",
194 dev, obddev->obd_type->typ_name);
199 /* get data structures */
200 err = copy_from_user(input, (void *)arg, sizeof(*input));
206 err = getdata(input->att_typelen + 1, &input->att_type);
213 nm = input->att_type;
214 type = obd_nm_to_type(nm);
217 if ( !request_module(nm) ) {
218 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
219 type = obd_nm_to_type(nm);
221 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
226 OBD_FREE(input->att_type, input->att_typelen + 1);
228 printk(__FUNCTION__ ": unknown obd type dev %d\n",
233 obddev->obd_type = type;
235 /* get the attach data */
236 err = getdata(input->att_datalen, &input->att_data);
242 INIT_LIST_HEAD(&obddev->obd_gen_clients);
243 obddev->obd_multi_count = 0;
245 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
246 dev, input->att_datalen, obddev->obd_type->typ_name);
247 /* maybe we are done */
248 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
249 obddev->obd_flags |= OBD_ATTACHED;
251 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
254 OBD_FREE(input->att_data, input->att_datalen);
261 err = OBP(obddev, attach)(obddev, input->att_datalen,
264 OBD_FREE(input->att_data, input->att_datalen);
267 obddev->obd_flags &= ~OBD_ATTACHED;
268 obddev->obd_type = NULL;
271 obddev->obd_flags |= OBD_ATTACHED;
273 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
281 case OBD_IOC_DETACH: {
284 if (obddev->obd_flags & OBD_SET_UP) {
288 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
289 CDEBUG(D_IOCTL, "Device not attached\n");
293 if ( !list_empty(&obddev->obd_gen_clients) ) {
294 CDEBUG(D_IOCTL, "Device has connected clients\n");
299 CDEBUG(D_PSDEV, "Detach %d, type %s\n", dev,
300 obddev->obd_type->typ_name);
301 obddev->obd_flags &= ~OBD_ATTACHED;
302 obddev->obd_type->typ_refcnt--;
303 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
304 obddev->obd_type->typ_refcnt);
305 obddev->obd_type = NULL;
311 case OBD_IOC_SETUP: {
319 /* have we attached a type to this device */
320 if (!(obddev->obd_flags & OBD_ATTACHED)) {
321 CDEBUG(D_IOCTL, "Device not attached\n");
326 /* has this been done already? */
327 if ( obddev->obd_flags & OBD_SET_UP ) {
328 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
329 dev, obddev->obd_type->typ_name);
334 /* get main structure */
335 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
341 err = getdata(setup->setup_datalen, &setup->setup_data);
348 CDEBUG(D_PSDEV, "Setup %d, type %s\n", dev,
349 obddev->obd_type->typ_name);
350 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
351 obddev->obd_type->typ_refcnt++;
352 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
353 dev, obddev->obd_type->typ_refcnt);
354 if (setup->setup_data)
355 OBD_FREE(setup->setup_data,
356 setup->setup_datalen);
357 obddev->obd_flags |= OBD_SET_UP;
362 err = OBP(obddev, setup)(obddev, setup->setup_datalen,
366 obddev->obd_flags &= ~OBD_SET_UP;
369 obddev->obd_type->typ_refcnt++;
370 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
371 dev, obddev->obd_type->typ_refcnt);
372 obddev->obd_flags |= OBD_SET_UP;
375 if (setup->setup_data)
376 OBD_FREE(setup->setup_data, setup->setup_datalen);
379 case OBD_IOC_CLEANUP: {
381 /* has this minor been registered? */
382 if (!obddev->obd_type) {
383 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
388 if ( !obddev->obd_type->typ_refcnt ) {
389 CDEBUG(D_IOCTL, "dev %d has refcount (%d)!\n",
390 dev, obddev->obd_type->typ_refcnt);
395 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
396 (!(obddev->obd_flags & OBD_ATTACHED))) {
397 CDEBUG(D_IOCTL, "device not attached or set up\n");
402 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
405 /* cleanup has no argument */
406 err = OBP(obddev, cleanup)(obddev);
413 obddev->obd_flags &= ~OBD_SET_UP;
414 obddev->obd_type->typ_refcnt--;
415 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
416 obddev->obd_type->typ_refcnt);
420 case OBD_IOC_CONNECT:
422 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
423 (!(obddev->obd_flags & OBD_ATTACHED))) {
424 CDEBUG(D_IOCTL, "Device not attached or set up\n");
428 if ( !OBT(obddev) || !OBP(obddev, connect) )
431 err = OBP(obddev, connect)(&conn);
435 return copy_to_user((int *)arg, &conn.oc_id,
438 case OBD_IOC_DISCONNECT:
439 /* frees data structures */
440 /* has this minor been registered? */
441 if (!obddev->obd_type)
444 get_user(cli_id, (int *) arg);
447 if ( !OBT(obddev) || !OBP(obddev, disconnect))
450 OBP(obddev, disconnect)(&conn);
454 struct oic_range_s *range = tmp_buf;
456 if (!obddev->obd_type)
459 err = copy_from_user(range, (const void *)arg, sizeof(*range));
466 if ( !OBT(obddev) || !OBP(obddev, sync) ) {
472 /* XXX sync needs to be tested/verified */
473 err = OBP(obddev, sync)(&conn, &range->obdo, range->count,
481 return put_user(err, (int *) arg);
483 case OBD_IOC_CREATE: {
484 struct oic_attr_s *attr = tmp_buf;
486 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
492 /* has this minor been registered? */
493 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
494 !(obddev->obd_flags & OBD_SET_UP)) {
495 CDEBUG(D_IOCTL, "Device not attached or set up\n");
498 conn.oc_id = attr->conn_id;
500 if ( !OBT(obddev) || !OBP(obddev, create) )
503 err = OBP(obddev, create)(&conn, &attr->obdo);
509 err = copy_to_user((int *)arg, attr, sizeof(*attr));
514 case OBD_IOC_DESTROY: {
515 struct oic_attr_s *attr = tmp_buf;
517 /* has this minor been registered? */
518 if (!obddev->obd_type)
521 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
527 if ( !OBT(obddev) || !OBP(obddev, destroy) )
530 conn.oc_id = attr->conn_id;
531 err = OBP(obddev, destroy)(&conn, &attr->obdo);
536 case OBD_IOC_SETATTR: {
537 struct oic_attr_s *attr = tmp_buf;
539 /* has this minor been registered? */
540 if (!obddev->obd_type)
543 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
547 if ( !OBT(obddev) || !OBP(obddev, setattr) )
550 conn.oc_id = attr->conn_id;
551 err = OBP(obddev, setattr)(&conn, &attr->obdo);
556 case OBD_IOC_GETATTR: {
557 struct oic_attr_s *attr = tmp_buf;
559 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
563 conn.oc_id = attr->conn_id;
565 err = OBP(obddev, getattr)(&conn, &attr->obdo);
571 err = copy_to_user((int *)arg, attr, sizeof(*attr));
578 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
580 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
586 conn.oc_id = rw_s->conn_id;
588 if ( !OBT(obddev) || !OBP(obddev, read) ) {
595 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
596 &rw_s->count, rw_s->offset);
599 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
600 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
606 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
611 case OBD_IOC_WRITE: {
612 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
614 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
620 conn.oc_id = rw_s->conn_id;
622 if ( !OBT(obddev) || !OBP(obddev, write) ) {
627 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
628 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
629 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
630 &rw_s->count, rw_s->offset);
637 err = copy_to_user((int *)arg, &rw_s->count,
638 sizeof(rw_s->count));
642 case OBD_IOC_PREALLOCATE: {
643 struct oic_prealloc_s *prealloc = tmp_buf;
645 /* has this minor been registered? */
646 if (!obddev->obd_type)
649 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
653 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
654 !(obddev->obd_flags & OBD_SET_UP)) {
655 CDEBUG(D_IOCTL, "Device not attached or set up\n");
659 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
662 conn.oc_id = prealloc->conn_id;
663 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
670 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
674 case OBD_IOC_STATFS: {
676 unsigned int conn_id;
679 /* has this minor been registered? */
680 if (!obddev->obd_type)
683 tmp = (void *)arg + sizeof(unsigned int);
684 get_user(conn_id, (int *) arg);
686 if ( !OBT(obddev) || !OBP(obddev, statfs) )
689 conn.oc_id = conn_id;
690 err = OBP(obddev, statfs)(&conn, &buf);
695 err = copy_to_user(tmp, &buf, sizeof(buf));
701 struct ioc_mv_s *mvdata = tmp_buf;
703 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
704 (!(obddev->obd_flags & OBD_ATTACHED))) {
705 CDEBUG(D_IOCTL, "Device not attached or set up\n");
709 /* get main structure */
710 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
716 if ( !OBT(obddev) || !OBP(obddev, copy) )
719 /* do the partition */
720 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
721 obddev->obd_type->typ_name, mvdata->dst.o_id,
724 conn.oc_id = mvdata->src_conn_id;
726 err = OBP(obddev, copy)(&conn, &mvdata->dst,
728 mvdata->src.o_size, 0);
733 struct ioc_mv_s *mvdata = tmp_buf;
735 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
736 (!(obddev->obd_flags & OBD_ATTACHED))) {
737 CDEBUG(D_IOCTL, "Device not attached or set up\n");
741 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
747 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
749 if ( !OBT(obddev) || !OBP(obddev, migrate) )
752 /* do the partition */
753 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
754 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
755 mvdata->src.o_id, mvdata->dst.o_id);
757 conn.oc_id = mvdata->src_conn_id;
758 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
759 mvdata->src.o_size, 0);
765 struct obd_type *type;
766 struct oic_generic input;
770 /* get data structures */
771 err = copy_from_user(&input, (void *)arg, sizeof(input));
777 err = getdata(input.att_typelen + 1, &input.att_type);
785 type = obd_nm_to_type(nm);
788 if ( !request_module(nm) ) {
789 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
790 type = obd_nm_to_type(nm);
792 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
796 OBD_FREE(input.att_type, input.att_typelen + 1);
798 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
803 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
807 conn.oc_id = input.att_connid;
809 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
810 cmd, type->typ_name, input.att_datalen);
812 /* get the generic data */
813 karg = input.att_data;
814 err = getdata(input.att_datalen, &karg);
820 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
821 karg, input.att_data);
822 OBD_FREE(karg, input.att_datalen);
828 } /* obd_class_ioctl */
830 /* Driver interface done, utility functions follow */
832 int obd_register_type(struct obd_ops *ops, char *nm)
834 struct obd_type *type;
837 if (obd_init_magic != 0x11223344) {
838 printk(__FUNCTION__ ": bad magic for type\n");
843 if ( obd_nm_to_type(nm) ) {
844 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
849 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
854 memset(type, 0, sizeof(*type));
855 INIT_LIST_HEAD(&type->typ_chain);
857 list_add(&type->typ_chain, obd_types.next);
864 int obd_unregister_type(char *nm)
866 struct obd_type *type = obd_nm_to_type(nm);
870 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
875 if ( type->typ_refcnt ) {
877 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
878 "(%d)\n", nm, type->typ_refcnt);
883 list_del(&type->typ_chain);
884 OBD_FREE(type, sizeof(*type));
887 } /* obd_unregister_type */
889 /* declare character device */
890 static struct file_operations obd_psdev_fops = {
894 NULL, /* presto_psdev_readdir */
896 obd_class_ioctl, /* ioctl */
897 NULL, /* presto_psdev_mmap */
898 obd_class_open, /* open */
900 obd_class_release, /* release */
914 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
916 INIT_LIST_HEAD(&obd_types);
918 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
920 printk(KERN_ERR __FUNCTION__ ": unable to get major %d\n",
925 for (i = 0; i < MAX_OBD_DEVICES; i++) {
926 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
927 obd_dev[i].obd_minor = i;
928 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
931 err = obd_init_obdo_cache();
935 obd_init_magic = 0x11223344;
939 EXPORT_SYMBOL(obd_register_type);
940 EXPORT_SYMBOL(obd_unregister_type);
942 EXPORT_SYMBOL(obd_print_entry);
943 EXPORT_SYMBOL(obd_debug_level);
944 EXPORT_SYMBOL(obd_dev);
946 EXPORT_SYMBOL(gen_connect);
947 EXPORT_SYMBOL(gen_client);
948 EXPORT_SYMBOL(gen_cleanup);
949 EXPORT_SYMBOL(gen_disconnect);
950 EXPORT_SYMBOL(gen_copy_data);
951 EXPORT_SYMBOL(obdo_cachep);
953 /* EXPORT_SYMBOL(gen_multi_attach); */
954 EXPORT_SYMBOL(gen_multi_setup);
955 EXPORT_SYMBOL(gen_multi_cleanup);
959 int init_module(void)
964 void cleanup_module(void)
969 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
970 for (i = 0; i < MAX_OBD_DEVICES; i++) {
971 struct obd_device *obddev = &obd_dev[i];
972 if ( obddev->obd_type &&
973 (obddev->obd_flags & OBD_SET_UP) &&
974 OBT(obddev) && OBP(obddev, detach) ) {
975 /* XXX should this call generic detach otherwise? */
976 OBP(obddev, detach)(obddev);
980 obd_cleanup_obdo_cache();
982 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);