linux_file.c revision 1.125 1 1.125 christos /* $NetBSD: linux_file.c,v 1.125 2024/09/28 19:35:56 christos Exp $ */
2 1.23 erh
3 1.23 erh /*-
4 1.93 ad * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
5 1.23 erh * All rights reserved.
6 1.23 erh *
7 1.23 erh * This code is derived from software contributed to The NetBSD Foundation
8 1.25 fvdl * by Frank van der Linden and Eric Haszlakiewicz.
9 1.23 erh *
10 1.23 erh * Redistribution and use in source and binary forms, with or without
11 1.23 erh * modification, are permitted provided that the following conditions
12 1.23 erh * are met:
13 1.23 erh * 1. Redistributions of source code must retain the above copyright
14 1.23 erh * notice, this list of conditions and the following disclaimer.
15 1.23 erh * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 erh * notice, this list of conditions and the following disclaimer in the
17 1.23 erh * documentation and/or other materials provided with the distribution.
18 1.23 erh *
19 1.23 erh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.23 erh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.23 erh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.23 erh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.23 erh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.23 erh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.23 erh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.23 erh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.23 erh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.23 erh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.23 erh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 fvdl */
31 1.1 fvdl
32 1.23 erh /*
33 1.23 erh * Functions in multiarch:
34 1.23 erh * linux_sys_llseek : linux_llseek.c
35 1.23 erh */
36 1.43 lukem
37 1.43 lukem #include <sys/cdefs.h>
38 1.125 christos __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.125 2024/09/28 19:35:56 christos Exp $");
39 1.23 erh
40 1.125 christos #include <sys/types.h>
41 1.1 fvdl #include <sys/param.h>
42 1.1 fvdl #include <sys/systm.h>
43 1.1 fvdl #include <sys/namei.h>
44 1.1 fvdl #include <sys/proc.h>
45 1.1 fvdl #include <sys/file.h>
46 1.103 christos #include <sys/fcntl.h>
47 1.1 fvdl #include <sys/stat.h>
48 1.125 christos #include <sys/vfs_syscalls.h>
49 1.1 fvdl #include <sys/filedesc.h>
50 1.1 fvdl #include <sys/ioctl.h>
51 1.1 fvdl #include <sys/kernel.h>
52 1.1 fvdl #include <sys/mount.h>
53 1.81 dsl #include <sys/namei.h>
54 1.13 fvdl #include <sys/vnode.h>
55 1.13 fvdl #include <sys/tty.h>
56 1.39 manu #include <sys/socketvar.h>
57 1.13 fvdl #include <sys/conf.h>
58 1.41 jdolecek #include <sys/pipe.h>
59 1.125 christos #include <sys/fstrans.h>
60 1.1 fvdl #include <sys/syscallargs.h>
61 1.81 dsl #include <sys/vfs_syscalls.h>
62 1.1 fvdl
63 1.24 christos #include <compat/linux/common/linux_types.h>
64 1.24 christos #include <compat/linux/common/linux_signal.h>
65 1.24 christos #include <compat/linux/common/linux_fcntl.h>
66 1.24 christos #include <compat/linux/common/linux_util.h>
67 1.24 christos #include <compat/linux/common/linux_machdep.h>
68 1.84 njoly #include <compat/linux/common/linux_ipc.h>
69 1.84 njoly #include <compat/linux/common/linux_sem.h>
70 1.24 christos
71 1.1 fvdl #include <compat/linux/linux_syscallargs.h>
72 1.14 christos
73 1.125 christos #ifdef DEBUG_LINUX
74 1.125 christos #define DPRINTF(a, ...) uprintf(a, __VA_ARGS__)
75 1.125 christos #else
76 1.125 christos #define DPRINTF(a, ...)
77 1.125 christos #endif
78 1.125 christos
79 1.125 christos #define LINUX_COPY_FILE_RANGE_MAX_CHUNK 8192
80 1.125 christos
81 1.87 dsl static int bsd_to_linux_ioflags(int);
82 1.121 ryo #if !defined(__aarch64__) && !defined(__amd64__)
83 1.87 dsl static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
84 1.67 manu #endif
85 1.14 christos
86 1.92 dsl conv_linux_flock(linux, flock)
87 1.92 dsl
88 1.1 fvdl /*
89 1.1 fvdl * Some file-related calls are handled here. The usual flag conversion
90 1.1 fvdl * an structure conversion is done, and alternate emul path searching.
91 1.1 fvdl */
92 1.1 fvdl
93 1.1 fvdl /*
94 1.1 fvdl * The next two functions convert between the Linux and NetBSD values
95 1.1 fvdl * of the flags used in open(2) and fcntl(2).
96 1.1 fvdl */
97 1.112 njoly int
98 1.88 dsl linux_to_bsd_ioflags(int lflags)
99 1.1 fvdl {
100 1.1 fvdl int res = 0;
101 1.1 fvdl
102 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
103 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
104 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
105 1.113 njoly
106 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
107 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
108 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
109 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
110 1.113 njoly res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
111 1.113 njoly res |= cvtto_bsd_mask(lflags, LINUX_O_NONBLOCK, O_NONBLOCK);
112 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
113 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
114 1.1 fvdl res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
115 1.113 njoly res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECT, O_DIRECT);
116 1.100 chs res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
117 1.113 njoly res |= cvtto_bsd_mask(lflags, LINUX_O_NOFOLLOW, O_NOFOLLOW);
118 1.102 christos res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
119 1.1 fvdl
120 1.1 fvdl return res;
121 1.1 fvdl }
122 1.1 fvdl
123 1.1 fvdl static int
124 1.88 dsl bsd_to_linux_ioflags(int bflags)
125 1.1 fvdl {
126 1.1 fvdl int res = 0;
127 1.1 fvdl
128 1.1 fvdl res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
129 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
130 1.1 fvdl res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
131 1.113 njoly
132 1.1 fvdl res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
133 1.1 fvdl res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
134 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
135 1.1 fvdl res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
136 1.113 njoly res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
137 1.113 njoly res |= cvtto_linux_mask(bflags, O_NONBLOCK, LINUX_O_NONBLOCK);
138 1.1 fvdl res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
139 1.1 fvdl res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
140 1.1 fvdl res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
141 1.113 njoly res |= cvtto_linux_mask(bflags, O_DIRECT, LINUX_O_DIRECT);
142 1.100 chs res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
143 1.113 njoly res |= cvtto_linux_mask(bflags, O_NOFOLLOW, LINUX_O_NOFOLLOW);
144 1.102 christos res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
145 1.1 fvdl
146 1.1 fvdl return res;
147 1.1 fvdl }
148 1.1 fvdl
149 1.120 thorpej static inline off_t
150 1.120 thorpej linux_hilo_to_off_t(unsigned long hi, unsigned long lo)
151 1.120 thorpej {
152 1.120 thorpej #ifdef _LP64
153 1.120 thorpej /*
154 1.120 thorpej * Linux discards the "hi" portion on LP64 platforms; even though
155 1.120 thorpej * glibc puts of the upper 32-bits of the offset into the "hi"
156 1.120 thorpej * argument regardless, the "lo" argument has all the bits in
157 1.120 thorpej * this case.
158 1.120 thorpej */
159 1.120 thorpej (void) hi;
160 1.120 thorpej return (off_t)lo;
161 1.120 thorpej #else
162 1.120 thorpej return (((off_t)hi) << 32) | lo;
163 1.120 thorpej #endif /* _LP64 */
164 1.120 thorpej }
165 1.120 thorpej
166 1.121 ryo #if !defined(__aarch64__)
167 1.1 fvdl /*
168 1.1 fvdl * creat(2) is an obsolete function, but it's present as a Linux
169 1.1 fvdl * system call, so let's deal with it.
170 1.1 fvdl *
171 1.23 erh * Note: On the Alpha this doesn't really exist in Linux, but it's defined
172 1.23 erh * in syscalls.master anyway so this doesn't have to be special cased.
173 1.23 erh *
174 1.1 fvdl * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
175 1.1 fvdl */
176 1.1 fvdl int
177 1.90 dsl linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
178 1.11 thorpej {
179 1.90 dsl /* {
180 1.27 christos syscallarg(const char *) path;
181 1.108 njoly syscallarg(linux_umode_t) mode;
182 1.90 dsl } */
183 1.12 mycroft struct sys_open_args oa;
184 1.1 fvdl
185 1.1 fvdl SCARG(&oa, path) = SCARG(uap, path);
186 1.1 fvdl SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
187 1.1 fvdl SCARG(&oa, mode) = SCARG(uap, mode);
188 1.12 mycroft
189 1.56 thorpej return sys_open(l, &oa, retval);
190 1.1 fvdl }
191 1.121 ryo #endif
192 1.1 fvdl
193 1.106 chs static void
194 1.106 chs linux_open_ctty(struct lwp *l, int flags, int fd)
195 1.106 chs {
196 1.106 chs struct proc *p = l->l_proc;
197 1.106 chs
198 1.106 chs /*
199 1.106 chs * this bit from sunos_misc.c (and svr4_fcntl.c).
200 1.106 chs * If we are a session leader, and we don't have a controlling
201 1.106 chs * terminal yet, and the O_NOCTTY flag is not set, try to make
202 1.106 chs * this the controlling terminal.
203 1.106 chs */
204 1.106 chs if (!(flags & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
205 1.106 chs file_t *fp;
206 1.106 chs
207 1.106 chs fp = fd_getfile(fd);
208 1.106 chs
209 1.106 chs /* ignore any error, just give it a try */
210 1.106 chs if (fp != NULL) {
211 1.106 chs if (fp->f_type == DTYPE_VNODE) {
212 1.106 chs (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
213 1.106 chs }
214 1.106 chs fd_putfile(fd);
215 1.106 chs }
216 1.106 chs }
217 1.106 chs }
218 1.106 chs
219 1.1 fvdl /*
220 1.1 fvdl * open(2). Take care of the different flag values, and let the
221 1.1 fvdl * NetBSD syscall do the real work. See if this operation
222 1.1 fvdl * gives the current process a controlling terminal.
223 1.1 fvdl * (XXX is this necessary?)
224 1.1 fvdl */
225 1.1 fvdl int
226 1.90 dsl linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
227 1.11 thorpej {
228 1.90 dsl /* {
229 1.27 christos syscallarg(const char *) path;
230 1.1 fvdl syscallarg(int) flags;
231 1.108 njoly syscallarg(linux_umode_t) mode;
232 1.90 dsl } */
233 1.1 fvdl int error, fl;
234 1.12 mycroft struct sys_open_args boa;
235 1.1 fvdl
236 1.2 fvdl fl = linux_to_bsd_ioflags(SCARG(uap, flags));
237 1.1 fvdl
238 1.1 fvdl SCARG(&boa, path) = SCARG(uap, path);
239 1.1 fvdl SCARG(&boa, flags) = fl;
240 1.1 fvdl SCARG(&boa, mode) = SCARG(uap, mode);
241 1.2 fvdl
242 1.56 thorpej if ((error = sys_open(l, &boa, retval)))
243 1.113 njoly return (error == EFTYPE) ? ELOOP : error;
244 1.1 fvdl
245 1.106 chs linux_open_ctty(l, fl, *retval);
246 1.106 chs return 0;
247 1.106 chs }
248 1.106 chs
249 1.106 chs int
250 1.106 chs linux_sys_openat(struct lwp *l, const struct linux_sys_openat_args *uap, register_t *retval)
251 1.106 chs {
252 1.106 chs /* {
253 1.107 njoly syscallarg(int) fd;
254 1.106 chs syscallarg(const char *) path;
255 1.106 chs syscallarg(int) flags;
256 1.108 njoly syscallarg(linux_umode_t) mode;
257 1.106 chs } */
258 1.106 chs int error, fl;
259 1.106 chs struct sys_openat_args boa;
260 1.106 chs
261 1.106 chs fl = linux_to_bsd_ioflags(SCARG(uap, flags));
262 1.106 chs
263 1.106 chs SCARG(&boa, fd) = SCARG(uap, fd);
264 1.106 chs SCARG(&boa, path) = SCARG(uap, path);
265 1.106 chs SCARG(&boa, oflags) = fl;
266 1.106 chs SCARG(&boa, mode) = SCARG(uap, mode);
267 1.38 thorpej
268 1.106 chs if ((error = sys_openat(l, &boa, retval)))
269 1.113 njoly return (error == EFTYPE) ? ELOOP : error;
270 1.1 fvdl
271 1.106 chs linux_open_ctty(l, fl, *retval);
272 1.1 fvdl return 0;
273 1.1 fvdl }
274 1.1 fvdl
275 1.1 fvdl /*
276 1.1 fvdl * Most actions in the fcntl() call are straightforward; simply
277 1.1 fvdl * pass control to the NetBSD system call. A few commands need
278 1.1 fvdl * conversions after the actual system call has done its work,
279 1.1 fvdl * because the flag values and lock structure are different.
280 1.1 fvdl */
281 1.1 fvdl int
282 1.90 dsl linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
283 1.11 thorpej {
284 1.90 dsl /* {
285 1.1 fvdl syscallarg(int) fd;
286 1.1 fvdl syscallarg(int) cmd;
287 1.1 fvdl syscallarg(void *) arg;
288 1.90 dsl } */
289 1.56 thorpej struct proc *p = l->l_proc;
290 1.23 erh int fd, cmd, error;
291 1.23 erh u_long val;
292 1.83 dsl void *arg;
293 1.12 mycroft struct sys_fcntl_args fca;
294 1.93 ad file_t *fp;
295 1.13 fvdl struct vnode *vp;
296 1.13 fvdl struct vattr va;
297 1.13 fvdl long pgid;
298 1.13 fvdl struct pgrp *pgrp;
299 1.94 ad struct tty *tp;
300 1.1 fvdl
301 1.1 fvdl fd = SCARG(uap, fd);
302 1.1 fvdl cmd = SCARG(uap, cmd);
303 1.92 dsl arg = SCARG(uap, arg);
304 1.1 fvdl
305 1.1 fvdl switch (cmd) {
306 1.92 dsl
307 1.1 fvdl case LINUX_F_DUPFD:
308 1.1 fvdl cmd = F_DUPFD;
309 1.1 fvdl break;
310 1.92 dsl
311 1.1 fvdl case LINUX_F_GETFD:
312 1.1 fvdl cmd = F_GETFD;
313 1.1 fvdl break;
314 1.92 dsl
315 1.1 fvdl case LINUX_F_SETFD:
316 1.1 fvdl cmd = F_SETFD;
317 1.1 fvdl break;
318 1.92 dsl
319 1.1 fvdl case LINUX_F_GETFL:
320 1.1 fvdl SCARG(&fca, fd) = fd;
321 1.1 fvdl SCARG(&fca, cmd) = F_GETFL;
322 1.1 fvdl SCARG(&fca, arg) = arg;
323 1.56 thorpej if ((error = sys_fcntl(l, &fca, retval)))
324 1.1 fvdl return error;
325 1.1 fvdl retval[0] = bsd_to_linux_ioflags(retval[0]);
326 1.1 fvdl return 0;
327 1.92 dsl
328 1.41 jdolecek case LINUX_F_SETFL: {
329 1.93 ad file_t *fp1 = NULL;
330 1.41 jdolecek
331 1.23 erh val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
332 1.39 manu /*
333 1.41 jdolecek * Linux seems to have same semantics for sending SIGIO to the
334 1.64 abs * read side of socket, but slightly different semantics
335 1.41 jdolecek * for SIGIO to the write side. Rather than sending the SIGIO
336 1.41 jdolecek * every time it's possible to write (directly) more data, it
337 1.41 jdolecek * only sends SIGIO if last write(2) failed due to insufficient
338 1.41 jdolecek * memory to hold the data. This is compatible enough
339 1.41 jdolecek * with NetBSD semantics to not do anything about the
340 1.41 jdolecek * difference.
341 1.65 perry *
342 1.41 jdolecek * Linux does NOT send SIGIO for pipes. Deal with socketpair
343 1.41 jdolecek * ones and DTYPE_PIPE ones. For these, we don't set
344 1.41 jdolecek * the underlying flags (we don't pass O_ASYNC flag down
345 1.41 jdolecek * to sys_fcntl()), but set the FASYNC flag for file descriptor,
346 1.41 jdolecek * so that F_GETFL would report the ASYNC i/o is on.
347 1.39 manu */
348 1.41 jdolecek if (val & O_ASYNC) {
349 1.93 ad if (((fp1 = fd_getfile(fd)) == NULL))
350 1.39 manu return (EBADF);
351 1.69 christos if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
352 1.69 christos && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
353 1.69 christos || (fp1->f_type == DTYPE_PIPE))
354 1.41 jdolecek val &= ~O_ASYNC;
355 1.41 jdolecek else {
356 1.41 jdolecek /* not a pipe, do not modify anything */
357 1.93 ad fd_putfile(fd);
358 1.69 christos fp1 = NULL;
359 1.39 manu }
360 1.41 jdolecek }
361 1.41 jdolecek
362 1.41 jdolecek SCARG(&fca, fd) = fd;
363 1.41 jdolecek SCARG(&fca, cmd) = F_SETFL;
364 1.79 christos SCARG(&fca, arg) = (void *) val;
365 1.39 manu
366 1.56 thorpej error = sys_fcntl(l, &fca, retval);
367 1.41 jdolecek
368 1.41 jdolecek /* Now set the FASYNC flag for pipes */
369 1.69 christos if (fp1) {
370 1.93 ad if (!error) {
371 1.93 ad mutex_enter(&fp1->f_lock);
372 1.69 christos fp1->f_flag |= FASYNC;
373 1.93 ad mutex_exit(&fp1->f_lock);
374 1.93 ad }
375 1.93 ad fd_putfile(fd);
376 1.39 manu }
377 1.41 jdolecek
378 1.41 jdolecek return (error);
379 1.41 jdolecek }
380 1.92 dsl
381 1.1 fvdl case LINUX_F_GETLK:
382 1.92 dsl do_linux_getlk(fd, cmd, arg, linux, flock);
383 1.47 christos
384 1.1 fvdl case LINUX_F_SETLK:
385 1.1 fvdl case LINUX_F_SETLKW:
386 1.92 dsl do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
387 1.47 christos
388 1.1 fvdl case LINUX_F_SETOWN:
389 1.65 perry case LINUX_F_GETOWN:
390 1.13 fvdl /*
391 1.49 jdolecek * We need to route fcntl() for tty descriptors around normal
392 1.49 jdolecek * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
393 1.49 jdolecek * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
394 1.49 jdolecek * this is not a problem.
395 1.13 fvdl */
396 1.93 ad if ((fp = fd_getfile(fd)) == NULL)
397 1.13 fvdl return EBADF;
398 1.60 jdolecek
399 1.60 jdolecek /* Check it's a character device vnode */
400 1.60 jdolecek if (fp->f_type != DTYPE_VNODE
401 1.60 jdolecek || (vp = (struct vnode *)fp->f_data) == NULL
402 1.60 jdolecek || vp->v_type != VCHR) {
403 1.93 ad fd_putfile(fd);
404 1.60 jdolecek
405 1.53 gehenna not_tty:
406 1.49 jdolecek /* Not a tty, proceed with common fcntl() */
407 1.13 fvdl cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
408 1.49 jdolecek break;
409 1.49 jdolecek }
410 1.47 christos
411 1.104 hannken vn_lock(vp, LK_SHARED | LK_RETRY);
412 1.86 pooka error = VOP_GETATTR(vp, &va, l->l_cred);
413 1.104 hannken VOP_UNLOCK(vp);
414 1.60 jdolecek
415 1.93 ad fd_putfile(fd);
416 1.60 jdolecek
417 1.60 jdolecek if (error)
418 1.49 jdolecek return error;
419 1.60 jdolecek
420 1.94 ad if ((tp = cdev_tty(va.va_rdev)) == NULL)
421 1.53 gehenna goto not_tty;
422 1.49 jdolecek
423 1.49 jdolecek /* set tty pg_id appropriately */
424 1.118 ad mutex_enter(&proc_lock);
425 1.49 jdolecek if (cmd == LINUX_F_GETOWN) {
426 1.58 dsl retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
427 1.118 ad mutex_exit(&proc_lock);
428 1.49 jdolecek return 0;
429 1.49 jdolecek }
430 1.49 jdolecek if ((long)arg <= 0) {
431 1.49 jdolecek pgid = -(long)arg;
432 1.49 jdolecek } else {
433 1.99 rmind struct proc *p1 = proc_find((long)arg);
434 1.99 rmind if (p1 == NULL) {
435 1.118 ad mutex_exit(&proc_lock);
436 1.49 jdolecek return (ESRCH);
437 1.99 rmind }
438 1.49 jdolecek pgid = (long)p1->p_pgrp->pg_id;
439 1.13 fvdl }
440 1.99 rmind pgrp = pgrp_find(pgid);
441 1.78 dsl if (pgrp == NULL || pgrp->pg_session != p->p_session) {
442 1.118 ad mutex_exit(&proc_lock);
443 1.49 jdolecek return EPERM;
444 1.78 dsl }
445 1.49 jdolecek tp->t_pgrp = pgrp;
446 1.118 ad mutex_exit(&proc_lock);
447 1.49 jdolecek return 0;
448 1.47 christos
449 1.115 njoly case LINUX_F_DUPFD_CLOEXEC:
450 1.115 njoly cmd = F_DUPFD_CLOEXEC;
451 1.115 njoly break;
452 1.115 njoly
453 1.123 christos case LINUX_F_ADD_SEALS:
454 1.123 christos cmd = F_ADD_SEALS;
455 1.123 christos break;
456 1.123 christos
457 1.123 christos case LINUX_F_GET_SEALS:
458 1.123 christos cmd = F_GET_SEALS;
459 1.123 christos break;
460 1.123 christos
461 1.1 fvdl default:
462 1.1 fvdl return EOPNOTSUPP;
463 1.1 fvdl }
464 1.1 fvdl
465 1.1 fvdl SCARG(&fca, fd) = fd;
466 1.1 fvdl SCARG(&fca, cmd) = cmd;
467 1.1 fvdl SCARG(&fca, arg) = arg;
468 1.12 mycroft
469 1.56 thorpej return sys_fcntl(l, &fca, retval);
470 1.1 fvdl }
471 1.1 fvdl
472 1.121 ryo #if !defined(__aarch64__) && !defined(__amd64__)
473 1.1 fvdl /*
474 1.1 fvdl * Convert a NetBSD stat structure to a Linux stat structure.
475 1.1 fvdl * Only the order of the fields and the padding in the structure
476 1.9 fvdl * is different. linux_fakedev is a machine-dependent function
477 1.9 fvdl * which optionally converts device driver major/minor numbers
478 1.9 fvdl * (XXX horrible, but what can you do against code that compares
479 1.9 fvdl * things against constant major device numbers? sigh)
480 1.1 fvdl */
481 1.1 fvdl static void
482 1.88 dsl bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
483 1.1 fvdl {
484 1.12 mycroft
485 1.119 riastrad memset(lsp, 0, sizeof(*lsp));
486 1.45 christos lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
487 1.1 fvdl lsp->lst_ino = bsp->st_ino;
488 1.19 christos lsp->lst_mode = (linux_mode_t)bsp->st_mode;
489 1.19 christos if (bsp->st_nlink >= (1 << 15))
490 1.19 christos lsp->lst_nlink = (1 << 15) - 1;
491 1.19 christos else
492 1.19 christos lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
493 1.1 fvdl lsp->lst_uid = bsp->st_uid;
494 1.1 fvdl lsp->lst_gid = bsp->st_gid;
495 1.45 christos lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
496 1.1 fvdl lsp->lst_size = bsp->st_size;
497 1.1 fvdl lsp->lst_blksize = bsp->st_blksize;
498 1.1 fvdl lsp->lst_blocks = bsp->st_blocks;
499 1.1 fvdl lsp->lst_atime = bsp->st_atime;
500 1.1 fvdl lsp->lst_mtime = bsp->st_mtime;
501 1.1 fvdl lsp->lst_ctime = bsp->st_ctime;
502 1.66 christos #ifdef LINUX_STAT_HAS_NSEC
503 1.66 christos lsp->lst_atime_nsec = bsp->st_atimensec;
504 1.66 christos lsp->lst_mtime_nsec = bsp->st_mtimensec;
505 1.66 christos lsp->lst_ctime_nsec = bsp->st_ctimensec;
506 1.66 christos #endif
507 1.1 fvdl }
508 1.1 fvdl
509 1.1 fvdl /*
510 1.1 fvdl * The stat functions below are plain sailing. stat and lstat are handled
511 1.1 fvdl * by one function to avoid code duplication.
512 1.1 fvdl */
513 1.1 fvdl int
514 1.90 dsl linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
515 1.11 thorpej {
516 1.90 dsl /* {
517 1.1 fvdl syscallarg(int) fd;
518 1.1 fvdl syscallarg(linux_stat *) sp;
519 1.90 dsl } */
520 1.1 fvdl struct linux_stat tmplst;
521 1.81 dsl struct stat tmpst;
522 1.1 fvdl int error;
523 1.1 fvdl
524 1.93 ad error = do_sys_fstat(SCARG(uap, fd), &tmpst);
525 1.81 dsl if (error != 0)
526 1.1 fvdl return error;
527 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
528 1.1 fvdl
529 1.81 dsl return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
530 1.1 fvdl }
531 1.1 fvdl
532 1.1 fvdl static int
533 1.93 ad linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
534 1.1 fvdl {
535 1.1 fvdl struct linux_stat tmplst;
536 1.81 dsl struct stat tmpst;
537 1.1 fvdl int error;
538 1.1 fvdl
539 1.93 ad error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
540 1.81 dsl if (error != 0)
541 1.1 fvdl return error;
542 1.1 fvdl
543 1.1 fvdl bsd_to_linux_stat(&tmpst, &tmplst);
544 1.1 fvdl
545 1.81 dsl return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
546 1.1 fvdl }
547 1.1 fvdl
548 1.1 fvdl int
549 1.90 dsl linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
550 1.11 thorpej {
551 1.90 dsl /* {
552 1.27 christos syscallarg(const char *) path;
553 1.1 fvdl syscallarg(struct linux_stat *) sp;
554 1.90 dsl } */
555 1.11 thorpej
556 1.93 ad return linux_stat1(uap, retval, FOLLOW);
557 1.1 fvdl }
558 1.1 fvdl
559 1.23 erh /* Note: this is "newlstat" in the Linux sources */
560 1.23 erh /* (we don't bother with the old lstat currently) */
561 1.1 fvdl int
562 1.90 dsl linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
563 1.11 thorpej {
564 1.90 dsl /* {
565 1.27 christos syscallarg(const char *) path;
566 1.1 fvdl syscallarg(struct linux_stat *) sp;
567 1.90 dsl } */
568 1.11 thorpej
569 1.93 ad return linux_stat1((const void *)uap, retval, NOFOLLOW);
570 1.1 fvdl }
571 1.121 ryo #endif /* !__aarch64__ && !__amd64__ */
572 1.1 fvdl
573 1.1 fvdl /*
574 1.10 fvdl * The following syscalls are mostly here because of the alternate path check.
575 1.1 fvdl */
576 1.106 chs
577 1.1 fvdl int
578 1.106 chs linux_sys_linkat(struct lwp *l, const struct linux_sys_linkat_args *uap, register_t *retval)
579 1.2 fvdl {
580 1.90 dsl /* {
581 1.106 chs syscallarg(int) fd1;
582 1.106 chs syscallarg(const char *) name1;
583 1.106 chs syscallarg(int) fd2;
584 1.106 chs syscallarg(const char *) name2;
585 1.106 chs syscallarg(int) flags;
586 1.90 dsl } */
587 1.106 chs int fd1 = SCARG(uap, fd1);
588 1.106 chs const char *name1 = SCARG(uap, name1);
589 1.106 chs int fd2 = SCARG(uap, fd2);
590 1.106 chs const char *name2 = SCARG(uap, name2);
591 1.106 chs int follow;
592 1.106 chs
593 1.106 chs follow = SCARG(uap, flags) & LINUX_AT_SYMLINK_FOLLOW;
594 1.106 chs
595 1.106 chs return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
596 1.106 chs }
597 1.106 chs
598 1.106 chs static int
599 1.106 chs linux_unlink_dircheck(const char *path)
600 1.106 chs {
601 1.106 chs struct nameidata nd;
602 1.101 dholland struct pathbuf *pb;
603 1.106 chs int error;
604 1.63 jdolecek
605 1.63 jdolecek /*
606 1.63 jdolecek * Linux returns EISDIR if unlink(2) is called on a directory.
607 1.63 jdolecek * We return EPERM in such cases. To emulate correct behaviour,
608 1.63 jdolecek * check if the path points to directory and return EISDIR if this
609 1.63 jdolecek * is the case.
610 1.101 dholland *
611 1.101 dholland * XXX this should really not copy in the path buffer twice...
612 1.63 jdolecek */
613 1.106 chs error = pathbuf_copyin(path, &pb);
614 1.106 chs if (error) {
615 1.106 chs return error;
616 1.101 dholland }
617 1.101 dholland NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
618 1.63 jdolecek if (namei(&nd) == 0) {
619 1.63 jdolecek struct stat sb;
620 1.63 jdolecek
621 1.93 ad if (vn_stat(nd.ni_vp, &sb) == 0
622 1.63 jdolecek && S_ISDIR(sb.st_mode))
623 1.63 jdolecek error = EISDIR;
624 1.63 jdolecek
625 1.63 jdolecek vput(nd.ni_vp);
626 1.63 jdolecek }
627 1.101 dholland pathbuf_destroy(pb);
628 1.106 chs return error ? error : EPERM;
629 1.106 chs }
630 1.63 jdolecek
631 1.106 chs int
632 1.106 chs linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
633 1.106 chs {
634 1.106 chs /* {
635 1.106 chs syscallarg(const char *) path;
636 1.106 chs } */
637 1.106 chs int error;
638 1.106 chs
639 1.106 chs error = sys_unlink(l, (const void *)uap, retval);
640 1.106 chs if (error == EPERM)
641 1.106 chs error = linux_unlink_dircheck(SCARG(uap, path));
642 1.106 chs
643 1.106 chs return error;
644 1.106 chs }
645 1.106 chs
646 1.106 chs int
647 1.106 chs linux_sys_unlinkat(struct lwp *l, const struct linux_sys_unlinkat_args *uap, register_t *retval)
648 1.106 chs {
649 1.106 chs /* {
650 1.106 chs syscallarg(int) fd;
651 1.106 chs syscallarg(const char *) path;
652 1.106 chs syscallarg(int) flag;
653 1.106 chs } */
654 1.106 chs struct sys_unlinkat_args ua;
655 1.106 chs int error;
656 1.106 chs
657 1.106 chs SCARG(&ua, fd) = SCARG(uap, fd);
658 1.106 chs SCARG(&ua, path) = SCARG(uap, path);
659 1.106 chs SCARG(&ua, flag) = linux_to_bsd_atflags(SCARG(uap, flag));
660 1.106 chs
661 1.106 chs error = sys_unlinkat(l, &ua, retval);
662 1.106 chs if (error == EPERM)
663 1.106 chs error = linux_unlink_dircheck(SCARG(uap, path));
664 1.106 chs
665 1.106 chs return error;
666 1.2 fvdl }
667 1.2 fvdl
668 1.10 fvdl int
669 1.90 dsl linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
670 1.11 thorpej {
671 1.90 dsl /* {
672 1.27 christos syscallarg(const char *) path;
673 1.106 chs syscallarg(linux_umode_t) mode;
674 1.106 chs syscallarg(unsigned) dev;
675 1.106 chs } */
676 1.106 chs struct linux_sys_mknodat_args ua;
677 1.106 chs
678 1.106 chs SCARG(&ua, fd) = LINUX_AT_FDCWD;
679 1.106 chs SCARG(&ua, path) = SCARG(uap, path);
680 1.106 chs SCARG(&ua, mode) = SCARG(uap, mode);
681 1.106 chs SCARG(&ua, dev) = SCARG(uap, dev);
682 1.106 chs
683 1.106 chs return linux_sys_mknodat(l, &ua, retval);
684 1.106 chs }
685 1.106 chs
686 1.106 chs int
687 1.106 chs linux_sys_mknodat(struct lwp *l, const struct linux_sys_mknodat_args *uap, register_t *retval)
688 1.106 chs {
689 1.106 chs /* {
690 1.106 chs syscallarg(int) fd;
691 1.106 chs syscallarg(const char *) path;
692 1.106 chs syscallarg(linux_umode_t) mode;
693 1.106 chs syscallarg(unsigned) dev;
694 1.90 dsl } */
695 1.2 fvdl
696 1.10 fvdl /*
697 1.40 wiz * BSD handles FIFOs separately
698 1.10 fvdl */
699 1.74 pavel if (S_ISFIFO(SCARG(uap, mode))) {
700 1.106 chs struct sys_mkfifoat_args bma;
701 1.54 jdolecek
702 1.106 chs SCARG(&bma, fd) = SCARG(uap, fd);
703 1.21 thorpej SCARG(&bma, path) = SCARG(uap, path);
704 1.21 thorpej SCARG(&bma, mode) = SCARG(uap, mode);
705 1.106 chs return sys_mkfifoat(l, &bma, retval);
706 1.54 jdolecek } else {
707 1.54 jdolecek
708 1.54 jdolecek /*
709 1.54 jdolecek * Linux device numbers uses 8 bits for minor and 8 bits
710 1.54 jdolecek * for major. Due to how we map our major and minor,
711 1.82 dsl * this just fits into our dev_t. Just mask off the
712 1.54 jdolecek * upper 16bit to remove any random junk.
713 1.54 jdolecek */
714 1.106 chs
715 1.106 chs return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
716 1.116 kamil SCARG(uap, mode), SCARG(uap, dev) & 0xffff, UIO_USERSPACE);
717 1.54 jdolecek }
718 1.2 fvdl }
719 1.2 fvdl
720 1.106 chs int
721 1.106 chs linux_sys_fchmodat(struct lwp *l, const struct linux_sys_fchmodat_args *uap, register_t *retval)
722 1.106 chs {
723 1.106 chs /* {
724 1.106 chs syscallarg(int) fd;
725 1.106 chs syscallarg(const char *) path;
726 1.106 chs syscallarg(linux_umode_t) mode;
727 1.106 chs } */
728 1.106 chs
729 1.106 chs return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
730 1.106 chs SCARG(uap, mode), AT_SYMLINK_FOLLOW);
731 1.106 chs }
732 1.106 chs
733 1.106 chs int
734 1.106 chs linux_sys_fchownat(struct lwp *l, const struct linux_sys_fchownat_args *uap, register_t *retval)
735 1.106 chs {
736 1.106 chs /* {
737 1.106 chs syscallarg(int) fd;
738 1.106 chs syscallarg(const char *) path;
739 1.106 chs syscallarg(uid_t) owner;
740 1.106 chs syscallarg(gid_t) group;
741 1.106 chs syscallarg(int) flag;
742 1.106 chs } */
743 1.106 chs int flag;
744 1.106 chs
745 1.106 chs flag = linux_to_bsd_atflags(SCARG(uap, flag));
746 1.106 chs return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
747 1.106 chs SCARG(uap, owner), SCARG(uap, group), flag);
748 1.106 chs }
749 1.106 chs
750 1.106 chs int
751 1.106 chs linux_sys_faccessat(struct lwp *l, const struct linux_sys_faccessat_args *uap, register_t *retval)
752 1.106 chs {
753 1.106 chs /* {
754 1.106 chs syscallarg(int) fd;
755 1.106 chs syscallarg(const char *) path;
756 1.106 chs syscallarg(int) amode;
757 1.106 chs } */
758 1.106 chs
759 1.106 chs return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
760 1.106 chs SCARG(uap, amode), AT_SYMLINK_FOLLOW);
761 1.106 chs }
762 1.106 chs
763 1.15 fvdl /*
764 1.15 fvdl * This is just fsync() for now (just as it is in the Linux kernel)
765 1.23 erh * Note: this is not implemented under Linux on Alpha and Arm
766 1.23 erh * but should still be defined in our syscalls.master.
767 1.23 erh * (syscall #148 on the arm)
768 1.15 fvdl */
769 1.15 fvdl int
770 1.90 dsl linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
771 1.15 fvdl {
772 1.90 dsl /* {
773 1.15 fvdl syscallarg(int) fd;
774 1.90 dsl } */
775 1.90 dsl
776 1.90 dsl return sys_fsync(l, (const void *)uap, retval);
777 1.28 tron }
778 1.28 tron
779 1.28 tron /*
780 1.28 tron * pread(2).
781 1.28 tron */
782 1.28 tron int
783 1.90 dsl linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
784 1.28 tron {
785 1.90 dsl /* {
786 1.28 tron syscallarg(int) fd;
787 1.28 tron syscallarg(void *) buf;
788 1.28 tron syscallarg(size_t) nbyte;
789 1.109 njoly syscallarg(off_t) offset;
790 1.90 dsl } */
791 1.28 tron struct sys_pread_args pra;
792 1.28 tron
793 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
794 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
795 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
796 1.109 njoly SCARG(&pra, PAD) = 0;
797 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
798 1.28 tron
799 1.62 jdolecek return sys_pread(l, &pra, retval);
800 1.28 tron }
801 1.28 tron
802 1.28 tron /*
803 1.28 tron * pwrite(2).
804 1.28 tron */
805 1.28 tron int
806 1.90 dsl linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
807 1.28 tron {
808 1.90 dsl /* {
809 1.28 tron syscallarg(int) fd;
810 1.28 tron syscallarg(void *) buf;
811 1.28 tron syscallarg(size_t) nbyte;
812 1.110 njoly syscallarg(off_t) offset;
813 1.90 dsl } */
814 1.28 tron struct sys_pwrite_args pra;
815 1.28 tron
816 1.28 tron SCARG(&pra, fd) = SCARG(uap, fd);
817 1.28 tron SCARG(&pra, buf) = SCARG(uap, buf);
818 1.28 tron SCARG(&pra, nbyte) = SCARG(uap, nbyte);
819 1.109 njoly SCARG(&pra, PAD) = 0;
820 1.28 tron SCARG(&pra, offset) = SCARG(uap, offset);
821 1.28 tron
822 1.62 jdolecek return sys_pwrite(l, &pra, retval);
823 1.1 fvdl }
824 1.68 fvdl
825 1.120 thorpej /*
826 1.120 thorpej * preadv(2)
827 1.120 thorpej */
828 1.120 thorpej int
829 1.120 thorpej linux_sys_preadv(struct lwp *l, const struct linux_sys_preadv_args *uap,
830 1.120 thorpej register_t *retval)
831 1.120 thorpej {
832 1.120 thorpej /* {
833 1.120 thorpej syscallarg(int) fd;
834 1.120 thorpej syscallarg(const struct iovec *) iovp;
835 1.120 thorpej syscallarg(int) iovcnt;
836 1.120 thorpej syscallarg(unsigned long) off_lo;
837 1.120 thorpej syscallarg(unsigned long) off_hi;
838 1.120 thorpej } */
839 1.120 thorpej struct sys_preadv_args ua;
840 1.120 thorpej
841 1.120 thorpej SCARG(&ua, fd) = SCARG(uap, fd);
842 1.120 thorpej SCARG(&ua, iovp) = SCARG(uap, iovp);
843 1.120 thorpej SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
844 1.120 thorpej SCARG(&ua, PAD) = 0;
845 1.120 thorpej SCARG(&ua, offset) = linux_hilo_to_off_t(SCARG(uap, off_hi),
846 1.120 thorpej SCARG(uap, off_lo));
847 1.120 thorpej return sys_preadv(l, &ua, retval);
848 1.120 thorpej }
849 1.120 thorpej
850 1.120 thorpej /*
851 1.120 thorpej * pwritev(2)
852 1.120 thorpej */
853 1.120 thorpej int
854 1.120 thorpej linux_sys_pwritev(struct lwp *l, const struct linux_sys_pwritev_args *uap,
855 1.120 thorpej register_t *retval)
856 1.120 thorpej {
857 1.120 thorpej /* {
858 1.120 thorpej syscallarg(int) fd;
859 1.120 thorpej syscallarg(const struct iovec *) iovp;
860 1.120 thorpej syscallarg(int) iovcnt;
861 1.120 thorpej syscallarg(unsigned long) off_lo;
862 1.120 thorpej syscallarg(unsigned long) off_hi;
863 1.120 thorpej } */
864 1.120 thorpej struct sys_pwritev_args ua;
865 1.120 thorpej
866 1.120 thorpej SCARG(&ua, fd) = SCARG(uap, fd);
867 1.120 thorpej SCARG(&ua, iovp) = (const void *)SCARG(uap, iovp);
868 1.120 thorpej SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
869 1.120 thorpej SCARG(&ua, PAD) = 0;
870 1.120 thorpej SCARG(&ua, offset) = linux_hilo_to_off_t(SCARG(uap, off_hi),
871 1.120 thorpej SCARG(uap, off_lo));
872 1.120 thorpej return sys_pwritev(l, &ua, retval);
873 1.120 thorpej }
874 1.120 thorpej
875 1.103 christos int
876 1.103 christos linux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
877 1.103 christos register_t *retval)
878 1.103 christos {
879 1.103 christos /* {
880 1.103 christos syscallarg(int) from;
881 1.103 christos syscallarg(int) to;
882 1.103 christos syscallarg(int) flags;
883 1.103 christos } */
884 1.111 njoly int flags;
885 1.111 njoly
886 1.111 njoly flags = linux_to_bsd_ioflags(SCARG(uap, flags));
887 1.111 njoly if ((flags & ~O_CLOEXEC) != 0)
888 1.111 njoly return EINVAL;
889 1.103 christos
890 1.111 njoly if (SCARG(uap, from) == SCARG(uap, to))
891 1.111 njoly return EINVAL;
892 1.103 christos
893 1.111 njoly return dodup(l, SCARG(uap, from), SCARG(uap, to), flags, retval);
894 1.103 christos }
895 1.103 christos
896 1.105 njoly
897 1.105 njoly int
898 1.105 njoly linux_to_bsd_atflags(int lflags)
899 1.105 njoly {
900 1.105 njoly int bflags = 0;
901 1.105 njoly
902 1.105 njoly if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
903 1.105 njoly bflags |= AT_SYMLINK_NOFOLLOW;
904 1.105 njoly if (lflags & LINUX_AT_REMOVEDIR)
905 1.105 njoly bflags |= AT_REMOVEDIR;
906 1.105 njoly if (lflags & LINUX_AT_SYMLINK_FOLLOW)
907 1.105 njoly bflags |= AT_SYMLINK_FOLLOW;
908 1.105 njoly
909 1.105 njoly return bflags;
910 1.105 njoly }
911 1.105 njoly
912 1.124 christos int
913 1.124 christos linux_sys_faccessat2(lwp_t *l, const struct linux_sys_faccessat2_args *uap,
914 1.124 christos register_t *retval)
915 1.124 christos {
916 1.124 christos /* {
917 1.124 christos syscallarg(int) fd;
918 1.124 christos syscallarg(const char *) path;
919 1.124 christos syscallarg(int) amode;
920 1.124 christos syscallarg(int) flags;
921 1.124 christos }*/
922 1.124 christos int flag = linux_to_bsd_atflags(SCARG(uap, flags));
923 1.124 christos int mode = SCARG(uap, amode);
924 1.124 christos int fd = SCARG(uap, fd);
925 1.124 christos const char *path = SCARG(uap, path);
926 1.124 christos
927 1.124 christos return do_sys_accessat(l, fd, path, mode, flag);
928 1.124 christos }
929 1.124 christos
930 1.105 njoly
931 1.125 christos int
932 1.125 christos linux_sys_sync_file_range(lwp_t *l,
933 1.125 christos const struct linux_sys_sync_file_range_args *uap, register_t *retval)
934 1.125 christos {
935 1.125 christos /* {
936 1.125 christos syscallarg(int) fd;
937 1.125 christos syscallarg(off_t) offset;
938 1.125 christos syscallarg(off_t) nbytes;
939 1.125 christos syscallarg(unsigned int) flags;
940 1.125 christos } */
941 1.125 christos
942 1.125 christos struct sys_fsync_range_args ua;
943 1.125 christos
944 1.125 christos if (SCARG(uap, offset) < 0 || SCARG(uap, nbytes) < 0 ||
945 1.125 christos ((SCARG(uap, flags) & ~LINUX_SYNC_FILE_RANGE_ALL) != 0))
946 1.125 christos return EINVAL;
947 1.125 christos
948 1.125 christos /* Fill ua with uap */
949 1.125 christos SCARG(&ua, fd) = SCARG(uap, fd);
950 1.125 christos SCARG(&ua, flags) = SCARG(uap, flags);
951 1.125 christos
952 1.125 christos /* Round down offset to page boundary */
953 1.125 christos SCARG(&ua, start) = rounddown(SCARG(uap, offset), PAGE_SIZE);
954 1.125 christos SCARG(&ua, length) = SCARG(uap, nbytes);
955 1.125 christos if (SCARG(&ua, length) != 0) {
956 1.125 christos /* Round up length to nbytes+offset to page boundary */
957 1.125 christos SCARG(&ua, length) = roundup(SCARG(uap, nbytes)
958 1.125 christos + SCARG(uap, offset) - SCARG(&ua, start), PAGE_SIZE);
959 1.125 christos }
960 1.125 christos
961 1.125 christos return sys_fsync_range(l, &ua, retval);
962 1.125 christos }
963 1.125 christos
964 1.125 christos int
965 1.125 christos linux_sys_syncfs(lwp_t *l, const struct linux_sys_syncfs_args *uap,
966 1.125 christos register_t *retval)
967 1.125 christos {
968 1.125 christos /* {
969 1.125 christos syscallarg(int) fd;
970 1.125 christos } */
971 1.125 christos
972 1.125 christos struct mount *mp;
973 1.125 christos struct vnode *vp;
974 1.125 christos file_t *fp;
975 1.125 christos int error, fd;
976 1.125 christos fd = SCARG(uap, fd);
977 1.125 christos
978 1.125 christos /* Get file pointer */
979 1.125 christos if ((error = fd_getvnode(fd, &fp)) != 0)
980 1.125 christos return error;
981 1.125 christos
982 1.125 christos /* Get vnode and mount point */
983 1.125 christos vp = fp->f_vnode;
984 1.125 christos mp = vp->v_mount;
985 1.125 christos
986 1.125 christos mutex_enter(mp->mnt_updating);
987 1.125 christos if ((mp->mnt_flag & MNT_RDONLY) == 0) {
988 1.125 christos int asyncflag = mp->mnt_flag & MNT_ASYNC;
989 1.125 christos mp->mnt_flag &= ~MNT_ASYNC;
990 1.125 christos VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
991 1.125 christos if (asyncflag)
992 1.125 christos mp->mnt_flag |= MNT_ASYNC;
993 1.125 christos }
994 1.125 christos mutex_exit(mp->mnt_updating);
995 1.125 christos
996 1.125 christos /* Cleanup vnode and file pointer */
997 1.125 christos vrele(vp);
998 1.125 christos fd_putfile(fd);
999 1.125 christos return 0;
1000 1.125 christos
1001 1.125 christos }
1002 1.125 christos
1003 1.125 christos int
1004 1.125 christos linux_sys_renameat2(struct lwp *l, const struct linux_sys_renameat2_args *uap,
1005 1.125 christos register_t *retval)
1006 1.125 christos {
1007 1.125 christos /* {
1008 1.125 christos syscallarg(int) fromfd;
1009 1.125 christos syscallarg(const char *) from;
1010 1.125 christos syscallarg(int) tofd;
1011 1.125 christos syscallarg(const char *) to;
1012 1.125 christos syscallarg(unsigned int) flags;
1013 1.125 christos } */
1014 1.125 christos
1015 1.125 christos struct sys_renameat_args ua;
1016 1.125 christos SCARG(&ua, fromfd) = SCARG(uap, fromfd);
1017 1.125 christos SCARG(&ua, from) = SCARG(uap, from);
1018 1.125 christos SCARG(&ua, tofd) = SCARG(uap, tofd);
1019 1.125 christos SCARG(&ua, to) = SCARG(uap, to);
1020 1.125 christos
1021 1.125 christos unsigned int flags = SCARG(uap, flags);
1022 1.125 christos int error;
1023 1.125 christos
1024 1.125 christos if (flags != 0) {
1025 1.125 christos if (flags & ~LINUX_RENAME_ALL)
1026 1.125 christos return EINVAL;
1027 1.125 christos if ((flags & LINUX_RENAME_EXCHANGE) != 0 &&
1028 1.125 christos (flags & (LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
1029 1.125 christos != 0)
1030 1.125 christos return EINVAL;
1031 1.125 christos /*
1032 1.125 christos * Suppoting renameat2 flags without support from file systems
1033 1.125 christos * becomes a messy affair cause of locks and how VOP_RENAME
1034 1.125 christos * protocol is implemented. So, return EOPNOTSUPP for now.
1035 1.125 christos */
1036 1.125 christos return EOPNOTSUPP;
1037 1.125 christos }
1038 1.125 christos
1039 1.125 christos error = sys_renameat(l, &ua, retval);
1040 1.125 christos return error;
1041 1.125 christos }
1042 1.125 christos
1043 1.125 christos int linux_sys_copy_file_range(lwp_t *l,
1044 1.125 christos const struct linux_sys_copy_file_range_args *uap, register_t *retval)
1045 1.125 christos {
1046 1.125 christos /* {
1047 1.125 christos syscallarg(int) fd_in;
1048 1.125 christos syscallarg(unsigned long) off_in;
1049 1.125 christos syscallarg(int) fd_out;
1050 1.125 christos syscallarg(unsigned long) off_out;
1051 1.125 christos syscallarg(size_t) len;
1052 1.125 christos syscallarg(unsigned int) flags;
1053 1.125 christos } */
1054 1.125 christos
1055 1.125 christos int fd_in, fd_out;
1056 1.125 christos file_t *fp_in, *fp_out;
1057 1.125 christos struct vnode *invp, *outvp;
1058 1.125 christos off_t off_in = 0, off_out = 0;
1059 1.125 christos struct vattr vattr_in, vattr_out;
1060 1.125 christos ssize_t total_copied = 0;
1061 1.125 christos size_t bytes_left, to_copy;
1062 1.125 christos bool have_off_in = false, have_off_out = false;
1063 1.125 christos int error = 0;
1064 1.125 christos size_t len = SCARG(uap, len);
1065 1.125 christos unsigned int flags = SCARG(uap, flags);
1066 1.125 christos // Structures for actual copy
1067 1.125 christos char *buffer = NULL;
1068 1.125 christos struct uio auio;
1069 1.125 christos struct iovec aiov;
1070 1.125 christos
1071 1.125 christos
1072 1.125 christos if (len > SSIZE_MAX) {
1073 1.125 christos DPRINTF("%s: len is greater than SSIZE_MAX\n",
1074 1.125 christos __func__);
1075 1.125 christos return EOVERFLOW;
1076 1.125 christos }
1077 1.125 christos
1078 1.125 christos if(flags != 0) {
1079 1.125 christos DPRINTF("%s: unsupported flags %#x\n", __func__, flags);
1080 1.125 christos return EINVAL;
1081 1.125 christos }
1082 1.125 christos
1083 1.125 christos fd_in = SCARG(uap, fd_in);
1084 1.125 christos fd_out = SCARG(uap, fd_out);
1085 1.125 christos error = fd_getvnode(fd_in, &fp_in);
1086 1.125 christos if (error) {
1087 1.125 christos return error;
1088 1.125 christos }
1089 1.125 christos
1090 1.125 christos error = fd_getvnode(fd_out, &fp_out);
1091 1.125 christos if (error) {
1092 1.125 christos fd_putfile(fd_in);
1093 1.125 christos return error;
1094 1.125 christos }
1095 1.125 christos
1096 1.125 christos invp = fp_in->f_vnode;
1097 1.125 christos outvp = fp_out->f_vnode;
1098 1.125 christos
1099 1.125 christos /* Get attributes of input and output files */
1100 1.125 christos VOP_GETATTR(invp, &vattr_in, l->l_cred);
1101 1.125 christos VOP_GETATTR(outvp, &vattr_out, l->l_cred);
1102 1.125 christos
1103 1.125 christos /* Check if input and output files are regular files */
1104 1.125 christos if (vattr_in.va_type == VDIR || vattr_out.va_type == VDIR) {
1105 1.125 christos error = EISDIR;
1106 1.125 christos DPRINTF("%s: Input or output is a directory\n", __func__);
1107 1.125 christos goto out;
1108 1.125 christos }
1109 1.125 christos if ((SCARG(uap, off_in) != NULL && *SCARG(uap, off_in) < 0) ||
1110 1.125 christos (SCARG(uap, off_out) != NULL && *SCARG(uap, off_out) < 0) ||
1111 1.125 christos vattr_in.va_type != VREG || vattr_out.va_type != VREG)
1112 1.125 christos {
1113 1.125 christos error = EINVAL;
1114 1.125 christos DPRINTF("%s: Invalid offset or file type\n", __func__);
1115 1.125 christos goto out;
1116 1.125 christos }
1117 1.125 christos
1118 1.125 christos if ((fp_in->f_flag & FREAD) == 0 ||
1119 1.125 christos (fp_out->f_flag & FWRITE) == 0 || (fp_out->f_flag & FAPPEND) != 0) {
1120 1.125 christos DPRINTF("%s: input file can't be read or output file "
1121 1.125 christos "can't be written\n", __func__);
1122 1.125 christos error = EBADF;
1123 1.125 christos goto out;
1124 1.125 christos }
1125 1.125 christos /* Retrieve and validate offsets if provided */
1126 1.125 christos if (SCARG(uap, off_in) != NULL) {
1127 1.125 christos error = copyin(SCARG(uap, off_in), &off_in, sizeof(off_in));
1128 1.125 christos if (error) {
1129 1.125 christos goto out;
1130 1.125 christos }
1131 1.125 christos have_off_in = true;
1132 1.125 christos }
1133 1.125 christos
1134 1.125 christos if (SCARG(uap, off_out) != NULL) {
1135 1.125 christos error = copyin(SCARG(uap, off_out), &off_out, sizeof(off_out));
1136 1.125 christos if (error) {
1137 1.125 christos goto out;
1138 1.125 christos }
1139 1.125 christos have_off_out = true;
1140 1.125 christos }
1141 1.125 christos
1142 1.125 christos off_t new_size = off_out + len;
1143 1.125 christos if (new_size < 0) {
1144 1.125 christos DPRINTF("%s: New size is greater than OFF_MAX\n", __func__);
1145 1.125 christos error = EFBIG;
1146 1.125 christos goto out;
1147 1.125 christos }
1148 1.125 christos
1149 1.125 christos /* Identify overlapping ranges */
1150 1.125 christos if ((invp == outvp) &&
1151 1.125 christos ((off_in <= off_out && off_in + (off_t)len > off_out) ||
1152 1.125 christos (off_in > off_out && off_out + (off_t)len > off_in))) {
1153 1.125 christos DPRINTF("%s: Ranges overlap\n", __func__);
1154 1.125 christos error = EINVAL;
1155 1.125 christos goto out;
1156 1.125 christos }
1157 1.125 christos
1158 1.125 christos buffer = kmem_alloc(LINUX_COPY_FILE_RANGE_MAX_CHUNK, KM_SLEEP);
1159 1.125 christos /* Allocation cannot fail, so no need for error handling? */
1160 1.125 christos if (buffer == NULL) {
1161 1.125 christos error = ENOMEM;
1162 1.125 christos goto out;
1163 1.125 christos }
1164 1.125 christos
1165 1.125 christos bytes_left = len;
1166 1.125 christos
1167 1.125 christos while (bytes_left > 0) {
1168 1.125 christos to_copy = MIN(bytes_left, LINUX_COPY_FILE_RANGE_MAX_CHUNK);
1169 1.125 christos
1170 1.125 christos /* Lock the input vnode for reading */
1171 1.125 christos vn_lock(fp_in->f_vnode, LK_SHARED | LK_RETRY);
1172 1.125 christos /* Set up iovec and uio for reading */
1173 1.125 christos aiov.iov_base = buffer;
1174 1.125 christos aiov.iov_len = to_copy;
1175 1.125 christos auio.uio_iov = &aiov;
1176 1.125 christos auio.uio_iovcnt = 1;
1177 1.125 christos auio.uio_offset = have_off_in ? off_in : fp_in->f_offset;
1178 1.125 christos auio.uio_resid = to_copy;
1179 1.125 christos auio.uio_rw = UIO_READ;
1180 1.125 christos auio.uio_vmspace = l->l_proc->p_vmspace;
1181 1.125 christos UIO_SETUP_SYSSPACE(&auio);
1182 1.125 christos
1183 1.125 christos /* Perform read using vn_read */
1184 1.125 christos error = VOP_READ(fp_in->f_vnode, &auio, 0, l->l_cred);
1185 1.125 christos VOP_UNLOCK(fp_in->f_vnode);
1186 1.125 christos if (error) {
1187 1.125 christos DPRINTF("%s: Read error %d\n", __func__, error);
1188 1.125 christos break;
1189 1.125 christos }
1190 1.125 christos
1191 1.125 christos size_t read_bytes = to_copy - auio.uio_resid;
1192 1.125 christos if (read_bytes == 0) {
1193 1.125 christos /* EOF reached */
1194 1.125 christos break;
1195 1.125 christos }
1196 1.125 christos
1197 1.125 christos /* Lock the output vnode for writing */
1198 1.125 christos vn_lock(fp_out->f_vnode, LK_EXCLUSIVE | LK_RETRY);
1199 1.125 christos /* Set up iovec and uio for writing */
1200 1.125 christos aiov.iov_base = buffer;
1201 1.125 christos aiov.iov_len = read_bytes;
1202 1.125 christos auio.uio_iov = &aiov;
1203 1.125 christos auio.uio_iovcnt = 1;
1204 1.125 christos auio.uio_offset = have_off_out ? off_out : fp_out->f_offset;
1205 1.125 christos auio.uio_resid = read_bytes;
1206 1.125 christos auio.uio_rw = UIO_WRITE;
1207 1.125 christos auio.uio_vmspace = l->l_proc->p_vmspace;
1208 1.125 christos UIO_SETUP_SYSSPACE(&auio);
1209 1.125 christos
1210 1.125 christos /* Perform the write */
1211 1.125 christos error = VOP_WRITE(fp_out->f_vnode, &auio, 0, l->l_cred);
1212 1.125 christos VOP_UNLOCK(fp_out->f_vnode);
1213 1.125 christos if (error) {
1214 1.125 christos DPRINTF("%s: Write error %d\n", __func__, error);
1215 1.125 christos break;
1216 1.125 christos }
1217 1.125 christos size_t written_bytes = read_bytes - auio.uio_resid;
1218 1.125 christos total_copied += written_bytes;
1219 1.125 christos bytes_left -= written_bytes;
1220 1.125 christos
1221 1.125 christos /* Update offsets if provided */
1222 1.125 christos if (have_off_in) {
1223 1.125 christos off_in += written_bytes;
1224 1.125 christos } else {
1225 1.125 christos fp_in->f_offset += written_bytes;
1226 1.125 christos }
1227 1.125 christos if (have_off_out) {
1228 1.125 christos off_out += written_bytes;
1229 1.125 christos } else {
1230 1.125 christos fp_out->f_offset += written_bytes;
1231 1.125 christos }
1232 1.125 christos }
1233 1.125 christos
1234 1.125 christos if (have_off_in) {
1235 1.125 christos /* Adjust user space offset */
1236 1.125 christos error = copyout(&off_in, SCARG(uap, off_in), sizeof(off_t));
1237 1.125 christos if (error) {
1238 1.125 christos DPRINTF("%s: Error adjusting user space offset\n",
1239 1.125 christos __func__);
1240 1.125 christos }
1241 1.125 christos goto out;
1242 1.125 christos }
1243 1.125 christos
1244 1.125 christos if (have_off_out) {
1245 1.125 christos /* Adjust user space offset */
1246 1.125 christos error = copyout(&off_out, SCARG(uap, off_out), sizeof(off_t));
1247 1.125 christos if (error) {
1248 1.125 christos DPRINTF("%s: Error adjusting user space offset\n",
1249 1.125 christos __func__);
1250 1.125 christos }
1251 1.125 christos }
1252 1.125 christos
1253 1.125 christos *retval = total_copied;
1254 1.125 christos out:
1255 1.125 christos if (buffer) {
1256 1.125 christos kmem_free(buffer, LINUX_COPY_FILE_RANGE_MAX_CHUNK);
1257 1.125 christos }
1258 1.125 christos if (fp_out) {
1259 1.125 christos fd_putfile(fd_out);
1260 1.125 christos }
1261 1.125 christos if (fp_in) {
1262 1.125 christos fd_putfile(fd_in);
1263 1.125 christos }
1264 1.125 christos return error;
1265 1.125 christos }
1266 1.125 christos
1267 1.125 christos
1268 1.75 christos #define LINUX_NOT_SUPPORTED(fun) \
1269 1.75 christos int \
1270 1.90 dsl fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
1271 1.75 christos { \
1272 1.75 christos return EOPNOTSUPP; \
1273 1.75 christos }
1274 1.75 christos
1275 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
1276 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
1277 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
1278 1.75 christos
1279 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
1280 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
1281 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
1282 1.75 christos
1283 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
1284 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
1285 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
1286 1.75 christos
1287 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
1288 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
1289 1.75 christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
1290 1.117 jdolecek
1291 1.117 jdolecek /*
1292 1.117 jdolecek * For now just return EOPNOTSUPP, this makes glibc posix_fallocate()
1293 1.117 jdolecek * to fallback to emulation.
1294 1.117 jdolecek * XXX Right now no filesystem actually implements fallocate support,
1295 1.117 jdolecek * so no need for mapping.
1296 1.117 jdolecek */
1297 1.117 jdolecek LINUX_NOT_SUPPORTED(linux_sys_fallocate)
1298