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;
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_PSDEV, "Loaded module '%s'\n", nm);
121 type = obd_search_type(nm);
123 CDEBUG(D_PSDEV, "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);
199 if ( OBT(obd) && OBP(obd, attach) ) {
200 err = OBP(obd, attach)(obd, sizeof(*data), data);
204 obd->obd_type = NULL;
207 obd->obd_flags |= OBD_ATTACHED;
209 CDEBUG(D_IOCTL, "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 CERROR("OBD device %d still set up\n", obd->obd_minor);
226 if (! (obd->obd_flags & OBD_ATTACHED) ) {
227 CERROR("OBD device %d not attached\n", obd->obd_minor);
230 if ( !list_empty(&obd->obd_gen_clients) ) {
231 CERROR("OBD device %d has connected clients\n",
236 if (obd->obd_proc_entry)
237 proc_lustre_release_obd_device(obd);
239 obd->obd_flags &= ~OBD_ATTACHED;
240 obd->obd_type->typ_refcnt--;
241 obd->obd_type = NULL;
247 case OBD_IOC_SETUP: {
249 /* have we attached a type to this device? */
250 if (!(obd->obd_flags & OBD_ATTACHED)) {
251 CERROR("Device %d not attached\n", obd->obd_minor);
255 /* has this been done already? */
256 if ( obd->obd_flags & OBD_SET_UP ) {
257 CERROR("Device %d already setup (type %s)\n",
258 obd->obd_minor, obd->obd_type->typ_name);
262 if ( OBT(obd) && OBP(obd, setup) )
263 err = OBP(obd, setup)(obd, sizeof(*data), data);
266 obd->obd_type->typ_refcnt++;
267 obd->obd_flags |= OBD_SET_UP;
273 case OBD_IOC_CLEANUP: {
276 if ( !(obd->obd_flags & OBD_SET_UP) ) {
281 err = obd_cleanup(obd);
287 obd->obd_flags &= ~OBD_SET_UP;
288 obd->obd_type->typ_refcnt--;
293 case OBD_IOC_CONNECT:
295 conn.oc_id = data->ioc_conn1;
298 err = obd_connect(&conn);
300 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
301 data->ioc_conn1 = conn.oc_id;
305 return copy_to_user((int *)arg, data, sizeof(*data));
308 case OBD_IOC_DISCONNECT: {
309 conn.oc_id = data->ioc_conn1;
312 err = obd_disconnect(&conn);
316 case OBD_IOC_DEC_USE_COUNT: {
321 case OBD_IOC_CREATE: {
322 conn.oc_id = data->ioc_conn1;
325 err = obd_create(&conn, &data->ioc_obdo1);
331 err = copy_to_user((int *)arg, data, sizeof(*data));
336 case OBD_IOC_GETATTR: {
337 conn.oc_id = data->ioc_conn1;
340 err = obd_getattr(&conn, &data->ioc_obdo1);
346 err = copy_to_user((int *)arg, data, sizeof(*data));
351 case OBD_IOC_SETATTR: {
352 conn.oc_id = data->ioc_conn1;
355 err = obd_setattr(&conn, &data->ioc_obdo1);
361 err = copy_to_user((int *)arg, data, sizeof(*data));
366 case OBD_IOC_DESTROY: {
367 conn.oc_id = data->ioc_conn1;
370 err = obd_destroy(&conn, &data->ioc_obdo1);
376 err = copy_to_user((int *)arg, data, sizeof(*data));
383 struct oic_range_s *range = tmp_buf;
388 err = copy_from_user(range, (const void *)arg, sizeof(*range));
395 if ( !OBT(obd) || !OBP(obd, sync) ) {
401 /* XXX sync needs to be tested/verified */
402 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
410 return put_user(err, (int *) arg);
415 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
417 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
423 conn.oc_id = rw_s->conn_id;
425 if ( !OBT(obd) || !OBP(obd, read) ) {
432 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
433 &rw_s->count, rw_s->offset);
436 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
437 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
443 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
448 case OBD_IOC_WRITE: {
449 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
451 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
457 conn.oc_id = rw_s->conn_id;
459 if ( !OBT(obd) || !OBP(obd, write) ) {
464 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
465 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
467 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
468 &rw_s->count, rw_s->offset);
475 err = copy_to_user((int *)arg, &rw_s->count,
476 sizeof(rw_s->count));
480 case OBD_IOC_PREALLOCATE: {
481 struct oic_prealloc_s *prealloc = tmp_buf;
483 /* has this minor been registered? */
487 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
491 if ( !(obd->obd_flags & OBD_ATTACHED) ||
492 !(obd->obd_flags & OBD_SET_UP)) {
493 CDEBUG(D_IOCTL, "Device not attached or set up\n");
497 if ( !OBT(obd) || !OBP(obd, preallocate) )
500 conn.oc_id = prealloc->conn_id;
501 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
508 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
512 case OBD_IOC_STATFS: {
514 unsigned int conn_id;
517 /* has this minor been registered? */
521 tmp = (void *)arg + sizeof(unsigned int);
522 get_user(conn_id, (int *) arg);
524 if ( !OBT(obd) || !OBP(obd, statfs) )
527 conn.oc_id = conn_id;
528 err = OBP(obd, statfs)(&conn, &buf);
533 err = copy_to_user(tmp, &buf, sizeof(buf));
539 struct ioc_mv_s *mvdata = tmp_buf;
541 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
542 (!(obd->obd_flags & OBD_ATTACHED))) {
543 CDEBUG(D_IOCTL, "Device not attached or set up\n");
547 /* get main structure */
548 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
554 if ( !OBT(obd) || !OBP(obd, copy) )
557 /* do the partition */
558 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
559 obd->obd_type->typ_name, mvdata->dst.o_id,
562 conn.oc_id = mvdata->src_conn_id;
564 err = OBP(obd, copy)(&conn, &mvdata->dst,
566 mvdata->src.o_size, 0);
571 struct ioc_mv_s *mvdata = tmp_buf;
573 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
574 (!(obd->obd_flags & OBD_ATTACHED))) {
575 CDEBUG(D_IOCTL, "Device not attached or set up\n");
579 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
585 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
587 if ( !OBT(obd) || !OBP(obd, migrate) )
590 /* do the partition */
591 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
592 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
593 mvdata->src.o_id, mvdata->dst.o_id);
595 conn.oc_id = mvdata->src_conn_id;
596 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
597 mvdata->src.o_size, 0);
601 case OBD_IOC_PUNCH: {
602 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
604 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
610 conn.oc_id = rw_s->conn_id;
612 if ( !OBT(obd) || !OBP(obd, punch) ) {
617 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
618 rw_s->conn_id, rw_s->count, rw_s->offset);
619 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
631 struct obd_type *type;
632 struct oic_generic input;
636 /* get data structures */
637 err = copy_from_user(&input, (void *)arg, sizeof(input));
643 err = getdata(input.att_typelen + 1, &input.att_type);
651 type = obd_nm_to_type(nm);
654 if ( !request_module(nm) ) {
655 CDEBUG(D_PSDEV, "Loaded module '%s'\n", nm);
656 type = obd_nm_to_type(nm);
658 CDEBUG(D_PSDEV, "Can't load module '%s'\n", nm);
662 OBD_FREE(input.att_type, input.att_typelen + 1);
664 CERROR("unknown obd type dev %d\n", dev);
669 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
673 conn.oc_id = input.att_connid;
675 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
676 cmd, type->typ_name, input.att_datalen);
678 /* get the generic data */
679 karg = input.att_data;
680 err = getdata(input.att_datalen, &karg);
686 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
687 karg, input.att_data);
688 OBD_FREE(karg, input.att_datalen);
698 } /* obd_class_ioctl */
701 /* Driver interface done, utility functions follow */
702 int obd_register_type(struct obd_ops *ops, char *nm)
704 struct obd_type *type;
706 if (obd_init_magic != 0x11223344) {
707 CERROR("bad magic for type\n");
712 if ( obd_nm_to_type(nm) ) {
713 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
718 OBD_ALLOC(type, sizeof(*type));
723 memset(type, 0, sizeof(*type));
724 INIT_LIST_HEAD(&type->typ_chain);
726 list_add(&type->typ_chain, obd_types.next);
733 int obd_unregister_type(char *nm)
735 struct obd_type *type = obd_nm_to_type(nm);
739 CERROR("unknown obd type\n");
744 if ( type->typ_refcnt ) {
746 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
751 list_del(&type->typ_chain);
752 OBD_FREE(type, sizeof(*type));
755 } /* obd_unregister_type */
757 /* declare character device */
758 static struct file_operations obd_psdev_fops = {
759 ioctl: obd_class_ioctl, /* ioctl */
760 open: obd_class_open, /* open */
761 release: obd_class_release, /* release */
765 #define OBD_MINOR 241
766 static struct miscdevice obd_psdev = {
777 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
779 INIT_LIST_HEAD(&obd_types);
781 if ( (err = misc_register(&obd_psdev)) ) {
782 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
786 for (i = 0; i < MAX_OBD_DEVICES; i++) {
787 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
788 obd_dev[i].obd_minor = i;
789 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
792 err = obd_init_obdo_cache();
796 obd_init_magic = 0x11223344;
800 EXPORT_SYMBOL(obd_register_type);
801 EXPORT_SYMBOL(obd_unregister_type);
803 EXPORT_SYMBOL(obd_print_entry);
804 EXPORT_SYMBOL(obd_debug_level);
805 EXPORT_SYMBOL(obd_dev);
807 EXPORT_SYMBOL(gen_connect);
808 EXPORT_SYMBOL(gen_client);
809 EXPORT_SYMBOL(gen_cleanup);
810 EXPORT_SYMBOL(gen_disconnect);
811 EXPORT_SYMBOL(gen_copy_data);
812 EXPORT_SYMBOL(obdo_cachep);
814 /* EXPORT_SYMBOL(gen_multi_attach); */
815 EXPORT_SYMBOL(gen_multi_setup);
816 EXPORT_SYMBOL(gen_multi_cleanup);
817 EXPORT_SYMBOL(obd_memory);
820 int init_module(void)
825 void cleanup_module(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 CDEBUG(D_MALLOC, "CLASS mem used %ld\n", obd_memory);