/*-
* Copyright (c) 2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* eventfd
*
* Eventfd objects present a simple counting object associated with a
* file descriptor. Writes and reads to this file descriptor increment
* and decrement the count, respectively. When the count is non-zero,
* the descriptor is considered "readable", and when less than the max
* value (EVENTFD_MAXVAL), is considered "writable".
*
* This implementation is API compatible with the Linux eventfd(2)
* interface.
*/
/*
* Information kept for stat(2).
*/
struct timespec efd_btime; /* time created */
struct timespec efd_mtime; /* last write */
struct timespec efd_atime; /* last read */
};
#define EVENTFD_MAXVAL (UINT64_MAX - 1)
/*
* eventfd_create:
*
* Create an eventfd object.
*/
static struct eventfd *
eventfd_create(unsigned int const val, int const flags)
{
struct eventfd * const efd = kmem_zalloc(sizeof(*efd), KM_SLEEP);
/*
* eventfd_wait:
*
* Block on an eventfd. Handles non-blocking, as well as
* the restart cases.
*/
static int
eventfd_wait(struct eventfd * const efd, int const fflag, bool const is_write)
{
kcondvar_t *waitcv;
int error;
if (fflag & FNONBLOCK) {
return EAGAIN;
}
/*
* We're going to block. Check if we need to return ERESTART.
*/
if (efd->efd_restarting) {
return ERESTART;
}
/*
* If a restart was triggered while we were asleep, we need
* to return ERESTART if no other error was returned.
*/
if (efd->efd_restarting) {
if (error == 0) {
error = ERESTART;
}
}
return error;
}
/*
* eventfd_wake:
*
* Wake LWPs block on an eventfd.
*/
static void
eventfd_wake(struct eventfd * const efd, bool const is_write)
{
kcondvar_t *waitcv = NULL;
struct selinfo *sel;
int pollev;
out:
if (error) {
/*
* Undo the effect of uiomove() so that the error
* gets reported correctly; see dofilewrite().
*/
uio->uio_resid += sizeof(write_value);
}
return error;
}
case FIONSPACE:
/*
* FIONSPACE doesn't really work for eventfd, because the
* writability depends on the contents (value) being written.
*/
break;
default:
break;
}
return EPASSTHROUGH;
}
static int
eventfd_fop_poll(file_t * const fp, int const events)
{
struct eventfd * const efd = fp->f_eventfd;
int revents = 0;
/*
* Note that Linux will return POLLERR if the eventfd count
* overflows, but that is not possible in the normal read/write
* API, only with Linux kernel-internal interfaces. So, this
* implementation never returns POLLERR.
*
* Also note that the Linux eventfd(2) man page does not
* specifically discuss returning POLLRDNORM, but we check
* for that event in addition to POLLIN.
*/