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 #define DEBUG_SUBSYSTEM S_CLASS
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;
63 unsigned long obd_memory = 0;
64 struct obd_device obd_dev[MAX_OBD_DEVICES];
65 struct list_head obd_types;
67 /* opening /dev/obd */
68 static int obd_class_open(struct inode * inode, struct file * file)
72 file->private_data = NULL;
78 /* closing /dev/obd */
79 static int obd_class_release(struct inode * inode, struct file * file)
83 if (file->private_data)
84 file->private_data = NULL;
92 * support functions: we could use inter-module communication, but this
93 * is more portable to other OS's
95 static struct obd_type *obd_search_type(char *nm)
97 struct list_head *tmp;
98 struct obd_type *type;
99 CDEBUG(D_INFO, "SEARCH %s\n", nm);
102 while ( (tmp = tmp->next) != &obd_types ) {
103 type = list_entry(tmp, struct obd_type, typ_chain);
104 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
105 if (strlen(type->typ_name) == strlen(nm) &&
106 strcmp(type->typ_name, nm) == 0 ) {
113 static struct obd_type *obd_nm_to_type(char *nm)
115 struct obd_type *type = obd_search_type(nm);
119 if ( !request_module(nm) ) {
120 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
121 type = obd_search_type(nm);
123 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
130 /* to control /dev/obd */
131 static int obd_class_ioctl (struct inode * inode, struct file * filp,
132 unsigned int cmd, unsigned long arg)
134 /* NOTE this must be larger than any of the ioctl data structs */
136 struct obd_ioctl_data *data;
137 struct obd_device *obd = filp->private_data;
138 struct obd_conn conn;
142 memset(buf, 0, sizeof(buf));
144 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
145 CERROR("OBD ioctl: No device\n");
148 if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) {
149 CERROR("OBD ioctl: data error\n");
152 data = (struct obd_ioctl_data *)buf;
159 case OBD_IOC_DEVICE: {
160 CDEBUG(D_IOCTL, "\n");
161 if (data->ioc_dev >= MAX_OBD_DEVICES ||
163 CERROR("OBD ioctl: DEVICE insufficient devices\n");
166 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
168 filp->private_data = &obd_dev[data->ioc_dev];
173 case OBD_IOC_ATTACH: {
174 struct obd_type *type;
177 /* have we attached a type to this device */
178 if ( obd->obd_flags & OBD_ATTACHED ) {
179 CERROR("OBD: Device %d already typed as %s.\n",
180 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
184 CDEBUG(D_IOCTL, "attach %s %s\n", MKSTR(data->ioc_inlbuf1),
185 MKSTR(data->ioc_inlbuf2));
188 type = obd_nm_to_type(data->ioc_inlbuf1);
190 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
194 obd->obd_type = type;
195 obd->obd_multi_count = 0;
196 INIT_LIST_HEAD(&obd->obd_gen_clients);
197 INIT_LIST_HEAD(&obd->obd_req_list);
200 if ( OBT(obd) && OBP(obd, attach) ) {
201 err = OBP(obd, attach)(obd, sizeof(*data), data);
205 obd->obd_type = NULL;
208 obd->obd_flags |= OBD_ATTACHED;
210 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
211 obd->obd_minor, data->ioc_inlbuf1);
212 obd->obd_proc_entry =
213 proc_lustre_register_obd_device(obd);
221 case OBD_IOC_DETACH: {
223 if (obd->obd_flags & OBD_SET_UP) {
224 CERROR("OBD device %d still set up\n", obd->obd_minor);
227 if (! (obd->obd_flags & OBD_ATTACHED) ) {
228 CERROR("OBD device %d not attached\n", obd->obd_minor);
231 if ( !list_empty(&obd->obd_gen_clients) ) {
232 CERROR("OBD device %d has connected clients\n",
236 if ( !list_empty(&obd->obd_req_list) ) {
237 CERROR("OBD device %d has hanging requests\n",
242 if (obd->obd_proc_entry)
243 proc_lustre_release_obd_device(obd);
245 obd->obd_flags &= ~OBD_ATTACHED;
246 obd->obd_type->typ_refcnt--;
247 obd->obd_type = NULL;
253 case OBD_IOC_SETUP: {
255 /* have we attached a type to this device? */
256 if (!(obd->obd_flags & OBD_ATTACHED)) {
257 CERROR("Device %d not attached\n", obd->obd_minor);
261 /* has this been done already? */
262 if ( obd->obd_flags & OBD_SET_UP ) {
263 CERROR("Device %d already setup (type %s)\n",
264 obd->obd_minor, obd->obd_type->typ_name);
268 if ( OBT(obd) && OBP(obd, setup) )
269 err = obd_setup(obd, sizeof(*data), data);
272 obd->obd_type->typ_refcnt++;
273 obd->obd_flags |= OBD_SET_UP;
279 case OBD_IOC_CLEANUP: {
282 /* have we attached a type to this device? */
283 if (!(obd->obd_flags & OBD_ATTACHED)) {
284 CERROR("Device %d not attached\n", obd->obd_minor);
288 if ( OBT(obd) && OBP(obd, cleanup) )
289 err = obd_cleanup(obd);
292 obd->obd_flags &= ~OBD_SET_UP;
293 obd->obd_type->typ_refcnt--;
299 case OBD_IOC_CONNECT:
301 conn.oc_id = data->ioc_conn1;
304 err = obd_connect(&conn);
306 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
307 data->ioc_conn1 = conn.oc_id;
311 return copy_to_user((int *)arg, data, sizeof(*data));
314 case OBD_IOC_DISCONNECT: {
315 conn.oc_id = data->ioc_conn1;
318 err = obd_disconnect(&conn);
322 case OBD_IOC_DEC_USE_COUNT: {
327 case OBD_IOC_CREATE: {
328 conn.oc_id = data->ioc_conn1;
331 err = obd_create(&conn, &data->ioc_obdo1);
337 err = copy_to_user((int *)arg, data, sizeof(*data));
342 case OBD_IOC_GETATTR: {
343 conn.oc_id = data->ioc_conn1;
346 err = obd_getattr(&conn, &data->ioc_obdo1);
352 err = copy_to_user((int *)arg, data, sizeof(*data));
357 case OBD_IOC_SETATTR: {
358 conn.oc_id = data->ioc_conn1;
361 err = obd_setattr(&conn, &data->ioc_obdo1);
367 err = copy_to_user((int *)arg, data, sizeof(*data));
372 case OBD_IOC_DESTROY: {
373 conn.oc_id = data->ioc_conn1;
376 err = obd_destroy(&conn, &data->ioc_obdo1);
382 err = copy_to_user((int *)arg, data, sizeof(*data));
389 struct oic_range_s *range = tmp_buf;
394 err = copy_from_user(range, (const void *)arg, sizeof(*range));
401 if ( !OBT(obd) || !OBP(obd, sync) ) {
407 /* XXX sync needs to be tested/verified */
408 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
416 return put_user(err, (int *) arg);
421 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
423 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
429 conn.oc_id = rw_s->conn_id;
431 if ( !OBT(obd) || !OBP(obd, read) ) {
438 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
439 &rw_s->count, rw_s->offset);
441 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
442 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
448 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
453 case OBD_IOC_WRITE: {
454 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
456 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
462 conn.oc_id = rw_s->conn_id;
464 if ( !OBT(obd) || !OBP(obd, write) ) {
469 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
470 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
472 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
473 &rw_s->count, rw_s->offset);
479 err = copy_to_user((int *)arg, &rw_s->count,
480 sizeof(rw_s->count));
484 case OBD_IOC_PREALLOCATE: {
485 struct oic_prealloc_s *prealloc = tmp_buf;
487 /* has this minor been registered? */
491 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
495 if ( !(obd->obd_flags & OBD_ATTACHED) ||
496 !(obd->obd_flags & OBD_SET_UP)) {
497 CDEBUG(D_IOCTL, "Device not attached or set up\n");
501 if ( !OBT(obd) || !OBP(obd, preallocate) )
504 conn.oc_id = prealloc->conn_id;
505 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
512 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
516 case OBD_IOC_STATFS: {
518 unsigned int conn_id;
521 /* has this minor been registered? */
525 tmp = (void *)arg + sizeof(unsigned int);
526 get_user(conn_id, (int *) arg);
528 if ( !OBT(obd) || !OBP(obd, statfs) )
531 conn.oc_id = conn_id;
532 err = OBP(obd, statfs)(&conn, &buf);
537 err = copy_to_user(tmp, &buf, sizeof(buf));
543 struct ioc_mv_s *mvdata = tmp_buf;
545 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
546 (!(obd->obd_flags & OBD_ATTACHED))) {
547 CDEBUG(D_IOCTL, "Device not attached or set up\n");
551 /* get main structure */
552 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
558 if ( !OBT(obd) || !OBP(obd, copy) )
561 /* do the partition */
562 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
563 obd->obd_type->typ_name, mvdata->dst.o_id,
566 conn.oc_id = mvdata->src_conn_id;
568 err = OBP(obd, copy)(&conn, &mvdata->dst,
570 mvdata->src.o_size, 0);
575 struct ioc_mv_s *mvdata = tmp_buf;
577 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
578 (!(obd->obd_flags & OBD_ATTACHED))) {
579 CDEBUG(D_IOCTL, "Device not attached or set up\n");
583 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
589 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
591 if ( !OBT(obd) || !OBP(obd, migrate) )
594 /* do the partition */
595 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
596 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
597 mvdata->src.o_id, mvdata->dst.o_id);
599 conn.oc_id = mvdata->src_conn_id;
600 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
601 mvdata->src.o_size, 0);
605 case OBD_IOC_PUNCH: {
606 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
608 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
614 conn.oc_id = rw_s->conn_id;
616 if ( !OBT(obd) || !OBP(obd, punch) ) {
621 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
622 rw_s->conn_id, rw_s->count, rw_s->offset);
623 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
634 struct obd_type *type;
635 struct oic_generic input;
639 /* get data structures */
640 err = copy_from_user(&input, (void *)arg, sizeof(input));
646 err = getdata(input.att_typelen + 1, &input.att_type);
654 type = obd_nm_to_type(nm);
657 if ( !request_module(nm) ) {
658 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
659 type = obd_nm_to_type(nm);
661 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
665 OBD_FREE(input.att_type, input.att_typelen + 1);
667 CERROR("unknown obd type dev %d\n", dev);
672 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
676 conn.oc_id = input.att_connid;
678 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
679 cmd, type->typ_name, input.att_datalen);
681 /* get the generic data */
682 karg = input.att_data;
683 err = getdata(input.att_datalen, &karg);
689 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
690 karg, input.att_data);
691 OBD_FREE(karg, input.att_datalen);
701 } /* obd_class_ioctl */
704 /* Driver interface done, utility functions follow */
705 int obd_register_type(struct obd_ops *ops, char *nm)
707 struct obd_type *type;
709 if (obd_init_magic != 0x11223344) {
710 CERROR("bad magic for type\n");
715 if ( obd_nm_to_type(nm) ) {
716 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
721 OBD_ALLOC(type, sizeof(*type));
726 memset(type, 0, sizeof(*type));
727 INIT_LIST_HEAD(&type->typ_chain);
729 list_add(&type->typ_chain, obd_types.next);
736 int obd_unregister_type(char *nm)
738 struct obd_type *type = obd_nm_to_type(nm);
742 CERROR("unknown obd type\n");
747 if ( type->typ_refcnt ) {
749 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
754 list_del(&type->typ_chain);
755 OBD_FREE(type, sizeof(*type));
758 } /* obd_unregister_type */
760 /* declare character device */
761 static struct file_operations obd_psdev_fops = {
762 ioctl: obd_class_ioctl, /* ioctl */
763 open: obd_class_open, /* open */
764 release: obd_class_release, /* release */
768 #define OBD_MINOR 241
769 static struct miscdevice obd_psdev = {
776 EXPORT_SYMBOL(obd_register_type);
777 EXPORT_SYMBOL(obd_unregister_type);
779 EXPORT_SYMBOL(obd_print_entry);
780 EXPORT_SYMBOL(obd_debug_level);
781 EXPORT_SYMBOL(obd_dev);
783 EXPORT_SYMBOL(gen_connect);
784 EXPORT_SYMBOL(gen_client);
785 EXPORT_SYMBOL(gen_cleanup);
786 EXPORT_SYMBOL(gen_disconnect);
787 EXPORT_SYMBOL(gen_copy_data);
788 EXPORT_SYMBOL(obdo_cachep);
790 /* EXPORT_SYMBOL(gen_multi_attach); */
791 EXPORT_SYMBOL(gen_multi_setup);
792 EXPORT_SYMBOL(gen_multi_cleanup);
793 EXPORT_SYMBOL(obd_memory);
795 static int __init init_obdclass(void)
800 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
802 INIT_LIST_HEAD(&obd_types);
804 if ( (err = misc_register(&obd_psdev)) ) {
805 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
809 for (i = 0; i < MAX_OBD_DEVICES; i++) {
810 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
811 obd_dev[i].obd_minor = i;
812 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
813 INIT_LIST_HEAD(&obd_dev[i].obd_req_list);
814 init_waitqueue_head(&obd_dev[i].obd_req_waitq);
817 err = obd_init_obdo_cache();
821 obd_init_magic = 0x11223344;
825 static void __exit cleanup_obdclass(void)
830 misc_deregister(&obd_psdev);
831 for (i = 0; i < MAX_OBD_DEVICES; i++) {
832 struct obd_device *obd = &obd_dev[i];
833 if ( obd->obd_type &&
834 (obd->obd_flags & OBD_SET_UP) &&
835 OBT(obd) && OBP(obd, detach) ) {
836 /* XXX should this call generic detach otherwise? */
837 OBP(obd, detach)(obd);
841 obd_cleanup_obdo_cache();
843 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
848 MODULE_AUTHOR("Cluster File Systems, Inc. <braam@clusterfs.com>");
849 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
850 MODULE_LICENSE("GPL");
852 module_init(init_obdclass);
853 module_exit(cleanup_obdclass);