Blame view
fs/coda/psdev.c
10.2 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 |
/* * An implementation of a loadable kernel mode driver providing * multiple kernel/user space bidirectional communications links. * |
526719ba5 Switch to a valid... |
6 |
* Author: Alan Cox <alan@lxorguk.ukuu.org.uk> |
1da177e4c Linux-2.6.12-rc2 |
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> |
174cd4b1e sched/headers: Pr... |
21 |
#include <linux/sched/signal.h> |
1da177e4c Linux-2.6.12-rc2 |
22 23 24 25 26 27 |
#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> |
1da177e4c Linux-2.6.12-rc2 |
28 29 30 31 32 33 |
#include <linux/vmalloc.h> #include <linux/fs.h> #include <linux/file.h> #include <linux/poll.h> #include <linux/init.h> #include <linux/list.h> |
da47c19e5 Coda: replace BKL... |
34 |
#include <linux/mutex.h> |
1da177e4c Linux-2.6.12-rc2 |
35 |
#include <linux/device.h> |
9fd973e08 coda: Restrict co... |
36 |
#include <linux/pid_namespace.h> |
1da177e4c Linux-2.6.12-rc2 |
37 |
#include <asm/io.h> |
834b46c37 fs/coda: use linu... |
38 |
#include <linux/uaccess.h> |
1da177e4c Linux-2.6.12-rc2 |
39 40 |
#include <linux/coda.h> |
8fc8b9df8 coda: move intern... |
41 |
#include "coda_psdev.h" |
31a203df9 take coda-private... |
42 |
#include "coda_linux.h" |
c98d8cfbc [PATCH] fs/coda/:... |
43 |
#include "coda_int.h" |
1da177e4c Linux-2.6.12-rc2 |
44 |
|
1da177e4c Linux-2.6.12-rc2 |
45 46 47 48 49 50 |
/* 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]; |
1db560afe [PATCH] class: co... |
51 |
static struct class *coda_psdev_class; |
1da177e4c Linux-2.6.12-rc2 |
52 53 54 55 |
/* * Device operations */ |
076ccb76e fs: annotate ->po... |
56 |
static __poll_t coda_psdev_poll(struct file *file, poll_table * wait) |
1da177e4c Linux-2.6.12-rc2 |
57 58 |
{ struct venus_comm *vcp = (struct venus_comm *) file->private_data; |
a9a08845e vfs: do bulk POLL... |
59 |
__poll_t mask = EPOLLOUT | EPOLLWRNORM; |
1da177e4c Linux-2.6.12-rc2 |
60 61 |
poll_wait(file, &vcp->vc_waitq, wait); |
da47c19e5 Coda: replace BKL... |
62 |
mutex_lock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
63 |
if (!list_empty(&vcp->vc_pending)) |
a9a08845e vfs: do bulk POLL... |
64 |
mask |= EPOLLIN | EPOLLRDNORM; |
da47c19e5 Coda: replace BKL... |
65 |
mutex_unlock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
66 67 68 |
return mask; } |
977183902 coda/psdev: Remov... |
69 |
static long coda_psdev_ioctl(struct file * filp, unsigned int cmd, unsigned long arg) |
1da177e4c Linux-2.6.12-rc2 |
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
{ 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; |
6e51f8aa7 coda: potential b... |
98 99 100 |
/* make sure there is enough to copy out the (opcode, unique) values */ if (nbytes < (2 * sizeof(u_int32_t))) return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
101 |
/* Peek at the opcode, uniquefier */ |
6e51f8aa7 coda: potential b... |
102 |
if (copy_from_user(&hdr, buf, 2 * sizeof(u_int32_t))) |
1da177e4c Linux-2.6.12-rc2 |
103 104 105 |
return -EFAULT; if (DOWNCALL(hdr.opcode)) { |
f7cc02b87 Coda: push BKL re... |
106 |
union outputArgs *dcbuf; |
1da177e4c Linux-2.6.12-rc2 |
107 |
int size = sizeof(*dcbuf); |
1da177e4c Linux-2.6.12-rc2 |
108 |
if ( nbytes < sizeof(struct coda_out_hdr) ) { |
d9b4b3195 fs/coda: replace ... |
109 110 111 |
pr_warn("coda_downcall opc %d uniq %d, not enough! ", hdr.opcode, hdr.unique); |
1da177e4c Linux-2.6.12-rc2 |
112 113 114 115 |
count = nbytes; goto out; } if ( nbytes > size ) { |
f38cfb256 fs/coda: logging ... |
116 |
pr_warn("downcall opc %d, uniq %d, too much!", |
d9b4b3195 fs/coda: replace ... |
117 |
hdr.opcode, hdr.unique); |
1da177e4c Linux-2.6.12-rc2 |
118 119 |
nbytes = size; } |
4dc48193d coda: get rid of ... |
120 121 122 123 124 |
dcbuf = kvmalloc(nbytes, GFP_KERNEL); if (!dcbuf) { retval = -ENOMEM; goto out; } |
1da177e4c Linux-2.6.12-rc2 |
125 |
if (copy_from_user(dcbuf, buf, nbytes)) { |
936dae452 coda: get rid of ... |
126 |
kvfree(dcbuf); |
1da177e4c Linux-2.6.12-rc2 |
127 128 129 130 131 |
retval = -EFAULT; goto out; } /* what downcall errors does Venus handle ? */ |
6e51f8aa7 coda: potential b... |
132 |
error = coda_downcall(vcp, hdr.opcode, dcbuf, nbytes); |
1da177e4c Linux-2.6.12-rc2 |
133 |
|
936dae452 coda: get rid of ... |
134 |
kvfree(dcbuf); |
1da177e4c Linux-2.6.12-rc2 |
135 |
if (error) { |
6d6bd94f4 fs/coda: use __fu... |
136 137 138 |
pr_warn("%s: coda_downcall error: %d ", __func__, error); |
1da177e4c Linux-2.6.12-rc2 |
139 140 141 142 143 144 145 146 |
retval = error; goto out; } count = nbytes; goto out; } /* Look for the message on the processing queue. */ |
da47c19e5 Coda: replace BKL... |
147 |
mutex_lock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
148 149 150 151 152 153 154 155 |
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; } } |
da47c19e5 Coda: replace BKL... |
156 |
mutex_unlock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
157 158 |
if (!req) { |
6d6bd94f4 fs/coda: use __fu... |
159 160 161 |
pr_warn("%s: msg (%d, %d) not found ", __func__, hdr.opcode, hdr.unique); |
1da177e4c Linux-2.6.12-rc2 |
162 163 164 165 166 167 |
retval = -ESRCH; goto out; } /* move data into response buffer. */ if (req->uc_outSize < nbytes) { |
6d6bd94f4 fs/coda: use __fu... |
168 169 170 171 |
pr_warn("%s: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d. ", __func__, req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique); |
1da177e4c Linux-2.6.12-rc2 |
172 173 174 |
nbytes = req->uc_outSize; /* don't have more space! */ } if (copy_from_user(req->uc_data, buf, nbytes)) { |
4aeefdc69 coda: fixup clash... |
175 |
req->uc_flags |= CODA_REQ_ABORT; |
1da177e4c Linux-2.6.12-rc2 |
176 177 178 179 180 181 |
wake_up(&req->uc_sleep); retval = -EFAULT; goto out; } /* adjust outsize. is this useful ?? */ |
112d421df Coda: mount hangs... |
182 183 |
req->uc_outSize = nbytes; req->uc_flags |= CODA_REQ_WRITE; |
1da177e4c Linux-2.6.12-rc2 |
184 185 186 187 188 189 |
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; |
02551c23b coda: add error h... |
190 |
if (!outp->oh.result) { |
38c2e4370 coda: do not grab... |
191 |
outp->fh = fget(outp->fd); |
02551c23b coda: add error h... |
192 193 194 |
if (!outp->fh) return -EBADF; } |
1da177e4c Linux-2.6.12-rc2 |
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
} 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; |
da47c19e5 Coda: replace BKL... |
216 |
mutex_lock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
217 218 219 220 221 222 223 224 225 226 227 228 229 |
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; } |
da47c19e5 Coda: replace BKL... |
230 |
mutex_unlock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
231 |
schedule(); |
da47c19e5 Coda: replace BKL... |
232 |
mutex_lock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
} 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) { |
6d6bd94f4 fs/coda: use __fu... |
247 248 249 |
pr_warn("%s: Venus read %ld bytes of %d in message ", __func__, (long)nbytes, req->uc_inSize); |
1da177e4c Linux-2.6.12-rc2 |
250 251 252 253 254 255 256 |
count = nbytes; } if (copy_to_user(buf, req->uc_data, count)) retval = -EFAULT; /* If request was not a signal, enqueue and don't free */ |
4aeefdc69 coda: fixup clash... |
257 258 |
if (!(req->uc_flags & CODA_REQ_ASYNC)) { req->uc_flags |= CODA_REQ_READ; |
8e13059a3 [PATCH] use list_... |
259 |
list_add_tail(&(req->uc_chain), &vcp->vc_processing); |
1da177e4c Linux-2.6.12-rc2 |
260 261 |
goto out; } |
936dae452 coda: get rid of ... |
262 |
kvfree(req->uc_data); |
37461e195 coda: replace upc... |
263 |
kfree(req); |
1da177e4c Linux-2.6.12-rc2 |
264 |
out: |
da47c19e5 Coda: replace BKL... |
265 |
mutex_unlock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
266 267 268 269 270 |
return (count ? count : retval); } static int coda_psdev_open(struct inode * inode, struct file * file) { |
870655196 coda: cleanup /de... |
271 272 |
struct venus_comm *vcp; int idx, err; |
1da177e4c Linux-2.6.12-rc2 |
273 |
|
9fd973e08 coda: Restrict co... |
274 275 |
if (task_active_pid_ns(current) != &init_pid_ns) return -EINVAL; |
d83f5901b coda: Restrict co... |
276 277 |
if (current_user_ns() != &init_user_ns) return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
278 |
idx = iminor(inode); |
870655196 coda: cleanup /de... |
279 |
if (idx < 0 || idx >= MAX_CODADEVS) |
1da177e4c Linux-2.6.12-rc2 |
280 |
return -ENODEV; |
1da177e4c Linux-2.6.12-rc2 |
281 |
|
870655196 coda: cleanup /de... |
282 |
err = -EBUSY; |
1da177e4c Linux-2.6.12-rc2 |
283 |
vcp = &coda_comms[idx]; |
da47c19e5 Coda: replace BKL... |
284 |
mutex_lock(&vcp->vc_mutex); |
870655196 coda: cleanup /de... |
285 286 |
if (!vcp->vc_inuse) { vcp->vc_inuse++; |
1da177e4c Linux-2.6.12-rc2 |
287 288 289 290 291 |
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; |
870655196 coda: cleanup /de... |
292 293 294 |
file->private_data = vcp; err = 0; |
1da177e4c Linux-2.6.12-rc2 |
295 |
} |
1da177e4c Linux-2.6.12-rc2 |
296 |
|
da47c19e5 Coda: replace BKL... |
297 |
mutex_unlock(&vcp->vc_mutex); |
870655196 coda: cleanup /de... |
298 |
return err; |
1da177e4c Linux-2.6.12-rc2 |
299 300 301 302 303 |
} static int coda_psdev_release(struct inode * inode, struct file * file) { |
870655196 coda: cleanup /de... |
304 305 |
struct venus_comm *vcp = (struct venus_comm *) file->private_data; struct upc_req *req, *tmp; |
1da177e4c Linux-2.6.12-rc2 |
306 |
|
870655196 coda: cleanup /de... |
307 |
if (!vcp || !vcp->vc_inuse ) { |
6d6bd94f4 fs/coda: use __fu... |
308 309 |
pr_warn("%s: Not open. ", __func__); |
1da177e4c Linux-2.6.12-rc2 |
310 311 |
return -1; } |
da47c19e5 Coda: replace BKL... |
312 |
mutex_lock(&vcp->vc_mutex); |
870655196 coda: cleanup /de... |
313 314 |
/* Wakeup clients so they can return. */ |
1da177e4c Linux-2.6.12-rc2 |
315 |
list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) { |
870655196 coda: cleanup /de... |
316 |
list_del(&req->uc_chain); |
1da177e4c Linux-2.6.12-rc2 |
317 |
/* Async requests need to be freed here */ |
4aeefdc69 coda: fixup clash... |
318 |
if (req->uc_flags & CODA_REQ_ASYNC) { |
936dae452 coda: get rid of ... |
319 |
kvfree(req->uc_data); |
37461e195 coda: replace upc... |
320 |
kfree(req); |
1da177e4c Linux-2.6.12-rc2 |
321 322 |
continue; } |
4aeefdc69 coda: fixup clash... |
323 |
req->uc_flags |= CODA_REQ_ABORT; |
1da177e4c Linux-2.6.12-rc2 |
324 |
wake_up(&req->uc_sleep); |
870655196 coda: cleanup /de... |
325 326 327 328 |
} list_for_each_entry_safe(req, tmp, &vcp->vc_processing, uc_chain) { list_del(&req->uc_chain); |
4aeefdc69 coda: fixup clash... |
329 |
req->uc_flags |= CODA_REQ_ABORT; |
870655196 coda: cleanup /de... |
330 331 |
wake_up(&req->uc_sleep); } |
1da177e4c Linux-2.6.12-rc2 |
332 |
|
870655196 coda: cleanup /de... |
333 334 |
file->private_data = NULL; vcp->vc_inuse--; |
da47c19e5 Coda: replace BKL... |
335 |
mutex_unlock(&vcp->vc_mutex); |
1da177e4c Linux-2.6.12-rc2 |
336 337 |
return 0; } |
4b6f5d20b [PATCH] Make most... |
338 |
static const struct file_operations coda_psdev_fops = { |
1da177e4c Linux-2.6.12-rc2 |
339 340 341 342 |
.owner = THIS_MODULE, .read = coda_psdev_read, .write = coda_psdev_write, .poll = coda_psdev_poll, |
977183902 coda/psdev: Remov... |
343 |
.unlocked_ioctl = coda_psdev_ioctl, |
1da177e4c Linux-2.6.12-rc2 |
344 345 |
.open = coda_psdev_open, .release = coda_psdev_release, |
6038f373a llseek: automatic... |
346 |
.llseek = noop_llseek, |
1da177e4c Linux-2.6.12-rc2 |
347 |
}; |
f94845284 coda: add __init ... |
348 |
static int __init init_coda_psdev(void) |
1da177e4c Linux-2.6.12-rc2 |
349 350 351 |
{ int i, err = 0; if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) { |
6d6bd94f4 fs/coda: use __fu... |
352 353 354 |
pr_err("%s: unable to get major %d ", __func__, CODA_PSDEV_MAJOR); |
850622136 coda: clean up in... |
355 |
return -EIO; |
1da177e4c Linux-2.6.12-rc2 |
356 |
} |
1db560afe [PATCH] class: co... |
357 |
coda_psdev_class = class_create(THIS_MODULE, "coda"); |
1da177e4c Linux-2.6.12-rc2 |
358 359 360 361 |
if (IS_ERR(coda_psdev_class)) { err = PTR_ERR(coda_psdev_class); goto out_chrdev; } |
da47c19e5 Coda: replace BKL... |
362 363 |
for (i = 0; i < MAX_CODADEVS; i++) { mutex_init(&(&coda_comms[i])->vc_mutex); |
a9b12619f device create: mi... |
364 365 |
device_create(coda_psdev_class, NULL, MKDEV(CODA_PSDEV_MAJOR, i), NULL, "cfs%d", i); |
da47c19e5 Coda: replace BKL... |
366 |
} |
1da177e4c Linux-2.6.12-rc2 |
367 368 |
coda_sysctl_init(); goto out; |
1da177e4c Linux-2.6.12-rc2 |
369 370 371 372 373 |
out_chrdev: unregister_chrdev(CODA_PSDEV_MAJOR, "coda"); out: return err; } |
5b7f13bd2 coda: update modu... |
374 375 376 |
MODULE_AUTHOR("Jan Harkes, Peter J. Braam"); MODULE_DESCRIPTION("Coda Distributed File System VFS interface"); MODULE_ALIAS_CHARDEV_MAJOR(CODA_PSDEV_MAJOR); |
1da177e4c Linux-2.6.12-rc2 |
377 |
MODULE_LICENSE("GPL"); |
a9fba24c6 coda: add hinting... |
378 |
MODULE_VERSION("7.0"); |
1da177e4c Linux-2.6.12-rc2 |
379 |
|
1da177e4c Linux-2.6.12-rc2 |
380 381 382 383 |
static int __init init_coda(void) { int status; int i; |
1da177e4c Linux-2.6.12-rc2 |
384 385 386 387 388 389 |
status = coda_init_inodecache(); if (status) goto out2; status = init_coda_psdev(); if ( status ) { |
d9b4b3195 fs/coda: replace ... |
390 391 |
pr_warn("Problem (%d) in init_coda_psdev ", status); |
1da177e4c Linux-2.6.12-rc2 |
392 393 394 395 396 |
goto out1; } status = register_filesystem(&coda_fs_type); if (status) { |
f38cfb256 fs/coda: logging ... |
397 398 |
pr_warn("failed to register filesystem! "); |
1da177e4c Linux-2.6.12-rc2 |
399 400 401 402 |
goto out; } return 0; out: |
8ab5e4c15 [PATCH] devfs: Re... |
403 |
for (i = 0; i < MAX_CODADEVS; i++) |
62ca87925 coda: convert str... |
404 |
device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i)); |
1db560afe [PATCH] class: co... |
405 |
class_destroy(coda_psdev_class); |
1da177e4c Linux-2.6.12-rc2 |
406 407 408 409 410 411 412 413 414 415 416 417 418 |
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); |
d9b4b3195 fs/coda: replace ... |
419 |
if (err != 0) |
f38cfb256 fs/coda: logging ... |
420 421 |
pr_warn("failed to unregister filesystem "); |
8ab5e4c15 [PATCH] devfs: Re... |
422 |
for (i = 0; i < MAX_CODADEVS; i++) |
62ca87925 coda: convert str... |
423 |
device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i)); |
1db560afe [PATCH] class: co... |
424 |
class_destroy(coda_psdev_class); |
1da177e4c Linux-2.6.12-rc2 |
425 426 427 428 429 430 431 |
unregister_chrdev(CODA_PSDEV_MAJOR, "coda"); coda_sysctl_clean(); coda_destroy_inodecache(); } module_init(init_coda); module_exit(exit_coda); |