coverity appeasement
[minix.git] / servers / vfs / device.c
blob0d7d5da96fa65dcf12c5cffa8bc84a3723b07629
1 /* When a needed block is not in the cache, it must be fetched from the disk.
2 * Special character files also require I/O. The routines for these are here.
4 * The entry points in this file are:
5 * dev_open: open a character device
6 * dev_reopen: reopen a character device after a driver crash
7 * dev_close: close a character device
8 * bdev_open: open a block device
9 * bdev_close: close a block device
10 * dev_io: FS does a read or write on a device
11 * dev_status: FS processes callback request alert
12 * gen_opcl: generic call to a task to perform an open/close
13 * gen_io: generic call to a task to perform an I/O operation
14 * no_dev: open/close processing for devices that don't exist
15 * no_dev_io: i/o processing for devices that don't exist
16 * tty_opcl: perform tty-specific processing for open/close
17 * ctty_opcl: perform controlling-tty-specific processing for open/close
18 * ctty_io: perform controlling-tty-specific processing for I/O
19 * pm_setsid: perform VFS's side of setsid system call
20 * do_ioctl: perform the IOCTL system call
23 #include "fs.h"
24 #include <string.h>
25 #include <fcntl.h>
26 #include <assert.h>
27 #include <sys/stat.h>
28 #include <minix/callnr.h>
29 #include <minix/com.h>
30 #include <minix/endpoint.h>
31 #include <minix/ioctl.h>
32 #include <minix/u64.h>
33 #include "file.h"
34 #include "fproc.h"
35 #include "scratchpad.h"
36 #include "dmap.h"
37 #include <minix/vfsif.h>
38 #include "vnode.h"
39 #include "vmnt.h"
40 #include "param.h"
42 static void restart_reopen(int major);
43 static int safe_io_conversion(endpoint_t, cp_grant_id_t *, int *,
44 endpoint_t *, void **, size_t, u32_t *);
46 static int dummyproc;
49 /*===========================================================================*
50 * dev_open *
51 *===========================================================================*/
52 int dev_open(
53 dev_t dev, /* device to open */
54 endpoint_t proc_e, /* process to open for */
55 int flags /* mode bits and flags */
58 /* Open a character device. */
59 int major, r;
61 /* Determine the major device number so as to call the device class specific
62 * open/close routine. (This is the only routine that must check the
63 * device number for being in range. All others can trust this check.)
65 major = major(dev);
66 if (major < 0 || major >= NR_DEVICES) major = 0;
67 if (dmap[major].dmap_driver == NONE) return(ENXIO);
68 r = (*dmap[major].dmap_opcl)(DEV_OPEN, dev, proc_e, flags);
69 return(r);
73 /*===========================================================================*
74 * dev_reopen *
75 *===========================================================================*/
76 int dev_reopen(
77 dev_t dev, /* device to open */
78 int filp_no, /* filp to reopen for */
79 int flags /* mode bits and flags */
82 /* Reopen a character device after a failing device driver. */
84 int major, r;
85 struct dmap *dp;
87 /* Determine the major device number and call the device class specific
88 * open/close routine. (This is the only routine that must check the device
89 * number for being in range. All others can trust this check.)
92 major = major(dev);
93 if (major < 0 || major >= NR_DEVICES) major = 0;
94 dp = &dmap[major];
95 if (dp->dmap_driver == NONE) return(ENXIO);
96 r = (*dp->dmap_opcl)(DEV_REOPEN, dev, filp_no, flags);
97 if (r == SUSPEND) r = OK;
98 return(r);
102 /*===========================================================================*
103 * dev_close *
104 *===========================================================================*/
105 int dev_close(
106 dev_t dev, /* device to close */
107 int filp_no
110 /* Close a character device. */
111 int r, major;
113 /* See if driver is roughly valid. */
114 major = major(dev);
115 if (major < 0 || major >= NR_DEVICES) return(ENXIO);
116 if (dmap[major].dmap_driver == NONE) return(ENXIO);
117 r = (*dmap[major].dmap_opcl)(DEV_CLOSE, dev, filp_no, 0);
118 return(r);
122 /*===========================================================================*
123 * dev_open *
124 *===========================================================================*/
125 int bdev_open(dev_t dev, int access)
127 /* Open a block device. */
128 int major;
130 major = major(dev);
131 if (major < 0 || major >= NR_DEVICES) return(ENXIO);
132 if (dmap[major].dmap_driver == NONE) return(ENXIO);
134 return (*dmap[major].dmap_opcl)(BDEV_OPEN, dev, 0, access);
138 /*===========================================================================*
139 * bdev_close *
140 *===========================================================================*/
141 int bdev_close(dev_t dev)
143 /* Close a block device. */
144 int major;
146 major = major(dev);
147 if (major < 0 || major >= NR_DEVICES) return(ENXIO);
148 if (dmap[major].dmap_driver == NONE) return(ENXIO);
150 return (*dmap[major].dmap_opcl)(BDEV_CLOSE, dev, 0, 0);
154 /*===========================================================================*
155 * bdev_ioctl *
156 *===========================================================================*/
157 static int bdev_ioctl(dev_t dev, endpoint_t proc_e, int req, void *buf)
159 /* Perform an I/O control operation on a block device. */
160 struct dmap *dp;
161 u32_t dummy;
162 cp_grant_id_t gid;
163 message dev_mess;
164 int op, major_dev, minor_dev;
166 major_dev = major(dev);
167 minor_dev = minor(dev);
169 /* Determine task dmap. */
170 dp = &dmap[major_dev];
171 if (dp->dmap_driver == NONE) {
172 printf("VFS: dev_io: no driver for major %d\n", major_dev);
173 return(ENXIO);
176 /* Set up a grant if necessary. */
177 op = VFS_DEV_IOCTL;
178 (void) safe_io_conversion(dp->dmap_driver, &gid, &op, &proc_e, &buf, req,
179 &dummy);
181 /* Set up the message passed to the task. */
182 memset(&dev_mess, 0, sizeof(dev_mess));
184 dev_mess.m_type = BDEV_IOCTL;
185 dev_mess.BDEV_MINOR = minor_dev;
186 dev_mess.BDEV_REQUEST = req;
187 dev_mess.BDEV_GRANT = gid;
188 dev_mess.BDEV_ID = 0;
190 /* Call the task. */
191 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
193 /* Clean up. */
194 if (GRANT_VALID(gid)) cpf_revoke(gid);
196 if (dp->dmap_driver == NONE) {
197 printf("VFS: block driver gone!?\n");
198 return(EIO);
201 /* Return the result. */
202 return(dev_mess.BDEV_STATUS);
206 /*===========================================================================*
207 * find_suspended_ep *
208 *===========================================================================*/
209 endpoint_t find_suspended_ep(endpoint_t driver, cp_grant_id_t g)
211 /* A process is suspended on a driver for which VFS issued a grant. Find out
212 * which process it was.
214 struct fproc *rfp;
215 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
216 if(rfp->fp_pid == PID_FREE)
217 continue;
219 if(rfp->fp_blocked_on == FP_BLOCKED_ON_OTHER &&
220 rfp->fp_task == driver && rfp->fp_grant == g)
221 return(rfp->fp_endpoint);
224 return(NONE);
228 /*===========================================================================*
229 * dev_status *
230 *===========================================================================*/
231 void dev_status(endpoint_t drv_e)
233 /* A device sent us a notification it has something for us. Retrieve it. */
235 message st;
236 int major, get_more = 1;
237 endpoint_t endpt;
239 for (major = 0; major < NR_DEVICES; major++)
240 if (dmap_driver_match(drv_e, major))
241 break; /* 'major' is the device that sent the message */
243 if (major >= NR_DEVICES) /* Device endpoint not found; nothing to do */
244 return;
246 if (dev_style_asyn(dmap[major].dmap_style)) {
247 printf("VFS: not doing dev_status for async driver %d\n", drv_e);
248 return;
251 /* Continuously send DEV_STATUS messages until the device has nothing to
252 * say to us anymore. */
253 do {
254 int r;
255 st.m_type = DEV_STATUS;
256 r = drv_sendrec(drv_e, &st);
257 if (r == OK && st.REP_STATUS == ERESTART) r = EDEADEPT;
258 if (r != OK) {
259 printf("VFS: DEV_STATUS failed to %d: %d\n", drv_e, r);
260 if (r == EDEADSRCDST || r == EDEADEPT) return;
261 panic("VFS: couldn't sendrec for DEV_STATUS: %d", r);
264 switch(st.m_type) {
265 case DEV_REVIVE:
266 /* We've got results for a read/write/ioctl call to a
267 * synchronous character driver */
268 endpt = st.REP_ENDPT;
269 if (endpt == VFS_PROC_NR) {
270 endpt = find_suspended_ep(drv_e, st.REP_IO_GRANT);
271 if (endpt == NONE) {
272 printf("VFS: proc with grant %d from %d not found\n",
273 st.REP_IO_GRANT, st.m_source);
274 continue;
277 revive(endpt, st.REP_STATUS);
278 break;
279 case DEV_IO_READY:
280 /* Reply to a select request: driver is ready for I/O */
281 select_reply2(st.m_source, st.DEV_MINOR, st.DEV_SEL_OPS);
282 break;
283 default:
284 printf("VFS: unrecognized reply %d to DEV_STATUS\n",st.m_type);
285 /* Fall through. */
286 case DEV_NO_STATUS:
287 get_more = 0;
288 break;
290 } while(get_more);
293 /*===========================================================================*
294 * safe_io_conversion *
295 *===========================================================================*/
296 static int safe_io_conversion(driver, gid, op, io_ept, buf, bytes, pos_lo)
297 endpoint_t driver;
298 cp_grant_id_t *gid;
299 int *op;
300 endpoint_t *io_ept;
301 void **buf;
302 size_t bytes;
303 u32_t *pos_lo;
305 /* Convert operation to the 'safe' variant (i.e., grant based) if applicable.
306 * If no copying of data is involved, there is also no need to convert. */
308 int access = 0;
309 size_t size;
311 *gid = GRANT_INVALID; /* Grant to buffer */
313 switch(*op) {
314 case VFS_DEV_READ:
315 case VFS_DEV_WRITE:
316 /* Change to safe op. */
317 *op = (*op == VFS_DEV_READ) ? DEV_READ_S : DEV_WRITE_S;
318 *gid = cpf_grant_magic(driver, *io_ept, (vir_bytes) *buf, bytes,
319 *op == DEV_READ_S ? CPF_WRITE : CPF_READ);
320 if (*gid < 0)
321 panic("VFS: cpf_grant_magic of READ/WRITE buffer failed");
322 break;
323 case VFS_DEV_IOCTL:
324 *pos_lo = *io_ept; /* Old endpoint in POSITION field. */
325 *op = DEV_IOCTL_S;
326 /* For IOCTLs, the bytes parameter encodes requested access method
327 * and buffer size */
328 if(_MINIX_IOCTL_IOR(bytes)) access |= CPF_WRITE;
329 if(_MINIX_IOCTL_IOW(bytes)) access |= CPF_READ;
330 if(_MINIX_IOCTL_BIG(bytes))
331 size = _MINIX_IOCTL_SIZE_BIG(bytes);
332 else
333 size = _MINIX_IOCTL_SIZE(bytes);
335 /* Grant access to the buffer even if no I/O happens with the ioctl, in
336 * order to disambiguate requests with DEV_IOCTL_S.
338 *gid = cpf_grant_magic(driver, *io_ept, (vir_bytes) *buf, size, access);
339 if (*gid < 0)
340 panic("VFS: cpf_grant_magic IOCTL buffer failed");
342 break;
343 case VFS_DEV_SELECT:
344 *op = DEV_SELECT;
345 break;
346 default:
347 panic("VFS: unknown operation %d for safe I/O conversion", *op);
350 /* If we have converted to a safe operation, I/O endpoint becomes VFS if it
351 * wasn't already.
353 if(GRANT_VALID(*gid)) {
354 *io_ept = VFS_PROC_NR;
355 return(1);
358 /* Not converted to a safe operation (because there is no copying involved in
359 * this operation).
361 return(0);
364 static int cancel_nblock(struct dmap * dp,
365 int minor,
366 int call,
367 endpoint_t ioproc,
368 cp_grant_id_t gid)
370 message dev_mess;
372 dev_mess.m_type = CANCEL;
373 dev_mess.USER_ENDPT = ioproc;
374 dev_mess.IO_GRANT = (char *) gid;
376 /* This R_BIT/W_BIT check taken from suspend()/unpause()
377 * logic. Mode is expected in the COUNT field.
379 dev_mess.COUNT = 0;
380 if (call == READ)
381 dev_mess.COUNT = R_BIT;
382 else if (call == WRITE)
383 dev_mess.COUNT = W_BIT;
384 dev_mess.DEVICE = minor;
385 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
387 return dev_mess.REP_STATUS;
390 /*===========================================================================*
391 * dev_io *
392 *===========================================================================*/
393 int dev_io(
394 int op, /* DEV_READ, DEV_WRITE, DEV_IOCTL, etc. */
395 dev_t dev, /* major-minor device number */
396 int proc_e, /* in whose address space is buf? */
397 void *buf, /* virtual address of the buffer */
398 u64_t pos, /* byte position */
399 size_t bytes, /* how many bytes to transfer */
400 int flags, /* special flags, like O_NONBLOCK */
401 int suspend_reopen /* Just suspend the process */
404 /* Read from or write to a device. The parameter 'dev' tells which one. */
405 struct dmap *dp;
406 u32_t pos_lo, pos_high;
407 message dev_mess;
408 cp_grant_id_t gid = GRANT_INVALID;
409 int safe, minor_dev, major_dev;
410 void *buf_used;
411 endpoint_t ioproc;
412 int ret, is_asyn;
414 pos_lo = ex64lo(pos);
415 pos_high = ex64hi(pos);
416 major_dev = major(dev);
417 minor_dev = minor(dev);
419 /* Determine task dmap. */
420 dp = &dmap[major_dev];
422 /* See if driver is roughly valid. */
423 if (dp->dmap_driver == NONE) return(ENXIO);
425 if (suspend_reopen) {
426 /* Suspend user. */
427 fp->fp_grant = GRANT_INVALID;
428 fp->fp_ioproc = NONE;
429 wait_for(dp->dmap_driver);
430 fp->fp_flags |= FP_SUSP_REOPEN;
431 return(SUSPEND);
434 if(isokendpt(dp->dmap_driver, &dummyproc) != OK) {
435 printf("VFS: dev_io: old driver for major %x (%d)\n", major_dev,
436 dp->dmap_driver);
437 return(ENXIO);
440 /* By default, these are right. */
441 dev_mess.USER_ENDPT = proc_e;
442 dev_mess.ADDRESS = buf;
444 /* Convert DEV_* to DEV_*_S variants. */
445 buf_used = buf;
446 safe = safe_io_conversion(dp->dmap_driver, &gid, &op,
447 (endpoint_t *) &dev_mess.USER_ENDPT, &buf_used,
448 bytes, &pos_lo);
450 is_asyn = dev_style_asyn(dp->dmap_style);
452 /* If the safe conversion was done, set the IO_GRANT to
453 * the grant id.
455 if(safe) dev_mess.IO_GRANT = (char *) gid;
457 /* Set up the rest of the message passed to task. */
458 dev_mess.m_type = op;
459 dev_mess.DEVICE = minor_dev;
460 dev_mess.POSITION = pos_lo;
461 dev_mess.COUNT = bytes;
462 dev_mess.HIGHPOS = pos_high;
463 dev_mess.FLAGS = 0;
465 if (flags & O_NONBLOCK)
466 dev_mess.FLAGS |= FLG_OP_NONBLOCK;
468 /* This will be used if the i/o is suspended. */
469 ioproc = dev_mess.USER_ENDPT;
471 /* Call the task. */
472 (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
474 if(dp->dmap_driver == NONE) {
475 /* Driver has vanished. */
476 printf("VFS: driver gone?!\n");
477 if(safe) cpf_revoke(gid);
478 return(EIO);
481 ret = dev_mess.REP_STATUS;
483 /* Task has completed. See if call completed. */
484 if (ret == SUSPEND) {
485 if ((flags & O_NONBLOCK) && !is_asyn) {
486 /* Not supposed to block. */
487 ret = cancel_nblock(dp, minor_dev, job_call_nr, ioproc, gid);
488 if (ret == EINTR)
489 ret = EAGAIN;
490 } else {
491 /* select() will do suspending itself. */
492 if(op != DEV_SELECT) {
493 /* Suspend user. */
494 wait_for(dp->dmap_driver);
496 assert(!GRANT_VALID(fp->fp_grant));
497 fp->fp_grant = gid; /* revoke this when unsuspended. */
498 fp->fp_ioproc = ioproc;
500 if ((flags & O_NONBLOCK) && !is_asyn) {
501 /* Not supposed to block, send cancel message */
502 cancel_nblock(dp, minor_dev, job_call_nr, ioproc, gid);
504 * FIXME Should do something about EINTR -> EAGAIN
505 * mapping
508 return(SUSPEND);
512 /* No suspend, or cancelled suspend, so I/O is over and can be cleaned up. */
513 if(safe) cpf_revoke(gid);
515 return ret;
518 /*===========================================================================*
519 * gen_opcl *
520 *===========================================================================*/
521 int gen_opcl(
522 int op, /* operation, (B)DEV_OPEN or (B)DEV_CLOSE */
523 dev_t dev, /* device to open or close */
524 endpoint_t proc_e, /* process to open/close for */
525 int flags /* mode bits and flags */
528 /* Called from the dmap struct on opens & closes of special files.*/
529 int r, minor_dev, major_dev, is_bdev;
530 struct dmap *dp;
531 message dev_mess;
533 /* Determine task dmap. */
534 major_dev = major(dev);
535 minor_dev = minor(dev);
536 assert(major_dev >= 0 && major_dev < NR_DEVICES);
537 dp = &dmap[major_dev];
538 assert(dp->dmap_driver != NONE);
540 is_bdev = IS_BDEV_RQ(op);
542 if (is_bdev) {
543 memset(&dev_mess, 0, sizeof(dev_mess));
544 dev_mess.m_type = op;
545 dev_mess.BDEV_MINOR = minor_dev;
546 dev_mess.BDEV_ACCESS = flags;
547 dev_mess.BDEV_ID = 0;
548 } else {
549 dev_mess.m_type = op;
550 dev_mess.DEVICE = minor_dev;
551 dev_mess.USER_ENDPT = proc_e;
552 dev_mess.COUNT = flags;
555 /* Call the task. */
556 r = (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
557 if (r != OK) return(r);
559 if (op == DEV_OPEN && dp->dmap_style == STYLE_DEVA) {
560 fp->fp_task = dp->dmap_driver;
561 worker_wait();
564 if (is_bdev)
565 return(dev_mess.BDEV_STATUS);
566 else
567 return(dev_mess.REP_STATUS);
570 /*===========================================================================*
571 * tty_opcl *
572 *===========================================================================*/
573 int tty_opcl(
574 int op, /* operation, DEV_OPEN or DEV_CLOSE */
575 dev_t dev, /* device to open or close */
576 endpoint_t proc_e, /* process to open/close for */
577 int flags /* mode bits and flags */
580 /* This procedure is called from the dmap struct on tty open/close. */
582 int r;
583 register struct fproc *rfp;
585 assert(!IS_BDEV_RQ(op));
587 /* Add O_NOCTTY to the flags if this process is not a session leader, or
588 * if it already has a controlling tty, or if it is someone elses
589 * controlling tty.
591 if (!(fp->fp_flags & FP_SESLDR) || fp->fp_tty != 0) {
592 flags |= O_NOCTTY;
593 } else {
594 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
595 if(rfp->fp_pid == PID_FREE) continue;
596 if (rfp->fp_tty == dev) flags |= O_NOCTTY;
600 r = gen_opcl(op, dev, proc_e, flags);
602 /* Did this call make the tty the controlling tty? */
603 if (r == 1) {
604 fp->fp_tty = dev;
605 r = OK;
608 return(r);
612 /*===========================================================================*
613 * ctty_opcl *
614 *===========================================================================*/
615 int ctty_opcl(
616 int op, /* operation, DEV_OPEN or DEV_CLOSE */
617 dev_t UNUSED(dev), /* device to open or close */
618 endpoint_t UNUSED(proc_e), /* process to open/close for */
619 int UNUSED(flags) /* mode bits and flags */
622 /* This procedure is called from the dmap struct on opening or closing
623 * /dev/tty, the magic device that translates to the controlling tty.
626 assert(!IS_BDEV_RQ(op));
628 return(fp->fp_tty == 0 ? ENXIO : OK);
632 /*===========================================================================*
633 * pm_setsid *
634 *===========================================================================*/
635 void pm_setsid(endpoint_t proc_e)
637 /* Perform the VFS side of the SETSID call, i.e. get rid of the controlling
638 * terminal of a process, and make the process a session leader.
640 register struct fproc *rfp;
641 int slot;
643 /* Make the process a session leader with no controlling tty. */
644 okendpt(proc_e, &slot);
645 rfp = &fproc[slot];
646 rfp->fp_flags |= FP_SESLDR;
647 rfp->fp_tty = 0;
651 /*===========================================================================*
652 * do_ioctl *
653 *===========================================================================*/
654 int do_ioctl()
656 /* Perform the ioctl(ls_fd, request, argx) system call */
658 int r = OK, suspend_reopen, ioctlrequest;
659 struct filp *f;
660 register struct vnode *vp;
661 dev_t dev;
662 void *argx;
664 scratch(fp).file.fd_nr = job_m_in.ls_fd;
665 ioctlrequest = job_m_in.REQUEST;
666 argx = job_m_in.ADDRESS;
668 if ((f = get_filp(scratch(fp).file.fd_nr, VNODE_READ)) == NULL)
669 return(err_code);
670 vp = f->filp_vno; /* get vnode pointer */
671 if (!S_ISCHR(vp->v_mode) && !S_ISBLK(vp->v_mode)) {
672 r = ENOTTY;
675 if (r == OK) {
676 suspend_reopen = (f->filp_state & FS_NEEDS_REOPEN);
677 dev = (dev_t) vp->v_sdev;
679 if (S_ISBLK(vp->v_mode))
680 r = bdev_ioctl(dev, who_e, ioctlrequest, argx);
681 else
682 r = dev_io(VFS_DEV_IOCTL, dev, who_e, argx, cvu64(0),
683 ioctlrequest, f->filp_flags, suspend_reopen);
686 unlock_filp(f);
688 return(r);
692 /*===========================================================================*
693 * gen_io *
694 *===========================================================================*/
695 int gen_io(driver_e, mess_ptr)
696 endpoint_t driver_e; /* which endpoint to call */
697 message *mess_ptr; /* pointer to message for task */
699 /* All file system I/O ultimately comes down to I/O on major/minor device
700 * pairs. These lead to calls on the following routines via the dmap table.
702 int r, status, proc_e = NONE, is_bdev, retry_count;
703 message mess_retry;
705 is_bdev = IS_BDEV_RQ(mess_ptr->m_type);
706 mess_retry = *mess_ptr;
707 retry_count = 0;
709 if (!is_bdev) proc_e = mess_ptr->USER_ENDPT;
711 do {
712 r = drv_sendrec(driver_e, mess_ptr);
713 if (r == OK) {
714 if (is_bdev)
715 status = mess_ptr->BDEV_STATUS;
716 else
717 status = mess_ptr->REP_STATUS;
718 if (status == ERESTART) {
719 r = EDEADEPT;
720 *mess_ptr = mess_retry;
721 retry_count++;
724 } while (r == EDEADEPT && retry_count < 5);
726 if (r != OK) {
727 if (r == EDEADSRCDST || r == EDEADEPT) {
728 printf("VFS: dead driver %d\n", driver_e);
729 dmap_unmap_by_endpt(driver_e);
730 return(r);
731 } else if (r == ELOCKED) {
732 printf("VFS: ELOCKED talking to %d\n", driver_e);
733 return(r);
735 panic("call_task: can't send/receive: %d", r);
738 /* Did the process we did the sendrec() for get a result? */
739 if (!is_bdev && mess_ptr->REP_ENDPT != proc_e && mess_ptr->m_type != EIO) {
740 printf("VFS: strange device reply from %d, type = %d, "
741 "proc = %d (not %d) (2) ignored\n", mess_ptr->m_source,
742 mess_ptr->m_type, proc_e, mess_ptr->REP_ENDPT);
744 return(EIO);
745 } else if (!IS_DRV_REPLY(mess_ptr->m_type))
746 return(EIO);
748 return(OK);
752 /*===========================================================================*
753 * asyn_io *
754 *===========================================================================*/
755 int asyn_io(endpoint_t drv_e, message *mess_ptr)
757 /* All file system I/O ultimately comes down to I/O on major/minor device
758 * pairs. These lead to calls on the following routines via the dmap table.
761 int r;
763 assert(!IS_BDEV_RQ(mess_ptr->m_type));
764 self->w_drv_sendrec = mess_ptr; /* Remember where result should be stored */
765 self->w_task = drv_e;
767 r = asynsend3(drv_e, mess_ptr, AMF_NOREPLY);
769 if (r != OK) panic("VFS: asynsend in asyn_io failed: %d", r);
771 /* Fake a SUSPEND */
772 mess_ptr->REP_STATUS = SUSPEND;
773 return(OK);
777 /*===========================================================================*
778 * ctty_io *
779 *===========================================================================*/
780 int ctty_io(
781 endpoint_t UNUSED(task_nr), /* not used - for compatibility with dmap_t */
782 message *mess_ptr /* pointer to message for task */
785 /* This routine is only called for one device, namely /dev/tty. Its job
786 * is to change the message to use the controlling terminal, instead of the
787 * major/minor pair for /dev/tty itself.
790 struct dmap *dp;
792 if (fp->fp_tty == 0) {
793 /* No controlling tty present anymore, return an I/O error. */
794 mess_ptr->REP_STATUS = EIO;
795 } else {
796 /* Substitute the controlling terminal device. */
797 dp = &dmap[major(fp->fp_tty)];
798 mess_ptr->DEVICE = minor(fp->fp_tty);
800 if (dp->dmap_driver == NONE) {
801 printf("FS: ctty_io: no driver for dev\n");
802 return(EIO);
805 if (isokendpt(dp->dmap_driver, &dummyproc) != OK) {
806 printf("VFS: ctty_io: old driver %d\n", dp->dmap_driver);
807 return(EIO);
810 (*dp->dmap_io)(dp->dmap_driver, mess_ptr);
813 return(OK);
817 /*===========================================================================*
818 * no_dev *
819 *===========================================================================*/
820 int no_dev(
821 int UNUSED(op), /* operation, DEV_OPEN or DEV_CLOSE */
822 dev_t UNUSED(dev), /* device to open or close */
823 int UNUSED(proc), /* process to open/close for */
824 int UNUSED(flags) /* mode bits and flags */
827 /* Called when opening a nonexistent device. */
828 return(ENODEV);
831 /*===========================================================================*
832 * no_dev_io *
833 *===========================================================================*/
834 int no_dev_io(endpoint_t UNUSED(proc), message *UNUSED(m))
836 /* Called when doing i/o on a nonexistent device. */
837 printf("VFS: I/O on unmapped device number\n");
838 return(EIO);
842 /*===========================================================================*
843 * clone_opcl *
844 *===========================================================================*/
845 int clone_opcl(
846 int op, /* operation, DEV_OPEN or DEV_CLOSE */
847 dev_t dev, /* device to open or close */
848 int proc_e, /* process to open/close for */
849 int flags /* mode bits and flags */
852 /* Some devices need special processing upon open. Such a device is "cloned",
853 * i.e. on a succesful open it is replaced by a new device with a new unique
854 * minor device number. This new device number identifies a new object (such
855 * as a new network connection) that has been allocated within a task.
857 struct dmap *dp;
858 int r, minor_dev, major_dev;
859 message dev_mess;
861 assert(!IS_BDEV_RQ(op));
863 /* Determine task dmap. */
864 minor_dev = minor(dev);
865 major_dev = major(dev);
866 assert(major_dev >= 0 && major_dev < NR_DEVICES);
867 dp = &dmap[major_dev];
868 assert(dp->dmap_driver != NONE);
870 dev_mess.m_type = op;
871 dev_mess.DEVICE = minor_dev;
872 dev_mess.USER_ENDPT = proc_e;
873 dev_mess.COUNT = flags;
875 if(isokendpt(dp->dmap_driver, &dummyproc) != OK) {
876 printf("VFS clone_opcl: bad driver endpoint for major %d (%d)\n",
877 major_dev, dp->dmap_driver);
878 return(ENXIO);
881 /* Call the task. */
882 r = (*dp->dmap_io)(dp->dmap_driver, &dev_mess);
883 if (r != OK) return(r);
885 if (op == DEV_OPEN && dev_style_asyn(dp->dmap_style)) {
886 /* Wait for reply when driver is asynchronous */
887 fp->fp_task = dp->dmap_driver;
888 worker_wait();
891 if (op == DEV_OPEN && dev_mess.REP_STATUS >= 0) {
892 if (dev_mess.REP_STATUS != minor_dev) {
893 struct vnode *vp;
894 struct node_details res;
896 /* A new minor device number has been returned.
897 * Request PFS to create a temporary device file to hold it.
900 /* Device number of the new device. */
901 dev = (dev & ~(BYTE << MINOR)) | (dev_mess.REP_STATUS << MINOR);
903 /* Issue request */
904 r = req_newnode(PFS_PROC_NR, fp->fp_effuid, fp->fp_effgid,
905 ALL_MODES | I_CHAR_SPECIAL, dev, &res);
906 if (r != OK) {
907 (void) clone_opcl(DEV_CLOSE, dev, proc_e, 0);
908 return r;
911 /* Drop old node and use the new values */
912 if ((vp = get_free_vnode()) == NULL)
913 return(err_code);
914 lock_vnode(vp, VNODE_OPCL);
916 assert(FD_ISSET(scratch(fp).file.fd_nr, &fp->fp_filp_inuse));
917 unlock_vnode(fp->fp_filp[scratch(fp).file.fd_nr]->filp_vno);
918 put_vnode(fp->fp_filp[scratch(fp).file.fd_nr]->filp_vno);
920 vp->v_fs_e = res.fs_e;
921 vp->v_vmnt = NULL;
922 vp->v_dev = NO_DEV;
923 vp->v_fs_e = res.fs_e;
924 vp->v_inode_nr = res.inode_nr;
925 vp->v_mode = res.fmode;
926 vp->v_sdev = dev;
927 vp->v_fs_count = 1;
928 vp->v_ref_count = 1;
929 fp->fp_filp[scratch(fp).file.fd_nr]->filp_vno = vp;
931 dev_mess.REP_STATUS = OK;
933 return(dev_mess.REP_STATUS);
937 /*===========================================================================*
938 * bdev_up *
939 *===========================================================================*/
940 void bdev_up(int maj)
942 /* A new block device driver has been mapped in. This may affect both mounted
943 * file systems and open block-special files.
945 int r, found, bits;
946 struct filp *rfilp;
947 struct vmnt *vmp;
948 struct vnode *vp;
949 char *label;
951 if (maj < 0 || maj >= NR_DEVICES) panic("VFS: out-of-bound major");
952 label = dmap[maj].dmap_label;
953 found = 0;
955 /* For each block-special file that was previously opened on the affected
956 * device, we need to reopen it on the new driver.
958 for (rfilp = filp; rfilp < &filp[NR_FILPS]; rfilp++) {
959 if (rfilp->filp_count < 1 || !(vp = rfilp->filp_vno)) continue;
960 if (major(vp->v_sdev) != maj) continue;
961 if (!S_ISBLK(vp->v_mode)) continue;
963 /* Reopen the device on the driver, once per filp. */
964 bits = mode_map[rfilp->filp_mode & O_ACCMODE];
965 if ((r = bdev_open(vp->v_sdev, bits)) != OK) {
966 printf("VFS: mounted dev %d/%d re-open failed: %d.\n",
967 maj, minor(vp->v_sdev), r);
968 dmap[maj].dmap_recovering = 0;
969 return; /* Give up entirely */
972 found = 1;
975 /* Tell each affected mounted file system about the new endpoint.
977 for (vmp = &vmnt[0]; vmp < &vmnt[NR_MNTS]; ++vmp) {
978 if (major(vmp->m_dev) != maj) continue;
980 /* Send the driver label to the mounted file system. */
981 if (OK != req_newdriver(vmp->m_fs_e, vmp->m_dev, label))
982 printf("VFS dev_up: error sending new driver label to %d\n",
983 vmp->m_fs_e);
986 /* If any block-special file was open for this major at all, also inform the
987 * root file system about the new driver. We do this even if the
988 * block-special file is linked to another mounted file system, merely
989 * because it is more work to check for that case.
991 if (found) {
992 if (OK != req_newdriver(ROOT_FS_E, makedev(maj, 0), label))
993 printf("VFSdev_up: error sending new driver label to %d\n",
994 ROOT_FS_E);
1000 /*===========================================================================*
1001 * cdev_up *
1002 *===========================================================================*/
1003 void cdev_up(int maj)
1005 /* A new character device driver has been mapped in.
1007 int needs_reopen, fd_nr;
1008 struct filp *rfilp;
1009 struct fproc *rfp;
1010 struct vnode *vp;
1012 /* Look for processes that are suspended in an OPEN call. Set FP_SUSP_REOPEN
1013 * to indicate that this process was suspended before the call to dev_up.
1015 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
1016 if(rfp->fp_pid == PID_FREE) continue;
1017 if(rfp->fp_blocked_on != FP_BLOCKED_ON_DOPEN) continue;
1019 fd_nr = scratch(rfp).file.fd_nr;
1020 printf("VFS: dev_up: found process in FP_BLOCKED_ON_DOPEN, fd %d\n",
1021 fd_nr);
1022 rfilp = rfp->fp_filp[fd_nr];
1023 vp = rfilp->filp_vno;
1024 if (!vp) panic("VFS: cdev_up: no vp");
1025 if (!S_ISCHR(vp->v_mode)) continue;
1026 if (major(vp->v_sdev) != maj) continue;
1028 rfp->fp_flags |= FP_SUSP_REOPEN;
1031 needs_reopen= FALSE;
1032 for (rfilp = filp; rfilp < &filp[NR_FILPS]; rfilp++) {
1033 if (rfilp->filp_count < 1 || !(vp = rfilp->filp_vno)) continue;
1034 if (major(vp->v_sdev) != maj) continue;
1035 if (!S_ISCHR(vp->v_mode)) continue;
1037 rfilp->filp_state |= FS_NEEDS_REOPEN;
1038 needs_reopen = TRUE;
1041 if (needs_reopen)
1042 restart_reopen(maj);
1045 /*===========================================================================*
1046 * open_reply *
1047 *===========================================================================*/
1048 void open_reply(void)
1050 struct fproc *rfp;
1051 struct worker_thread *wp;
1052 endpoint_t proc_e;
1053 int slot;
1055 proc_e = job_m_in.REP_ENDPT;
1056 if (isokendpt(proc_e, &slot) != OK) return;
1057 rfp = &fproc[slot];
1058 wp = worker_get(rfp->fp_wtid);
1059 if (wp == NULL || wp->w_task != who_e) {
1060 printf("VFS: no worker thread waiting for a reply from %d\n", who_e);
1061 return;
1063 *wp->w_drv_sendrec = job_m_in;
1064 wp->w_drv_sendrec = NULL;
1065 wp->w_task = NONE;
1066 worker_signal(wp); /* Continue open */
1069 /*===========================================================================*
1070 * dev_reply *
1071 *===========================================================================*/
1072 void dev_reply(struct dmap *dp)
1074 struct worker_thread *wp;
1076 assert(dp != NULL);
1077 assert(dp->dmap_servicing != NONE);
1079 wp = worker_get(dp->dmap_servicing);
1080 if (wp == NULL || wp->w_task != who_e) {
1081 printf("VFS: no worker thread waiting for a reply from %d\n",
1082 who_e);
1083 return;
1086 assert(wp->w_drv_sendrec != NULL);
1087 *wp->w_drv_sendrec = m_in;
1088 wp->w_drv_sendrec = NULL;
1089 worker_signal(wp);
1092 /*===========================================================================*
1093 * restart_reopen *
1094 *===========================================================================*/
1095 static void restart_reopen(maj)
1096 int maj;
1098 int n, r, minor_dev, major_dev, fd_nr;
1099 endpoint_t driver_e;
1100 struct vnode *vp;
1101 struct filp *rfilp;
1102 struct fproc *rfp;
1104 if (maj < 0 || maj >= NR_DEVICES) panic("VFS: out-of-bound major");
1105 for (rfilp = filp; rfilp < &filp[NR_FILPS]; rfilp++) {
1106 if (rfilp->filp_count < 1 || !(vp = rfilp->filp_vno)) continue;
1107 if (!(rfilp->filp_state & FS_NEEDS_REOPEN)) continue;
1108 if (!S_ISCHR(vp->v_mode)) continue;
1110 major_dev = major(vp->v_sdev);
1111 minor_dev = minor(vp->v_sdev);
1112 if (major_dev != maj) continue;
1114 if (rfilp->filp_flags & O_REOPEN) {
1115 /* Try to reopen a file upon driver restart */
1116 r = dev_reopen(vp->v_sdev, rfilp-filp,
1117 vp->v_mode & (R_BIT|W_BIT));
1119 if (r == OK)
1120 return;
1122 printf("VFS: file on dev %d/%d re-open failed: %d\n",
1123 major_dev, minor_dev, r);
1126 /* File descriptor is to be closed when driver restarts. */
1127 n = invalidate_filp(rfilp);
1128 if (n != rfilp->filp_count) {
1129 printf("VFS: warning: invalidate/count "
1130 "discrepancy (%d, %d)\n", n, rfilp->filp_count);
1132 rfilp->filp_count = 0;
1134 /* We have to clean up this filp and vnode, but can't do that yet as
1135 * it's locked by a worker thread. Start a new job to garbage collect
1136 * invalidated filps associated with this device driver.
1138 sys_worker_start(do_filp_gc);
1141 /* Nothing more to re-open. Restart suspended processes */
1142 driver_e = dmap[maj].dmap_driver;
1144 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
1145 if(rfp->fp_pid == PID_FREE) continue;
1146 if(rfp->fp_blocked_on == FP_BLOCKED_ON_OTHER &&
1147 rfp->fp_task == driver_e && (rfp->fp_flags & FP_SUSP_REOPEN)) {
1148 rfp->fp_flags &= ~FP_SUSP_REOPEN;
1149 rfp->fp_blocked_on = FP_BLOCKED_ON_NONE;
1150 reply(rfp->fp_endpoint, ERESTART);
1154 /* Look for processes that are suspened in an OPEN call */
1155 for (rfp = &fproc[0]; rfp < &fproc[NR_PROCS]; rfp++) {
1156 if (rfp->fp_pid == PID_FREE) continue;
1157 if (rfp->fp_blocked_on == FP_BLOCKED_ON_DOPEN ||
1158 !(rfp->fp_flags & FP_SUSP_REOPEN)) continue;
1160 fd_nr = scratch(rfp).file.fd_nr;
1161 printf("VFS: restart_reopen: process in FP_BLOCKED_ON_DOPEN fd=%d\n",
1162 fd_nr);
1163 rfilp = rfp->fp_filp[fd_nr];
1165 if (!rfilp) {
1166 /* Open failed, and automatic reopen was not requested */
1167 rfp->fp_blocked_on = FP_BLOCKED_ON_NONE;
1168 FD_CLR(fd_nr, &rfp->fp_filp_inuse);
1169 reply(rfp->fp_endpoint, EIO);
1170 continue;
1173 vp = rfilp->filp_vno;
1174 if (!vp) panic("VFS: restart_reopen: no vp");
1175 if (!S_ISCHR(vp->v_mode)) continue;
1176 if (major(vp->v_sdev) != maj) continue;
1178 rfp->fp_blocked_on = FP_BLOCKED_ON_NONE;
1179 reply(rfp->fp_endpoint, fd_nr);
1184 /*===========================================================================*
1185 * reopen_reply *
1186 *===========================================================================*/
1187 void reopen_reply()
1189 endpoint_t driver_e;
1190 int filp_no, status, maj;
1191 struct filp *rfilp;
1192 struct vnode *vp;
1193 struct dmap *dp;
1195 driver_e = job_m_in.m_source;
1196 filp_no = job_m_in.REP_ENDPT;
1197 status = job_m_in.REP_STATUS;
1199 if (filp_no < 0 || filp_no >= NR_FILPS) {
1200 printf("VFS: reopen_reply: bad filp number %d from driver %d\n",
1201 filp_no, driver_e);
1202 return;
1205 rfilp = &filp[filp_no];
1206 if (rfilp->filp_count < 1) {
1207 printf("VFS: reopen_reply: filp number %d not inuse (from driver %d)\n",
1208 filp_no, driver_e);
1209 return;
1212 vp = rfilp->filp_vno;
1213 if (!vp) {
1214 printf("VFS: reopen_reply: no vnode for filp number %d (from driver "
1215 "%d)\n", filp_no, driver_e);
1216 return;
1219 if (!(rfilp->filp_state & FS_NEEDS_REOPEN)) {
1220 printf("VFS: reopen_reply: bad state %d for filp number %d"
1221 " (from driver %d)\n", rfilp->filp_state, filp_no, driver_e);
1222 return;
1225 if (!S_ISCHR(vp->v_mode)) {
1226 printf("VFS: reopen_reply: bad mode 0%o for filp number %d"
1227 " (from driver %d)\n", vp->v_mode, filp_no, driver_e);
1228 return;
1231 maj = major(vp->v_sdev);
1232 dp = &dmap[maj];
1233 if (dp->dmap_driver != driver_e) {
1234 printf("VFS: reopen_reply: bad major %d for filp number %d "
1235 "(from driver %d, current driver is %d)\n", maj, filp_no,
1236 driver_e, dp->dmap_driver);
1237 return;
1240 if (status == OK) {
1241 rfilp->filp_state &= ~FS_NEEDS_REOPEN;
1242 } else {
1243 printf("VFS: reopen_reply: should handle error status\n");
1244 return;
1247 restart_reopen(maj);