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 * version 2 as published by the Free Software Foundation.
11 * Adapted to become the Linux 2.0 Coda pseudo device
12 * Peter Braam <braam@maths.ox.ac.uk>
13 * Michael Callahan <mjc@emmy.smith.edu>
15 * Changes for Linux 2.1
16 * Copyright (c) 1997 Carnegie-Mellon University
18 * Redone again for Intermezzo
19 * Copyright (c) 1998 Peter J. Braam
21 * Hacked up again for simulated OBD
22 * Copyright (c) 1999 Stelias Computing, Inc.
23 * (authors {pschwan,braam}@stelias.com)
24 * Copyright (C) 1999 Seagate Technology, Inc.
25 * Copyright (C) 2001 Cluster File Systems, Inc.
31 #include <linux/config.h> /* for CONFIG_PROC_FS */
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/major.h>
36 #include <linux/kmod.h> /* for request_module() */
37 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/ioport.h>
41 #include <linux/fcntl.h>
42 #include <linux/delay.h>
43 #include <linux/skbuff.h>
44 #include <linux/proc_fs.h>
46 #include <linux/poll.h>
47 #include <linux/init.h>
48 #include <linux/list.h>
50 #include <asm/system.h>
52 #include <asm/uaccess.h>
53 #include <linux/miscdevice.h>
55 #include <linux/obd_support.h>
56 #include <linux/obd_class.h>
58 static int obd_init_magic;
59 int obd_print_entry = 1;
60 int obd_debug_level = ~0;
62 struct obd_device obd_dev[MAX_OBD_DEVICES];
63 struct list_head obd_types;
65 /* opening /dev/obd */
66 static int obd_class_open(struct inode * inode, struct file * file)
70 file->private_data = NULL;
76 /* closing /dev/obd */
77 static int obd_class_release(struct inode * inode, struct file * file)
81 if (file->private_data)
82 file->private_data = NULL;
90 * support functions: we could use inter-module communication, but this
91 * is more portable to other OS's
93 static struct obd_type *obd_search_type(char *nm)
95 struct list_head *tmp;
96 struct obd_type *type;
97 CDEBUG(D_INFO, "SEARCH %s\n", nm);
100 while ( (tmp = tmp->next) != &obd_types ) {
101 type = list_entry(tmp, struct obd_type, typ_chain);
102 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
103 if (strlen(type->typ_name) == strlen(nm) &&
104 strcmp(type->typ_name, nm) == 0 ) {
111 static struct obd_type *obd_nm_to_type(char *nm)
113 struct obd_type *type = obd_search_type(nm);
117 if ( !request_module(nm) ) {
118 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
119 type = obd_search_type(nm);
121 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
128 /* to control /dev/obd */
129 static int obd_class_ioctl (struct inode * inode, struct file * filp,
130 unsigned int cmd, unsigned long arg)
132 /* NOTE this must be larger than any of the ioctl data structs */
134 struct obd_ioctl_data *data;
135 struct obd_device *obd = filp->private_data;
136 struct obd_conn conn;
140 memset(buf, 0, sizeof(buf));
142 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
143 printk("OBD ioctl: No device\n");
146 if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) {
147 printk("OBD ioctl: data error\n");
150 data = (struct obd_ioctl_data *)buf;
157 case OBD_IOC_DEVICE: {
158 CDEBUG(D_IOCTL, "\n");
159 if (data->ioc_dev >= MAX_OBD_DEVICES ||
161 printk("OBD ioctl: DEVICE insufficient devices\n");
164 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
166 filp->private_data = &obd_dev[data->ioc_dev];
171 case OBD_IOC_ATTACH: {
172 struct obd_type *type;
175 /* have we attached a type to this device */
176 if ( obd->obd_flags & OBD_ATTACHED ) {
177 printk("OBD: Device %d already typed as %s.\n",
178 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
182 printk("-----> attach %s %s\n", MKSTR(data->ioc_inlbuf1),
183 MKSTR(data->ioc_inlbuf2));
186 type = obd_nm_to_type(data->ioc_inlbuf1);
188 printk("OBD: unknown type dev %d\n", obd->obd_minor);
192 obd->obd_type = type;
193 obd->obd_multi_count = 0;
194 INIT_LIST_HEAD(&obd->obd_gen_clients);
197 if ( OBT(obd) && OBP(obd, attach) ) {
198 err = OBP(obd, attach)(obd, sizeof(*data), data);
202 obd->obd_type = NULL;
205 obd->obd_flags |= OBD_ATTACHED;
207 printk("OBD: dev %d attached type %s\n",
208 obd->obd_minor, data->ioc_inlbuf1);
209 obd->obd_proc_entry =
210 proc_lustre_register_obd_device(obd);
218 case OBD_IOC_DETACH: {
220 if (obd->obd_flags & OBD_SET_UP) {
221 printk("OBD device %d still set up\n", obd->obd_minor);
224 if (! (obd->obd_flags & OBD_ATTACHED) ) {
225 printk("OBD device %d not attached\n", obd->obd_minor);
228 if ( !list_empty(&obd->obd_gen_clients) ) {
229 printk("OBD device %d has connected clients\n", obd->obd_minor);
233 if (obd->obd_proc_entry)
234 proc_lustre_release_obd_device(obd);
236 obd->obd_flags &= ~OBD_ATTACHED;
237 obd->obd_type->typ_refcnt--;
238 obd->obd_type = NULL;
244 case OBD_IOC_SETUP: {
246 /* have we attached a type to this device? */
247 if (!(obd->obd_flags & OBD_ATTACHED)) {
248 printk("Device %d not attached\n", obd->obd_minor);
252 /* has this been done already? */
253 if ( obd->obd_flags & OBD_SET_UP ) {
254 printk("Device %d already setup (type %s)\n",
255 obd->obd_minor, obd->obd_type->typ_name);
259 if ( OBT(obd) && OBP(obd, setup) )
260 err = OBP(obd, setup)(obd, sizeof(*data), data);
263 obd->obd_type->typ_refcnt++;
264 obd->obd_flags |= OBD_SET_UP;
270 case OBD_IOC_CLEANUP: {
273 if ( !(obd->obd_flags & OBD_SET_UP) ) {
278 err = obd_cleanup(obd);
284 obd->obd_flags &= ~OBD_SET_UP;
285 obd->obd_type->typ_refcnt--;
290 case OBD_IOC_CONNECT:
292 conn.oc_id = data->ioc_conn1;
295 err = obd_connect(&conn);
297 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
298 data->ioc_conn1 = conn.oc_id;
302 return copy_to_user((int *)arg, data, sizeof(*data));
305 case OBD_IOC_DISCONNECT: {
306 conn.oc_id = data->ioc_conn1;
309 err = obd_disconnect(&conn);
313 case OBD_IOC_DEC_USE_COUNT: {
318 case OBD_IOC_CREATE: {
319 conn.oc_id = data->ioc_conn1;
322 err = obd_create(&conn, &data->ioc_obdo1);
328 err = copy_to_user((int *)arg, data, sizeof(*data));
333 case OBD_IOC_GETATTR: {
334 conn.oc_id = data->ioc_conn1;
337 err = obd_getattr(&conn, &data->ioc_obdo1);
343 err = copy_to_user((int *)arg, data, sizeof(*data));
348 case OBD_IOC_SETATTR: {
349 conn.oc_id = data->ioc_conn1;
352 err = obd_setattr(&conn, &data->ioc_obdo1);
358 err = copy_to_user((int *)arg, data, sizeof(*data));
363 case OBD_IOC_DESTROY: {
364 conn.oc_id = data->ioc_conn1;
367 err = obd_destroy(&conn, &data->ioc_obdo1);
373 err = copy_to_user((int *)arg, data, sizeof(*data));
380 struct oic_range_s *range = tmp_buf;
385 err = copy_from_user(range, (const void *)arg, sizeof(*range));
392 if ( !OBT(obd) || !OBP(obd, sync) ) {
398 /* XXX sync needs to be tested/verified */
399 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
407 return put_user(err, (int *) arg);
412 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
414 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
420 conn.oc_id = rw_s->conn_id;
422 if ( !OBT(obd) || !OBP(obd, read) ) {
429 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
430 &rw_s->count, rw_s->offset);
433 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
434 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
440 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
445 case OBD_IOC_WRITE: {
446 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
448 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
454 conn.oc_id = rw_s->conn_id;
456 if ( !OBT(obd) || !OBP(obd, write) ) {
461 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
462 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
464 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
465 &rw_s->count, rw_s->offset);
472 err = copy_to_user((int *)arg, &rw_s->count,
473 sizeof(rw_s->count));
477 case OBD_IOC_PREALLOCATE: {
478 struct oic_prealloc_s *prealloc = tmp_buf;
480 /* has this minor been registered? */
484 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
488 if ( !(obd->obd_flags & OBD_ATTACHED) ||
489 !(obd->obd_flags & OBD_SET_UP)) {
490 CDEBUG(D_IOCTL, "Device not attached or set up\n");
494 if ( !OBT(obd) || !OBP(obd, preallocate) )
497 conn.oc_id = prealloc->conn_id;
498 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
505 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
509 case OBD_IOC_STATFS: {
511 unsigned int conn_id;
514 /* has this minor been registered? */
518 tmp = (void *)arg + sizeof(unsigned int);
519 get_user(conn_id, (int *) arg);
521 if ( !OBT(obd) || !OBP(obd, statfs) )
524 conn.oc_id = conn_id;
525 err = OBP(obd, statfs)(&conn, &buf);
530 err = copy_to_user(tmp, &buf, sizeof(buf));
536 struct ioc_mv_s *mvdata = tmp_buf;
538 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
539 (!(obd->obd_flags & OBD_ATTACHED))) {
540 CDEBUG(D_IOCTL, "Device not attached or set up\n");
544 /* get main structure */
545 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
551 if ( !OBT(obd) || !OBP(obd, copy) )
554 /* do the partition */
555 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
556 obd->obd_type->typ_name, mvdata->dst.o_id,
559 conn.oc_id = mvdata->src_conn_id;
561 err = OBP(obd, copy)(&conn, &mvdata->dst,
563 mvdata->src.o_size, 0);
568 struct ioc_mv_s *mvdata = tmp_buf;
570 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
571 (!(obd->obd_flags & OBD_ATTACHED))) {
572 CDEBUG(D_IOCTL, "Device not attached or set up\n");
576 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
582 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
584 if ( !OBT(obd) || !OBP(obd, migrate) )
587 /* do the partition */
588 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
589 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
590 mvdata->src.o_id, mvdata->dst.o_id);
592 conn.oc_id = mvdata->src_conn_id;
593 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
594 mvdata->src.o_size, 0);
598 case OBD_IOC_PUNCH: {
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(obd) || !OBP(obd, punch) ) {
614 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
615 rw_s->conn_id, rw_s->count, rw_s->offset);
616 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
628 struct obd_type *type;
629 struct oic_generic input;
633 /* get data structures */
634 err = copy_from_user(&input, (void *)arg, sizeof(input));
640 err = getdata(input.att_typelen + 1, &input.att_type);
648 type = obd_nm_to_type(nm);
651 if ( !request_module(nm) ) {
652 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
653 type = obd_nm_to_type(nm);
655 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
659 OBD_FREE(input.att_type, input.att_typelen + 1);
661 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
666 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
670 conn.oc_id = input.att_connid;
672 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
673 cmd, type->typ_name, input.att_datalen);
675 /* get the generic data */
676 karg = input.att_data;
677 err = getdata(input.att_datalen, &karg);
683 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
684 karg, input.att_data);
685 OBD_FREE(karg, input.att_datalen);
695 } /* obd_class_ioctl */
698 /* Driver interface done, utility functions follow */
699 int obd_register_type(struct obd_ops *ops, char *nm)
701 struct obd_type *type;
703 if (obd_init_magic != 0x11223344) {
704 printk(__FUNCTION__ ": bad magic for type\n");
709 if ( obd_nm_to_type(nm) ) {
710 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
715 OBD_ALLOC(type, sizeof(*type));
720 memset(type, 0, sizeof(*type));
721 INIT_LIST_HEAD(&type->typ_chain);
723 list_add(&type->typ_chain, obd_types.next);
730 int obd_unregister_type(char *nm)
732 struct obd_type *type = obd_nm_to_type(nm);
736 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
741 if ( type->typ_refcnt ) {
743 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
744 "(%d)\n", nm, type->typ_refcnt);
749 list_del(&type->typ_chain);
750 OBD_FREE(type, sizeof(*type));
753 } /* obd_unregister_type */
755 /* declare character device */
756 static struct file_operations obd_psdev_fops = {
757 ioctl: obd_class_ioctl, /* ioctl */
758 open: obd_class_open, /* open */
759 release: obd_class_release, /* release */
763 #define OBD_MINOR 241
764 static struct miscdevice obd_psdev = {
775 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
777 INIT_LIST_HEAD(&obd_types);
779 if ( (err = misc_register(&obd_psdev)) ) {
780 printk(KERN_ERR __FUNCTION__ ": cannot register %d err %d\n",
785 for (i = 0; i < MAX_OBD_DEVICES; i++) {
786 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
787 obd_dev[i].obd_minor = i;
788 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
791 err = obd_init_obdo_cache();
795 obd_init_magic = 0x11223344;
799 EXPORT_SYMBOL(obd_register_type);
800 EXPORT_SYMBOL(obd_unregister_type);
802 EXPORT_SYMBOL(obd_print_entry);
803 EXPORT_SYMBOL(obd_debug_level);
804 EXPORT_SYMBOL(obd_dev);
806 EXPORT_SYMBOL(gen_connect);
807 EXPORT_SYMBOL(gen_client);
808 EXPORT_SYMBOL(gen_cleanup);
809 EXPORT_SYMBOL(gen_disconnect);
810 EXPORT_SYMBOL(gen_copy_data);
811 EXPORT_SYMBOL(obdo_cachep);
813 /* EXPORT_SYMBOL(gen_multi_attach); */
814 EXPORT_SYMBOL(gen_multi_setup);
815 EXPORT_SYMBOL(gen_multi_cleanup);
816 EXPORT_SYMBOL(obd_memory);
819 int init_module(void)
824 void cleanup_module(void)
829 misc_deregister(&obd_psdev);
830 for (i = 0; i < MAX_OBD_DEVICES; i++) {
831 struct obd_device *obd = &obd_dev[i];
832 if ( obd->obd_type &&
833 (obd->obd_flags & OBD_SET_UP) &&
834 OBT(obd) && OBP(obd, detach) ) {
835 /* XXX should this call generic detach otherwise? */
836 OBP(obd, detach)(obd);
840 obd_cleanup_obdo_cache();
842 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);