1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1994-1995 Søren Schmidt
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/dirent.h>
32 #include <sys/fcntl.h>
33 #include <sys/file.h>
34 #include <sys/filedesc.h>
35 #include <sys/inotify.h>
36 #include <sys/lock.h>
37 #include <sys/mman.h>
38 #include <sys/selinfo.h>
39 #include <sys/pipe.h>
40 #include <sys/proc.h>
41 #include <sys/specialfd.h>
42 #include <sys/stat.h>
43 #include <sys/sx.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysproto.h>
46 #include <sys/tty.h>
47 #include <sys/unistd.h>
48 #include <sys/vnode.h>
49
50 #ifdef COMPAT_LINUX32
51 #include <compat/freebsd32/freebsd32_misc.h>
52 #include <compat/freebsd32/freebsd32_util.h>
53 #include <machine/../linux32/linux.h>
54 #include <machine/../linux32/linux32_proto.h>
55 #else
56 #include <machine/../linux/linux.h>
57 #include <machine/../linux/linux_proto.h>
58 #endif
59 #include <compat/linux/linux_misc.h>
60 #include <compat/linux/linux_util.h>
61 #include <compat/linux/linux_file.h>
62
63 static int linux_common_open(struct thread *, int, const char *, int, int,
64 enum uio_seg);
65 static int linux_do_accessat(struct thread *t, int, const char *, int, int);
66 static int linux_getdents_error(struct thread *, int, int);
67
68 static struct bsd_to_linux_bitmap seal_bitmap[] = {
69 BITMAP_1t1_LINUX(F_SEAL_SEAL),
70 BITMAP_1t1_LINUX(F_SEAL_SHRINK),
71 BITMAP_1t1_LINUX(F_SEAL_GROW),
72 BITMAP_1t1_LINUX(F_SEAL_WRITE),
73 };
74
75 #define MFD_HUGETLB_ENTRY(_size) \
76 { \
77 .bsd_value = MFD_HUGE_##_size, \
78 .linux_value = LINUX_HUGETLB_FLAG_ENCODE_##_size \
79 }
80 static struct bsd_to_linux_bitmap mfd_bitmap[] = {
81 BITMAP_1t1_LINUX(MFD_CLOEXEC),
82 BITMAP_1t1_LINUX(MFD_ALLOW_SEALING),
83 BITMAP_1t1_LINUX(MFD_HUGETLB),
84 MFD_HUGETLB_ENTRY(64KB),
85 MFD_HUGETLB_ENTRY(512KB),
86 MFD_HUGETLB_ENTRY(1MB),
87 MFD_HUGETLB_ENTRY(2MB),
88 MFD_HUGETLB_ENTRY(8MB),
89 MFD_HUGETLB_ENTRY(16MB),
90 MFD_HUGETLB_ENTRY(32MB),
91 MFD_HUGETLB_ENTRY(256MB),
92 MFD_HUGETLB_ENTRY(512MB),
93 MFD_HUGETLB_ENTRY(1GB),
94 MFD_HUGETLB_ENTRY(2GB),
95 MFD_HUGETLB_ENTRY(16GB),
96 };
97 #undef MFD_HUGETLB_ENTRY
98
99 #ifdef LINUX_LEGACY_SYSCALLS
100 int
linux_creat(struct thread * td,struct linux_creat_args * args)101 linux_creat(struct thread *td, struct linux_creat_args *args)
102 {
103
104 return (kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE,
105 O_WRONLY | O_CREAT | O_TRUNC, args->mode));
106 }
107 #endif
108
109 int
linux_common_openflags(int l_flags)110 linux_common_openflags(int l_flags)
111 {
112 int bsd_flags;
113
114 bsd_flags = 0;
115 switch (l_flags & LINUX_O_ACCMODE) {
116 case LINUX_O_WRONLY:
117 bsd_flags |= O_WRONLY;
118 break;
119 case LINUX_O_RDWR:
120 bsd_flags |= O_RDWR;
121 break;
122 default:
123 bsd_flags |= O_RDONLY;
124 }
125 if (l_flags & LINUX_O_NDELAY)
126 bsd_flags |= O_NONBLOCK;
127 if (l_flags & LINUX_O_APPEND)
128 bsd_flags |= O_APPEND;
129 if (l_flags & LINUX_O_SYNC)
130 bsd_flags |= O_FSYNC;
131 if (l_flags & LINUX_O_CLOEXEC)
132 bsd_flags |= O_CLOEXEC;
133 if (l_flags & LINUX_O_NONBLOCK)
134 bsd_flags |= O_NONBLOCK;
135 if (l_flags & LINUX_O_ASYNC)
136 bsd_flags |= O_ASYNC;
137 if (l_flags & LINUX_O_CREAT)
138 bsd_flags |= O_CREAT;
139 if (l_flags & LINUX_O_TRUNC)
140 bsd_flags |= O_TRUNC;
141 if (l_flags & LINUX_O_EXCL)
142 bsd_flags |= O_EXCL;
143 if (l_flags & LINUX_O_NOCTTY)
144 bsd_flags |= O_NOCTTY;
145 if (l_flags & LINUX_O_DIRECT)
146 bsd_flags |= O_DIRECT;
147 if (l_flags & LINUX_O_NOFOLLOW)
148 bsd_flags |= O_NOFOLLOW;
149 if (l_flags & LINUX_O_DIRECTORY)
150 bsd_flags |= O_DIRECTORY;
151 if (l_flags & LINUX_O_PATH)
152 bsd_flags |= O_PATH;
153 /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
154 return (bsd_flags);
155 }
156
157 static int
linux_common_open(struct thread * td,int dirfd,const char * path,int l_flags,int mode,enum uio_seg seg)158 linux_common_open(struct thread *td, int dirfd, const char *path, int l_flags,
159 int mode, enum uio_seg seg)
160 {
161 struct proc *p = td->td_proc;
162 struct file *fp;
163 int fd;
164 int bsd_flags, error;
165
166 bsd_flags = linux_common_openflags(l_flags);
167 error = kern_openat(td, dirfd, path, seg, bsd_flags, mode);
168 if (error != 0) {
169 if (error == EMLINK)
170 error = ELOOP;
171 goto done;
172 }
173 if (p->p_flag & P_CONTROLT)
174 goto done;
175 if (bsd_flags & O_NOCTTY)
176 goto done;
177
178 /*
179 * XXX In between kern_openat() and fget(), another process
180 * having the same filedesc could use that fd without
181 * checking below.
182 */
183 fd = td->td_retval[0];
184 if (fget(td, fd, &cap_ioctl_rights, &fp) == 0) {
185 if (fp->f_type != DTYPE_VNODE) {
186 fdrop(fp, td);
187 goto done;
188 }
189 sx_slock(&proctree_lock);
190 PROC_LOCK(p);
191 if (SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
192 PROC_UNLOCK(p);
193 sx_sunlock(&proctree_lock);
194 /* XXXPJD: Verify if TIOCSCTTY is allowed. */
195 (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
196 td->td_ucred, td);
197 } else {
198 PROC_UNLOCK(p);
199 sx_sunlock(&proctree_lock);
200 }
201 fdrop(fp, td);
202 }
203
204 done:
205 return (error);
206 }
207
208 int
linux_openat(struct thread * td,struct linux_openat_args * args)209 linux_openat(struct thread *td, struct linux_openat_args *args)
210 {
211 int dfd;
212
213 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
214 return (linux_common_open(td, dfd, args->filename, args->flags,
215 args->mode, UIO_USERSPACE));
216 }
217
218 #ifdef LINUX_LEGACY_SYSCALLS
219 int
linux_open(struct thread * td,struct linux_open_args * args)220 linux_open(struct thread *td, struct linux_open_args *args)
221 {
222
223 return (linux_common_open(td, AT_FDCWD, args->path, args->flags,
224 args->mode, UIO_USERSPACE));
225 }
226 #endif
227
228 int
linux_name_to_handle_at(struct thread * td,struct linux_name_to_handle_at_args * args)229 linux_name_to_handle_at(struct thread *td,
230 struct linux_name_to_handle_at_args *args)
231 {
232 static const l_int valid_flags = (LINUX_AT_SYMLINK_FOLLOW |
233 LINUX_AT_EMPTY_PATH);
234 static const l_uint fh_size = sizeof(fhandle_t);
235
236 fhandle_t fh;
237 l_uint fh_bytes;
238 l_int mount_id;
239 int error, fd, bsd_flags;
240
241 if (args->flags & ~valid_flags)
242 return (EINVAL);
243
244 fd = args->dirfd;
245 if (fd == LINUX_AT_FDCWD)
246 fd = AT_FDCWD;
247
248 bsd_flags = 0;
249 if (!(args->flags & LINUX_AT_SYMLINK_FOLLOW))
250 bsd_flags |= AT_SYMLINK_NOFOLLOW;
251 if ((args->flags & LINUX_AT_EMPTY_PATH) != 0)
252 bsd_flags |= AT_EMPTY_PATH;
253
254 error = kern_getfhat(td, bsd_flags, fd, args->name,
255 UIO_USERSPACE, &fh, UIO_SYSSPACE);
256 if (error != 0)
257 return (error);
258
259 /* Emit mount_id -- required before EOVERFLOW case. */
260 mount_id = (fh.fh_fsid.val[0] ^ fh.fh_fsid.val[1]);
261 error = copyout(&mount_id, args->mnt_id, sizeof(mount_id));
262 if (error != 0)
263 return (error);
264
265 /* Check if there is room for handle. */
266 error = copyin(&args->handle->handle_bytes, &fh_bytes,
267 sizeof(fh_bytes));
268 if (error != 0)
269 return (error);
270
271 if (fh_bytes < fh_size) {
272 error = copyout(&fh_size, &args->handle->handle_bytes,
273 sizeof(fh_size));
274 if (error == 0)
275 error = EOVERFLOW;
276 return (error);
277 }
278
279 /* Emit handle. */
280 mount_id = 0;
281 /*
282 * We don't use handle_type for anything yet, but initialize a known
283 * value.
284 */
285 error = copyout(&mount_id, &args->handle->handle_type,
286 sizeof(mount_id));
287 if (error != 0)
288 return (error);
289
290 error = copyout(&fh, &args->handle->f_handle,
291 sizeof(fh));
292 return (error);
293 }
294
295 int
linux_open_by_handle_at(struct thread * td,struct linux_open_by_handle_at_args * args)296 linux_open_by_handle_at(struct thread *td,
297 struct linux_open_by_handle_at_args *args)
298 {
299 l_uint fh_bytes;
300 int bsd_flags, error;
301
302 error = copyin(&args->handle->handle_bytes, &fh_bytes,
303 sizeof(fh_bytes));
304 if (error != 0)
305 return (error);
306
307 if (fh_bytes < sizeof(fhandle_t))
308 return (EINVAL);
309
310 bsd_flags = linux_common_openflags(args->flags);
311 return (kern_fhopen(td, (void *)&args->handle->f_handle, bsd_flags));
312 }
313
314 int
linux_lseek(struct thread * td,struct linux_lseek_args * args)315 linux_lseek(struct thread *td, struct linux_lseek_args *args)
316 {
317
318 return (kern_lseek(td, args->fdes, args->off, args->whence));
319 }
320
321 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
322 int
linux_llseek(struct thread * td,struct linux_llseek_args * args)323 linux_llseek(struct thread *td, struct linux_llseek_args *args)
324 {
325 int error;
326 off_t off;
327
328 off = (args->olow) | (((off_t) args->ohigh) << 32);
329
330 error = kern_lseek(td, args->fd, off, args->whence);
331 if (error != 0)
332 return (error);
333
334 error = copyout(td->td_retval, args->res, sizeof(off_t));
335 if (error != 0)
336 return (error);
337
338 td->td_retval[0] = 0;
339 return (0);
340 }
341 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
342
343 /*
344 * Note that linux_getdents(2) and linux_getdents64(2) have the same
345 * arguments. They only differ in the definition of struct dirent they
346 * operate on.
347 * Note that linux_readdir(2) is a special case of linux_getdents(2)
348 * where count is always equals 1, meaning that the buffer is one
349 * dirent-structure in size and that the code can't handle more anyway.
350 * Note that linux_readdir(2) can't be implemented by means of linux_getdents(2)
351 * as in case when the *dent buffer size is equal to 1 linux_getdents(2) will
352 * trash user stack.
353 */
354
355 static int
linux_getdents_error(struct thread * td,int fd,int err)356 linux_getdents_error(struct thread *td, int fd, int err)
357 {
358 struct vnode *vp;
359 struct file *fp;
360 int error;
361
362 /* Linux return ENOTDIR in case when fd is not a directory. */
363 error = getvnode(td, fd, &cap_read_rights, &fp);
364 if (error != 0)
365 return (error);
366 vp = fp->f_vnode;
367 if (vp->v_type != VDIR) {
368 fdrop(fp, td);
369 return (ENOTDIR);
370 }
371 fdrop(fp, td);
372 return (err);
373 }
374
375 struct l_dirent {
376 l_ulong d_ino;
377 l_off_t d_off;
378 l_ushort d_reclen;
379 char d_name[LINUX_NAME_MAX + 1];
380 };
381
382 struct l_dirent64 {
383 uint64_t d_ino;
384 int64_t d_off;
385 l_ushort d_reclen;
386 u_char d_type;
387 char d_name[LINUX_NAME_MAX + 1];
388 };
389
390 /*
391 * Linux uses the last byte in the dirent buffer to store d_type,
392 * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
393 */
394 #define LINUX_RECLEN(namlen) \
395 roundup(offsetof(struct l_dirent, d_name) + (namlen) + 2, sizeof(l_ulong))
396
397 #define LINUX_RECLEN64(namlen) \
398 roundup(offsetof(struct l_dirent64, d_name) + (namlen) + 1, \
399 sizeof(uint64_t))
400
401 /*
402 * Do kern_getdirentries() and then skip over any invalid entries.
403 * (Repeat, if there are no valid entries.)
404 * Adjust bufp and lenp.
405 */
406 static int
linux_getdirentries(struct thread * td,int fd,caddr_t * bufp,int buflen,off_t * basep,int * lenp)407 linux_getdirentries(struct thread *td, int fd, caddr_t *bufp, int buflen,
408 off_t *basep, int *lenp)
409 {
410 struct dirent *bdp;
411 caddr_t buf;
412 int error, len;
413
414 /* Loop around until a valid entry is found or at EOF. */
415 for (;;) {
416 error = kern_getdirentries(td, fd, *bufp, buflen,
417 basep, NULL, UIO_SYSSPACE);
418 if (error != 0)
419 return (error);
420 len = td->td_retval[0];
421 if (len == 0) {
422 *lenp = 0;
423 return (0);
424 }
425 buf = *bufp;
426 while (len > 0) {
427 bdp = (struct dirent *)buf;
428 if (bdp->d_fileno != 0) {
429 *bufp = buf;
430 *lenp = len;
431 return (0);
432 }
433 buf += bdp->d_reclen;
434 len -= bdp->d_reclen;
435 }
436 }
437 }
438
439 #ifdef LINUX_LEGACY_SYSCALLS
440 int
linux_getdents(struct thread * td,struct linux_getdents_args * args)441 linux_getdents(struct thread *td, struct linux_getdents_args *args)
442 {
443 struct dirent *bdp;
444 caddr_t inp, buf, bufsav; /* BSD-format */
445 int len, reclen; /* BSD-format */
446 caddr_t outp; /* Linux-format */
447 int resid, linuxreclen; /* Linux-format */
448 caddr_t lbuf; /* Linux-format */
449 off_t base;
450 struct l_dirent *linux_dirent;
451 int buflen, error;
452 size_t retval;
453
454 buflen = min(roundup2(args->count, DEV_BSIZE), MAXBSIZE);
455 bufsav = buf = malloc(buflen, M_LINUX, M_WAITOK);
456
457 error = linux_getdirentries(td, args->fd, &buf, buflen,
458 &base, &len);
459 if (error != 0) {
460 error = linux_getdents_error(td, args->fd, error);
461 goto out1;
462 }
463
464 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, M_WAITOK | M_ZERO);
465
466 inp = buf;
467 outp = (caddr_t)args->dent;
468 resid = args->count;
469 retval = 0;
470
471 while (len > 0) {
472 bdp = (struct dirent *) inp;
473 reclen = bdp->d_reclen;
474 /* Copy a valid entry out. */
475 if (bdp->d_fileno != 0) {
476 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
477 /*
478 * No more space in the user supplied dirent buffer.
479 * Return EINVAL.
480 */
481 if (resid < linuxreclen) {
482 error = EINVAL;
483 goto out;
484 }
485
486 linux_dirent = (struct l_dirent*)lbuf;
487 linux_dirent->d_ino = bdp->d_fileno;
488 linux_dirent->d_off = bdp->d_off;
489 linux_dirent->d_reclen = linuxreclen;
490 /*
491 * Copy d_type to last byte of l_dirent buffer
492 */
493 lbuf[linuxreclen - 1] = bdp->d_type;
494 strlcpy(linux_dirent->d_name, bdp->d_name,
495 linuxreclen - offsetof(struct l_dirent, d_name)-1);
496 error = copyout(linux_dirent, outp, linuxreclen);
497 if (error != 0)
498 goto out;
499 retval += linuxreclen;
500 outp += linuxreclen;
501 resid -= linuxreclen;
502 }
503
504 inp += reclen;
505 base += reclen;
506 len -= reclen;
507
508 }
509 td->td_retval[0] = retval;
510
511 out:
512 free(lbuf, M_LINUX);
513 out1:
514 free(bufsav, M_LINUX);
515 return (error);
516 }
517 #endif
518
519 int
linux_getdents64(struct thread * td,struct linux_getdents64_args * args)520 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
521 {
522 struct dirent *bdp;
523 caddr_t inp, buf, bufsav; /* BSD-format */
524 int len, reclen; /* BSD-format */
525 caddr_t outp; /* Linux-format */
526 int resid, linuxreclen; /* Linux-format */
527 off_t base;
528 struct l_dirent64 *linux_dirent64;
529 int buflen, error;
530 size_t retval;
531
532 buflen = min(roundup2(args->count, DEV_BSIZE), MAXBSIZE);
533 bufsav = buf = malloc(buflen, M_LINUX, M_WAITOK);
534
535 error = linux_getdirentries(td, args->fd, &buf, buflen,
536 &base, &len);
537 if (error != 0) {
538 error = linux_getdents_error(td, args->fd, error);
539 goto out1;
540 }
541
542 linux_dirent64 = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_LINUX,
543 M_WAITOK | M_ZERO);
544
545 inp = buf;
546 outp = (caddr_t)args->dirent;
547 resid = args->count;
548 retval = 0;
549
550 while (len > 0) {
551 bdp = (struct dirent *) inp;
552 reclen = bdp->d_reclen;
553 /* Copy a valid entry out. */
554 if (bdp->d_fileno != 0) {
555 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
556 /*
557 * No more space in the user supplied dirent buffer.
558 * Return EINVAL.
559 */
560 if (resid < linuxreclen) {
561 error = EINVAL;
562 goto out;
563 }
564
565 linux_dirent64->d_ino = bdp->d_fileno;
566 linux_dirent64->d_off = bdp->d_off;
567 linux_dirent64->d_reclen = linuxreclen;
568 linux_dirent64->d_type = bdp->d_type;
569 strlcpy(linux_dirent64->d_name, bdp->d_name,
570 linuxreclen - offsetof(struct l_dirent64, d_name));
571 error = copyout(linux_dirent64, outp, linuxreclen);
572 if (error != 0)
573 goto out;
574 retval += linuxreclen;
575 outp += linuxreclen;
576 resid -= linuxreclen;
577 }
578
579 inp += reclen;
580 base += reclen;
581 len -= reclen;
582
583 }
584 td->td_retval[0] = retval;
585
586 out:
587 free(linux_dirent64, M_LINUX);
588 out1:
589 free(bufsav, M_LINUX);
590 return (error);
591 }
592
593 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
594 int
linux_readdir(struct thread * td,struct linux_readdir_args * args)595 linux_readdir(struct thread *td, struct linux_readdir_args *args)
596 {
597 struct dirent *bdp;
598 caddr_t buf, bufsav; /* BSD-format */
599 int linuxreclen; /* Linux-format */
600 off_t base;
601 struct l_dirent *linux_dirent; /* Linux-format */
602 int buflen, error, len;
603
604 buflen = DEV_BSIZE;
605 bufsav = buf = malloc(buflen, M_LINUX, M_WAITOK);
606
607 error = linux_getdirentries(td, args->fd, &buf, buflen,
608 &base, &len);
609 if (error != 0) {
610 error = linux_getdents_error(td, args->fd, error);
611 goto out;
612 }
613 if (len == 0)
614 goto out;
615
616 linux_dirent = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX,
617 M_WAITOK | M_ZERO);
618
619 bdp = (struct dirent *) buf;
620 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
621
622 linux_dirent->d_ino = bdp->d_fileno;
623 linux_dirent->d_off = bdp->d_off;
624 linux_dirent->d_reclen = bdp->d_namlen;
625 strlcpy(linux_dirent->d_name, bdp->d_name,
626 linuxreclen - offsetof(struct l_dirent, d_name));
627 error = copyout(linux_dirent, args->dent, linuxreclen);
628 if (error == 0)
629 td->td_retval[0] = linuxreclen;
630
631 free(linux_dirent, M_LINUX);
632 out:
633 free(bufsav, M_LINUX);
634 return (error);
635 }
636 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
637
638 /*
639 * These exist mainly for hooks for doing /compat/linux translation.
640 */
641
642 #ifdef LINUX_LEGACY_SYSCALLS
643 int
linux_access(struct thread * td,struct linux_access_args * args)644 linux_access(struct thread *td, struct linux_access_args *args)
645 {
646
647 /* Linux convention. */
648 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
649 return (EINVAL);
650
651 return (kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
652 args->amode));
653 }
654 #endif
655
656 static int
linux_do_accessat(struct thread * td,int ldfd,const char * filename,int amode,int flags)657 linux_do_accessat(struct thread *td, int ldfd, const char *filename,
658 int amode, int flags)
659 {
660 int dfd;
661
662 /* Linux convention. */
663 if (amode & ~(F_OK | X_OK | W_OK | R_OK))
664 return (EINVAL);
665
666 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
667 return (kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode));
668 }
669
670 int
linux_faccessat(struct thread * td,struct linux_faccessat_args * args)671 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
672 {
673
674 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
675 0));
676 }
677
678 int
linux_faccessat2(struct thread * td,struct linux_faccessat2_args * args)679 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args)
680 {
681 int flags, unsupported;
682
683 unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH |
684 LINUX_AT_SYMLINK_NOFOLLOW);
685 if (unsupported != 0) {
686 linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported);
687 return (EINVAL);
688 }
689
690 flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 :
691 AT_EACCESS;
692 flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
693 AT_EMPTY_PATH;
694 flags |= (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
695 AT_SYMLINK_NOFOLLOW;
696 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
697 flags));
698 }
699
700
701 #ifdef LINUX_LEGACY_SYSCALLS
702 int
linux_unlink(struct thread * td,struct linux_unlink_args * args)703 linux_unlink(struct thread *td, struct linux_unlink_args *args)
704 {
705 int error;
706 struct stat st;
707
708 error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
709 UIO_USERSPACE, 0, 0);
710 if (error == EPERM) {
711 /* Introduce POSIX noncompliant behaviour of Linux */
712 if (kern_statat(td, 0, AT_FDCWD, args->path,
713 UIO_USERSPACE, &st) == 0) {
714 if (S_ISDIR(st.st_mode))
715 error = EISDIR;
716 }
717 }
718
719 return (error);
720 }
721 #endif
722
723 static int
linux_unlinkat_impl(struct thread * td,enum uio_seg pathseg,const char * path,int dfd,struct linux_unlinkat_args * args)724 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
725 int dfd, struct linux_unlinkat_args *args)
726 {
727 struct stat st;
728 int error;
729
730 if (args->flag & LINUX_AT_REMOVEDIR)
731 error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
732 else
733 error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
734 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
735 /* Introduce POSIX noncompliant behaviour of Linux */
736 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
737 pathseg, &st) == 0 && S_ISDIR(st.st_mode))
738 error = EISDIR;
739 }
740 return (error);
741 }
742
743 int
linux_unlinkat(struct thread * td,struct linux_unlinkat_args * args)744 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
745 {
746 int dfd;
747
748 if (args->flag & ~LINUX_AT_REMOVEDIR)
749 return (EINVAL);
750 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
751 return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
752 dfd, args));
753 }
754
755 int
linux_chdir(struct thread * td,struct linux_chdir_args * args)756 linux_chdir(struct thread *td, struct linux_chdir_args *args)
757 {
758
759 return (kern_chdir(td, args->path, UIO_USERSPACE));
760 }
761
762 #ifdef LINUX_LEGACY_SYSCALLS
763 int
linux_chmod(struct thread * td,struct linux_chmod_args * args)764 linux_chmod(struct thread *td, struct linux_chmod_args *args)
765 {
766
767 return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
768 args->mode, 0));
769 }
770 #endif
771
772 int
linux_fchmodat(struct thread * td,struct linux_fchmodat_args * args)773 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
774 {
775 int dfd;
776
777 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
778 return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
779 args->mode, 0));
780 }
781
782 #ifdef LINUX_LEGACY_SYSCALLS
783 int
linux_mkdir(struct thread * td,struct linux_mkdir_args * args)784 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
785 {
786
787 return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
788 }
789 #endif
790
791 int
linux_mkdirat(struct thread * td,struct linux_mkdirat_args * args)792 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
793 {
794 int dfd;
795
796 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
797 return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
798 }
799
800 #ifdef LINUX_LEGACY_SYSCALLS
801 int
linux_rmdir(struct thread * td,struct linux_rmdir_args * args)802 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
803 {
804
805 return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
806 UIO_USERSPACE, 0));
807 }
808
809 int
linux_rename(struct thread * td,struct linux_rename_args * args)810 linux_rename(struct thread *td, struct linux_rename_args *args)
811 {
812
813 return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
814 args->to, UIO_USERSPACE, 0));
815 }
816 #endif
817
818 int
linux_renameat(struct thread * td,struct linux_renameat_args * args)819 linux_renameat(struct thread *td, struct linux_renameat_args *args)
820 {
821 struct linux_renameat2_args renameat2_args = {
822 .olddfd = args->olddfd,
823 .oldname = args->oldname,
824 .newdfd = args->newdfd,
825 .newname = args->newname,
826 .flags = 0
827 };
828
829 return (linux_renameat2(td, &renameat2_args));
830 }
831
832 int
linux_renameat2(struct thread * td,struct linux_renameat2_args * args)833 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
834 {
835 int olddfd, newdfd;
836 u_int atflags;
837
838 atflags = 0;
839 if ((args->flags & ~(LINUX_RENAME_EXCHANGE |
840 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT)) != 0)
841 return (EINVAL);
842 if ((args->flags & LINUX_RENAME_EXCHANGE) != 0 &&
843 (args->flags & (LINUX_RENAME_NOREPLACE |
844 LINUX_RENAME_WHITEOUT)) != 0)
845 return (EINVAL);
846 if ((args->flags & LINUX_RENAME_NOREPLACE) != 0) {
847 if ((args->flags & (LINUX_RENAME_EXCHANGE |
848 LINUX_RENAME_WHITEOUT)) != 0)
849 return (EINVAL);
850 args->flags &= ~LINUX_RENAME_NOREPLACE;
851 atflags |= AT_RENAME_NOREPLACE;
852 }
853
854 if (args->flags != 0) {
855 /*
856 * This spams the console on Ubuntu Focal.
857 *
858 * What's needed here is a general mechanism to let
859 * users know about missing features without hogging
860 * the system.
861 */
862 #if 0
863 linux_msg(td, "renameat2 unsupported flags %#x",
864 args->flags);
865 #endif
866 return (EINVAL);
867 }
868
869 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
870 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
871 return (kern_renameat(td, olddfd, args->oldname, newdfd,
872 args->newname, UIO_USERSPACE, atflags));
873 }
874
875 #ifdef LINUX_LEGACY_SYSCALLS
876 int
linux_symlink(struct thread * td,struct linux_symlink_args * args)877 linux_symlink(struct thread *td, struct linux_symlink_args *args)
878 {
879
880 return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
881 UIO_USERSPACE));
882 }
883 #endif
884
885 int
linux_symlinkat(struct thread * td,struct linux_symlinkat_args * args)886 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
887 {
888 int dfd;
889
890 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
891 return (kern_symlinkat(td, args->oldname, dfd, args->newname,
892 UIO_USERSPACE));
893 }
894
895 #ifdef LINUX_LEGACY_SYSCALLS
896 int
linux_readlink(struct thread * td,struct linux_readlink_args * args)897 linux_readlink(struct thread *td, struct linux_readlink_args *args)
898 {
899
900 if (args->count <= 0)
901 return (EINVAL);
902
903 return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
904 args->buf, UIO_USERSPACE, args->count));
905 }
906 #endif
907
908 int
linux_readlinkat(struct thread * td,struct linux_readlinkat_args * args)909 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
910 {
911 int dfd;
912
913 if (args->bufsiz <= 0)
914 return (EINVAL);
915
916 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
917 return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
918 args->buf, UIO_USERSPACE, args->bufsiz));
919 }
920
921 int
linux_truncate(struct thread * td,struct linux_truncate_args * args)922 linux_truncate(struct thread *td, struct linux_truncate_args *args)
923 {
924
925 return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
926 }
927
928 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
929 int
linux_truncate64(struct thread * td,struct linux_truncate64_args * args)930 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
931 {
932 off_t length;
933
934 #if defined(__amd64__) && defined(COMPAT_LINUX32)
935 length = PAIR32TO64(off_t, args->length);
936 #else
937 length = args->length;
938 #endif
939
940 return (kern_truncate(td, args->path, UIO_USERSPACE, length));
941 }
942 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
943
944 int
linux_ftruncate(struct thread * td,struct linux_ftruncate_args * args)945 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
946 {
947
948 return (kern_ftruncate(td, args->fd, args->length));
949 }
950
951 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
952 int
linux_ftruncate64(struct thread * td,struct linux_ftruncate64_args * args)953 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
954 {
955 off_t length;
956
957 #if defined(__amd64__) && defined(COMPAT_LINUX32)
958 length = PAIR32TO64(off_t, args->length);
959 #else
960 length = args->length;
961 #endif
962
963 return (kern_ftruncate(td, args->fd, length));
964 }
965 #endif
966
967 #ifdef LINUX_LEGACY_SYSCALLS
968 int
linux_link(struct thread * td,struct linux_link_args * args)969 linux_link(struct thread *td, struct linux_link_args *args)
970 {
971
972 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
973 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
974 }
975 #endif
976
977 int
linux_linkat(struct thread * td,struct linux_linkat_args * args)978 linux_linkat(struct thread *td, struct linux_linkat_args *args)
979 {
980 int olddfd, newdfd, flag;
981
982 if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
983 return (EINVAL);
984
985 flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
986 0;
987 flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
988
989 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
990 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
991 return (kern_linkat(td, olddfd, newdfd, args->oldname,
992 args->newname, UIO_USERSPACE, flag));
993 }
994
995 int
linux_fdatasync(struct thread * td,struct linux_fdatasync_args * uap)996 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
997 {
998
999 return (kern_fsync(td, uap->fd, false));
1000 }
1001
1002 int
linux_sync_file_range(struct thread * td,struct linux_sync_file_range_args * uap)1003 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
1004 {
1005 off_t nbytes, offset;
1006
1007 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1008 nbytes = PAIR32TO64(off_t, uap->nbytes);
1009 offset = PAIR32TO64(off_t, uap->offset);
1010 #else
1011 nbytes = uap->nbytes;
1012 offset = uap->offset;
1013 #endif
1014
1015 if (offset < 0 || nbytes < 0 ||
1016 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
1017 LINUX_SYNC_FILE_RANGE_WRITE |
1018 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
1019 return (EINVAL);
1020 }
1021
1022 return (kern_fsync(td, uap->fd, false));
1023 }
1024
1025 int
linux_pread(struct thread * td,struct linux_pread_args * uap)1026 linux_pread(struct thread *td, struct linux_pread_args *uap)
1027 {
1028 struct vnode *vp;
1029 off_t offset;
1030 int error;
1031
1032 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1033 offset = PAIR32TO64(off_t, uap->offset);
1034 #else
1035 offset = uap->offset;
1036 #endif
1037
1038 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
1039 if (error == 0) {
1040 /* This seems to violate POSIX but Linux does it. */
1041 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
1042 if (error != 0)
1043 return (error);
1044 if (vp->v_type == VDIR)
1045 error = EISDIR;
1046 vrele(vp);
1047 }
1048 return (error);
1049 }
1050
1051 int
linux_pwrite(struct thread * td,struct linux_pwrite_args * uap)1052 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1053 {
1054 off_t offset;
1055
1056 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1057 offset = PAIR32TO64(off_t, uap->offset);
1058 #else
1059 offset = uap->offset;
1060 #endif
1061
1062 return (linux_enobufs2eagain(td, uap->fd,
1063 kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset)));
1064 }
1065
1066 #define HALF_LONG_BITS ((sizeof(l_long) * NBBY / 2))
1067
1068 static inline off_t
pos_from_hilo(unsigned long high,unsigned long low)1069 pos_from_hilo(unsigned long high, unsigned long low)
1070 {
1071
1072 return (((off_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1073 }
1074
1075 int
linux_preadv(struct thread * td,struct linux_preadv_args * uap)1076 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1077 {
1078 struct uio *auio;
1079 int error;
1080 off_t offset;
1081
1082 /*
1083 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1084 * pos_l and pos_h, respectively, contain the
1085 * low order and high order 32 bits of offset.
1086 */
1087 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1088 if (offset < 0)
1089 return (EINVAL);
1090 #ifdef COMPAT_LINUX32
1091 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1092 #else
1093 error = copyinuio(uap->vec, uap->vlen, &auio);
1094 #endif
1095 if (error != 0)
1096 return (error);
1097 error = kern_preadv(td, uap->fd, auio, offset);
1098 freeuio(auio);
1099 return (error);
1100 }
1101
1102 int
linux_pwritev(struct thread * td,struct linux_pwritev_args * uap)1103 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1104 {
1105 struct uio *auio;
1106 int error;
1107 off_t offset;
1108
1109 /*
1110 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1111 * pos_l and pos_h, respectively, contain the
1112 * low order and high order 32 bits of offset.
1113 */
1114 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1115 if (offset < 0)
1116 return (EINVAL);
1117 #ifdef COMPAT_LINUX32
1118 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1119 #else
1120 error = copyinuio(uap->vec, uap->vlen, &auio);
1121 #endif
1122 if (error != 0)
1123 return (error);
1124 error = kern_pwritev(td, uap->fd, auio, offset);
1125 freeuio(auio);
1126 return (linux_enobufs2eagain(td, uap->fd, error));
1127 }
1128
1129 int
linux_mount(struct thread * td,struct linux_mount_args * args)1130 linux_mount(struct thread *td, struct linux_mount_args *args)
1131 {
1132 struct mntarg *ma = NULL;
1133 char *fstypename, *mntonname, *mntfromname, *data;
1134 int error, fsflags;
1135
1136 fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1137 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1138 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1139 data = NULL;
1140 error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1141 NULL);
1142 if (error != 0)
1143 goto out;
1144 if (args->specialfile != NULL) {
1145 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1146 if (error != 0)
1147 goto out;
1148 } else {
1149 mntfromname[0] = '\0';
1150 }
1151 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1152 if (error != 0)
1153 goto out;
1154
1155 if (strcmp(fstypename, "ext2") == 0) {
1156 strcpy(fstypename, "ext2fs");
1157 } else if (strcmp(fstypename, "proc") == 0) {
1158 strcpy(fstypename, "linprocfs");
1159 } else if (strcmp(fstypename, "vfat") == 0) {
1160 strcpy(fstypename, "msdosfs");
1161 } else if (strcmp(fstypename, "fuse") == 0 ||
1162 strncmp(fstypename, "fuse.", 5) == 0) {
1163 char *fuse_options, *fuse_option, *fuse_name;
1164
1165 strcpy(mntfromname, "/dev/fuse");
1166 strcpy(fstypename, "fusefs");
1167 data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1168 error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1169 if (error != 0)
1170 goto out;
1171
1172 fuse_options = data;
1173 while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1174 fuse_name = strsep(&fuse_option, "=");
1175 if (fuse_name == NULL || fuse_option == NULL)
1176 goto out;
1177 ma = mount_arg(ma, fuse_name, fuse_option, -1);
1178 }
1179
1180 /*
1181 * The FUSE server uses Linux errno values instead of FreeBSD
1182 * ones; add a flag to tell fuse(4) to do errno translation.
1183 */
1184 ma = mount_arg(ma, "linux_errnos", "1", -1);
1185 }
1186
1187 fsflags = 0;
1188
1189 /*
1190 * Linux SYNC flag is not included; the closest equivalent
1191 * FreeBSD has is !ASYNC, which is our default.
1192 */
1193 if (args->rwflag & LINUX_MS_RDONLY)
1194 fsflags |= MNT_RDONLY;
1195 if (args->rwflag & LINUX_MS_NOSUID)
1196 fsflags |= MNT_NOSUID;
1197 if (args->rwflag & LINUX_MS_NOEXEC)
1198 fsflags |= MNT_NOEXEC;
1199 if (args->rwflag & LINUX_MS_REMOUNT)
1200 fsflags |= MNT_UPDATE;
1201
1202 ma = mount_arg(ma, "fstype", fstypename, -1);
1203 ma = mount_arg(ma, "fspath", mntonname, -1);
1204 ma = mount_arg(ma, "from", mntfromname, -1);
1205 error = kernel_mount(ma, fsflags);
1206 out:
1207 free(fstypename, M_TEMP);
1208 free(mntonname, M_TEMP);
1209 free(mntfromname, M_TEMP);
1210 free(data, M_TEMP);
1211 return (error);
1212 }
1213
1214 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1215 int
linux_oldumount(struct thread * td,struct linux_oldumount_args * args)1216 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1217 {
1218
1219 return (kern_unmount(td, args->path, 0));
1220 }
1221 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1222
1223 #ifdef LINUX_LEGACY_SYSCALLS
1224 int
linux_umount(struct thread * td,struct linux_umount_args * args)1225 linux_umount(struct thread *td, struct linux_umount_args *args)
1226 {
1227 uint64_t flags;
1228
1229 flags = 0;
1230 if ((args->flags & LINUX_MNT_FORCE) != 0) {
1231 args->flags &= ~LINUX_MNT_FORCE;
1232 flags |= MNT_FORCE;
1233 }
1234 if (args->flags != 0) {
1235 linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1236 return (EINVAL);
1237 }
1238
1239 return (kern_unmount(td, args->path, flags));
1240 }
1241 #endif
1242
1243 /*
1244 * fcntl family of syscalls
1245 */
1246
1247 struct l_flock {
1248 l_short l_type;
1249 l_short l_whence;
1250 l_off_t l_start;
1251 l_off_t l_len;
1252 l_pid_t l_pid;
1253 }
1254 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1255 __packed
1256 #endif
1257 ;
1258
1259 static void
linux_to_bsd_flock(struct l_flock * linux_flock,struct flock * bsd_flock)1260 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1261 {
1262 switch (linux_flock->l_type) {
1263 case LINUX_F_RDLCK:
1264 bsd_flock->l_type = F_RDLCK;
1265 break;
1266 case LINUX_F_WRLCK:
1267 bsd_flock->l_type = F_WRLCK;
1268 break;
1269 case LINUX_F_UNLCK:
1270 bsd_flock->l_type = F_UNLCK;
1271 break;
1272 default:
1273 bsd_flock->l_type = -1;
1274 break;
1275 }
1276 bsd_flock->l_whence = linux_flock->l_whence;
1277 bsd_flock->l_start = (off_t)linux_flock->l_start;
1278 bsd_flock->l_len = (off_t)linux_flock->l_len;
1279 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1280 bsd_flock->l_sysid = 0;
1281 }
1282
1283 static void
bsd_to_linux_flock(struct flock * bsd_flock,struct l_flock * linux_flock)1284 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1285 {
1286 switch (bsd_flock->l_type) {
1287 case F_RDLCK:
1288 linux_flock->l_type = LINUX_F_RDLCK;
1289 break;
1290 case F_WRLCK:
1291 linux_flock->l_type = LINUX_F_WRLCK;
1292 break;
1293 case F_UNLCK:
1294 linux_flock->l_type = LINUX_F_UNLCK;
1295 break;
1296 }
1297 linux_flock->l_whence = bsd_flock->l_whence;
1298 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1299 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1300 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1301 }
1302
1303 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1304 struct l_flock64 {
1305 l_short l_type;
1306 l_short l_whence;
1307 l_loff_t l_start;
1308 l_loff_t l_len;
1309 l_pid_t l_pid;
1310 }
1311 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1312 __packed
1313 #endif
1314 ;
1315
1316 static void
linux_to_bsd_flock64(struct l_flock64 * linux_flock,struct flock * bsd_flock)1317 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1318 {
1319 switch (linux_flock->l_type) {
1320 case LINUX_F_RDLCK:
1321 bsd_flock->l_type = F_RDLCK;
1322 break;
1323 case LINUX_F_WRLCK:
1324 bsd_flock->l_type = F_WRLCK;
1325 break;
1326 case LINUX_F_UNLCK:
1327 bsd_flock->l_type = F_UNLCK;
1328 break;
1329 default:
1330 bsd_flock->l_type = -1;
1331 break;
1332 }
1333 bsd_flock->l_whence = linux_flock->l_whence;
1334 bsd_flock->l_start = (off_t)linux_flock->l_start;
1335 bsd_flock->l_len = (off_t)linux_flock->l_len;
1336 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1337 bsd_flock->l_sysid = 0;
1338 }
1339
1340 static void
bsd_to_linux_flock64(struct flock * bsd_flock,struct l_flock64 * linux_flock)1341 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1342 {
1343 switch (bsd_flock->l_type) {
1344 case F_RDLCK:
1345 linux_flock->l_type = LINUX_F_RDLCK;
1346 break;
1347 case F_WRLCK:
1348 linux_flock->l_type = LINUX_F_WRLCK;
1349 break;
1350 case F_UNLCK:
1351 linux_flock->l_type = LINUX_F_UNLCK;
1352 break;
1353 }
1354 linux_flock->l_whence = bsd_flock->l_whence;
1355 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1356 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1357 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1358 }
1359 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1360
1361 static int
fcntl_common(struct thread * td,struct linux_fcntl_args * args)1362 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1363 {
1364 struct l_flock linux_flock;
1365 struct flock bsd_flock;
1366 struct pipe *fpipe;
1367 struct file *fp;
1368 long arg;
1369 int error, result;
1370
1371 switch (args->cmd) {
1372 case LINUX_F_DUPFD:
1373 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1374
1375 case LINUX_F_GETFD:
1376 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1377
1378 case LINUX_F_SETFD:
1379 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1380
1381 case LINUX_F_GETFL:
1382 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1383 result = td->td_retval[0];
1384 td->td_retval[0] = 0;
1385 if (result & O_RDONLY)
1386 td->td_retval[0] |= LINUX_O_RDONLY;
1387 if (result & O_WRONLY)
1388 td->td_retval[0] |= LINUX_O_WRONLY;
1389 if (result & O_RDWR)
1390 td->td_retval[0] |= LINUX_O_RDWR;
1391 if (result & O_NDELAY)
1392 td->td_retval[0] |= LINUX_O_NONBLOCK;
1393 if (result & O_APPEND)
1394 td->td_retval[0] |= LINUX_O_APPEND;
1395 if (result & O_FSYNC)
1396 td->td_retval[0] |= LINUX_O_SYNC;
1397 if (result & O_ASYNC)
1398 td->td_retval[0] |= LINUX_O_ASYNC;
1399 #ifdef LINUX_O_NOFOLLOW
1400 if (result & O_NOFOLLOW)
1401 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1402 #endif
1403 #ifdef LINUX_O_DIRECT
1404 if (result & O_DIRECT)
1405 td->td_retval[0] |= LINUX_O_DIRECT;
1406 #endif
1407 return (error);
1408
1409 case LINUX_F_SETFL:
1410 arg = 0;
1411 if (args->arg & LINUX_O_NDELAY)
1412 arg |= O_NONBLOCK;
1413 if (args->arg & LINUX_O_APPEND)
1414 arg |= O_APPEND;
1415 if (args->arg & LINUX_O_SYNC)
1416 arg |= O_FSYNC;
1417 if (args->arg & LINUX_O_ASYNC)
1418 arg |= O_ASYNC;
1419 #ifdef LINUX_O_NOFOLLOW
1420 if (args->arg & LINUX_O_NOFOLLOW)
1421 arg |= O_NOFOLLOW;
1422 #endif
1423 #ifdef LINUX_O_DIRECT
1424 if (args->arg & LINUX_O_DIRECT)
1425 arg |= O_DIRECT;
1426 #endif
1427 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1428
1429 case LINUX_F_GETLK:
1430 error = copyin((void *)args->arg, &linux_flock,
1431 sizeof(linux_flock));
1432 if (error)
1433 return (error);
1434 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1435 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1436 if (error)
1437 return (error);
1438 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1439 return (copyout(&linux_flock, (void *)args->arg,
1440 sizeof(linux_flock)));
1441
1442 case LINUX_F_SETLK:
1443 error = copyin((void *)args->arg, &linux_flock,
1444 sizeof(linux_flock));
1445 if (error)
1446 return (error);
1447 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1448 return (kern_fcntl(td, args->fd, F_SETLK,
1449 (intptr_t)&bsd_flock));
1450
1451 case LINUX_F_SETLKW:
1452 error = copyin((void *)args->arg, &linux_flock,
1453 sizeof(linux_flock));
1454 if (error)
1455 return (error);
1456 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1457 return (kern_fcntl(td, args->fd, F_SETLKW,
1458 (intptr_t)&bsd_flock));
1459
1460 case LINUX_F_GETOWN:
1461 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1462
1463 case LINUX_F_SETOWN:
1464 /*
1465 * XXX some Linux applications depend on F_SETOWN having no
1466 * significant effect for pipes (SIGIO is not delivered for
1467 * pipes under Linux-2.2.35 at least).
1468 */
1469 error = fget(td, args->fd,
1470 &cap_fcntl_rights, &fp);
1471 if (error)
1472 return (error);
1473 if (fp->f_type == DTYPE_PIPE) {
1474 fdrop(fp, td);
1475 return (EINVAL);
1476 }
1477 fdrop(fp, td);
1478
1479 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1480
1481 case LINUX_F_DUPFD_CLOEXEC:
1482 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1483 /*
1484 * Our F_SEAL_* values match Linux one for maximum compatibility. So we
1485 * only needed to account for different values for fcntl(2) commands.
1486 */
1487 case LINUX_F_GET_SEALS:
1488 error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1489 if (error != 0)
1490 return (error);
1491 td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1492 seal_bitmap, 0);
1493 return (0);
1494
1495 case LINUX_F_ADD_SEALS:
1496 return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1497 linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1498
1499 case LINUX_F_GETPIPE_SZ:
1500 error = fget(td, args->fd,
1501 &cap_fcntl_rights, &fp);
1502 if (error != 0)
1503 return (error);
1504 if (fp->f_type != DTYPE_PIPE) {
1505 fdrop(fp, td);
1506 return (EINVAL);
1507 }
1508 fpipe = fp->f_data;
1509 td->td_retval[0] = fpipe->pipe_buffer.size;
1510 fdrop(fp, td);
1511 return (0);
1512
1513 case LINUX_F_DUPFD_QUERY:
1514 error = kern_kcmp(td, td->td_proc->p_pid, td->td_proc->p_pid,
1515 KCMP_FILE, args->fd, args->arg);
1516 if (error != 0)
1517 return (error);
1518 td->td_retval[0] = (td->td_retval[0] == 0) ? 1 : 0;
1519 return (0);
1520
1521 default:
1522 linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1523 return (EINVAL);
1524 }
1525 }
1526
1527 int
linux_fcntl(struct thread * td,struct linux_fcntl_args * args)1528 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1529 {
1530
1531 return (fcntl_common(td, args));
1532 }
1533
1534 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1535 int
linux_fcntl64(struct thread * td,struct linux_fcntl64_args * args)1536 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1537 {
1538 struct l_flock64 linux_flock;
1539 struct flock bsd_flock;
1540 struct linux_fcntl_args fcntl_args;
1541 int error;
1542
1543 switch (args->cmd) {
1544 case LINUX_F_GETLK64:
1545 error = copyin((void *)args->arg, &linux_flock,
1546 sizeof(linux_flock));
1547 if (error)
1548 return (error);
1549 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1550 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1551 if (error)
1552 return (error);
1553 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1554 return (copyout(&linux_flock, (void *)args->arg,
1555 sizeof(linux_flock)));
1556
1557 case LINUX_F_SETLK64:
1558 error = copyin((void *)args->arg, &linux_flock,
1559 sizeof(linux_flock));
1560 if (error)
1561 return (error);
1562 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1563 return (kern_fcntl(td, args->fd, F_SETLK,
1564 (intptr_t)&bsd_flock));
1565
1566 case LINUX_F_SETLKW64:
1567 error = copyin((void *)args->arg, &linux_flock,
1568 sizeof(linux_flock));
1569 if (error)
1570 return (error);
1571 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1572 return (kern_fcntl(td, args->fd, F_SETLKW,
1573 (intptr_t)&bsd_flock));
1574 }
1575
1576 fcntl_args.fd = args->fd;
1577 fcntl_args.cmd = args->cmd;
1578 fcntl_args.arg = args->arg;
1579 return (fcntl_common(td, &fcntl_args));
1580 }
1581 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1582
1583 #ifdef LINUX_LEGACY_SYSCALLS
1584 int
linux_chown(struct thread * td,struct linux_chown_args * args)1585 linux_chown(struct thread *td, struct linux_chown_args *args)
1586 {
1587
1588 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1589 args->uid, args->gid, 0));
1590 }
1591 #endif
1592
1593 int
linux_fchownat(struct thread * td,struct linux_fchownat_args * args)1594 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1595 {
1596 int dfd, flag, unsupported;
1597
1598 unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1599 if (unsupported != 0) {
1600 linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1601 return (EINVAL);
1602 }
1603
1604 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1605 AT_SYMLINK_NOFOLLOW;
1606 flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1607 AT_EMPTY_PATH;
1608
1609 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1610 return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1611 args->uid, args->gid, flag));
1612 }
1613
1614 #ifdef LINUX_LEGACY_SYSCALLS
1615 int
linux_lchown(struct thread * td,struct linux_lchown_args * args)1616 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1617 {
1618
1619 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1620 args->gid, AT_SYMLINK_NOFOLLOW));
1621 }
1622 #endif
1623
1624 static int
convert_fadvice(int advice)1625 convert_fadvice(int advice)
1626 {
1627 switch (advice) {
1628 case LINUX_POSIX_FADV_NORMAL:
1629 return (POSIX_FADV_NORMAL);
1630 case LINUX_POSIX_FADV_RANDOM:
1631 return (POSIX_FADV_RANDOM);
1632 case LINUX_POSIX_FADV_SEQUENTIAL:
1633 return (POSIX_FADV_SEQUENTIAL);
1634 case LINUX_POSIX_FADV_WILLNEED:
1635 return (POSIX_FADV_WILLNEED);
1636 case LINUX_POSIX_FADV_DONTNEED:
1637 return (POSIX_FADV_DONTNEED);
1638 case LINUX_POSIX_FADV_NOREUSE:
1639 return (POSIX_FADV_NOREUSE);
1640 default:
1641 return (-1);
1642 }
1643 }
1644
1645 int
linux_fadvise64(struct thread * td,struct linux_fadvise64_args * args)1646 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1647 {
1648 off_t offset;
1649 int advice;
1650
1651 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1652 offset = PAIR32TO64(off_t, args->offset);
1653 #else
1654 offset = args->offset;
1655 #endif
1656
1657 advice = convert_fadvice(args->advice);
1658 if (advice == -1)
1659 return (EINVAL);
1660 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1661 }
1662
1663 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1664 int
linux_fadvise64_64(struct thread * td,struct linux_fadvise64_64_args * args)1665 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1666 {
1667 off_t len, offset;
1668 int advice;
1669
1670 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1671 len = PAIR32TO64(off_t, args->len);
1672 offset = PAIR32TO64(off_t, args->offset);
1673 #else
1674 len = args->len;
1675 offset = args->offset;
1676 #endif
1677
1678 advice = convert_fadvice(args->advice);
1679 if (advice == -1)
1680 return (EINVAL);
1681 return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1682 }
1683 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1684
1685 #ifdef LINUX_LEGACY_SYSCALLS
1686 int
linux_pipe(struct thread * td,struct linux_pipe_args * args)1687 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1688 {
1689 int fildes[2];
1690 int error;
1691
1692 error = kern_pipe(td, fildes, 0, NULL, NULL);
1693 if (error != 0)
1694 return (error);
1695
1696 error = copyout(fildes, args->pipefds, sizeof(fildes));
1697 if (error != 0) {
1698 (void)kern_close(td, fildes[0]);
1699 (void)kern_close(td, fildes[1]);
1700 }
1701
1702 return (error);
1703 }
1704 #endif
1705
1706 int
linux_pipe2(struct thread * td,struct linux_pipe2_args * args)1707 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1708 {
1709 int fildes[2];
1710 int error, flags;
1711
1712 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1713 return (EINVAL);
1714
1715 flags = 0;
1716 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1717 flags |= O_NONBLOCK;
1718 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1719 flags |= O_CLOEXEC;
1720 error = kern_pipe(td, fildes, flags, NULL, NULL);
1721 if (error != 0)
1722 return (error);
1723
1724 error = copyout(fildes, args->pipefds, sizeof(fildes));
1725 if (error != 0) {
1726 (void)kern_close(td, fildes[0]);
1727 (void)kern_close(td, fildes[1]);
1728 }
1729
1730 return (error);
1731 }
1732
1733 int
linux_dup3(struct thread * td,struct linux_dup3_args * args)1734 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1735 {
1736 int cmd;
1737 intptr_t newfd;
1738
1739 if (args->oldfd == args->newfd)
1740 return (EINVAL);
1741 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1742 return (EINVAL);
1743 if (args->flags & LINUX_O_CLOEXEC)
1744 cmd = F_DUP2FD_CLOEXEC;
1745 else
1746 cmd = F_DUP2FD;
1747
1748 newfd = args->newfd;
1749 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1750 }
1751
1752 int
linux_fallocate(struct thread * td,struct linux_fallocate_args * args)1753 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1754 {
1755 off_t len, offset;
1756
1757 /*
1758 * We emulate only posix_fallocate system call for which
1759 * mode should be 0.
1760 */
1761 if (args->mode != 0)
1762 return (EOPNOTSUPP);
1763
1764 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1765 len = PAIR32TO64(off_t, args->len);
1766 offset = PAIR32TO64(off_t, args->offset);
1767 #else
1768 len = args->len;
1769 offset = args->offset;
1770 #endif
1771
1772 return (kern_posix_fallocate(td, args->fd, offset, len));
1773 }
1774
1775 int
linux_copy_file_range(struct thread * td,struct linux_copy_file_range_args * args)1776 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1777 *args)
1778 {
1779 l_loff_t inoff, outoff, *inoffp, *outoffp;
1780 int error, flags;
1781
1782 /*
1783 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1784 * the native implementation. Enforce it.
1785 */
1786 if (args->flags != 0) {
1787 linux_msg(td, "copy_file_range unsupported flags 0x%x",
1788 args->flags);
1789 return (EINVAL);
1790 }
1791 flags = 0;
1792 inoffp = outoffp = NULL;
1793 if (args->off_in != NULL) {
1794 error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
1795 if (error != 0)
1796 return (error);
1797 inoffp = &inoff;
1798 }
1799 if (args->off_out != NULL) {
1800 error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
1801 if (error != 0)
1802 return (error);
1803 outoffp = &outoff;
1804 }
1805
1806 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
1807 outoffp, args->len, flags);
1808 if (error == 0 && args->off_in != NULL)
1809 error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
1810 if (error == 0 && args->off_out != NULL)
1811 error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
1812 return (error);
1813 }
1814
1815 #define LINUX_MEMFD_PREFIX "memfd:"
1816
1817 int
linux_memfd_create(struct thread * td,struct linux_memfd_create_args * args)1818 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
1819 {
1820 char memfd_name[LINUX_NAME_MAX + 1];
1821 int error, flags, shmflags, oflags;
1822
1823 /*
1824 * This is our clever trick to avoid the heap allocation to copy in the
1825 * uname. We don't really need to go this far out of our way, but it
1826 * does keep the rest of this function fairly clean as they don't have
1827 * to worry about cleanup on the way out.
1828 */
1829 error = copyinstr(args->uname_ptr,
1830 memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
1831 LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
1832 if (error != 0) {
1833 if (error == ENAMETOOLONG)
1834 error = EINVAL;
1835 return (error);
1836 }
1837
1838 memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
1839 flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
1840 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
1841 MFD_HUGE_MASK)) != 0)
1842 return (EINVAL);
1843 /* Size specified but no HUGETLB. */
1844 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
1845 return (EINVAL);
1846 /* We don't actually support HUGETLB. */
1847 if ((flags & MFD_HUGETLB) != 0)
1848 return (ENOSYS);
1849 oflags = O_RDWR;
1850 shmflags = SHM_GROW_ON_WRITE;
1851 if ((flags & MFD_CLOEXEC) != 0)
1852 oflags |= O_CLOEXEC;
1853 if ((flags & MFD_ALLOW_SEALING) != 0)
1854 shmflags |= SHM_ALLOW_SEALING;
1855 return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
1856 memfd_name, NULL));
1857 }
1858
1859 int
linux_splice(struct thread * td,struct linux_splice_args * args)1860 linux_splice(struct thread *td, struct linux_splice_args *args)
1861 {
1862
1863 linux_msg(td, "syscall splice not really implemented");
1864
1865 /*
1866 * splice(2) is documented to return EINVAL in various circumstances;
1867 * returning it instead of ENOSYS should hint the caller to use fallback
1868 * instead.
1869 */
1870 return (EINVAL);
1871 }
1872
1873 int
linux_close_range(struct thread * td,struct linux_close_range_args * args)1874 linux_close_range(struct thread *td, struct linux_close_range_args *args)
1875 {
1876 u_int flags = 0;
1877
1878 /*
1879 * Implementing close_range(CLOSE_RANGE_UNSHARE) allows Linux to
1880 * unshare filedesc table of the calling thread from others threads
1881 * in a thread group (i.e., process in the FreeBSD) or others processes,
1882 * which shares the same table, before closing the files. FreeBSD does
1883 * not have compatible unsharing mechanism due to the fact that sharing
1884 * process resources, including filedesc table, is at thread level in the
1885 * Linux, while in the FreeBSD it is at the process level.
1886 * Return EINVAL for now if the CLOSE_RANGE_UNSHARE flag is specified
1887 * until this new Linux API stabilizes.
1888 */
1889
1890 if ((args->flags & ~(LINUX_CLOSE_RANGE_CLOEXEC)) != 0)
1891 return (EINVAL);
1892 if (args->first > args->last)
1893 return (EINVAL);
1894 if ((args->flags & LINUX_CLOSE_RANGE_CLOEXEC) != 0)
1895 flags |= CLOSE_RANGE_CLOEXEC;
1896 return (kern_close_range(td, flags, args->first, args->last));
1897 }
1898
1899 int
linux_enobufs2eagain(struct thread * td,int fd,int error)1900 linux_enobufs2eagain(struct thread *td, int fd, int error)
1901 {
1902 struct file *fp;
1903
1904 if (error != ENOBUFS)
1905 return (error);
1906 if (fget(td, fd, &cap_no_rights, &fp) != 0)
1907 return (error);
1908 if (fp->f_type == DTYPE_SOCKET && (fp->f_flag & FNONBLOCK) != 0)
1909 error = EAGAIN;
1910 fdrop(fp, td);
1911 return (error);
1912 }
1913
1914 int
linux_write(struct thread * td,struct linux_write_args * args)1915 linux_write(struct thread *td, struct linux_write_args *args)
1916 {
1917 struct write_args bargs = {
1918 .fd = args->fd,
1919 .buf = args->buf,
1920 .nbyte = args->nbyte,
1921 };
1922
1923 return (linux_enobufs2eagain(td, args->fd, sys_write(td, &bargs)));
1924 }
1925
1926 int
linux_writev(struct thread * td,struct linux_writev_args * args)1927 linux_writev(struct thread *td, struct linux_writev_args *args)
1928 {
1929 struct uio *auio;
1930 int error;
1931
1932 #ifdef COMPAT_LINUX32
1933 error = freebsd32_copyinuio(PTRIN(args->iovp), args->iovcnt, &auio);
1934 #else
1935 error = copyinuio(args->iovp, args->iovcnt, &auio);
1936 #endif
1937 if (error != 0)
1938 return (error);
1939 error = kern_writev(td, args->fd, auio);
1940 freeuio(auio);
1941 return (linux_enobufs2eagain(td, args->fd, error));
1942 }
1943
1944 static int
linux_inotify_init_flags(int l_flags)1945 linux_inotify_init_flags(int l_flags)
1946 {
1947 int bsd_flags;
1948
1949 if ((l_flags & ~(LINUX_IN_CLOEXEC | LINUX_IN_NONBLOCK)) != 0)
1950 linux_msg(NULL, "inotify_init1 unsupported flags 0x%x",
1951 l_flags);
1952
1953 bsd_flags = 0;
1954 if ((l_flags & LINUX_IN_CLOEXEC) != 0)
1955 bsd_flags |= O_CLOEXEC;
1956 if ((l_flags & LINUX_IN_NONBLOCK) != 0)
1957 bsd_flags |= O_NONBLOCK;
1958 return (bsd_flags);
1959 }
1960
1961 static int
inotify_init_common(struct thread * td,int flags)1962 inotify_init_common(struct thread *td, int flags)
1963 {
1964 struct specialfd_inotify si;
1965
1966 si.flags = linux_inotify_init_flags(flags);
1967 return (kern_specialfd(td, SPECIALFD_INOTIFY, &si));
1968 }
1969
1970 #if defined(__i386__) || defined(__amd64__)
1971 int
linux_inotify_init(struct thread * td,struct linux_inotify_init_args * args)1972 linux_inotify_init(struct thread *td, struct linux_inotify_init_args *args)
1973 {
1974 return (inotify_init_common(td, 0));
1975 }
1976 #endif
1977
1978 int
linux_inotify_init1(struct thread * td,struct linux_inotify_init1_args * args)1979 linux_inotify_init1(struct thread *td, struct linux_inotify_init1_args *args)
1980 {
1981 return (inotify_init_common(td, args->flags));
1982 }
1983
1984 /*
1985 * The native implementation uses the same values for inotify events as
1986 * libinotify, which gives us binary compatibility with Linux. This simplifies
1987 * the shim implementation a lot, as otherwise we would have to handle read(2)
1988 * calls on inotify descriptors and translate events to Linux's ABI.
1989 */
1990 _Static_assert(LINUX_IN_ACCESS == IN_ACCESS,
1991 "IN_ACCESS mismatch");
1992 _Static_assert(LINUX_IN_MODIFY == IN_MODIFY,
1993 "IN_MODIFY mismatch");
1994 _Static_assert(LINUX_IN_ATTRIB == IN_ATTRIB,
1995 "IN_ATTRIB mismatch");
1996 _Static_assert(LINUX_IN_CLOSE_WRITE == IN_CLOSE_WRITE,
1997 "IN_CLOSE_WRITE mismatch");
1998 _Static_assert(LINUX_IN_CLOSE_NOWRITE == IN_CLOSE_NOWRITE,
1999 "IN_CLOSE_NOWRITE mismatch");
2000 _Static_assert(LINUX_IN_OPEN == IN_OPEN,
2001 "IN_OPEN mismatch");
2002 _Static_assert(LINUX_IN_MOVED_FROM == IN_MOVED_FROM,
2003 "IN_MOVED_FROM mismatch");
2004 _Static_assert(LINUX_IN_MOVED_TO == IN_MOVED_TO,
2005 "IN_MOVED_TO mismatch");
2006 _Static_assert(LINUX_IN_CREATE == IN_CREATE,
2007 "IN_CREATE mismatch");
2008 _Static_assert(LINUX_IN_DELETE == IN_DELETE,
2009 "IN_DELETE mismatch");
2010 _Static_assert(LINUX_IN_DELETE_SELF == IN_DELETE_SELF,
2011 "IN_DELETE_SELF mismatch");
2012 _Static_assert(LINUX_IN_MOVE_SELF == IN_MOVE_SELF,
2013 "IN_MOVE_SELF mismatch");
2014
2015 _Static_assert(LINUX_IN_UNMOUNT == IN_UNMOUNT,
2016 "IN_UNMOUNT mismatch");
2017 _Static_assert(LINUX_IN_Q_OVERFLOW == IN_Q_OVERFLOW,
2018 "IN_Q_OVERFLOW mismatch");
2019 _Static_assert(LINUX_IN_IGNORED == IN_IGNORED,
2020 "IN_IGNORED mismatch");
2021
2022 _Static_assert(LINUX_IN_ISDIR == IN_ISDIR,
2023 "IN_ISDIR mismatch");
2024 _Static_assert(LINUX_IN_ONLYDIR == IN_ONLYDIR,
2025 "IN_ONLYDIR mismatch");
2026 _Static_assert(LINUX_IN_DONT_FOLLOW == IN_DONT_FOLLOW,
2027 "IN_DONT_FOLLOW mismatch");
2028 _Static_assert(LINUX_IN_MASK_CREATE == IN_MASK_CREATE,
2029 "IN_MASK_CREATE mismatch");
2030 _Static_assert(LINUX_IN_MASK_ADD == IN_MASK_ADD,
2031 "IN_MASK_ADD mismatch");
2032 _Static_assert(LINUX_IN_ONESHOT == IN_ONESHOT,
2033 "IN_ONESHOT mismatch");
2034 _Static_assert(LINUX_IN_EXCL_UNLINK == IN_EXCL_UNLINK,
2035 "IN_EXCL_UNLINK mismatch");
2036
2037 static int
linux_inotify_watch_flags(int l_flags)2038 linux_inotify_watch_flags(int l_flags)
2039 {
2040 if ((l_flags & ~(LINUX_IN_ALL_EVENTS | LINUX_IN_ALL_FLAGS)) != 0) {
2041 linux_msg(NULL, "inotify_add_watch unsupported flags 0x%x",
2042 l_flags);
2043 }
2044
2045 return (l_flags);
2046 }
2047
2048 int
linux_inotify_add_watch(struct thread * td,struct linux_inotify_add_watch_args * args)2049 linux_inotify_add_watch(struct thread *td,
2050 struct linux_inotify_add_watch_args *args)
2051 {
2052 return (kern_inotify_add_watch(args->fd, AT_FDCWD, args->pathname,
2053 linux_inotify_watch_flags(args->mask), td));
2054 }
2055
2056 int
linux_inotify_rm_watch(struct thread * td,struct linux_inotify_rm_watch_args * args)2057 linux_inotify_rm_watch(struct thread *td,
2058 struct linux_inotify_rm_watch_args *args)
2059 {
2060 return (kern_inotify_rm_watch(args->fd, args->wd, td));
2061 }
2062