^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * An implementation of a loadable kernel mode driver providing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * multiple kernel/user space bidirectional communications links.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Alan Cox <alan@lxorguk.ukuu.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Adapted to become the Linux 2.0 Coda pseudo device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Peter Braam <braam@maths.ox.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Michael Callahan <mjc@emmy.smith.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Changes for Linux 2.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (c) 1997 Carnegie-Mellon University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <linux/coda.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include "coda_psdev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include "coda_linux.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "coda_int.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int coda_hard; /* allows signals during upcalls */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct venus_comm coda_comms[MAX_CODADEVS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static struct class *coda_psdev_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Device operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static __poll_t coda_psdev_poll(struct file *file, poll_table * wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct venus_comm *vcp = (struct venus_comm *) file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __poll_t mask = EPOLLOUT | EPOLLWRNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) poll_wait(file, &vcp->vc_waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!list_empty(&vcp->vc_pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) mask |= EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned int data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) switch(cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case CIOC_KERNEL_VERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) data = CODA_KERNEL_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return put_user(data, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * Receive a message written by Venus to the psdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) size_t nbytes, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct venus_comm *vcp = (struct venus_comm *) file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct upc_req *req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct upc_req *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct list_head *lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct coda_in_hdr hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ssize_t retval = 0, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* make sure there is enough to copy out the (opcode, unique) values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (nbytes < (2 * sizeof(u_int32_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* Peek at the opcode, uniquefier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (copy_from_user(&hdr, buf, 2 * sizeof(u_int32_t)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (DOWNCALL(hdr.opcode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) union outputArgs *dcbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int size = sizeof(*dcbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if ( nbytes < sizeof(struct coda_out_hdr) ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pr_warn("coda_downcall opc %d uniq %d, not enough!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) hdr.opcode, hdr.unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) count = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if ( nbytes > size ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pr_warn("downcall opc %d, uniq %d, too much!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) hdr.opcode, hdr.unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) nbytes = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dcbuf = kvmalloc(nbytes, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!dcbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (copy_from_user(dcbuf, buf, nbytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) kvfree(dcbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* what downcall errors does Venus handle ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) error = coda_downcall(vcp, hdr.opcode, dcbuf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) kvfree(dcbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) pr_warn("%s: coda_downcall error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) retval = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) count = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Look for the message on the processing queue. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) list_for_each(lh, &vcp->vc_processing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) tmp = list_entry(lh, struct upc_req , uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (tmp->uc_unique == hdr.unique) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) req = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) list_del(&req->uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) pr_warn("%s: msg (%d, %d) not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) __func__, hdr.opcode, hdr.unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) retval = -ESRCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* move data into response buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (req->uc_outSize < nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) pr_warn("%s: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) __func__, req->uc_outSize, (long)nbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) hdr.opcode, hdr.unique);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) nbytes = req->uc_outSize; /* don't have more space! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (copy_from_user(req->uc_data, buf, nbytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) req->uc_flags |= CODA_REQ_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) wake_up(&req->uc_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* adjust outsize. is this useful ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) req->uc_outSize = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) req->uc_flags |= CODA_REQ_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) count = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Convert filedescriptor into a file handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (req->uc_opcode == CODA_OPEN_BY_FD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct coda_open_by_fd_out *outp =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) (struct coda_open_by_fd_out *)req->uc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (!outp->oh.result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) outp->fh = fget(outp->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!outp->fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) wake_up(&req->uc_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return(count ? count : retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Read a message from the kernel to Venus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static ssize_t coda_psdev_read(struct file * file, char __user * buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) size_t nbytes, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct venus_comm *vcp = (struct venus_comm *) file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct upc_req *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ssize_t retval = 0, count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (nbytes == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) add_wait_queue(&vcp->vc_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) while (list_empty(&vcp->vc_pending)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) retval = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) remove_wait_queue(&vcp->vc_waitq, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) list_del(&req->uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* Move the input args into userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) count = req->uc_inSize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (nbytes < req->uc_inSize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) pr_warn("%s: Venus read %ld bytes of %d in message\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) __func__, (long)nbytes, req->uc_inSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) count = nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (copy_to_user(buf, req->uc_data, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) retval = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* If request was not a signal, enqueue and don't free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!(req->uc_flags & CODA_REQ_ASYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) req->uc_flags |= CODA_REQ_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) list_add_tail(&(req->uc_chain), &vcp->vc_processing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) kvfree(req->uc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return (count ? count : retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int coda_psdev_open(struct inode * inode, struct file * file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct venus_comm *vcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int idx, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (task_active_pid_ns(current) != &init_pid_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (current_user_ns() != &init_user_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) idx = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (idx < 0 || idx >= MAX_CODADEVS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) vcp = &coda_comms[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!vcp->vc_inuse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) vcp->vc_inuse++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) INIT_LIST_HEAD(&vcp->vc_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) INIT_LIST_HEAD(&vcp->vc_processing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) init_waitqueue_head(&vcp->vc_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) vcp->vc_sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) vcp->vc_seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) file->private_data = vcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int coda_psdev_release(struct inode * inode, struct file * file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct venus_comm *vcp = (struct venus_comm *) file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct upc_req *req, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!vcp || !vcp->vc_inuse ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) pr_warn("%s: Not open.\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) mutex_lock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* Wakeup clients so they can return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) list_del(&req->uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* Async requests need to be freed here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (req->uc_flags & CODA_REQ_ASYNC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) kvfree(req->uc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) req->uc_flags |= CODA_REQ_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) wake_up(&req->uc_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) list_del(&req->uc_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) req->uc_flags |= CODA_REQ_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) wake_up(&req->uc_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) vcp->vc_inuse--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) mutex_unlock(&vcp->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static const struct file_operations coda_psdev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) .read = coda_psdev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) .write = coda_psdev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) .poll = coda_psdev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) .unlocked_ioctl = coda_psdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .open = coda_psdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .release = coda_psdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) .llseek = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int __init init_coda_psdev(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) pr_err("%s: unable to get major %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) __func__, CODA_PSDEV_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) coda_psdev_class = class_create(THIS_MODULE, "coda");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (IS_ERR(coda_psdev_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err = PTR_ERR(coda_psdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) goto out_chrdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) for (i = 0; i < MAX_CODADEVS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) mutex_init(&(&coda_comms[i])->vc_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) device_create(coda_psdev_class, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) coda_sysctl_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) out_chrdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) MODULE_AUTHOR("Jan Harkes, Peter J. Braam");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) MODULE_DESCRIPTION("Coda Distributed File System VFS interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) MODULE_VERSION("7.0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int __init init_coda(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) status = coda_init_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) status = init_coda_psdev();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if ( status ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) pr_warn("Problem (%d) in init_coda_psdev\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) status = register_filesystem(&coda_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) pr_warn("failed to register filesystem!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) for (i = 0; i < MAX_CODADEVS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) class_destroy(coda_psdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) coda_sysctl_clean();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) coda_destroy_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static void __exit exit_coda(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) err = unregister_filesystem(&coda_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (err != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) pr_warn("failed to unregister filesystem\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) for (i = 0; i < MAX_CODADEVS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) class_destroy(coda_psdev_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) coda_sysctl_clean();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) coda_destroy_inodecache();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) module_init(init_coda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) module_exit(exit_coda);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)