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 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Adapted to become the Linux 2.0 Coda pseudo device
13 * Peter Braam <braam@maths.ox.ac.uk>
14 * Michael Callahan <mjc@emmy.smith.edu>
16 * Changes for Linux 2.1
17 * Copyright (c) 1997 Carnegie-Mellon University
19 * Redone again for Intermezzo
20 * Copyright (c) 1998 Peter J. Braam
22 * Hacked up again for simulated OBD
23 * Copyright (c) 1999 Stelias Computing, Inc.
24 * (authors {pschwan,braam}@stelias.com)
25 * Copyright (C) 1999 Seagate Technology, Inc.
32 #include <linux/config.h> /* for CONFIG_PROC_FS */
33 #include <linux/module.h>
34 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/sched.h>
39 #include <linux/malloc.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/segment.h>
52 #include <asm/system.h>
54 #include <asm/uaccess.h>
56 #include <linux/obd_support.h>
57 #include <linux/obd_class.h>
59 int obd_print_entry = 1;
60 int obd_debug_level = 4095;
61 struct obd_device obd_dev[MAX_OBD_DEVICES];
62 struct list_head obd_types;
64 static int obd_class_open(struct inode * inode, struct file * file)
71 dev = MINOR(inode->i_rdev);
72 if (dev >= MAX_OBD_DEVICES)
74 obd_dev[dev].refcnt++;
75 CDEBUG(D_PSDEV, "Refcount now %d\n", obd_dev[dev].refcnt++);
82 static int obd_class_release(struct inode * inode, struct file * file)
89 dev = MINOR(inode->i_rdev);
90 if (dev >= MAX_OBD_DEVICES)
92 fsync_dev(inode->i_rdev);
93 if (obd_dev[dev].refcnt <= 0)
94 printk(KERN_ALERT "presto_psdev_release: refcount(%d) <= 0\n",
96 obd_dev[dev].refcnt--;
98 CDEBUG(D_PSDEV, "Refcount now %d\n", obd_dev[dev].refcnt++);
106 static struct obd_type *obd_nm_to_type(char *nm)
108 struct list_head *tmp;
109 struct obd_type *type;
112 while ( (tmp = tmp->next) != &obd_types ) {
113 type = list_entry(tmp, struct obd_type, typ_chain);
114 if (strlen(type->typ_name) == strlen(nm) &&
115 strcmp(type->typ_name, nm) == 0 ) {
122 static int obd_class_ioctl (struct inode * inode, struct file * filp,
123 unsigned int cmd, unsigned long arg)
126 struct obd_device *obddev;
127 struct oic_rw_s rw_s; /* read, write */
128 long int cli_id; /* connect, disconnect */
130 struct oic_prealloc_s prealloc; /* preallocate */
133 dev = MINOR(inode->i_rdev);
134 if (dev > MAX_OBD_DEVICES)
137 obddev = &obd_dev[dev];
139 /* has this minor been registered? */
140 if (cmd != OBD_IOC_SETUP_SUPER && !obd_dev[dev].obd_type)
144 case OBD_IOC_SETUP_SUPER: {
145 struct obd_type *type;
152 if ( obddev->obd_type ) {
153 CDEBUG(D_IOCTL, "Device %d already setup (type %s)\n",
154 dev, obddev->obd_type->typ_name);
158 /* get data structures */
159 if ( (err= copy_from_user(&input, (void *) arg, sizeof(struct setup))) )
162 type = obd_nm_to_type(input.setup_type);
164 CDEBUG(D_IOCTL, "Trying to register non existent type %s\n",
168 obddev->obd_type = type;
170 CDEBUG(D_IOCTL, "Registering %d, type %s\n",
171 dev, input.setup_type);
172 if ( obddev->obd_type->typ_ops->o_setup(obddev,
174 obddev->obd_type = NULL;
177 type->typ_refcount++;
183 case OBD_IOC_CLEANUP_SUPER:
185 /* cleanup has no argument */
186 if ( obddev->obd_type->typ_refcount )
187 obddev->obd_type->typ_refcount--;
189 printk("OBD_CLEANUP: refcount wrap!\n");
191 if ( obddev->obd_type->typ_ops->o_cleanup )
192 return obddev->obd_type->typ_ops->o_cleanup(obddev);
196 case OBD_IOC_CONNECT:
198 struct obd_conn_info conninfo;
200 if (obddev->obd_type->typ_ops->o_connect(dev, &conninfo))
203 return copy_to_user((int *)arg, &conninfo,
204 sizeof(struct obd_conn_info));
206 case OBD_IOC_DISCONNECT:
207 /* frees data structures */
208 get_user(cli_id, (int *) arg);
210 obddev->obd_type->typ_ops->o_disconnect(cli_id);
214 /* sync doesn't need a connection ID, because it knows
215 * what device it was called on, and can thus get the
216 * superblock that it needs. */
217 if (!obddev->sb || !obddev->sb->s_dev) {
218 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
221 if ((err = fsync_dev(obddev->sb->s_dev)))
222 CDEBUG(D_IOCTL, "sync: fsync_dev failure\n");
224 CDEBUG(D_IOCTL, "sync: success\n");
227 return put_user(err, (int *) arg);
229 /* similarly, create doesn't need a connection ID for
230 * the same reasons. */
232 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
233 return put_user(-EINVAL, (int *) arg);
236 i_ino = obddev->obd_type->typ_ops->o_create(obddev, 0, &err);
238 CDEBUG(D_IOCTL, "create: obd_inode_new failure\n");
239 /* 0 is the only error value */
240 return put_user(0, (int *) arg);
243 return put_user(i_ino, (int *) arg);
244 case OBD_IOC_DESTROY:
247 unsigned int conn_id;
250 copy_from_user(&destroy, (int *)arg, sizeof(struct destroy_s));
251 if ( !obddev->obd_type ||
252 !obddev->obd_type->typ_ops->o_destroy)
255 return obddev->obd_type->typ_ops->o_destroy(destroy.conn_id, destroy.ino);
257 case OBD_IOC_SETATTR:
261 unsigned int conn_id;
266 err= copy_from_user(&foo, (int *)arg, sizeof(struct tmp));
270 if ( !obddev->obd_type ||
271 !obddev->obd_type->typ_ops->o_setattr)
274 return obddev->obd_type->typ_ops->o_setattr(foo.conn_id, foo.ino, &foo.iattr);
277 case OBD_IOC_GETATTR:
281 unsigned int conn_id;
285 copy_from_user(&foo, (int *)arg, sizeof(struct tmp));
287 if ( !obddev->obd_type ||
288 !obddev->obd_type->typ_ops->o_getattr)
291 if (obddev->obd_type->typ_ops->o_getattr(foo.conn_id,
295 err = copy_to_user((int *)arg, &iattr, sizeof(iattr));
303 err = copy_from_user(&rw_s, (int *)arg, sizeof(struct oic_rw_s));
307 if ( !obddev->obd_type->typ_ops ||
308 !obddev->obd_type->typ_ops->o_read )
311 rw_s.count = obddev->obd_type->typ_ops->o_read(rw_s.conn_id,
320 err = copy_to_user((int*)arg, &rw_s.count,
321 sizeof(unsigned long));
329 copy_from_user(&rw_s, (int *)arg, sizeof(struct oic_rw_s));
330 CDEBUG(D_IOCTL, "\n");
331 if ( !obddev->obd_type->typ_ops->o_write )
334 obddev->obd_type->typ_ops->o_write(rw_s.conn_id,
341 printk("Result rw_s.count %ld\n", rw_s.count);
342 return (int)rw_s.count;
343 copy_to_user((int *)arg, &rw_s.count,
344 sizeof(unsigned long));
347 case OBD_IOC_PREALLOCATE:
348 copy_from_user(&prealloc, (int *)arg,
349 sizeof(struct oic_prealloc_s));
351 if (!obddev->sb || !obddev->sb->s_dev) {
352 CDEBUG(D_IOCTL, "fatal: device not initialized.\n");
356 if (!obddev->obd_type ||
357 !obddev->obd_type->typ_ops->o_preallocate)
361 obddev->obd_type->typ_ops->o_preallocate(prealloc.cli_id, prealloc.alloc,
362 prealloc.inodes, &err);
365 return copy_to_user((int *)arg, &prealloc,
366 sizeof(struct oic_prealloc_s));
370 unsigned int conn_id;
372 tmp = (void *)arg + sizeof(unsigned int);
373 get_user(conn_id, (int *) arg);
374 if ( !obddev->obd_type ||
375 !obddev->obd_type->typ_ops->o_statfs)
378 return obddev->obd_type->typ_ops->o_statfs(conn_id, tmp);
381 printk("invalid ioctl: cmd = %u, arg = %lu\n", cmd, arg);
386 /* Driver interface done, utility functions follow */
388 int obd_register_type(struct obd_ops *ops, char *nm)
390 struct obd_type *type;
392 if ( obd_nm_to_type(nm) ) {
393 CDEBUG(D_IOCTL, "Type %s already registered\n", nm);
397 OBD_ALLOC(type, struct obd_type * , sizeof(*type));
400 memset(type, 0, sizeof(*type));
401 INIT_LIST_HEAD(&type->typ_chain);
403 list_add(&type->typ_chain, obd_types.next);
409 int obd_unregister_type(char *nm)
411 struct obd_type *type = obd_nm_to_type(nm);
416 if ( type->typ_refcount )
419 list_del(&type->typ_chain);
420 OBD_FREE(type, sizeof(*type));
424 /* declare character device */
425 static struct file_operations obd_psdev_fops = {
429 NULL, /* presto_psdev_readdir */
431 obd_class_ioctl, /* ioctl */
432 NULL, /* presto_psdev_mmap */
433 obd_class_open, /* open */
435 obd_class_release, /* release */
438 NULL, /* check_media_change */
439 NULL, /* revalidate */
450 printk(KERN_INFO "OBD class driver v0.002, braam@stelias.com\n");
452 INIT_LIST_HEAD(&obd_types);
454 if (register_chrdev(OBD_PSDEV_MAJOR,"obd_psdev",
456 printk(KERN_ERR "obd_psdev: unable to get major %d\n",
461 for (i = 0; i < MAX_OBD_DEVICES; i++) {
462 obd_dev[i].obd_type = 0;
463 obd_dev[i].refcnt = 0;
464 obd_dev[i].sb = NULL;
465 obd_dev[i].last_id = 0;
466 INIT_LIST_HEAD(&obd_dev[i].clients);
474 EXPORT_SYMBOL(obd_register_type);
475 EXPORT_SYMBOL(obd_unregister_type);
477 EXPORT_SYMBOL(obd_print_entry);
478 EXPORT_SYMBOL(obd_debug_level);
479 EXPORT_SYMBOL(obd_dev);
482 int init_module(void)
487 void cleanup_module(void)
492 unregister_chrdev(OBD_PSDEV_MAJOR, "obd_psdev");
493 for (i = 0; i < MAX_OBD_DEVICES; i++) {
494 struct obd_device *obddev = &obd_dev[i];
495 if ( obddev->obd_type &&
496 obddev->obd_type->typ_ops->o_cleanup_device )
497 return obddev->obd_type->typ_ops->o_cleanup_device(i);