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.
26 * Copyright (C) 2001 Cluster File Systems, Inc.
33 #include <linux/config.h> /* for CONFIG_PROC_FS */
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/kmod.h> /* for request_module() */
39 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <linux/delay.h>
45 #include <linux/skbuff.h>
46 #include <linux/proc_fs.h>
47 #include <linux/vmalloc.h>
49 #include <linux/poll.h>
50 #include <linux/init.h>
51 #include <linux/list.h>
53 #include <asm/segment.h>
54 #include <asm/system.h>
56 #include <asm/uaccess.h>
58 #include <linux/obd_support.h>
59 #include <linux/obd_class.h>
61 static int obd_init_magic;
62 int obd_print_entry = 1;
63 int obd_debug_level = ~0;
65 struct obd_device obd_dev[MAX_OBD_DEVICES];
66 struct list_head obd_types;
68 /* called when opening /dev/obdNNN */
69 static int obd_class_open(struct inode * inode, struct file * file)
76 dev = MINOR(inode->i_rdev);
77 if (dev >= MAX_OBD_DEVICES)
79 obd_dev[dev].obd_refcnt++;
80 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
81 obd_dev[dev].obd_refcnt);
83 obd_dev[dev].obd_proc_entry =
84 proc_lustre_register_obd_device(&obd_dev[dev]);
91 /* called when closing /dev/obdNNN */
92 static int obd_class_release(struct inode * inode, struct file * file)
99 dev = MINOR(inode->i_rdev);
100 if (dev >= MAX_OBD_DEVICES)
102 fsync_dev(inode->i_rdev);
103 if (obd_dev[dev].obd_refcnt <= 0)
104 printk(KERN_ALERT __FUNCTION__ ": refcount(%d) <= 0\n",
105 obd_dev[dev].obd_refcnt);
106 obd_dev[dev].obd_refcnt--;
108 if (obd_dev[dev].obd_proc_entry && (obd_dev[dev].obd_refcnt==0))
109 proc_lustre_release_obd_device(&obd_dev[dev]);
111 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
112 obd_dev[dev].obd_refcnt);
120 * support functions: we could use inter-module communication, but this
121 * is more portable to other OS's
123 static struct obd_type *obd_search_type(char *nm)
125 struct list_head *tmp;
126 struct obd_type *type;
127 CDEBUG(D_INFO, "SEARCH %s\n", nm);
130 while ( (tmp = tmp->next) != &obd_types ) {
131 type = list_entry(tmp, struct obd_type, typ_chain);
132 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
133 if (strlen(type->typ_name) == strlen(nm) &&
134 strcmp(type->typ_name, nm) == 0 ) {
141 static struct obd_type *obd_nm_to_type(char *nm)
143 struct obd_type *type = obd_search_type(nm);
147 if ( !request_module(nm) ) {
148 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
149 type = obd_search_type(nm);
151 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
159 static int getdata(int len, void **data)
168 CDEBUG(D_MALLOC, "len %d, add %p\n", len, *data);
170 OBD_ALLOC(tmp, void *, len);
175 if ( copy_from_user(tmp, *data, len)) {
184 /* to control /dev/obdNNN */
185 static int obd_class_ioctl (struct inode * inode, struct file * filp,
186 unsigned int cmd, unsigned long arg)
188 struct obd_device *obddev;
189 /* NOTE this must be larger than any of the ioctl data structs */
192 struct obd_conn conn;
194 long int cli_id; /* connect, disconnect */
197 CDEBUG(D_IOCTL, "invalid inode\n");
201 dev = MINOR(inode->i_rdev);
202 if (dev > MAX_OBD_DEVICES)
204 obddev = &obd_dev[dev];
205 conn.oc_dev = obddev;
210 case OBD_IOC_ATTACH: {
211 struct obd_type *type;
212 struct oic_generic *input = tmp_buf;
216 /* have we attached a type to this device */
217 if ( obddev->obd_type ||
218 (obddev->obd_flags & OBD_ATTACHED) ){
220 if (obddev->obd_type->typ_name)
221 name = obddev->obd_type->typ_name;
225 "OBD Device %d already attached to type %s.\n",
231 /* get data structures */
232 err = copy_from_user(input, (void *)arg, sizeof(*input));
238 err = getdata(input->att_typelen + 1, &input->att_type);
245 nm = input->att_type;
246 type = obd_nm_to_type(nm);
248 OBD_FREE(input->att_type, input->att_typelen + 1);
250 printk(__FUNCTION__ ": unknown obd type dev %d\n",
255 obddev->obd_type = type;
257 /* get the attach data */
258 err = getdata(input->att_datalen, &input->att_data);
264 INIT_LIST_HEAD(&obddev->obd_gen_clients);
265 obddev->obd_multi_count = 0;
267 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
268 dev, input->att_datalen, obddev->obd_type->typ_name);
269 /* maybe we are done */
270 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
271 obddev->obd_flags |= OBD_ATTACHED;
273 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
276 OBD_FREE(input->att_data, input->att_datalen);
283 err = OBP(obddev, attach)(obddev, input->att_datalen,
286 OBD_FREE(input->att_data, input->att_datalen);
289 obddev->obd_flags &= ~OBD_ATTACHED;
290 obddev->obd_type = NULL;
293 obddev->obd_flags |= OBD_ATTACHED;
295 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
303 case OBD_IOC_DETACH: {
306 if (obddev->obd_flags & OBD_SET_UP) {
310 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
311 CDEBUG(D_IOCTL, "Device not attached\n");
315 if ( !list_empty(&obddev->obd_gen_clients) ) {
316 CDEBUG(D_IOCTL, "Device has connected clients\n");
321 CDEBUG(D_PSDEV, "Detach %d, type %s\n", dev,
322 obddev->obd_type->typ_name);
323 obddev->obd_flags &= ~OBD_ATTACHED;
324 obddev->obd_type->typ_refcnt--;
325 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
326 obddev->obd_type->typ_refcnt);
327 obddev->obd_type = NULL;
333 case OBD_IOC_SETUP: {
345 /* have we attached a type to this device */
346 if (!(obddev->obd_flags & OBD_ATTACHED)) {
347 CDEBUG(D_IOCTL, "Device not attached\n");
352 /* has this been done already? */
353 if ( obddev->obd_flags & OBD_SET_UP ) {
354 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
355 dev, obddev->obd_type->typ_name);
360 /* get main structure */
361 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
366 user_path = setup->setup_data;
368 /* get the attach data */
369 err = getdata(setup->setup_datalen, &setup->setup_data);
374 obddev->obd_rdev = setup->setup_rdev;
375 obddev->obd_user_name = user_path;
378 CDEBUG(D_PSDEV, "Setup %d, type %s device %x\n", dev,
379 obddev->obd_type->typ_name, setup->setup_rdev);
380 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
381 obddev->obd_type->typ_refcnt++;
382 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
383 dev, obddev->obd_type->typ_refcnt);
384 if (setup->setup_data)
385 OBD_FREE(setup->setup_data,
386 setup->setup_datalen);
387 obddev->obd_flags |= OBD_SET_UP;
392 err = OBP(obddev, setup)(obddev, 0, NULL);
395 obddev->obd_flags &= ~OBD_SET_UP;
398 obddev->obd_type->typ_refcnt++;
399 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
400 dev, obddev->obd_type->typ_refcnt);
401 obddev->obd_flags |= OBD_SET_UP;
404 OBD_FREE(setup->setup_data, setup->setup_datalen);
407 case OBD_IOC_CLEANUP: {
409 /* has this minor been registered? */
410 if (!obddev->obd_type) {
411 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
416 if ( !obddev->obd_type->typ_refcnt ) {
417 CDEBUG(D_IOCTL, "dev %d has refcount (%d)!\n",
418 dev, obddev->obd_type->typ_refcnt);
423 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
424 (!(obddev->obd_flags & OBD_ATTACHED))) {
425 CDEBUG(D_IOCTL, "device not attached or set up\n");
430 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
433 /* cleanup has no argument */
434 err = OBP(obddev, cleanup)(obddev);
441 obddev->obd_flags &= ~OBD_SET_UP;
442 obddev->obd_type->typ_refcnt--;
443 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
444 obddev->obd_type->typ_refcnt);
448 case OBD_IOC_CONNECT:
450 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
451 (!(obddev->obd_flags & OBD_ATTACHED))) {
452 CDEBUG(D_IOCTL, "Device not attached or set up\n");
456 if ( !OBT(obddev) || !OBP(obddev, connect) )
459 err = OBP(obddev, connect)(&conn);
463 return copy_to_user((int *)arg, &conn.oc_id,
466 case OBD_IOC_DISCONNECT:
467 /* frees data structures */
468 /* has this minor been registered? */
469 if (!obddev->obd_type)
472 get_user(cli_id, (int *) arg);
475 if ( !OBT(obddev) || !OBP(obddev, disconnect))
478 OBP(obddev, disconnect)(&conn);
481 case OBD_IOC_DEC_USE_COUNT:
486 struct oic_range_s *range = tmp_buf;
488 if (!obddev->obd_type)
491 err = copy_from_user(range, (const void *)arg, sizeof(*range));
498 if ( !OBT(obddev) || !OBP(obddev, sync) ) {
504 /* XXX sync needs to be tested/verified */
505 err = OBP(obddev, sync)(&conn, &range->obdo, range->count,
513 return put_user(err, (int *) arg);
515 case OBD_IOC_CREATE: {
516 struct oic_attr_s *attr = tmp_buf;
518 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
524 /* has this minor been registered? */
525 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
526 !(obddev->obd_flags & OBD_SET_UP)) {
527 CDEBUG(D_IOCTL, "Device not attached or set up\n");
530 conn.oc_id = attr->conn_id;
532 if ( !OBT(obddev) || !OBP(obddev, create) )
535 err = OBP(obddev, create)(&conn, &attr->obdo);
541 err = copy_to_user((int *)arg, attr, sizeof(*attr));
546 case OBD_IOC_DESTROY: {
547 struct oic_attr_s *attr = tmp_buf;
549 /* has this minor been registered? */
550 if (!obddev->obd_type)
553 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
559 if ( !OBT(obddev) || !OBP(obddev, destroy) )
562 conn.oc_id = attr->conn_id;
563 err = OBP(obddev, destroy)(&conn, &attr->obdo);
568 case OBD_IOC_SETATTR: {
569 struct oic_attr_s *attr = tmp_buf;
571 /* has this minor been registered? */
572 if (!obddev->obd_type)
575 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
579 if ( !OBT(obddev) || !OBP(obddev, setattr) )
582 conn.oc_id = attr->conn_id;
583 err = OBP(obddev, setattr)(&conn, &attr->obdo);
588 case OBD_IOC_GETATTR: {
589 struct oic_attr_s *attr = tmp_buf;
591 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
595 conn.oc_id = attr->conn_id;
597 err = OBP(obddev, getattr)(&conn, &attr->obdo);
603 err = copy_to_user((int *)arg, attr, sizeof(*attr));
610 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
612 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
618 conn.oc_id = rw_s->conn_id;
620 if ( !OBT(obddev) || !OBP(obddev, read) ) {
627 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
628 &rw_s->count, rw_s->offset);
631 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
632 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
638 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
643 case OBD_IOC_WRITE: {
644 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
646 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
652 conn.oc_id = rw_s->conn_id;
654 if ( !OBT(obddev) || !OBP(obddev, write) ) {
659 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
660 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
662 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
663 &rw_s->count, rw_s->offset);
670 err = copy_to_user((int *)arg, &rw_s->count,
671 sizeof(rw_s->count));
675 case OBD_IOC_PREALLOCATE: {
676 struct oic_prealloc_s *prealloc = tmp_buf;
678 /* has this minor been registered? */
679 if (!obddev->obd_type)
682 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
686 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
687 !(obddev->obd_flags & OBD_SET_UP)) {
688 CDEBUG(D_IOCTL, "Device not attached or set up\n");
692 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
695 conn.oc_id = prealloc->conn_id;
696 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
703 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
707 case OBD_IOC_STATFS: {
709 unsigned int conn_id;
712 /* has this minor been registered? */
713 if (!obddev->obd_type)
716 tmp = (void *)arg + sizeof(unsigned int);
717 get_user(conn_id, (int *) arg);
719 if ( !OBT(obddev) || !OBP(obddev, statfs) )
722 conn.oc_id = conn_id;
723 err = OBP(obddev, statfs)(&conn, &buf);
728 err = copy_to_user(tmp, &buf, sizeof(buf));
734 struct ioc_mv_s *mvdata = tmp_buf;
736 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
737 (!(obddev->obd_flags & OBD_ATTACHED))) {
738 CDEBUG(D_IOCTL, "Device not attached or set up\n");
742 /* get main structure */
743 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
749 if ( !OBT(obddev) || !OBP(obddev, copy) )
752 /* do the partition */
753 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
754 obddev->obd_type->typ_name, mvdata->dst.o_id,
757 conn.oc_id = mvdata->src_conn_id;
759 err = OBP(obddev, copy)(&conn, &mvdata->dst,
761 mvdata->src.o_size, 0);
766 struct ioc_mv_s *mvdata = tmp_buf;
768 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
769 (!(obddev->obd_flags & OBD_ATTACHED))) {
770 CDEBUG(D_IOCTL, "Device not attached or set up\n");
774 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
780 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
782 if ( !OBT(obddev) || !OBP(obddev, migrate) )
785 /* do the partition */
786 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
787 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
788 mvdata->src.o_id, mvdata->dst.o_id);
790 conn.oc_id = mvdata->src_conn_id;
791 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
792 mvdata->src.o_size, 0);
796 case OBD_IOC_PUNCH: {
797 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
799 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
805 conn.oc_id = rw_s->conn_id;
807 if ( !OBT(obddev) || !OBP(obddev, punch) ) {
812 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
813 rw_s->conn_id, rw_s->count, rw_s->offset);
814 err = OBP(obddev, punch)(&conn, &rw_s->obdo, rw_s->count,
826 struct obd_type *type;
827 struct oic_generic input;
831 /* get data structures */
832 err = copy_from_user(&input, (void *)arg, sizeof(input));
838 err = getdata(input.att_typelen + 1, &input.att_type);
846 type = obd_nm_to_type(nm);
849 if ( !request_module(nm) ) {
850 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
851 type = obd_nm_to_type(nm);
853 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
857 OBD_FREE(input.att_type, input.att_typelen + 1);
859 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
864 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
868 conn.oc_id = input.att_connid;
870 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
871 cmd, type->typ_name, input.att_datalen);
873 /* get the generic data */
874 karg = input.att_data;
875 err = getdata(input.att_datalen, &karg);
881 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
882 karg, input.att_data);
883 OBD_FREE(karg, input.att_datalen);
889 } /* obd_class_ioctl */
891 /* Driver interface done, utility functions follow */
893 int obd_register_type(struct obd_ops *ops, char *nm)
895 struct obd_type *type;
898 if (obd_init_magic != 0x11223344) {
899 printk(__FUNCTION__ ": bad magic for type\n");
904 if ( obd_nm_to_type(nm) ) {
905 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
910 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
915 memset(type, 0, sizeof(*type));
916 INIT_LIST_HEAD(&type->typ_chain);
918 list_add(&type->typ_chain, obd_types.next);
925 int obd_unregister_type(char *nm)
927 struct obd_type *type = obd_nm_to_type(nm);
931 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
936 if ( type->typ_refcnt ) {
938 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
939 "(%d)\n", nm, type->typ_refcnt);
944 list_del(&type->typ_chain);
945 OBD_FREE(type, sizeof(*type));
948 } /* obd_unregister_type */
950 /* declare character device */
951 static struct file_operations obd_psdev_fops = {
952 ioctl: obd_class_ioctl, /* ioctl */
953 open: obd_class_open, /* open */
954 release: obd_class_release, /* release */
965 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
967 INIT_LIST_HEAD(&obd_types);
969 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
971 printk(KERN_ERR __FUNCTION__ ": unable to get major %d\n",
976 for (i = 0; i < MAX_OBD_DEVICES; i++) {
977 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
978 obd_dev[i].obd_minor = i;
979 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
982 err = obd_init_obdo_cache();
986 obd_init_magic = 0x11223344;
990 EXPORT_SYMBOL(obd_register_type);
991 EXPORT_SYMBOL(obd_unregister_type);
993 EXPORT_SYMBOL(obd_print_entry);
994 EXPORT_SYMBOL(obd_debug_level);
995 EXPORT_SYMBOL(obd_dev);
997 EXPORT_SYMBOL(gen_connect);
998 EXPORT_SYMBOL(gen_client);
999 EXPORT_SYMBOL(gen_cleanup);
1000 EXPORT_SYMBOL(gen_disconnect);
1001 EXPORT_SYMBOL(gen_copy_data);
1002 EXPORT_SYMBOL(obdo_cachep);
1004 /* EXPORT_SYMBOL(gen_multi_attach); */
1005 EXPORT_SYMBOL(gen_multi_setup);
1006 EXPORT_SYMBOL(gen_multi_cleanup);
1010 int init_module(void)
1015 void cleanup_module(void)
1020 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
1021 for (i = 0; i < MAX_OBD_DEVICES; i++) {
1022 struct obd_device *obddev = &obd_dev[i];
1023 if ( obddev->obd_type &&
1024 (obddev->obd_flags & OBD_SET_UP) &&
1025 OBT(obddev) && OBP(obddev, detach) ) {
1026 /* XXX should this call generic detach otherwise? */
1027 OBP(obddev, detach)(obddev);
1031 obd_cleanup_obdo_cache();
1033 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);