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_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_type = NULL;
206 obd->obd_flags |= OBD_ATTACHED;
208 printk("OBD: dev %d attached type %s\n",
209 obd->obd_minor, data->ioc_inlbuf1);
210 obd->obd_proc_entry =
211 proc_lustre_register_obd_device(obd);
219 case OBD_IOC_DETACH: {
221 if (obd->obd_flags & OBD_SET_UP) {
222 printk("OBD device %d still set up\n", obd->obd_minor);
225 if (! (obd->obd_flags & OBD_ATTACHED) ) {
226 printk("OBD device %d not attached\n", obd->obd_minor);
229 if ( !list_empty(&obd->obd_gen_clients) ) {
230 printk("OBD device %d has connected clients\n", obd->obd_minor);
234 if (obd->obd_proc_entry)
235 proc_lustre_release_obd_device(obd);
237 obd->obd_flags &= ~OBD_ATTACHED;
238 obd->obd_type->typ_refcnt--;
239 obd->obd_type = NULL;
245 case OBD_IOC_SETUP: {
247 /* have we attached a type to this device? */
248 if (!(obd->obd_flags & OBD_ATTACHED)) {
249 printk("Device %d not attached\n", obd->obd_minor);
253 /* has this been done already? */
254 if ( obd->obd_flags & OBD_SET_UP ) {
255 printk("Device %d already setup (type %s)\n",
256 obd->obd_minor, obd->obd_type->typ_name);
260 if ( OBT(obd) && OBP(obd, setup) )
261 err = OBP(obd, setup)(obd, sizeof(*data), data);
264 obd->obd_type->typ_refcnt++;
265 obd->obd_flags |= OBD_SET_UP;
271 case OBD_IOC_CLEANUP: {
274 if ( !(obd->obd_flags & OBD_SET_UP) ) {
279 err = obd_cleanup(obd);
285 obd->obd_flags &= ~OBD_SET_UP;
286 obd->obd_type->typ_refcnt--;
291 case OBD_IOC_CONNECT:
293 conn.oc_id = data->ioc_conn1;
296 err = obd_connect(&conn);
298 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
299 data->ioc_conn1 = conn.oc_id;
303 return copy_to_user((int *)arg, data, sizeof(*data));
306 case OBD_IOC_DISCONNECT: {
307 conn.oc_id = data->ioc_conn1;
310 err = obd_disconnect(&conn);
314 case OBD_IOC_DEC_USE_COUNT: {
319 case OBD_IOC_CREATE: {
320 conn.oc_id = data->ioc_conn1;
323 err = obd_create(&conn, &data->ioc_obdo1);
329 err = copy_to_user((int *)arg, data, sizeof(*data));
334 case OBD_IOC_GETATTR: {
335 conn.oc_id = data->ioc_conn1;
338 err = obd_getattr(&conn, &data->ioc_obdo1);
344 err = copy_to_user((int *)arg, data, sizeof(*data));
349 case OBD_IOC_SETATTR: {
350 conn.oc_id = data->ioc_conn1;
353 err = obd_setattr(&conn, &data->ioc_obdo1);
359 err = copy_to_user((int *)arg, data, sizeof(*data));
364 case OBD_IOC_DESTROY: {
365 conn.oc_id = data->ioc_conn1;
368 err = obd_destroy(&conn, &data->ioc_obdo1);
374 err = copy_to_user((int *)arg, data, sizeof(*data));
380 case OBD_IOC_MODULE_DEBUG:
382 struct module *mod = module_list;
383 char *tmp = data->ioc_inlbuf1;
386 snprintf(line, 128, "symbol-file\n"
387 "symbol-file /usr/src/lum/linux\n"
394 "add-symbol-file /usr/src/obd/%s/%s.o %p\n",
395 mod->name, mod->name, mod);
396 memcpy(tmp, line, strlen(line));
398 } while ( mod->next != NULL)
400 return copy_to_user((int *)arg, data, sizeof(*data) +
405 struct oic_range_s *range = tmp_buf;
410 err = copy_from_user(range, (const void *)arg, sizeof(*range));
417 if ( !OBT(obd) || !OBP(obd, sync) ) {
423 /* XXX sync needs to be tested/verified */
424 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
432 return put_user(err, (int *) arg);
437 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
439 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
445 conn.oc_id = rw_s->conn_id;
447 if ( !OBT(obd) || !OBP(obd, read) ) {
454 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
455 &rw_s->count, rw_s->offset);
458 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
459 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
465 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
470 case OBD_IOC_WRITE: {
471 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
473 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
479 conn.oc_id = rw_s->conn_id;
481 if ( !OBT(obd) || !OBP(obd, write) ) {
486 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
487 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
489 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
490 &rw_s->count, rw_s->offset);
497 err = copy_to_user((int *)arg, &rw_s->count,
498 sizeof(rw_s->count));
502 case OBD_IOC_PREALLOCATE: {
503 struct oic_prealloc_s *prealloc = tmp_buf;
505 /* has this minor been registered? */
509 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
513 if ( !(obd->obd_flags & OBD_ATTACHED) ||
514 !(obd->obd_flags & OBD_SET_UP)) {
515 CDEBUG(D_IOCTL, "Device not attached or set up\n");
519 if ( !OBT(obd) || !OBP(obd, preallocate) )
522 conn.oc_id = prealloc->conn_id;
523 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
530 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
534 case OBD_IOC_STATFS: {
536 unsigned int conn_id;
539 /* has this minor been registered? */
543 tmp = (void *)arg + sizeof(unsigned int);
544 get_user(conn_id, (int *) arg);
546 if ( !OBT(obd) || !OBP(obd, statfs) )
549 conn.oc_id = conn_id;
550 err = OBP(obd, statfs)(&conn, &buf);
555 err = copy_to_user(tmp, &buf, sizeof(buf));
561 struct ioc_mv_s *mvdata = tmp_buf;
563 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
564 (!(obd->obd_flags & OBD_ATTACHED))) {
565 CDEBUG(D_IOCTL, "Device not attached or set up\n");
569 /* get main structure */
570 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
576 if ( !OBT(obd) || !OBP(obd, copy) )
579 /* do the partition */
580 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
581 obd->obd_type->typ_name, mvdata->dst.o_id,
584 conn.oc_id = mvdata->src_conn_id;
586 err = OBP(obd, copy)(&conn, &mvdata->dst,
588 mvdata->src.o_size, 0);
593 struct ioc_mv_s *mvdata = tmp_buf;
595 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
596 (!(obd->obd_flags & OBD_ATTACHED))) {
597 CDEBUG(D_IOCTL, "Device not attached or set up\n");
601 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
607 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
609 if ( !OBT(obd) || !OBP(obd, migrate) )
612 /* do the partition */
613 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
614 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
615 mvdata->src.o_id, mvdata->dst.o_id);
617 conn.oc_id = mvdata->src_conn_id;
618 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
619 mvdata->src.o_size, 0);
623 case OBD_IOC_PUNCH: {
624 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
626 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
632 conn.oc_id = rw_s->conn_id;
634 if ( !OBT(obd) || !OBP(obd, punch) ) {
639 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
640 rw_s->conn_id, rw_s->count, rw_s->offset);
641 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
653 struct obd_type *type;
654 struct oic_generic input;
658 /* get data structures */
659 err = copy_from_user(&input, (void *)arg, sizeof(input));
665 err = getdata(input.att_typelen + 1, &input.att_type);
673 type = obd_nm_to_type(nm);
676 if ( !request_module(nm) ) {
677 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
678 type = obd_nm_to_type(nm);
680 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
684 OBD_FREE(input.att_type, input.att_typelen + 1);
686 printk(__FUNCTION__ ": unknown obd type dev %d\n", dev);
691 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
695 conn.oc_id = input.att_connid;
697 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
698 cmd, type->typ_name, input.att_datalen);
700 /* get the generic data */
701 karg = input.att_data;
702 err = getdata(input.att_datalen, &karg);
708 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
709 karg, input.att_data);
710 OBD_FREE(karg, input.att_datalen);
720 } /* obd_class_ioctl */
723 /* Driver interface done, utility functions follow */
724 int obd_register_type(struct obd_ops *ops, char *nm)
726 struct obd_type *type;
729 if (obd_init_magic != 0x11223344) {
730 printk(__FUNCTION__ ": bad magic for type\n");
735 if ( obd_nm_to_type(nm) ) {
736 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
741 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
746 memset(type, 0, sizeof(*type));
747 INIT_LIST_HEAD(&type->typ_chain);
749 list_add(&type->typ_chain, obd_types.next);
756 int obd_unregister_type(char *nm)
758 struct obd_type *type = obd_nm_to_type(nm);
762 printk(KERN_INFO __FUNCTION__ ": unknown obd type\n");
767 if ( type->typ_refcnt ) {
769 printk(KERN_ALERT __FUNCTION__ ":type %s has refcount "
770 "(%d)\n", nm, type->typ_refcnt);
775 list_del(&type->typ_chain);
776 OBD_FREE(type, sizeof(*type));
779 } /* obd_unregister_type */
781 /* declare character device */
782 static struct file_operations obd_psdev_fops = {
783 ioctl: obd_class_ioctl, /* ioctl */
784 open: obd_class_open, /* open */
785 release: obd_class_release, /* release */
790 #define OBD_MINOR 241
791 static struct miscdevice obd_psdev = {
802 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
804 INIT_LIST_HEAD(&obd_types);
806 if ( (err = misc_register(&obd_psdev)) ) {
807 printk(KERN_ERR __FUNCTION__ ": cannot register %d err %d\n",
812 for (i = 0; i < MAX_OBD_DEVICES; i++) {
813 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
814 obd_dev[i].obd_minor = i;
815 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
818 err = obd_init_obdo_cache();
822 obd_init_magic = 0x11223344;
826 EXPORT_SYMBOL(obd_register_type);
827 EXPORT_SYMBOL(obd_unregister_type);
829 EXPORT_SYMBOL(obd_print_entry);
830 EXPORT_SYMBOL(obd_debug_level);
831 EXPORT_SYMBOL(obd_dev);
833 EXPORT_SYMBOL(gen_connect);
834 EXPORT_SYMBOL(gen_client);
835 EXPORT_SYMBOL(gen_cleanup);
836 EXPORT_SYMBOL(gen_disconnect);
837 EXPORT_SYMBOL(gen_copy_data);
838 EXPORT_SYMBOL(obdo_cachep);
840 /* EXPORT_SYMBOL(gen_multi_attach); */
841 EXPORT_SYMBOL(gen_multi_setup);
842 EXPORT_SYMBOL(gen_multi_cleanup);
846 int init_module(void)
851 void cleanup_module(void)
856 misc_deregister(&obd_psdev);
857 for (i = 0; i < MAX_OBD_DEVICES; i++) {
858 struct obd_device *obd = &obd_dev[i];
859 if ( obd->obd_type &&
860 (obd->obd_flags & OBD_SET_UP) &&
861 OBT(obd) && OBP(obd, detach) ) {
862 /* XXX should this call generic detach otherwise? */
863 OBP(obd, detach)(obd);
867 obd_cleanup_obdo_cache();
869 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);