fs: dcache scale dentry refcount
[linux-2.6.git] / fs / coda / psdev.c
index 3d1cce3..62647a8 100644 (file)
@@ -2,7 +2,7 @@
  *             An implementation of a loadable kernel mode driver providing
  *             multiple kernel/user space bidirectional communications links.
  *
- *             Author:         Alan Cox <alan@redhat.com>
+ *             Author:         Alan Cox <alan@lxorguk.ukuu.org.uk>
  *
  *             This program is free software; you can redistribute it and/or
  *             modify it under the terms of the GNU General Public License
 #include <linux/kernel.h>
 #include <linux/major.h>
 #include <linux/time.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/ioport.h>
 #include <linux/fcntl.h>
 #include <linux/delay.h>
 #include <linux/skbuff.h>
 #include <linux/proc_fs.h>
-#include <linux/devfs_fs_kernel.h>
 #include <linux/vmalloc.h>
 #include <linux/fs.h>
 #include <linux/file.h>
 #include <linux/poll.h>
 #include <linux/init.h>
 #include <linux/list.h>
-#include <linux/smp_lock.h>
+#include <linux/mutex.h>
 #include <linux/device.h>
 #include <asm/io.h>
 #include <asm/system.h>
 #include <linux/coda_linux.h>
 #include <linux/coda_fs_i.h>
 #include <linux/coda_psdev.h>
-#include <linux/coda_proc.h>
 
-#define upc_free(r) kfree(r)
-
-/* 
- * Coda stuff
- */
-extern struct file_system_type coda_fs_type;
+#include "coda_int.h"
 
 /* statistics */
 int           coda_hard;         /* allows signals during upcalls */
@@ -73,14 +67,15 @@ static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
        unsigned int mask = POLLOUT | POLLWRNORM;
 
        poll_wait(file, &vcp->vc_waitq, wait);
+       mutex_lock(&vcp->vc_mutex);
        if (!list_empty(&vcp->vc_pending))
                 mask |= POLLIN | POLLRDNORM;
+       mutex_unlock(&vcp->vc_mutex);
 
        return mask;
 }
 
-static int coda_psdev_ioctl(struct inode * inode, struct file * filp, 
-                           unsigned int cmd, unsigned long arg)
+static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg)
 {
        unsigned int data;
 
@@ -115,16 +110,9 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
                return -EFAULT;
 
         if (DOWNCALL(hdr.opcode)) {
-               struct super_block *sb = NULL;
-                union outputArgs *dcbuf;
+               union outputArgs *dcbuf;
                int size = sizeof(*dcbuf);
 
-               sb = vcp->vc_sb;
-               if ( !sb ) {
-                        count = nbytes;
-                        goto out;
-               }
-
                if  ( nbytes < sizeof(struct coda_out_hdr) ) {
                        printk("coda_downcall opc %d uniq %d, not enough!\n",
                               hdr.opcode, hdr.unique);
@@ -144,9 +132,7 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
                }
 
                /* what downcall errors does Venus handle ? */
-               lock_kernel();
-               error = coda_downcall(hdr.opcode, dcbuf, sb);
-               unlock_kernel();
+               error = coda_downcall(vcp, hdr.opcode, dcbuf);
 
                CODA_FREE(dcbuf, nbytes);
                if (error) {
@@ -159,7 +145,7 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
        }
         
        /* Look for the message on the processing queue. */
-       lock_kernel();
+       mutex_lock(&vcp->vc_mutex);
        list_for_each(lh, &vcp->vc_processing) {
                tmp = list_entry(lh, struct upc_req , uc_chain);
                if (tmp->uc_unique == hdr.unique) {
@@ -168,7 +154,7 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
                        break;
                }
        }
-       unlock_kernel();
+       mutex_unlock(&vcp->vc_mutex);
 
        if (!req) {
                printk("psdev_write: msg (%d, %d) not found\n", 
@@ -184,22 +170,23 @@ static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
                nbytes = req->uc_outSize; /* don't have more space! */
        }
         if (copy_from_user(req->uc_data, buf, nbytes)) {
-               req->uc_flags |= REQ_ABORT;
+               req->uc_flags |= CODA_REQ_ABORT;
                wake_up(&req->uc_sleep);
                retval = -EFAULT;
                goto out;
        }
 
        /* adjust outsize. is this useful ?? */
-        req->uc_outSize = nbytes;      
-        req->uc_flags |= REQ_WRITE;
+       req->uc_outSize = nbytes;
+       req->uc_flags |= CODA_REQ_WRITE;
        count = nbytes;
 
        /* Convert filedescriptor into a file handle */
        if (req->uc_opcode == CODA_OPEN_BY_FD) {
                struct coda_open_by_fd_out *outp =
                        (struct coda_open_by_fd_out *)req->uc_data;
-               outp->fh = fget(outp->fd);
+               if (!outp->oh.result)
+                       outp->fh = fget(outp->fd);
        }
 
         wake_up(&req->uc_sleep);
@@ -222,7 +209,7 @@ static ssize_t coda_psdev_read(struct file * file, char __user * buf,
        if (nbytes == 0)
                return 0;
 
-       lock_kernel();
+       mutex_lock(&vcp->vc_mutex);
 
        add_wait_queue(&vcp->vc_waitq, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
@@ -236,7 +223,9 @@ static ssize_t coda_psdev_read(struct file * file, char __user * buf,
                        retval = -ERESTARTSYS;
                        break;
                }
+               mutex_unlock(&vcp->vc_mutex);
                schedule();
+               mutex_lock(&vcp->vc_mutex);
        }
 
        set_current_state(TASK_RUNNING);
@@ -260,99 +249,99 @@ static ssize_t coda_psdev_read(struct file * file, char __user * buf,
                retval = -EFAULT;
         
        /* If request was not a signal, enqueue and don't free */
-       if (!(req->uc_flags & REQ_ASYNC)) {
-               req->uc_flags |= REQ_READ;
-               list_add(&(req->uc_chain), vcp->vc_processing.prev);
+       if (!(req->uc_flags & CODA_REQ_ASYNC)) {
+               req->uc_flags |= CODA_REQ_READ;
+               list_add_tail(&(req->uc_chain), &vcp->vc_processing);
                goto out;
        }
 
        CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
-       upc_free(req);
+       kfree(req);
 out:
-       unlock_kernel();
+       mutex_unlock(&vcp->vc_mutex);
        return (count ? count : retval);
 }
 
 static int coda_psdev_open(struct inode * inode, struct file * file)
 {
-        struct venus_comm *vcp;
-       int idx;
+       struct venus_comm *vcp;
+       int idx, err;
 
-       lock_kernel();
        idx = iminor(inode);
-       if(idx >= MAX_CODADEVS) {
-               unlock_kernel();
+       if (idx < 0 || idx >= MAX_CODADEVS)
                return -ENODEV;
-       }
 
+       err = -EBUSY;
        vcp = &coda_comms[idx];
-       if(vcp->vc_inuse) {
-               unlock_kernel();
-               return -EBUSY;
-       }
-       
-       if (!vcp->vc_inuse++) {
+       mutex_lock(&vcp->vc_mutex);
+
+       if (!vcp->vc_inuse) {
+               vcp->vc_inuse++;
+
                INIT_LIST_HEAD(&vcp->vc_pending);
                INIT_LIST_HEAD(&vcp->vc_processing);
                init_waitqueue_head(&vcp->vc_waitq);
                vcp->vc_sb = NULL;
                vcp->vc_seq = 0;
+
+               file->private_data = vcp;
+               err = 0;
        }
-       
-       file->private_data = vcp;
 
-       unlock_kernel();
-        return 0;
+       mutex_unlock(&vcp->vc_mutex);
+       return err;
 }
 
 
 static int coda_psdev_release(struct inode * inode, struct file * file)
 {
-        struct venus_comm *vcp = (struct venus_comm *) file->private_data;
-        struct upc_req *req, *tmp;
+       struct venus_comm *vcp = (struct venus_comm *) file->private_data;
+       struct upc_req *req, *tmp;
 
-       lock_kernel();
-       if ( !vcp->vc_inuse ) {
-               unlock_kernel();
+       if (!vcp || !vcp->vc_inuse ) {
                printk("psdev_release: Not open.\n");
                return -1;
        }
 
-       if (--vcp->vc_inuse) {
-               unlock_kernel();
-               return 0;
-       }
-        
-        /* Wakeup clients so they can return. */
+       mutex_lock(&vcp->vc_mutex);
+
+       /* Wakeup clients so they can return. */
        list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
+               list_del(&req->uc_chain);
+
                /* Async requests need to be freed here */
-               if (req->uc_flags & REQ_ASYNC) {
+               if (req->uc_flags & CODA_REQ_ASYNC) {
                        CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
-                       upc_free(req);
+                       kfree(req);
                        continue;
                }
-               req->uc_flags |= REQ_ABORT;
+               req->uc_flags |= CODA_REQ_ABORT;
                wake_up(&req->uc_sleep);
-        }
-        
-       list_for_each_entry(req, &vcp->vc_processing, uc_chain) {
-               req->uc_flags |= REQ_ABORT;
-               wake_up(&req->uc_sleep);
-        }
+       }
+
+       list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) {
+               list_del(&req->uc_chain);
+
+               req->uc_flags |= CODA_REQ_ABORT;
+               wake_up(&req->uc_sleep);
+       }
 
-       unlock_kernel();
+       file->private_data = NULL;
+       vcp->vc_inuse--;
+       mutex_unlock(&vcp->vc_mutex);
        return 0;
 }
 
 
-static struct file_operations coda_psdev_fops = {
+static const struct file_operations coda_psdev_fops = {
        .owner          = THIS_MODULE,
        .read           = coda_psdev_read,
        .write          = coda_psdev_write,
        .poll           = coda_psdev_poll,
-       .ioctl          = coda_psdev_ioctl,
+       .unlocked_ioctl = coda_psdev_ioctl,
        .open           = coda_psdev_open,
        .release        = coda_psdev_release,
+       .llseek         = noop_llseek,
 };
 
 static int init_coda_psdev(void)
@@ -368,45 +357,30 @@ static int init_coda_psdev(void)
                err = PTR_ERR(coda_psdev_class);
                goto out_chrdev;
        }               
-       devfs_mk_dir ("coda");
        for (i = 0; i < MAX_CODADEVS; i++) {
-               class_device_create(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR,i),
-                               NULL, "cfs%d", i);
-               err = devfs_mk_cdev(MKDEV(CODA_PSDEV_MAJOR, i),
-                               S_IFCHR|S_IRUSR|S_IWUSR, "coda/%d", i);
-               if (err)
-                       goto out_class;
+               mutex_init(&(&coda_comms[i])->vc_mutex);
+               device_create(coda_psdev_class, NULL,
+                             MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i);
        }
        coda_sysctl_init();
        goto out;
 
-out_class:
-       for (i = 0; i < MAX_CODADEVS; i++) 
-               class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
-       class_destroy(coda_psdev_class);
 out_chrdev:
        unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
 out:
        return err;
 }
 
-
-MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
+MODULE_AUTHOR("Jan Harkes, Peter J. Braam");
+MODULE_DESCRIPTION("Coda Distributed File System VFS interface");
+MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR);
 MODULE_LICENSE("GPL");
+MODULE_VERSION("6.6");
 
-extern int coda_init_inodecache(void);
-extern void coda_destroy_inodecache(void);
 static int __init init_coda(void)
 {
        int status;
        int i;
-       printk(KERN_INFO "Coda Kernel/Venus communications, "
-#ifdef CONFIG_CODA_FS_OLD_API
-              "v5.3.20"
-#else
-              "v6.0.0"
-#endif
-              ", coda@cs.cmu.edu\n");
 
        status = coda_init_inodecache();
        if (status)
@@ -424,12 +398,9 @@ static int __init init_coda(void)
        }
        return 0;
 out:
-       for (i = 0; i < MAX_CODADEVS; i++) {
-               class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
-               devfs_remove("coda/%d", i);
-       }
+       for (i = 0; i < MAX_CODADEVS; i++)
+               device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
        class_destroy(coda_psdev_class);
-       devfs_remove("coda");
        unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
        coda_sysctl_clean();
 out1:
@@ -446,12 +417,9 @@ static void __exit exit_coda(void)
         if ( err != 0 ) {
                 printk("coda: failed to unregister filesystem\n");
         }
-       for (i = 0; i < MAX_CODADEVS; i++) {
-               class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
-               devfs_remove("coda/%d", i);
-       }
+       for (i = 0; i < MAX_CODADEVS; i++)
+               device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
        class_destroy(coda_psdev_class);
-       devfs_remove("coda");
        unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
        coda_sysctl_clean();
        coda_destroy_inodecache();