1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * An implementation of a loadable kernel mode driver providing
5 * multiple kernel/user space bidirectional communications links.
7 * Author: Alan Cox <alan@cymru.net>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * Adapted to become the Linux 2.0 Coda pseudo device
14 * Peter Braam <braam@maths.ox.ac.uk>
15 * Michael Callahan <mjc@emmy.smith.edu>
17 * Changes for Linux 2.1
18 * Copyright (c) 1997 Carnegie-Mellon University
20 * Redone again for Intermezzo
21 * Copyright (c) 1998 Peter J. Braam
23 * Hacked up again for simulated OBD
24 * Copyright (c) 1999 Stelias Computing, Inc.
25 * (authors {pschwan,braam}@stelias.com)
26 * Copyright (C) 1999 Seagate Technology, Inc.
27 * Copyright (C) 2001 Cluster File Systems, Inc.
33 #include <linux/config.h> /* for CONFIG_PROC_FS */
34 #include <linux/module.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/kmod.h> /* for request_module() */
39 #include <linux/sched.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <linux/delay.h>
45 #include <linux/skbuff.h>
46 #include <linux/proc_fs.h>
48 #include <linux/poll.h>
49 #include <linux/init.h>
50 #include <linux/list.h>
52 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/miscdevice.h>
57 #define DEBUG_SUBSYSTEM S_CLASS
59 #include <linux/obd_support.h>
60 #include <linux/obd_class.h>
62 static int obd_init_magic;
63 int obd_print_entry = 1;
64 int obd_debug_level = ~0;
65 unsigned long obd_memory = 0;
66 struct obd_device obd_dev[MAX_OBD_DEVICES];
67 struct list_head obd_types;
69 /* opening /dev/obd */
70 static int obd_class_open(struct inode * inode, struct file * file)
74 file->private_data = NULL;
80 /* closing /dev/obd */
81 static int obd_class_release(struct inode * inode, struct file * file)
85 if (file->private_data)
86 file->private_data = NULL;
94 * support functions: we could use inter-module communication, but this
95 * is more portable to other OS's
97 static struct obd_type *obd_search_type(char *nm)
99 struct list_head *tmp;
100 struct obd_type *type;
101 CDEBUG(D_INFO, "SEARCH %s\n", nm);
104 while ( (tmp = tmp->next) != &obd_types ) {
105 type = list_entry(tmp, struct obd_type, typ_chain);
106 CDEBUG(D_INFO, "TYP %s\n", type->typ_name);
107 if (strlen(type->typ_name) == strlen(nm) &&
108 strcmp(type->typ_name, nm) == 0 ) {
115 static struct obd_type *obd_nm_to_type(char *nm)
117 struct obd_type *type = obd_search_type(nm);
121 if ( !request_module(nm) ) {
122 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
123 type = obd_search_type(nm);
125 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
132 /* to control /dev/obd */
133 static int obd_class_ioctl (struct inode * inode, struct file * filp,
134 unsigned int cmd, unsigned long arg)
136 /* NOTE this must be larger than any of the ioctl data structs */
138 struct obd_ioctl_data *data;
139 struct obd_device *obd = filp->private_data;
140 struct obd_conn conn;
141 int rw = OBD_BRW_READ;
145 memset(buf, 0, sizeof(buf));
147 if (!obd && cmd != OBD_IOC_DEVICE && cmd != TCGETS) {
148 CERROR("OBD ioctl: No device\n");
151 if (obd_ioctl_getdata(buf, buf + 800, (void *)arg)) {
152 CERROR("OBD ioctl: data error\n");
155 data = (struct obd_ioctl_data *)buf;
162 case OBD_IOC_DEVICE: {
163 CDEBUG(D_IOCTL, "\n");
164 if (data->ioc_dev >= MAX_OBD_DEVICES ||
166 CERROR("OBD ioctl: DEVICE insufficient devices\n");
169 CDEBUG(D_IOCTL, "device %d\n", data->ioc_dev);
171 filp->private_data = &obd_dev[data->ioc_dev];
176 case OBD_IOC_ATTACH: {
177 struct obd_type *type;
180 /* have we attached a type to this device */
181 if ( obd->obd_flags & OBD_ATTACHED ) {
182 CERROR("OBD: Device %d already typed as %s.\n",
183 obd->obd_minor, MKSTR(obd->obd_type->typ_name));
187 CDEBUG(D_IOCTL, "attach %s %s\n", MKSTR(data->ioc_inlbuf1),
188 MKSTR(data->ioc_inlbuf2));
191 type = obd_nm_to_type(data->ioc_inlbuf1);
193 CERROR("OBD: unknown type dev %d\n", obd->obd_minor);
197 obd->obd_type = type;
198 obd->obd_multi_count = 0;
199 INIT_LIST_HEAD(&obd->obd_gen_clients);
200 INIT_LIST_HEAD(&obd->obd_req_list);
203 if ( OBT(obd) && OBP(obd, attach) ) {
204 err = OBP(obd, attach)(obd, sizeof(*data), data);
208 obd->obd_type = NULL;
211 obd->obd_flags |= OBD_ATTACHED;
213 CDEBUG(D_IOCTL, "OBD: dev %d attached type %s\n",
214 obd->obd_minor, data->ioc_inlbuf1);
215 obd->obd_proc_entry =
216 proc_lustre_register_obd_device(obd);
224 case OBD_IOC_DETACH: {
226 if (obd->obd_flags & OBD_SET_UP) {
227 CERROR("OBD device %d still set up\n", obd->obd_minor);
230 if (! (obd->obd_flags & OBD_ATTACHED) ) {
231 CERROR("OBD device %d not attached\n", obd->obd_minor);
234 if ( !list_empty(&obd->obd_gen_clients) ) {
235 CERROR("OBD device %d has connected clients\n",
239 if ( !list_empty(&obd->obd_req_list) ) {
240 CERROR("OBD device %d has hanging requests\n",
245 if (obd->obd_proc_entry)
246 proc_lustre_release_obd_device(obd);
248 obd->obd_flags &= ~OBD_ATTACHED;
249 obd->obd_type->typ_refcnt--;
250 obd->obd_type = NULL;
256 case OBD_IOC_SETUP: {
258 /* have we attached a type to this device? */
259 if (!(obd->obd_flags & OBD_ATTACHED)) {
260 CERROR("Device %d not attached\n", obd->obd_minor);
264 /* has this been done already? */
265 if ( obd->obd_flags & OBD_SET_UP ) {
266 CERROR("Device %d already setup (type %s)\n",
267 obd->obd_minor, obd->obd_type->typ_name);
271 if ( OBT(obd) && OBP(obd, setup) )
272 err = obd_setup(obd, sizeof(*data), data);
275 obd->obd_type->typ_refcnt++;
276 obd->obd_flags |= OBD_SET_UP;
282 case OBD_IOC_CLEANUP: {
285 /* have we attached a type to this device? */
286 if (!(obd->obd_flags & OBD_ATTACHED)) {
287 CERROR("Device %d not attached\n", obd->obd_minor);
291 if ( OBT(obd) && OBP(obd, cleanup) )
292 err = obd_cleanup(obd);
295 obd->obd_flags &= ~OBD_SET_UP;
296 obd->obd_type->typ_refcnt--;
302 case OBD_IOC_CONNECT:
304 conn.oc_id = data->ioc_conn1;
307 err = obd_connect(&conn);
309 CDEBUG(D_IOCTL, "assigned connection %d\n", conn.oc_id);
310 data->ioc_conn1 = conn.oc_id;
314 return copy_to_user((int *)arg, data, sizeof(*data));
317 case OBD_IOC_DISCONNECT: {
318 conn.oc_id = data->ioc_conn1;
321 err = obd_disconnect(&conn);
325 case OBD_IOC_DEC_USE_COUNT: {
330 case OBD_IOC_CREATE: {
331 conn.oc_id = data->ioc_conn1;
334 err = obd_create(&conn, &data->ioc_obdo1);
340 err = copy_to_user((int *)arg, data, sizeof(*data));
345 case OBD_IOC_GETATTR: {
346 conn.oc_id = data->ioc_conn1;
349 err = obd_getattr(&conn, &data->ioc_obdo1);
355 err = copy_to_user((int *)arg, data, sizeof(*data));
360 case OBD_IOC_SETATTR: {
361 conn.oc_id = data->ioc_conn1;
364 err = obd_setattr(&conn, &data->ioc_obdo1);
370 err = copy_to_user((int *)arg, data, sizeof(*data));
375 case OBD_IOC_DESTROY: {
376 conn.oc_id = data->ioc_conn1;
379 err = obd_destroy(&conn, &data->ioc_obdo1);
385 err = copy_to_user((int *)arg, data, sizeof(*data));
390 case OBD_IOC_BRW_WRITE:
392 case OBD_IOC_BRW_READ: {
393 /* FIXME: use a better ioctl data struct than obd_ioctl_data */
394 struct obd_conn conns[2];
395 struct obdo *obdos[2];
396 obd_count oa_bufs[2];
404 oa_bufs[0] = data->ioc_plen1 / PAGE_SIZE;
405 oa_bufs[1] = data->ioc_plen2 / PAGE_SIZE;
406 pages = oa_bufs[0] + oa_bufs[1];
408 CDEBUG(D_INODE, "BRW %s with %d+%d pages\n",
409 rw == OBD_BRW_READ ? "read" : "write",
410 oa_bufs[0], oa_bufs[1]);
411 bufs = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
413 CERROR("no memory for %d BRW bufs\n", pages);
418 obdos[0] = &data->ioc_obdo1;
419 obdos[1] = &data->ioc_obdo2;
422 for (i = 0; i < 2; i++) {
425 conns[i].oc_id = (&data->ioc_conn1)[i];
426 conns[i].oc_dev = obd;
428 from = (&data->ioc_pbuf1)[i];
430 for (j = 0; j < oa_bufs[i]; j++) {
433 to = __get_free_pages(GFP_KERNEL, 0);
436 copy_from_user((void *)to,from,PAGE_SIZE))
439 CERROR("no memory for brw pages\n");
443 bufs[pages++] = virt_to_page(to);
447 counts[i] = data->ioc_count;
448 offsets[i] = data->ioc_offset;
452 err = obd_brw(rw, conns, 2, obdos, oa_bufs, bufs,
453 counts, offsets, flags);
458 free_pages((unsigned long)page_address(bufs[pages]), 0);
464 struct oic_range_s *range = tmp_buf;
469 err = copy_from_user(range, (const void *)arg, sizeof(*range));
476 if ( !OBT(obd) || !OBP(obd, sync) ) {
482 /* XXX sync needs to be tested/verified */
483 err = OBP(obd, sync)(&conn, &range->obdo, range->count,
491 return put_user(err, (int *) arg);
496 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
498 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
504 conn.oc_id = rw_s->conn_id;
506 if ( !OBT(obd) || !OBP(obd, read) ) {
513 err = OBP(obd, read)(&conn, &rw_s->obdo, rw_s->buf,
514 &rw_s->count, rw_s->offset);
516 CDEBUG(D_INFO, "READ: conn %d, count %Ld, offset %Ld, '%s'\n",
517 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
523 err = copy_to_user((int*)arg, &rw_s->count, sizeof(rw_s->count));
528 case OBD_IOC_WRITE: {
529 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
531 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
537 conn.oc_id = rw_s->conn_id;
539 if ( !OBT(obd) || !OBP(obd, write) ) {
544 CDEBUG(D_INFO, "WRITE: conn %d, count %Ld, offset %Ld, '%s'\n",
545 rw_s->conn_id, rw_s->count, rw_s->offset, rw_s->buf);
547 err = OBP(obd, write)(&conn, &rw_s->obdo, rw_s->buf,
548 &rw_s->count, rw_s->offset);
554 err = copy_to_user((int *)arg, &rw_s->count,
555 sizeof(rw_s->count));
559 case OBD_IOC_PREALLOCATE: {
560 struct oic_prealloc_s *prealloc = tmp_buf;
562 /* has this minor been registered? */
566 err = copy_from_user(prealloc, (int *)arg, sizeof(*prealloc));
570 if ( !(obd->obd_flags & OBD_ATTACHED) ||
571 !(obd->obd_flags & OBD_SET_UP)) {
572 CDEBUG(D_IOCTL, "Device not attached or set up\n");
576 if ( !OBT(obd) || !OBP(obd, preallocate) )
579 conn.oc_id = prealloc->conn_id;
580 err = OBP(obd, preallocate)(&conn, &prealloc->alloc,
587 err =copy_to_user((int *)arg, prealloc, sizeof(*prealloc));
591 case OBD_IOC_STATFS: {
593 unsigned int conn_id;
596 /* has this minor been registered? */
600 tmp = (void *)arg + sizeof(unsigned int);
601 get_user(conn_id, (int *) arg);
603 if ( !OBT(obd) || !OBP(obd, statfs) )
606 conn.oc_id = conn_id;
607 err = OBP(obd, statfs)(&conn, &buf);
612 err = copy_to_user(tmp, &buf, sizeof(buf));
618 struct ioc_mv_s *mvdata = tmp_buf;
620 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
621 (!(obd->obd_flags & OBD_ATTACHED))) {
622 CDEBUG(D_IOCTL, "Device not attached or set up\n");
626 /* get main structure */
627 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
633 if ( !OBT(obd) || !OBP(obd, copy) )
636 /* do the partition */
637 CDEBUG(D_INFO, "Copy %d, type %s dst %Ld src %Ld\n", dev,
638 obd->obd_type->typ_name, mvdata->dst.o_id,
641 conn.oc_id = mvdata->src_conn_id;
643 err = OBP(obd, copy)(&conn, &mvdata->dst,
645 mvdata->src.o_size, 0);
650 struct ioc_mv_s *mvdata = tmp_buf;
652 if ( (!(obd->obd_flags & OBD_SET_UP)) ||
653 (!(obd->obd_flags & OBD_ATTACHED))) {
654 CDEBUG(D_IOCTL, "Device not attached or set up\n");
658 err = copy_from_user(mvdata, (void *) arg, sizeof(*mvdata));
664 CDEBUG(D_INFO, "Migrate copying %d bytes\n", sizeof(*mvdata));
666 if ( !OBT(obd) || !OBP(obd, migrate) )
669 /* do the partition */
670 CDEBUG(D_INFO, "Migrate %d, type %s conn %d src %Ld dst %Ld\n",
671 dev, obd->obd_type->typ_name, mvdata->src_conn_id,
672 mvdata->src.o_id, mvdata->dst.o_id);
674 conn.oc_id = mvdata->src_conn_id;
675 err = OBP(obd, migrate)(&conn, &mvdata->dst, &mvdata->src,
676 mvdata->src.o_size, 0);
680 case OBD_IOC_PUNCH: {
681 struct oic_rw_s *rw_s = tmp_buf; /* read, write ioctl str */
683 err = copy_from_user(rw_s, (int *)arg, sizeof(*rw_s));
689 conn.oc_id = rw_s->conn_id;
691 if ( !OBT(obd) || !OBP(obd, punch) ) {
696 CDEBUG(D_INFO, "PUNCH: conn %d, count %Ld, offset %Ld\n",
697 rw_s->conn_id, rw_s->count, rw_s->offset);
698 err = OBP(obd, punch)(&conn, &rw_s->obdo, rw_s->count,
709 struct obd_type *type;
710 struct oic_generic input;
714 /* get data structures */
715 err = copy_from_user(&input, (void *)arg, sizeof(input));
721 err = getdata(input.att_typelen + 1, &input.att_type);
729 type = obd_nm_to_type(nm);
732 if ( !request_module(nm) ) {
733 CDEBUG(D_INFO, "Loaded module '%s'\n", nm);
734 type = obd_nm_to_type(nm);
736 CDEBUG(D_INFO, "Can't load module '%s'\n", nm);
740 OBD_FREE(input.att_type, input.att_typelen + 1);
742 CERROR("unknown obd type dev %d\n", dev);
747 if ( !type->typ_ops || !type->typ_ops->o_iocontrol ) {
751 conn.oc_id = input.att_connid;
753 CDEBUG(D_INFO, "Calling ioctl %x for type %s, len %d\n",
754 cmd, type->typ_name, input.att_datalen);
756 /* get the generic data */
757 karg = input.att_data;
758 err = getdata(input.att_datalen, &karg);
764 err = type->typ_ops->o_iocontrol(cmd, &conn, input.att_datalen,
765 karg, input.att_data);
766 OBD_FREE(karg, input.att_datalen);
776 } /* obd_class_ioctl */
779 /* Driver interface done, utility functions follow */
780 int obd_register_type(struct obd_ops *ops, char *nm)
782 struct obd_type *type;
784 if (obd_init_magic != 0x11223344) {
785 CERROR("bad magic for type\n");
790 if ( obd_nm_to_type(nm) ) {
791 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
796 OBD_ALLOC(type, sizeof(*type));
801 memset(type, 0, sizeof(*type));
802 INIT_LIST_HEAD(&type->typ_chain);
804 list_add(&type->typ_chain, obd_types.next);
811 int obd_unregister_type(char *nm)
813 struct obd_type *type = obd_nm_to_type(nm);
817 CERROR("unknown obd type\n");
822 if ( type->typ_refcnt ) {
824 CERROR("type %s has refcount (%d)\n", nm, type->typ_refcnt);
829 list_del(&type->typ_chain);
830 OBD_FREE(type, sizeof(*type));
833 } /* obd_unregister_type */
835 /* declare character device */
836 static struct file_operations obd_psdev_fops = {
837 ioctl: obd_class_ioctl, /* ioctl */
838 open: obd_class_open, /* open */
839 release: obd_class_release, /* release */
843 #define OBD_MINOR 241
844 static struct miscdevice obd_psdev = {
851 EXPORT_SYMBOL(obd_register_type);
852 EXPORT_SYMBOL(obd_unregister_type);
854 EXPORT_SYMBOL(obd_print_entry);
855 EXPORT_SYMBOL(obd_debug_level);
856 EXPORT_SYMBOL(obd_dev);
858 EXPORT_SYMBOL(gen_connect);
859 EXPORT_SYMBOL(gen_client);
860 EXPORT_SYMBOL(gen_cleanup);
861 EXPORT_SYMBOL(gen_disconnect);
862 EXPORT_SYMBOL(gen_copy_data);
863 EXPORT_SYMBOL(obdo_cachep);
865 /* EXPORT_SYMBOL(gen_multi_attach); */
866 EXPORT_SYMBOL(gen_multi_setup);
867 EXPORT_SYMBOL(gen_multi_cleanup);
868 EXPORT_SYMBOL(obd_memory);
870 static int __init init_obdclass(void)
875 printk(KERN_INFO "OBD class driver v0.01, braam@stelias.com\n");
877 INIT_LIST_HEAD(&obd_types);
879 if ( (err = misc_register(&obd_psdev)) ) {
880 CERROR("cannot register %d err %d\n", OBD_MINOR, err);
884 for (i = 0; i < MAX_OBD_DEVICES; i++) {
885 memset(&(obd_dev[i]), 0, sizeof(obd_dev[i]));
886 obd_dev[i].obd_minor = i;
887 INIT_LIST_HEAD(&obd_dev[i].obd_gen_clients);
888 INIT_LIST_HEAD(&obd_dev[i].obd_req_list);
889 init_waitqueue_head(&obd_dev[i].obd_req_waitq);
892 err = obd_init_obdo_cache();
896 obd_init_magic = 0x11223344;
900 static void __exit cleanup_obdclass(void)
905 misc_deregister(&obd_psdev);
906 for (i = 0; i < MAX_OBD_DEVICES; i++) {
907 struct obd_device *obd = &obd_dev[i];
908 if ( obd->obd_type &&
909 (obd->obd_flags & OBD_SET_UP) &&
910 OBT(obd) && OBP(obd, detach) ) {
911 /* XXX should this call generic detach otherwise? */
912 OBP(obd, detach)(obd);
916 obd_cleanup_obdo_cache();
918 CERROR("obd memory leaked: %ld bytes\n", obd_memory);
923 MODULE_AUTHOR("Cluster File Systems, Inc. <braam@clusterfs.com>");
924 MODULE_DESCRIPTION("Lustre Class Driver v1.0");
925 MODULE_LICENSE("GPL");
927 module_init(init_obdclass);
928 module_exit(cleanup_obdclass);