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>
45 #include <linux/vmalloc.h>
47 #include <linux/poll.h>
48 #include <linux/init.h>
49 #include <linux/list.h>
51 #include <asm/system.h>
53 #include <asm/uaccess.h>
54 #include <linux/miscdevice.h>
56 #include <linux/obd_support.h>
57 #include <linux/obd_class.h>
59 static int obd_init_magic;
60 int obd_print_entry = 1;
61 int obd_debug_level = ~0;
63 struct obd_device obd_dev[MAX_OBD_DEVICES];
64 struct list_head obd_types;
66 /* opening /dev/obd */
67 static int obd_class_open(struct inode * inode, struct file * file)
71 file->private_data = NULL;
77 /* closing /dev/obd */
78 static int obd_class_release(struct inode * inode, struct file * file)
82 if (file->private_data)
83 file->private_data = NULL;
91 * support functions: we could use inter-module communication, but this
92 * is more portable to other OS's
94 static struct obd_type *obd_search_type(char *nm)
96 struct list_head *tmp;
97 struct obd_type *type;
98 CDEBUG(D_INFO, "SEARCH %s\n", nm);
101 while ( (tmp = tmp->next) != &obd_types ) {
102 type = list_entry(tmp, struct obd_type, typ_chain);
103 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
104 if (strlen(type->typ_name) == strlen(nm) &&
105 strcmp(type->typ_name, nm) == 0 ) {
112 static struct obd_type *obd_nm_to_type(char *nm)
114 struct obd_type *type = obd_search_type(nm);
118 if ( !request_module(nm) ) {
119 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
120 type = obd_search_type(nm);
122 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
129 /* to control /dev/obd */
130 static int obd_class_ioctl (struct inode * inode, struct file * filp,
131 unsigned int cmd, unsigned long arg)
133 /* NOTE this must be larger than any of the ioctl data structs */
135 struct obd_ioctl_data *data;
136 struct obd_device *obd = filp->private_data;
137 struct obd_conn conn;
141 memset(buf, 0, sizeof(buf));
143 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
144 printk("OBD ioctl: No device\n");
147 if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) {
148 printk("OBD ioctl: data error\n");
151 data = (struct obd_ioctl_data *)buf;
158 case OBD_IOC_DEVICE: {
159 CDEBUG(D_IOCTL, "\n");
160 if (data->ioc_dev >= MAX_OBD_DEVICES ||
162 printk("OBD ioctl: DEVICE insufficient devices\n");
165 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
167 filp->private_data = &obd_dev[data->ioc_dev];
172 case OBD_IOC_ATTACH: {
173 struct obd_type *type;
176 /* have we attached a type to this device */
177 if ( obd->obd_type || (obd->obd_flags & OBD_ATTACHED) ){
178 printk("OBD: Device %d already typed as %s.\n",
179 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
183 printk("-----> attach %s %s\n", MKSTR(data->ioc_inlbuf1),
184 MKSTR(data->ioc_inlbuf2));
187 type = obd_nm_to_type(data->ioc_inlbuf1);
189 printk("OBD: unknown type dev %d\n", obd->obd_minor);
193 obd->obd_type = type;
194 obd->obd_multi_count = 0;
195 INIT_LIST_HEAD(&obd->obd_gen_clients);
198 if ( OBT(obd) && OBP(obd, attach) ) {
199 err = OBP(obd, attach)(obd, sizeof(*data), data);
203 obd->obd_flags &= ~OBD_ATTACHED;
204 obd->obd_type = NULL;
207 obd->obd_flags |= OBD_ATTACHED;
209 printk("OBD: dev %d attached type %s\n",
210 obd->obd_minor, data->ioc_inlbuf1);
211 obd->obd_proc_entry =
212 proc_lustre_register_obd_device(obd);
220 case OBD_IOC_DETACH: {
222 if (obd->obd_flags & OBD_SET_UP) {
223 printk("OBD device %d still set up\n", obd->obd_minor);
226 if (! (obd->obd_flags & OBD_ATTACHED) ) {
227 printk("OBD device %d not attached\n", obd->obd_minor);
230 if ( !list_empty(&obd->obd_gen_clients) ) {
231 printk("OBD device %d has connected clients\n", obd->obd_minor);
235 if (obd->obd_proc_entry)
236 proc_lustre_release_obd_device(obd);
238 obd->obd_flags &= ~OBD_ATTACHED;
239 obd->obd_type->typ_refcnt--;
240 obd->obd_type = NULL;
246 case OBD_IOC_SETUP: {
248 /* have we attached a type to this device? */
249 if (!(obd->obd_flags & OBD_ATTACHED)) {
250 printk("Device %d not attached\n", obd->obd_minor);
254 /* has this been done already? */
255 if ( obd->obd_flags & OBD_SET_UP ) {
256 printk("Device %d already setup (type %s)\n",
257 obd->obd_minor, obd->obd_type->typ_name);
261 if ( OBT(obd) && OBP(obd, setup) )
262 err = OBP(obd, setup)(obd, sizeof(*data), data);
265 obd->obd_type->typ_refcnt++;
266 obd->obd_flags |= OBD_SET_UP;
272 case OBD_IOC_CLEANUP: {
275 err = obd_cleanup(obd);
281 obd->obd_flags &= ~OBD_SET_UP;
282 obd->obd_type->typ_refcnt--;
287 case OBD_IOC_CONNECT:
289 conn.oc_id = data->ioc_conn1;
292 err = obd_connect(&conn);
294 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
295 data->ioc_conn1 = conn.oc_id;
299 return copy_to_user((int *)arg, data, sizeof(*data));
302 case OBD_IOC_DISCONNECT: {
303 conn.oc_id = data->ioc_conn1;
306 err = obd_disconnect(&conn);
310 case OBD_IOC_DEC_USE_COUNT: {
315 case OBD_IOC_CREATE: {
316 conn.oc_id = data->ioc_conn1;
319 err = obd_create(&conn, &data->ioc_obdo1);
325 err = copy_to_user((int *)arg, data, sizeof(*data));
330 case OBD_IOC_GETATTR: {
331 conn.oc_id = data->ioc_conn1;
334 err = obd_getattr(&conn, &data->ioc_obdo1);
340 err = copy_to_user((int *)arg, data, sizeof(*data));
345 case OBD_IOC_SETATTR: {
346 conn.oc_id = data->ioc_conn1;
349 err = obd_setattr(&conn, &data->ioc_obdo1);
355 err = copy_to_user((int *)arg, data, sizeof(*data));
360 case OBD_IOC_DESTROY: {
361 conn.oc_id = data->ioc_conn1;
364 err = obd_destroy(&conn, &data->ioc_obdo1);
370 err = copy_to_user((int *)arg, data, sizeof(*data));
377 struct oic_range_s *range = tmp_buf;
382 err = copy_from_user(range, (const void *)arg, sizeof(*range));
389 if ( !OBT(obd) || !OBP(obd, sync) ) {
395 /* XXX sync needs to be tested/verified */
396 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
404 return put_user(err, (int *) arg);
409 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
411 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
417 conn.oc_id = rw_s->conn_id;
419 if ( !OBT(obd) || !OBP(obd, read) ) {
426 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
427 &rw_s->count, rw_s->offset);
430 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
431 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
437 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
442 case OBD_IOC_WRITE: {
443 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
445 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
451 conn.oc_id = rw_s->conn_id;
453 if ( !OBT(obd) || !OBP(obd, write) ) {
458 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
459 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
461 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
462 &rw_s->count, rw_s->offset);
469 err = copy_to_user((int *)arg, &rw_s->count,
470 sizeof(rw_s->count));
474 case OBD_IOC_PREALLOCATE: {
475 struct oic_prealloc_s *prealloc = tmp_buf;
477 /* has this minor been registered? */
481 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
485 if ( !(obd->obd_flags & OBD_ATTACHED) ||
486 !(obd->obd_flags & OBD_SET_UP)) {
487 CDEBUG(D_IOCTL, "Device not attached or set up\n");
491 if ( !OBT(obd) || !OBP(obd, preallocate) )
494 conn.oc_id = prealloc->conn_id;
495 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
502 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
506 case OBD_IOC_STATFS: {
508 unsigned int conn_id;
511 /* has this minor been registered? */
515 tmp = (void *)arg + sizeof(unsigned int);
516 get_user(conn_id, (int *) arg);
518 if ( !OBT(obd) || !OBP(obd, statfs) )
521 conn.oc_id = conn_id;
522 err = OBP(obd, statfs)(&conn, &buf);
527 err = copy_to_user(tmp, &buf, sizeof(buf));
533 struct ioc_mv_s *mvdata = tmp_buf;
535 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
536 (!(obd->obd_flags & OBD_ATTACHED))) {
537 CDEBUG(D_IOCTL, "Device not attached or set up\n");
541 /* get main structure */
542 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
548 if ( !OBT(obd) || !OBP(obd, copy) )
551 /* do the partition */
552 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
553 obd->obd_type->typ_name, mvdata->dst.o_id,
556 conn.oc_id = mvdata->src_conn_id;
558 err = OBP(obd, copy)(&conn, &mvdata->dst,
560 mvdata->src.o_size, 0);
565 struct ioc_mv_s *mvdata = tmp_buf;
567 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
568 (!(obd->obd_flags & OBD_ATTACHED))) {
569 CDEBUG(D_IOCTL, "Device not attached or set up\n");
573 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
579 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
581 if ( !OBT(obd) || !OBP(obd, migrate) )
584 /* do the partition */
585 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
586 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
587 mvdata->src.o_id, mvdata->dst.o_id);
589 conn.oc_id = mvdata->src_conn_id;
590 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
591 mvdata->src.o_size, 0);
595 case OBD_IOC_PUNCH: {
596 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
598 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
604 conn.oc_id = rw_s->conn_id;
606 if ( !OBT(obd) || !OBP(obd, punch) ) {
611 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
612 rw_s->conn_id, rw_s->count, rw_s->offset);
613 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
625 struct obd_type *type;
626 struct oic_generic input;
630 /* get data structures */
631 err = copy_from_user(&input, (void *)arg, sizeof(input));
637 err = getdata(input.att_typelen + 1, &input.att_type);
645 type = obd_nm_to_type(nm);
648 if ( !request_module(nm) ) {
649 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
650 type = obd_nm_to_type(nm);
652 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
656 OBD_FREE(input.att_type, input.att_typelen + 1);
658 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
663 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
667 conn.oc_id = input.att_connid;
669 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
670 cmd, type->typ_name, input.att_datalen);
672 /* get the generic data */
673 karg = input.att_data;
674 err = getdata(input.att_datalen, &karg);
680 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
681 karg, input.att_data);
682 OBD_FREE(karg, input.att_datalen);
692 } /* obd_class_ioctl */
695 /* Driver interface done, utility functions follow */
696 int obd_register_type(struct obd_ops *ops, char *nm)
698 struct obd_type *type;
701 if (obd_init_magic != 0x11223344) {
702 printk(__FUNCTION__ ": bad magic for type\n");
707 if ( obd_nm_to_type(nm) ) {
708 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
713 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
718 memset(type, 0, sizeof(*type));
719 INIT_LIST_HEAD(&type->typ_chain);
721 list_add(&type->typ_chain, obd_types.next);
728 int obd_unregister_type(char *nm)
730 struct obd_type *type = obd_nm_to_type(nm);
734 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
739 if ( type->typ_refcnt ) {
741 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
742 "(%d)\n", nm, type->typ_refcnt);
747 list_del(&type->typ_chain);
748 OBD_FREE(type, sizeof(*type));
751 } /* obd_unregister_type */
753 /* declare character device */
754 static struct file_operations obd_psdev_fops = {
755 ioctl: obd_class_ioctl, /* ioctl */
756 open: obd_class_open, /* open */
757 release: obd_class_release, /* release */
762 #define OBD_MINOR 241
763 static struct miscdevice obd_psdev = {
774 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
776 INIT_LIST_HEAD(&obd_types);
778 if ( (err = misc_register(&obd_psdev)) ) {
779 printk(KERN_ERR __FUNCTION__ ": cannot register %d err %d\n",
784 for (i = 0; i < MAX_OBD_DEVICES; i++) {
785 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
786 obd_dev[i].obd_minor = i;
787 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
790 err = obd_init_obdo_cache();
794 obd_init_magic = 0x11223344;
798 EXPORT_SYMBOL(obd_register_type);
799 EXPORT_SYMBOL(obd_unregister_type);
801 EXPORT_SYMBOL(obd_print_entry);
802 EXPORT_SYMBOL(obd_debug_level);
803 EXPORT_SYMBOL(obd_dev);
805 EXPORT_SYMBOL(gen_connect);
806 EXPORT_SYMBOL(gen_client);
807 EXPORT_SYMBOL(gen_cleanup);
808 EXPORT_SYMBOL(gen_disconnect);
809 EXPORT_SYMBOL(gen_copy_data);
810 EXPORT_SYMBOL(obdo_cachep);
812 /* EXPORT_SYMBOL(gen_multi_attach); */
813 EXPORT_SYMBOL(gen_multi_setup);
814 EXPORT_SYMBOL(gen_multi_cleanup);
818 int init_module(void)
823 void cleanup_module(void)
828 misc_deregister(&obd_psdev);
829 for (i = 0; i < MAX_OBD_DEVICES; i++) {
830 struct obd_device *obd = &obd_dev[i];
831 if ( obd->obd_type &&
832 (obd->obd_flags & OBD_SET_UP) &&
833 OBT(obd) && OBP(obd, detach) ) {
834 /* XXX should this call generic detach otherwise? */
835 OBP(obd, detach)(obd);
839 obd_cleanup_obdo_cache();
841 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);