Currently, F_SETFL always invokes FIONBIO and FIOASYNC ioctls on the
file descriptor even if the state of the associated flag has not
changed. This means that a character device driver that implements
non-blocking I/O but not async I/O needs a handler for FIOASYNC that
permits setting the value to 0. This also means that fcntl(fd,
F_SETFL, fcntl(fd, F_GETFL)) can fail for a character device driver
that does not handle both FIONBIO and FIOASYNC. These requirements
are not obvious nor well documented.
Instead, only invoke FIONBIO and FIOASYNC if the relevant flag changes
state. This only requires a device driver to implement support for
FIONBIO or FIOASYNC if it supports the corresponding flag.
While here, if a request aims to toggle both F_NOBLOCK and F_ASYNC and
FIOASYNC fails, pass the previous state of F_NONBLOCK to FIONBIO
instead of always disabling non-blocking I/O and then possibly
reverting the flag back to on in f_flags.