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