1*a200c8e5SJean-Sébastien Pédron /*-
2*a200c8e5SJean-Sébastien Pédron * Copyright (c) 2025 The FreeBSD Foundation
3*a200c8e5SJean-Sébastien Pédron * Copyright (c) 2025 Jean-Sébastien Pédron
4*a200c8e5SJean-Sébastien Pédron *
5*a200c8e5SJean-Sébastien Pédron * This software was developed by Jean-Sébastien Pédron under sponsorship
6*a200c8e5SJean-Sébastien Pédron * from the FreeBSD Foundation.
7*a200c8e5SJean-Sébastien Pédron *
8*a200c8e5SJean-Sébastien Pédron * Redistribution and use in source and binary forms, with or without
9*a200c8e5SJean-Sébastien Pédron * modification, are permitted provided that the following conditions
10*a200c8e5SJean-Sébastien Pédron * are met:
11*a200c8e5SJean-Sébastien Pédron * 1. Redistributions of source code must retain the above copyright
12*a200c8e5SJean-Sébastien Pédron * notice, this list of conditions and the following disclaimer.
13*a200c8e5SJean-Sébastien Pédron * 2. Redistributions in binary form must reproduce the above copyright
14*a200c8e5SJean-Sébastien Pédron * notice, this list of conditions and the following disclaimer in the
15*a200c8e5SJean-Sébastien Pédron * documentation and/or other materials provided with the distribution.
16*a200c8e5SJean-Sébastien Pédron *
17*a200c8e5SJean-Sébastien Pédron * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*a200c8e5SJean-Sébastien Pédron * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*a200c8e5SJean-Sébastien Pédron * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*a200c8e5SJean-Sébastien Pédron * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*a200c8e5SJean-Sébastien Pédron * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*a200c8e5SJean-Sébastien Pédron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*a200c8e5SJean-Sébastien Pédron * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*a200c8e5SJean-Sébastien Pédron * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*a200c8e5SJean-Sébastien Pédron * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*a200c8e5SJean-Sébastien Pédron * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*a200c8e5SJean-Sébastien Pédron * SUCH DAMAGE.
28*a200c8e5SJean-Sébastien Pédron */
29*a200c8e5SJean-Sébastien Pédron
30*a200c8e5SJean-Sébastien Pédron #include <sys/param.h>
31*a200c8e5SJean-Sébastien Pédron #include <sys/file.h>
32*a200c8e5SJean-Sébastien Pédron #include <sys/filedesc.h>
33*a200c8e5SJean-Sébastien Pédron
34*a200c8e5SJean-Sébastien Pédron #include <linux/eventfd.h>
35*a200c8e5SJean-Sébastien Pédron
36*a200c8e5SJean-Sébastien Pédron struct eventfd_ctx *
lkpi_eventfd_ctx_fdget(int fd)37*a200c8e5SJean-Sébastien Pédron lkpi_eventfd_ctx_fdget(int fd)
38*a200c8e5SJean-Sébastien Pédron {
39*a200c8e5SJean-Sébastien Pédron struct file *fp;
40*a200c8e5SJean-Sébastien Pédron struct eventfd_ctx *ctx;
41*a200c8e5SJean-Sébastien Pédron
42*a200c8e5SJean-Sébastien Pédron /* Lookup file pointer by file descriptor index. */
43*a200c8e5SJean-Sébastien Pédron if (fget_unlocked(curthread, fd, &cap_no_rights, &fp) != 0)
44*a200c8e5SJean-Sébastien Pédron return (ERR_PTR(-EBADF));
45*a200c8e5SJean-Sébastien Pédron
46*a200c8e5SJean-Sébastien Pédron /*
47*a200c8e5SJean-Sébastien Pédron * eventfd_get() bumps the refcount, so we can safely release the
48*a200c8e5SJean-Sébastien Pédron * reference on the file itself afterwards.
49*a200c8e5SJean-Sébastien Pédron */
50*a200c8e5SJean-Sébastien Pédron ctx = eventfd_get(fp);
51*a200c8e5SJean-Sébastien Pédron fdrop(fp, curthread);
52*a200c8e5SJean-Sébastien Pédron
53*a200c8e5SJean-Sébastien Pédron if (ctx == NULL)
54*a200c8e5SJean-Sébastien Pédron return (ERR_PTR(-EBADF));
55*a200c8e5SJean-Sébastien Pédron
56*a200c8e5SJean-Sébastien Pédron return (ctx);
57*a200c8e5SJean-Sébastien Pédron }
58*a200c8e5SJean-Sébastien Pédron
59*a200c8e5SJean-Sébastien Pédron void
lkpi_eventfd_ctx_put(struct eventfd_ctx * ctx)60*a200c8e5SJean-Sébastien Pédron lkpi_eventfd_ctx_put(struct eventfd_ctx *ctx)
61*a200c8e5SJean-Sébastien Pédron {
62*a200c8e5SJean-Sébastien Pédron eventfd_put(ctx);
63*a200c8e5SJean-Sébastien Pédron }
64