VFS: make all IPC asynchronous
authorThomas Veerman <thomas@minix3.org>
Tue, 28 Aug 2012 14:06:51 +0000 (14:06 +0000)
committerThomas Veerman <thomas@minix3.org>
Mon, 17 Sep 2012 11:01:45 +0000 (11:01 +0000)
commit992799b91f5950cb676b44cadff7d8c6b7155c38
treee1467b88389624c86578d89637a28e60cf5d5cce
parent7c9e3e24d7c2c225a3756f411a3518557599fa6d
VFS: make all IPC asynchronous

By decoupling synchronous drivers from VFS, we are a big step closer to
supporting driver crashes under all circumstances. That is, VFS can't
become stuck on IPC with a synchronous driver (e.g., INET) and can
recover from crashing block drivers during open/close/ioctl or during
communication with an FS.

In order to maintain serialized communication with a synchronous driver,
the communication is wrapped by a mutex on a per driver basis (not major
numbers as there can be multiple majors with identical endpoints). Majors
that share a driver endpoint point to a single mutex object.

In order to support crashes from block drivers, the file reopen tactic
had to be changed; first reopen files associated with the crashed
driver, then send the new driver endpoint to FSes. This solves a
deadlock between the FS and the block driver;
  - VFS would send REQ_NEW_DRIVER to an FS, but he FS only receives it
    after retrying the current request to the newly started driver.
  - The block driver would refuse the retried request until all files
    had been reopened.
  - VFS would reopen files only after getting a reply from the initial
    REQ_NEW_DRIVER.

When a character special driver crashes, all associated files have to
be marked invalid and closed (or reopened if flagged as such). However,
they can only be closed if a thread holds exclusive access to it. To
obtain exclusive access, the worker thread (which handles the new driver
endpoint event from DS) schedules a new job to garbage collect invalid
files. This way, we can signal the worker thread that was talking to the
crashed driver and will release exclusive access to a file associated
with the crashed driver and prevent the garbage collecting worker thread
from dead locking on that file.

Also, when a character special driver crashes, RS will unmap the driver
and remap it upon restart. During unmapping, associated files are marked
invalid instead of waiting for an endpoint up event from DS, as that
event might come later than new read/write/select requests and thus
cause confusion in the freshly started driver.

When locking a filp, the usage counters are no longer checked. The usage
counter can legally go down to zero during filp invalidation while there
are locks pending.

DS events are handled by a separate worker thread instead of the main
thread as reopening files could lead to another crash and a stuck thread.
An additional worker thread is then necessary to unlock it.

Finally, with everything asynchronous a race condition in do_select
surfaced. A select entry was only marked in use after succesfully sending
initial select requests to drivers and having to wait. When multiple
select() calls were handled there was opportunity that these entries
were overwritten. This had as effect that some select results were
ignored (and select() remained blocking instead if returning) or do_select
tried to access filps that were not present (because thrown away by
secondary select()). This bug manifested itself with sendrecs, but was
very hard to reproduce. However, it became awfully easy to trigger with
asynsends only.
17 files changed:
drivers/tty/tty.c
servers/vfs/comm.c
servers/vfs/const.h
servers/vfs/device.c
servers/vfs/dmap.c
servers/vfs/dmap.h
servers/vfs/file.h
servers/vfs/filedes.c
servers/vfs/fproc.h
servers/vfs/main.c
servers/vfs/misc.c
servers/vfs/open.c
servers/vfs/proto.h
servers/vfs/read.c
servers/vfs/select.c
servers/vfs/threads.h
servers/vfs/worker.c