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/slab.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;
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);
82 obd_dev[dev].obd_proc_entry =
83 proc_lustre_register_obd_device(&obd_dev[dev]);
90 /* called when closing /dev/obdNNN */
91 static int obd_class_release(struct inode * inode, struct file * file)
98 dev = MINOR(inode->i_rdev);
99 if (dev >= MAX_OBD_DEVICES)
101 fsync_dev(inode->i_rdev);
102 if (obd_dev[dev].obd_refcnt <= 0)
103 printk(KERN_ALERT __FUNCTION__ ": refcount(%d) <= 0\n",
104 obd_dev[dev].obd_refcnt);
105 obd_dev[dev].obd_refcnt--;
107 if (obd_dev[dev].obd_proc_entry && (obd_dev[dev].obd_refcnt==0))
108 proc_lustre_release_obd_device(&obd_dev[dev]);
110 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
111 obd_dev[dev].obd_refcnt);
118 /* support function */
119 static struct obd_type *obd_nm_to_type(char *nm)
121 struct list_head *tmp;
122 struct obd_type *type;
123 CDEBUG(D_INFO, "SEARCH %s\n", nm);
126 while ( (tmp = tmp->next) != &obd_types ) {
127 type = list_entry(tmp, struct obd_type, typ_chain);
128 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
129 if (strlen(type->typ_name) == strlen(nm) &&
130 strcmp(type->typ_name, nm) == 0 ) {
138 static int getdata(int len, void **data)
147 CDEBUG(D_MALLOC, "len %d, add %p\n", len, *data);
149 OBD_ALLOC(tmp, void *, len);
154 if ( copy_from_user(tmp, *data, len)) {
164 static int obd_devicename_from_path(obd_devicename* whoami,
169 whoami->name = kname;
171 /* err = user_path_walk(user_string, &nd); */
173 /* whoami->dentry = nd.dentry; */
174 /* path_release(&nd); */
183 /* to control /dev/obdNNN */
184 static int obd_class_ioctl (struct inode * inode, struct file * filp,
185 unsigned int cmd, unsigned long arg)
187 struct obd_device *obddev;
188 /* NOTE this must be larger than any of the ioctl data structs */
191 struct obd_conn conn;
193 long int cli_id; /* connect, disconnect */
196 CDEBUG(D_IOCTL, "invalid inode\n");
200 dev = MINOR(inode->i_rdev);
201 if (dev > MAX_OBD_DEVICES)
203 obddev = &obd_dev[dev];
204 conn.oc_dev = obddev;
209 case OBD_IOC_ATTACH: {
210 struct obd_type *type;
211 struct oic_generic *input = tmp_buf;
215 /* have we attached a type to this device */
216 if ( obddev->obd_type ||
217 (obddev->obd_flags & OBD_ATTACHED) ){
219 "OBD Device %d already attached to type %s.\n",
220 dev, obddev->obd_type->typ_name);
225 /* get data structures */
226 err = copy_from_user(input, (void *)arg, sizeof(*input));
232 err = getdata(input->att_typelen + 1, &input->att_type);
239 nm = input->att_type;
240 type = obd_nm_to_type(nm);
243 if ( !request_module(nm) ) {
244 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
245 type = obd_nm_to_type(nm);
247 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
252 OBD_FREE(input->att_type, input->att_typelen + 1);
254 printk(__FUNCTION__ ": unknown obd type dev %d\n",
259 obddev->obd_type = type;
261 /* get the attach data */
262 err = getdata(input->att_datalen, &input->att_data);
268 INIT_LIST_HEAD(&obddev->obd_gen_clients);
269 obddev->obd_multi_count = 0;
271 CDEBUG(D_IOCTL, "Attach %d, datalen %d, type %s\n",
272 dev, input->att_datalen, obddev->obd_type->typ_name);
273 /* maybe we are done */
274 if ( !OBT(obddev) || !OBP(obddev, attach) ) {
275 obddev->obd_flags |= OBD_ATTACHED;
277 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
280 OBD_FREE(input->att_data, input->att_datalen);
287 err = OBP(obddev, attach)(obddev, input->att_datalen,
290 OBD_FREE(input->att_data, input->att_datalen);
293 obddev->obd_flags &= ~OBD_ATTACHED;
294 obddev->obd_type = NULL;
297 obddev->obd_flags |= OBD_ATTACHED;
299 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
307 case OBD_IOC_DETACH: {
310 if (obddev->obd_flags & OBD_SET_UP) {
314 if (! (obddev->obd_flags & OBD_ATTACHED) ) {
315 CDEBUG(D_IOCTL, "Device not attached\n");
319 if ( !list_empty(&obddev->obd_gen_clients) ) {
320 CDEBUG(D_IOCTL, "Device has connected clients\n");
325 CDEBUG(D_PSDEV, "Detach %d, type %s\n", dev,
326 obddev->obd_type->typ_name);
327 obddev->obd_flags &= ~OBD_ATTACHED;
328 obddev->obd_type->typ_refcnt--;
329 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
330 obddev->obd_type->typ_refcnt);
331 obddev->obd_type = NULL;
337 case OBD_IOC_SETUP: {
347 /* have we attached a type to this device */
348 if (!(obddev->obd_flags & OBD_ATTACHED)) {
349 CDEBUG(D_IOCTL, "Device not attached\n");
354 /* has this been done already? */
355 if ( obddev->obd_flags & OBD_SET_UP ) {
356 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
357 dev, obddev->obd_type->typ_name);
364 /* get main structure */
365 err = copy_from_user(setup, (void *) arg, sizeof(*setup));
371 /* get the attach data */
372 err = getdata(setup->setup_datalen, &setup->setup_data);
378 err = obd_devicename_from_path(&(obddev->obd_fsname),
379 setup->setup_datalen,
380 (char*) setup->setup_data);
382 memset(&(obddev->obd_fsname), 0, sizeof(obd_devicename));
388 CDEBUG(D_PSDEV, "Setup %d, type %s\n", dev,
389 obddev->obd_type->typ_name);
390 if ( !OBT(obddev) || !OBP(obddev, setup) ) {
391 obddev->obd_type->typ_refcnt++;
392 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
393 dev, obddev->obd_type->typ_refcnt);
394 if (setup->setup_data)
395 OBD_FREE(setup->setup_data,
396 setup->setup_datalen);
397 obddev->obd_flags |= OBD_SET_UP;
402 err = OBP(obddev, setup)(obddev, 0, NULL);
405 obddev->obd_flags &= ~OBD_SET_UP;
408 obddev->obd_type->typ_refcnt++;
409 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n",
410 dev, obddev->obd_type->typ_refcnt);
411 obddev->obd_flags |= OBD_SET_UP;
417 case OBD_IOC_CLEANUP: {
419 /* has this minor been registered? */
420 if (!obddev->obd_type) {
421 CDEBUG(D_IOCTL, "OBD Device %d has no type.\n", dev);
426 if ( !obddev->obd_type->typ_refcnt ) {
427 CDEBUG(D_IOCTL, "dev %d has refcount (%d)!\n",
428 dev, obddev->obd_type->typ_refcnt);
433 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
434 (!(obddev->obd_flags & OBD_ATTACHED))) {
435 CDEBUG(D_IOCTL, "device not attached or set up\n");
440 if ( !OBT(obddev) || !OBP(obddev, cleanup) )
443 /* cleanup has no argument */
444 err = OBP(obddev, cleanup)(obddev);
451 obddev->obd_flags &= ~OBD_SET_UP;
452 obddev->obd_type->typ_refcnt--;
453 CDEBUG(D_PSDEV, "Dev %d refcount now %d\n", dev,
454 obddev->obd_type->typ_refcnt);
458 case OBD_IOC_CONNECT:
460 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
461 (!(obddev->obd_flags & OBD_ATTACHED))) {
462 CDEBUG(D_IOCTL, "Device not attached or set up\n");
466 if ( !OBT(obddev) || !OBP(obddev, connect) )
469 err = OBP(obddev, connect)(&conn);
473 return copy_to_user((int *)arg, &conn.oc_id,
476 case OBD_IOC_DISCONNECT:
477 /* frees data structures */
478 /* has this minor been registered? */
479 if (!obddev->obd_type)
482 get_user(cli_id, (int *) arg);
485 if ( !OBT(obddev) || !OBP(obddev, disconnect))
488 OBP(obddev, disconnect)(&conn);
492 struct oic_range_s *range = tmp_buf;
494 if (!obddev->obd_type)
497 err = copy_from_user(range, (const void *)arg, sizeof(*range));
504 if ( !OBT(obddev) || !OBP(obddev, sync) ) {
510 /* XXX sync needs to be tested/verified */
511 err = OBP(obddev, sync)(&conn, &range->obdo, range->count,
519 return put_user(err, (int *) arg);
521 case OBD_IOC_CREATE: {
522 struct oic_attr_s *attr = tmp_buf;
524 err = copy_from_user(attr, (const void *)arg, sizeof(*attr));
530 /* has this minor been registered? */
531 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
532 !(obddev->obd_flags & OBD_SET_UP)) {
533 CDEBUG(D_IOCTL, "Device not attached or set up\n");
536 conn.oc_id = attr->conn_id;
538 if ( !OBT(obddev) || !OBP(obddev, create) )
541 err = OBP(obddev, create)(&conn, &attr->obdo);
547 err = copy_to_user((int *)arg, attr, sizeof(*attr));
552 case OBD_IOC_DESTROY: {
553 struct oic_attr_s *attr = tmp_buf;
555 /* has this minor been registered? */
556 if (!obddev->obd_type)
559 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
565 if ( !OBT(obddev) || !OBP(obddev, destroy) )
568 conn.oc_id = attr->conn_id;
569 err = OBP(obddev, destroy)(&conn, &attr->obdo);
574 case OBD_IOC_SETATTR: {
575 struct oic_attr_s *attr = tmp_buf;
577 /* has this minor been registered? */
578 if (!obddev->obd_type)
581 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
585 if ( !OBT(obddev) || !OBP(obddev, setattr) )
588 conn.oc_id = attr->conn_id;
589 err = OBP(obddev, setattr)(&conn, &attr->obdo);
594 case OBD_IOC_GETATTR: {
595 struct oic_attr_s *attr = tmp_buf;
597 err = copy_from_user(attr, (int *)arg, sizeof(*attr));
601 conn.oc_id = attr->conn_id;
603 err = OBP(obddev, getattr)(&conn, &attr->obdo);
609 err = copy_to_user((int *)arg, attr, sizeof(*attr));
616 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
618 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
624 conn.oc_id = rw_s->conn_id;
626 if ( !OBT(obddev) || !OBP(obddev, read) ) {
633 err = OBP(obddev, read)(&conn, &rw_s->obdo, rw_s->buf,
634 &rw_s->count, rw_s->offset);
637 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
638 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
644 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
649 case OBD_IOC_WRITE: {
650 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
652 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
658 conn.oc_id = rw_s->conn_id;
660 if ( !OBT(obddev) || !OBP(obddev, write) ) {
665 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
666 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
668 err = OBP(obddev, write)(&conn, &rw_s->obdo, rw_s->buf,
669 &rw_s->count, rw_s->offset);
676 err = copy_to_user((int *)arg, &rw_s->count,
677 sizeof(rw_s->count));
681 case OBD_IOC_PREALLOCATE: {
682 struct oic_prealloc_s *prealloc = tmp_buf;
684 /* has this minor been registered? */
685 if (!obddev->obd_type)
688 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
692 if ( !(obddev->obd_flags & OBD_ATTACHED) ||
693 !(obddev->obd_flags & OBD_SET_UP)) {
694 CDEBUG(D_IOCTL, "Device not attached or set up\n");
698 if ( !OBT(obddev) || !OBP(obddev, preallocate) )
701 conn.oc_id = prealloc->conn_id;
702 err = OBP(obddev, preallocate)(&conn, &prealloc->alloc,
709 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
713 case OBD_IOC_STATFS: {
715 unsigned int conn_id;
718 /* has this minor been registered? */
719 if (!obddev->obd_type)
722 tmp = (void *)arg + sizeof(unsigned int);
723 get_user(conn_id, (int *) arg);
725 if ( !OBT(obddev) || !OBP(obddev, statfs) )
728 conn.oc_id = conn_id;
729 err = OBP(obddev, statfs)(&conn, &buf);
734 err = copy_to_user(tmp, &buf, sizeof(buf));
740 struct ioc_mv_s *mvdata = tmp_buf;
742 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
743 (!(obddev->obd_flags & OBD_ATTACHED))) {
744 CDEBUG(D_IOCTL, "Device not attached or set up\n");
748 /* get main structure */
749 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
755 if ( !OBT(obddev) || !OBP(obddev, copy) )
758 /* do the partition */
759 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
760 obddev->obd_type->typ_name, mvdata->dst.o_id,
763 conn.oc_id = mvdata->src_conn_id;
765 err = OBP(obddev, copy)(&conn, &mvdata->dst,
767 mvdata->src.o_size, 0);
772 struct ioc_mv_s *mvdata = tmp_buf;
774 if ( (!(obddev->obd_flags & OBD_SET_UP)) ||
775 (!(obddev->obd_flags & OBD_ATTACHED))) {
776 CDEBUG(D_IOCTL, "Device not attached or set up\n");
780 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
786 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
788 if ( !OBT(obddev) || !OBP(obddev, migrate) )
791 /* do the partition */
792 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
793 dev, obddev->obd_type->typ_name, mvdata->src_conn_id,
794 mvdata->src.o_id, mvdata->dst.o_id);
796 conn.oc_id = mvdata->src_conn_id;
797 err = OBP(obddev, migrate)(&conn, &mvdata->dst, &mvdata->src,
798 mvdata->src.o_size, 0);
802 case OBD_IOC_PUNCH: {
803 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
805 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
811 conn.oc_id = rw_s->conn_id;
813 if ( !OBT(obddev) || !OBP(obddev, punch) ) {
818 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
819 rw_s->conn_id, rw_s->count, rw_s->offset);
820 err = OBP(obddev, punch)(&conn, &rw_s->obdo, rw_s->count,
832 struct obd_type *type;
833 struct oic_generic input;
837 /* get data structures */
838 err = copy_from_user(&input, (void *)arg, sizeof(input));
844 err = getdata(input.att_typelen + 1, &input.att_type);
852 type = obd_nm_to_type(nm);
855 if ( !request_module(nm) ) {
856 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
857 type = obd_nm_to_type(nm);
859 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
863 OBD_FREE(input.att_type, input.att_typelen + 1);
865 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
870 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
874 conn.oc_id = input.att_connid;
876 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
877 cmd, type->typ_name, input.att_datalen);
879 /* get the generic data */
880 karg = input.att_data;
881 err = getdata(input.att_datalen, &karg);
887 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
888 karg, input.att_data);
889 OBD_FREE(karg, input.att_datalen);
895 } /* obd_class_ioctl */
897 /* Driver interface done, utility functions follow */
899 int obd_register_type(struct obd_ops *ops, char *nm)
901 struct obd_type *type;
904 if (obd_init_magic != 0x11223344) {
905 printk(__FUNCTION__ ": bad magic for type\n");
910 if ( obd_nm_to_type(nm) ) {
911 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
916 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
921 memset(type, 0, sizeof(*type));
922 INIT_LIST_HEAD(&type->typ_chain);
924 list_add(&type->typ_chain, obd_types.next);
931 int obd_unregister_type(char *nm)
933 struct obd_type *type = obd_nm_to_type(nm);
937 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
942 if ( type->typ_refcnt ) {
944 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
945 "(%d)\n", nm, type->typ_refcnt);
950 list_del(&type->typ_chain);
951 OBD_FREE(type, sizeof(*type));
954 } /* obd_unregister_type */
956 /* declare character device */
957 static struct file_operations obd_psdev_fops = {
958 ioctl: obd_class_ioctl, /* ioctl */
959 open: obd_class_open, /* open */
960 release: obd_class_release, /* release */
971 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
973 INIT_LIST_HEAD(&obd_types);
975 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
977 printk(KERN_ERR __FUNCTION__ ": unable to get major %d\n",
982 for (i = 0; i < MAX_OBD_DEVICES; i++) {
983 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
984 obd_dev[i].obd_minor = i;
985 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
988 err = obd_init_obdo_cache();
992 obd_init_magic = 0x11223344;
996 EXPORT_SYMBOL(obd_register_type);
997 EXPORT_SYMBOL(obd_unregister_type);
999 EXPORT_SYMBOL(obd_print_entry);
1000 EXPORT_SYMBOL(obd_debug_level);
1001 EXPORT_SYMBOL(obd_dev);
1003 EXPORT_SYMBOL(gen_connect);
1004 EXPORT_SYMBOL(gen_client);
1005 EXPORT_SYMBOL(gen_cleanup);
1006 EXPORT_SYMBOL(gen_disconnect);
1007 EXPORT_SYMBOL(gen_copy_data);
1008 EXPORT_SYMBOL(obdo_cachep);
1010 /* EXPORT_SYMBOL(gen_multi_attach); */
1011 EXPORT_SYMBOL(gen_multi_setup);
1012 EXPORT_SYMBOL(gen_multi_cleanup);
1016 int init_module(void)
1021 void cleanup_module(void)
1026 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
1027 for (i = 0; i < MAX_OBD_DEVICES; i++) {
1028 struct obd_device *obddev = &obd_dev[i];
1029 if ( obddev->obd_type &&
1030 (obddev->obd_flags & OBD_SET_UP) &&
1031 OBT(obddev) && OBP(obddev, detach) ) {
1032 /* XXX should this call generic detach otherwise? */
1033 OBP(obddev, detach)(obddev);
1037 obd_cleanup_obdo_cache();
1039 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);