psdev.c 10.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5
/*
 *      	An implementation of a loadable kernel mode driver providing
 *		multiple kernel/user space bidirectional communications links.
 *
A
Alan Cox 已提交
6
 * 		Author: 	Alan Cox <alan@lxorguk.ukuu.org.uk>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 
 *              Adapted to become the Linux 2.0 Coda pseudo device
 *              Peter  Braam  <braam@maths.ox.ac.uk> 
 *              Michael Callahan <mjc@emmy.smith.edu>           
 *
 *              Changes for Linux 2.1
 *              Copyright (c) 1997 Carnegie-Mellon University
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/time.h>
21
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33
#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/vmalloc.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/list.h>
Y
Yoshihisa Abe 已提交
34
#include <linux/mutex.h>
L
Linus Torvalds 已提交
35
#include <linux/device.h>
36
#include <linux/pid_namespace.h>
L
Linus Torvalds 已提交
37
#include <asm/io.h>
F
Fabian Frederick 已提交
38
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
39 40 41 42

#include <linux/coda.h>
#include <linux/coda_psdev.h>

43 44
#include "coda_linux.h"

45
#include "coda_int.h"
L
Linus Torvalds 已提交
46 47 48 49 50 51 52

/* statistics */
int           coda_hard;         /* allows signals during upcalls */
unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */


struct venus_comm coda_comms[MAX_CODADEVS];
53
static struct class *coda_psdev_class;
L
Linus Torvalds 已提交
54 55 56 57 58

/*
 * Device operations
 */

A
Al Viro 已提交
59
static __poll_t coda_psdev_poll(struct file *file, poll_table * wait)
L
Linus Torvalds 已提交
60 61
{
        struct venus_comm *vcp = (struct venus_comm *) file->private_data;
62
	__poll_t mask = EPOLLOUT | EPOLLWRNORM;
L
Linus Torvalds 已提交
63 64

	poll_wait(file, &vcp->vc_waitq, wait);
Y
Yoshihisa Abe 已提交
65
	mutex_lock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
66
	if (!list_empty(&vcp->vc_pending))
67
                mask |= EPOLLIN | EPOLLRDNORM;
Y
Yoshihisa Abe 已提交
68
	mutex_unlock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
69 70 71 72

	return mask;
}

73
static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
	unsigned int data;

	switch(cmd) {
	case CIOC_KERNEL_VERSION:
		data = CODA_KERNEL_VERSION;
		return put_user(data, (int __user *) arg);
	default:
		return -ENOTTY;
	}

	return 0;
}

/*
 *	Receive a message written by Venus to the psdev
 */
 
static ssize_t coda_psdev_write(struct file *file, const char __user *buf, 
				size_t nbytes, loff_t *off)
{
        struct venus_comm *vcp = (struct venus_comm *) file->private_data;
        struct upc_req *req = NULL;
        struct upc_req *tmp;
	struct list_head *lh;
	struct coda_in_hdr hdr;
	ssize_t retval = 0, count = 0;
	int error;

103 104 105 106
	/* make sure there is enough to copy out the (opcode, unique) values */
	if (nbytes < (2 * sizeof(u_int32_t)))
		return -EINVAL;

L
Linus Torvalds 已提交
107
        /* Peek at the opcode, uniquefier */
108
	if (copy_from_user(&hdr, buf, 2 * sizeof(u_int32_t)))
L
Linus Torvalds 已提交
109 110 111
	        return -EFAULT;

        if (DOWNCALL(hdr.opcode)) {
112
		union outputArgs *dcbuf;
L
Linus Torvalds 已提交
113 114 115
		int size = sizeof(*dcbuf);

		if  ( nbytes < sizeof(struct coda_out_hdr) ) {
116 117
			pr_warn("coda_downcall opc %d uniq %d, not enough!\n",
				hdr.opcode, hdr.unique);
L
Linus Torvalds 已提交
118 119 120 121
			count = nbytes;
			goto out;
		}
		if ( nbytes > size ) {
122
			pr_warn("downcall opc %d, uniq %d, too much!",
123
				hdr.opcode, hdr.unique);
L
Linus Torvalds 已提交
124 125
		        nbytes = size;
		}
D
Dan Carpenter 已提交
126 127 128 129 130
		dcbuf = kvmalloc(nbytes, GFP_KERNEL);
		if (!dcbuf) {
			retval = -ENOMEM;
			goto out;
		}
L
Linus Torvalds 已提交
131
		if (copy_from_user(dcbuf, buf, nbytes)) {
D
Dan Carpenter 已提交
132
			kvfree(dcbuf);
L
Linus Torvalds 已提交
133 134 135 136 137
			retval = -EFAULT;
			goto out;
		}

		/* what downcall errors does Venus handle ? */
138
		error = coda_downcall(vcp, hdr.opcode, dcbuf, nbytes);
L
Linus Torvalds 已提交
139

D
Dan Carpenter 已提交
140
		kvfree(dcbuf);
L
Linus Torvalds 已提交
141
		if (error) {
F
Fabian Frederick 已提交
142 143
			pr_warn("%s: coda_downcall error: %d\n",
				__func__, error);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151
			retval = error;
			goto out;
		}
		count = nbytes;
		goto out;
	}
        
	/* Look for the message on the processing queue. */
Y
Yoshihisa Abe 已提交
152
	mutex_lock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160
	list_for_each(lh, &vcp->vc_processing) {
		tmp = list_entry(lh, struct upc_req , uc_chain);
		if (tmp->uc_unique == hdr.unique) {
			req = tmp;
			list_del(&req->uc_chain);
			break;
		}
	}
Y
Yoshihisa Abe 已提交
161
	mutex_unlock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
162 163

	if (!req) {
F
Fabian Frederick 已提交
164 165
		pr_warn("%s: msg (%d, %d) not found\n",
			__func__, hdr.opcode, hdr.unique);
L
Linus Torvalds 已提交
166 167 168 169 170 171
		retval = -ESRCH;
		goto out;
	}

        /* move data into response buffer. */
	if (req->uc_outSize < nbytes) {
F
Fabian Frederick 已提交
172 173 174
		pr_warn("%s: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
			__func__, req->uc_outSize, (long)nbytes,
			hdr.opcode, hdr.unique);
L
Linus Torvalds 已提交
175 176 177
		nbytes = req->uc_outSize; /* don't have more space! */
	}
        if (copy_from_user(req->uc_data, buf, nbytes)) {
178
		req->uc_flags |= CODA_REQ_ABORT;
L
Linus Torvalds 已提交
179 180 181 182 183 184
		wake_up(&req->uc_sleep);
		retval = -EFAULT;
		goto out;
	}

	/* adjust outsize. is this useful ?? */
185 186
	req->uc_outSize = nbytes;
	req->uc_flags |= CODA_REQ_WRITE;
L
Linus Torvalds 已提交
187 188 189 190 191 192
	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;
Z
Zhouyang Jia 已提交
193
		if (!outp->oh.result) {
194
			outp->fh = fget(outp->fd);
Z
Zhouyang Jia 已提交
195 196 197
			if (!outp->fh)
				return -EBADF;
		}
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	}

        wake_up(&req->uc_sleep);
out:
        return(count ? count : retval);  
}

/*
 *	Read a message from the kernel to Venus
 */

static ssize_t coda_psdev_read(struct file * file, char __user * buf, 
			       size_t nbytes, loff_t *off)
{
	DECLARE_WAITQUEUE(wait, current);
        struct venus_comm *vcp = (struct venus_comm *) file->private_data;
        struct upc_req *req;
	ssize_t retval = 0, count = 0;

	if (nbytes == 0)
		return 0;

Y
Yoshihisa Abe 已提交
220
	mutex_lock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233

	add_wait_queue(&vcp->vc_waitq, &wait);
	set_current_state(TASK_INTERRUPTIBLE);

	while (list_empty(&vcp->vc_pending)) {
		if (file->f_flags & O_NONBLOCK) {
			retval = -EAGAIN;
			break;
		}
		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
Y
Yoshihisa Abe 已提交
234
		mutex_unlock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
235
		schedule();
Y
Yoshihisa Abe 已提交
236
		mutex_lock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250
	}

	set_current_state(TASK_RUNNING);
	remove_wait_queue(&vcp->vc_waitq, &wait);

	if (retval)
		goto out;

	req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
	list_del(&req->uc_chain);

	/* Move the input args into userspace */
	count = req->uc_inSize;
	if (nbytes < req->uc_inSize) {
F
Fabian Frederick 已提交
251 252
		pr_warn("%s: Venus read %ld bytes of %d in message\n",
			__func__, (long)nbytes, req->uc_inSize);
L
Linus Torvalds 已提交
253 254 255 256 257 258 259
		count = nbytes;
        }

	if (copy_to_user(buf, req->uc_data, count))
	        retval = -EFAULT;
        
	/* If request was not a signal, enqueue and don't free */
260 261
	if (!(req->uc_flags & CODA_REQ_ASYNC)) {
		req->uc_flags |= CODA_REQ_READ;
262
		list_add_tail(&(req->uc_chain), &vcp->vc_processing);
L
Linus Torvalds 已提交
263 264 265
		goto out;
	}

D
Dan Carpenter 已提交
266
	kvfree(req->uc_data);
267
	kfree(req);
L
Linus Torvalds 已提交
268
out:
Y
Yoshihisa Abe 已提交
269
	mutex_unlock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
270 271 272 273 274
	return (count ? count : retval);
}

static int coda_psdev_open(struct inode * inode, struct file * file)
{
275 276
	struct venus_comm *vcp;
	int idx, err;
L
Linus Torvalds 已提交
277

278 279 280
	if (task_active_pid_ns(current) != &init_pid_ns)
		return -EINVAL;

281 282 283
	if (current_user_ns() != &init_user_ns)
		return -EINVAL;

L
Linus Torvalds 已提交
284
	idx = iminor(inode);
285
	if (idx < 0 || idx >= MAX_CODADEVS)
L
Linus Torvalds 已提交
286 287
		return -ENODEV;

288
	err = -EBUSY;
L
Linus Torvalds 已提交
289
	vcp = &coda_comms[idx];
Y
Yoshihisa Abe 已提交
290 291
	mutex_lock(&vcp->vc_mutex);

292 293 294
	if (!vcp->vc_inuse) {
		vcp->vc_inuse++;

L
Linus Torvalds 已提交
295 296 297 298 299
		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;
300 301 302

		file->private_data = vcp;
		err = 0;
L
Linus Torvalds 已提交
303 304
	}

Y
Yoshihisa Abe 已提交
305
	mutex_unlock(&vcp->vc_mutex);
306
	return err;
L
Linus Torvalds 已提交
307 308 309 310 311
}


static int coda_psdev_release(struct inode * inode, struct file * file)
{
312 313
	struct venus_comm *vcp = (struct venus_comm *) file->private_data;
	struct upc_req *req, *tmp;
L
Linus Torvalds 已提交
314

315
	if (!vcp || !vcp->vc_inuse ) {
F
Fabian Frederick 已提交
316
		pr_warn("%s: Not open.\n", __func__);
L
Linus Torvalds 已提交
317 318 319
		return -1;
	}

Y
Yoshihisa Abe 已提交
320
	mutex_lock(&vcp->vc_mutex);
321 322

	/* Wakeup clients so they can return. */
L
Linus Torvalds 已提交
323
	list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
324 325
		list_del(&req->uc_chain);

L
Linus Torvalds 已提交
326
		/* Async requests need to be freed here */
327
		if (req->uc_flags & CODA_REQ_ASYNC) {
D
Dan Carpenter 已提交
328
			kvfree(req->uc_data);
329
			kfree(req);
L
Linus Torvalds 已提交
330 331
			continue;
		}
332
		req->uc_flags |= CODA_REQ_ABORT;
L
Linus Torvalds 已提交
333
		wake_up(&req->uc_sleep);
334 335 336 337 338
	}

	list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) {
		list_del(&req->uc_chain);

339
		req->uc_flags |= CODA_REQ_ABORT;
340 341
		wake_up(&req->uc_sleep);
	}
L
Linus Torvalds 已提交
342

343 344
	file->private_data = NULL;
	vcp->vc_inuse--;
Y
Yoshihisa Abe 已提交
345
	mutex_unlock(&vcp->vc_mutex);
L
Linus Torvalds 已提交
346 347 348 349
	return 0;
}


350
static const struct file_operations coda_psdev_fops = {
L
Linus Torvalds 已提交
351 352 353 354
	.owner		= THIS_MODULE,
	.read		= coda_psdev_read,
	.write		= coda_psdev_write,
	.poll		= coda_psdev_poll,
355
	.unlocked_ioctl	= coda_psdev_ioctl,
L
Linus Torvalds 已提交
356 357
	.open		= coda_psdev_open,
	.release	= coda_psdev_release,
358
	.llseek		= noop_llseek,
L
Linus Torvalds 已提交
359 360 361 362 363 364
};

static int init_coda_psdev(void)
{
	int i, err = 0;
	if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) {
F
Fabian Frederick 已提交
365 366
		pr_err("%s: unable to get major %d\n",
		       __func__, CODA_PSDEV_MAJOR);
367
		return -EIO;
L
Linus Torvalds 已提交
368
	}
369
	coda_psdev_class = class_create(THIS_MODULE, "coda");
L
Linus Torvalds 已提交
370 371 372 373
	if (IS_ERR(coda_psdev_class)) {
		err = PTR_ERR(coda_psdev_class);
		goto out_chrdev;
	}		
Y
Yoshihisa Abe 已提交
374 375
	for (i = 0; i < MAX_CODADEVS; i++) {
		mutex_init(&(&coda_comms[i])->vc_mutex);
376 377
		device_create(coda_psdev_class, NULL,
			      MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i);
Y
Yoshihisa Abe 已提交
378
	}
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387
	coda_sysctl_init();
	goto out;

out_chrdev:
	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
out:
	return err;
}

J
Jan Harkes 已提交
388 389 390
MODULE_AUTHOR("Jan Harkes, Peter J. Braam");
MODULE_DESCRIPTION("Coda Distributed File System VFS interface");
MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR);
L
Linus Torvalds 已提交
391
MODULE_LICENSE("GPL");
J
Jan Harkes 已提交
392
MODULE_VERSION("6.11");
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403

static int __init init_coda(void)
{
	int status;
	int i;

	status = coda_init_inodecache();
	if (status)
		goto out2;
	status = init_coda_psdev();
	if ( status ) {
404
		pr_warn("Problem (%d) in init_coda_psdev\n", status);
L
Linus Torvalds 已提交
405 406 407 408 409
		goto out1;
	}
	
	status = register_filesystem(&coda_fs_type);
	if (status) {
410
		pr_warn("failed to register filesystem!\n");
L
Linus Torvalds 已提交
411 412 413 414
		goto out;
	}
	return 0;
out:
415
	for (i = 0; i < MAX_CODADEVS; i++)
416
		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
417
	class_destroy(coda_psdev_class);
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430
	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
	coda_sysctl_clean();
out1:
	coda_destroy_inodecache();
out2:
	return status;
}

static void __exit exit_coda(void)
{
        int err, i;

	err = unregister_filesystem(&coda_fs_type);
431
	if (err != 0)
432
		pr_warn("failed to unregister filesystem\n");
433
	for (i = 0; i < MAX_CODADEVS; i++)
434
		device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
435
	class_destroy(coda_psdev_class);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443
	unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
	coda_sysctl_clean();
	coda_destroy_inodecache();
}

module_init(init_coda);
module_exit(exit_coda);