"Fossies" - the Fresh Open Source Software Archive

Member "linux-coda-6.9/linux2.4/psdev.c" (1 Dec 2009, 11098 Bytes) of package /linux/misc/old/linux-coda-6.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *          An implementation of a loadable kernel mode driver providing
    3  *      multiple kernel/user space bidirectional communications links.
    4  *
    5  *      Author:     Alan Cox <alan@redhat.com>
    6  *
    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.
   11  * 
   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>           
   15  *
   16  *              Changes for Linux 2.1
   17  *              Copyright (c) 1997 Carnegie-Mellon University
   18  */
   19 
   20 #include <linux/module.h>
   21 #include <linux/errno.h>
   22 #include <linux/kernel.h>
   23 #include <linux/major.h>
   24 #include <linux/sched.h>
   25 #include <linux/lp.h>
   26 #include <linux/slab.h>
   27 #include <linux/ioport.h>
   28 #include <linux/fcntl.h>
   29 #include <linux/delay.h>
   30 #include <linux/skbuff.h>
   31 #include <linux/proc_fs.h>
   32 #include <linux/devfs_fs_kernel.h>
   33 #include <linux/vmalloc.h>
   34 #include <linux/fs.h>
   35 #include <linux/file.h>
   36 #include <linux/poll.h>
   37 #include <linux/init.h>
   38 #include <linux/list.h>
   39 #include <linux/smp_lock.h>
   40 #include <asm/io.h>
   41 #include <asm/system.h>
   42 #include <asm/poll.h>
   43 #include <asm/uaccess.h>
   44 
   45 #include <linux/coda.h>
   46 #include <linux/coda_linux.h>
   47 #include <linux/coda_fs_i.h>
   48 #include <linux/coda_psdev.h>
   49 #include <linux/coda_proc.h>
   50 
   51 #define upc_free(r) kfree(r)
   52 
   53 /* 
   54  * Coda stuff
   55  */
   56 extern struct file_system_type coda_fs_type;
   57 
   58 /* statistics */
   59 int           coda_hard;         /* allows signals during upcalls */
   60 unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
   61 
   62 
   63 struct venus_comm coda_comms[MAX_CODADEVS];
   64 kmem_cache_t *cii_cache, *cred_cache, *upc_cache;
   65 
   66 /*
   67  * Device operations
   68  */
   69 
   70 static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
   71 {
   72         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
   73     unsigned int mask = POLLOUT | POLLWRNORM;
   74 
   75     poll_wait(file, &vcp->vc_waitq, wait);
   76     if (!list_empty(&vcp->vc_pending))
   77                 mask |= POLLIN | POLLRDNORM;
   78 
   79     return mask;
   80 }
   81 
   82 static int coda_psdev_ioctl(struct inode * inode, struct file * filp, 
   83                 unsigned int cmd, unsigned long arg)
   84 {
   85     unsigned int data;
   86 
   87     switch(cmd) {
   88     case CIOC_KERNEL_VERSION:
   89         data = CODA_KERNEL_VERSION;
   90         return put_user(data, (int *) arg);
   91     default:
   92         return -ENOTTY;
   93     }
   94 
   95     return 0;
   96 }
   97 
   98 /*
   99  *  Receive a message written by Venus to the psdev
  100  */
  101  
  102 static ssize_t coda_psdev_write(struct file *file, const char *buf, 
  103                 size_t nbytes, loff_t *off)
  104 {
  105         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  106         struct upc_req *req = NULL;
  107         struct upc_req *tmp;
  108     struct list_head *lh;
  109     struct coda_in_hdr hdr;
  110     ssize_t retval = 0, count = 0;
  111     int error;
  112 
  113         /* Peek at the opcode, uniquefier */
  114     if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
  115             return -EFAULT;
  116 
  117     CDEBUG(D_PSDEV, "(process,opc,uniq)=(%d,%d,%d), nbytes %ld\n", 
  118            current->pid, hdr.opcode, hdr.unique, (long)nbytes);
  119 
  120         if (DOWNCALL(hdr.opcode)) {
  121         struct super_block *sb = NULL;
  122                 union outputArgs *dcbuf;
  123         int size = sizeof(*dcbuf);
  124 
  125         sb = vcp->vc_sb;
  126         if ( !sb ) {
  127             CDEBUG(D_PSDEV, "coda_psdev_write: downcall, no SB!\n");
  128                         count = nbytes;
  129                         goto out;
  130         }
  131         CDEBUG(D_PSDEV, "handling downcall\n");
  132 
  133         if  ( nbytes < sizeof(struct coda_out_hdr) ) {
  134                 printk("coda_downcall opc %d uniq %d, not enough!\n",
  135                    hdr.opcode, hdr.unique);
  136             count = nbytes;
  137             goto out;
  138         }
  139         if ( nbytes > size ) {
  140                 printk("Coda: downcall opc %d, uniq %d, too much!",
  141                    hdr.opcode, hdr.unique);
  142                 nbytes = size;
  143         }
  144         CODA_ALLOC(dcbuf, union outputArgs *, nbytes);
  145         if (copy_from_user(dcbuf, buf, nbytes)) {
  146             CODA_FREE(dcbuf, nbytes);
  147             retval = -EFAULT;
  148             goto out;
  149         }
  150 
  151         /* what downcall errors does Venus handle ? */
  152         lock_kernel();
  153         error = coda_downcall(hdr.opcode, dcbuf, sb);
  154         unlock_kernel();
  155 
  156         CODA_FREE(dcbuf, nbytes);
  157         if (error) {
  158                 printk("psdev_write: coda_downcall error: %d\n", error);
  159             retval = error;
  160             goto out;
  161         }
  162         count = nbytes;
  163         goto out;
  164     }
  165         
  166     /* Look for the message on the processing queue. */
  167     lock_kernel();
  168     list_for_each(lh, &vcp->vc_processing) {
  169         tmp = list_entry(lh, struct upc_req , uc_chain);
  170         if (tmp->uc_unique == hdr.unique) {
  171             req = tmp;
  172             list_del(&req->uc_chain);
  173             break;
  174         }
  175     }
  176     unlock_kernel();
  177 
  178     if (!req) {
  179         printk("psdev_write: msg (%d, %d) not found\n", 
  180             hdr.opcode, hdr.unique);
  181         retval = -ESRCH;
  182         goto out;
  183     }
  184 
  185     CDEBUG(D_PSDEV,"Eureka: uniq %d on queue!\n", hdr.unique);
  186 
  187         /* move data into response buffer. */
  188     if (req->uc_outSize < nbytes) {
  189                 printk("psdev_write: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
  190                req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique);
  191         nbytes = req->uc_outSize; /* don't have more space! */
  192     }
  193         if (copy_from_user(req->uc_data, buf, nbytes)) {
  194         req->uc_flags |= REQ_ABORT;
  195         wake_up(&req->uc_sleep);
  196         retval = -EFAULT;
  197         goto out;
  198     }
  199 
  200     /* adjust outsize. is this useful ?? */
  201         req->uc_outSize = nbytes;   
  202         req->uc_flags |= REQ_WRITE;
  203     count = nbytes;
  204 
  205     /* Convert filedescriptor into a file handle */
  206     if (req->uc_opcode == CODA_OPEN_BY_FD) {
  207         struct coda_open_by_fd_out *outp =
  208             (struct coda_open_by_fd_out *)req->uc_data;
  209         outp->fh = outp->oh.result ? NULL : fget(outp->fd);
  210     }
  211 
  212     CDEBUG(D_PSDEV, 
  213            "Found! Count %ld for (opc,uniq)=(%d,%d), upc_req at %p\n", 
  214             (long)count, hdr.opcode, hdr.unique, &req);
  215 
  216         wake_up(&req->uc_sleep);
  217 out:
  218         return(count ? count : retval);  
  219 }
  220 
  221 /*
  222  *  Read a message from the kernel to Venus
  223  */
  224 
  225 static ssize_t coda_psdev_read(struct file * file, char * buf, 
  226                    size_t nbytes, loff_t *off)
  227 {
  228     DECLARE_WAITQUEUE(wait, current);
  229         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  230         struct upc_req *req;
  231     ssize_t retval = 0, count = 0;
  232 
  233     if (nbytes == 0)
  234         return 0;
  235 
  236     lock_kernel();
  237 
  238     add_wait_queue(&vcp->vc_waitq, &wait);
  239     set_current_state(TASK_INTERRUPTIBLE);
  240 
  241     while (list_empty(&vcp->vc_pending)) {
  242         if (file->f_flags & O_NONBLOCK) {
  243             retval = -EAGAIN;
  244             break;
  245         }
  246         if (signal_pending(current)) {
  247             retval = -ERESTARTSYS;
  248             break;
  249         }
  250         schedule();
  251     }
  252 
  253     set_current_state(TASK_RUNNING);
  254     remove_wait_queue(&vcp->vc_waitq, &wait);
  255 
  256     if (retval)
  257         goto out;
  258 
  259     req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
  260     list_del(&req->uc_chain);
  261 
  262     /* Move the input args into userspace */
  263     count = req->uc_inSize;
  264     if (nbytes < req->uc_inSize) {
  265                 printk ("psdev_read: Venus read %ld bytes of %d in message\n",
  266             (long)nbytes, req->uc_inSize);
  267         count = nbytes;
  268         }
  269 
  270     if (copy_to_user(buf, req->uc_data, count))
  271             retval = -EFAULT;
  272         
  273     /* If request was not a signal, enqueue and don't free */
  274     if (!(req->uc_flags & REQ_ASYNC)) {
  275         req->uc_flags |= REQ_READ;
  276         list_add(&(req->uc_chain), vcp->vc_processing.prev);
  277         goto out;
  278     }
  279 
  280     CDEBUG(D_PSDEV, "vcread: signal msg (%d, %d)\n", 
  281             req->uc_opcode, req->uc_unique);
  282 
  283     CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
  284     upc_free(req);
  285 out:
  286     unlock_kernel();
  287     return (count ? count : retval);
  288 }
  289 
  290 static int coda_psdev_open(struct inode * inode, struct file * file)
  291 {
  292         struct venus_comm *vcp;
  293     int idx;
  294 
  295     lock_kernel();
  296     idx = MINOR(inode->i_rdev);
  297     if(idx >= MAX_CODADEVS) {
  298         unlock_kernel();
  299         return -ENODEV;
  300     }
  301 
  302     vcp = &coda_comms[idx];
  303     if(vcp->vc_inuse) {
  304         unlock_kernel();
  305         return -EBUSY;
  306     }
  307     
  308     if (!vcp->vc_inuse++) {
  309         INIT_LIST_HEAD(&vcp->vc_pending);
  310         INIT_LIST_HEAD(&vcp->vc_processing);
  311         init_waitqueue_head(&vcp->vc_waitq);
  312         vcp->vc_sb = 0;
  313         vcp->vc_seq = 0;
  314     }
  315     
  316     file->private_data = vcp;
  317 
  318     CDEBUG(D_PSDEV, "device %i - inuse: %d\n", idx, vcp->vc_inuse);
  319 
  320     unlock_kernel();
  321         return 0;
  322 }
  323 
  324 
  325 static int coda_psdev_release(struct inode * inode, struct file * file)
  326 {
  327         struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  328         struct upc_req *req;
  329     struct list_head *lh, *next;
  330 
  331     lock_kernel();
  332     if ( !vcp->vc_inuse ) {
  333         unlock_kernel();
  334         printk("psdev_release: Not open.\n");
  335         return -1;
  336     }
  337 
  338     CDEBUG(D_PSDEV, "psdev_release: inuse %d\n", vcp->vc_inuse);
  339     if (--vcp->vc_inuse) {
  340         unlock_kernel();
  341         return 0;
  342     }
  343         
  344         /* Wakeup clients so they can return. */
  345     CDEBUG(D_PSDEV, "wake up pending clients\n");
  346     lh = &vcp->vc_pending;
  347     next = lh->next;
  348     while ( (lh = next) != &vcp->vc_pending) {
  349         next = lh->next;
  350         list_del(lh);
  351         req = list_entry(lh, struct upc_req, uc_chain);
  352         /* Async requests need to be freed here */
  353         if (req->uc_flags & REQ_ASYNC) {
  354             CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
  355             upc_free(req);
  356             continue;
  357         }
  358         req->uc_flags |= REQ_ABORT;
  359         wake_up(&req->uc_sleep);
  360         }
  361         
  362     lh = &vcp->vc_processing;
  363     next = lh->next;
  364     CDEBUG(D_PSDEV, "wake up processing clients\n");
  365     while ( (lh = next) != &vcp->vc_processing) {
  366         next = lh->next;
  367         list_del(lh);
  368         req = list_entry(lh, struct upc_req, uc_chain);
  369         req->uc_flags |= REQ_ABORT;
  370             wake_up(&req->uc_sleep);
  371         }
  372     CDEBUG(D_PSDEV, "Done.\n");
  373 
  374     unlock_kernel();
  375     return 0;
  376 }
  377 
  378 
  379 static struct file_operations coda_psdev_fops = {
  380     owner:      THIS_MODULE,
  381     read:       coda_psdev_read,
  382     write:      coda_psdev_write,
  383     poll:       coda_psdev_poll,
  384     ioctl:      coda_psdev_ioctl,
  385     open:       coda_psdev_open,
  386     release:    coda_psdev_release,
  387 };
  388 
  389 static devfs_handle_t devfs_handle;
  390 
  391 static int init_coda_psdev(void)
  392 {
  393     if(devfs_register_chrdev(CODA_PSDEV_MAJOR,"coda_psdev",
  394                  &coda_psdev_fops)) {
  395               printk(KERN_ERR "coda_psdev: unable to get major %d\n", 
  396              CODA_PSDEV_MAJOR);
  397               return -EIO;
  398     }
  399     devfs_handle = devfs_mk_dir (NULL, "coda", NULL);
  400     devfs_register_series (devfs_handle, "%u", MAX_CODADEVS, DEVFS_FL_NONE,
  401                    CODA_PSDEV_MAJOR, 0,
  402                    S_IFCHR | S_IRUSR | S_IWUSR,
  403                    &coda_psdev_fops, NULL);
  404 
  405     coda_sysctl_init();
  406 
  407     return 0;
  408 }
  409 
  410 
  411 MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
  412 MODULE_LICENSE("GPL");
  413 
  414 static int __init init_coda(void)
  415 {
  416     int status;
  417     printk(KERN_INFO "Coda Kernel/Venus communications, v6.1, coda@cs.cmu.edu\n");
  418 
  419     status = init_coda_psdev();
  420     if ( status ) {
  421         printk("Problem (%d) in init_coda_psdev\n", status);
  422         return status;
  423     }
  424     
  425     status = register_filesystem(&coda_fs_type);
  426     if (status) {
  427         printk("coda: failed to register filesystem!\n");
  428         devfs_unregister(devfs_handle);
  429         devfs_unregister_chrdev(CODA_PSDEV_MAJOR,"coda_psdev");
  430         coda_sysctl_clean();
  431     }
  432     return status;
  433 }
  434 
  435 static void __exit exit_coda(void)
  436 {
  437         int err;
  438 
  439     err = unregister_filesystem(&coda_fs_type);
  440         if ( err != 0 ) {
  441                 printk("coda: failed to unregister filesystem\n");
  442         }
  443     devfs_unregister(devfs_handle);
  444     devfs_unregister_chrdev(CODA_PSDEV_MAJOR, "coda_psdev");
  445     coda_sysctl_clean();
  446 }
  447 
  448 module_init(init_coda);
  449 module_exit(exit_coda);
  450