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 #ifdef LINUX_LEGACY_SYSCALLS
402 int
linux_getdents(struct thread * td,struct linux_getdents_args * args)403 linux_getdents(struct thread *td, struct linux_getdents_args *args)
404 {
405 struct dirent *bdp;
406 caddr_t inp, buf; /* BSD-format */
407 int len, reclen; /* BSD-format */
408 caddr_t outp; /* Linux-format */
409 int resid, linuxreclen; /* Linux-format */
410 caddr_t lbuf; /* Linux-format */
411 off_t base;
412 struct l_dirent *linux_dirent;
413 int buflen, error;
414 size_t retval;
415
416 buflen = min(args->count, MAXBSIZE);
417 buf = malloc(buflen, M_LINUX, M_WAITOK);
418
419 error = kern_getdirentries(td, args->fd, buf, buflen,
420 &base, NULL, UIO_SYSSPACE);
421 if (error != 0) {
422 error = linux_getdents_error(td, args->fd, error);
423 goto out1;
424 }
425
426 lbuf = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX, M_WAITOK | M_ZERO);
427
428 len = td->td_retval[0];
429 inp = buf;
430 outp = (caddr_t)args->dent;
431 resid = args->count;
432 retval = 0;
433
434 while (len > 0) {
435 bdp = (struct dirent *) inp;
436 reclen = bdp->d_reclen;
437 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
438 /*
439 * No more space in the user supplied dirent buffer.
440 * Return EINVAL.
441 */
442 if (resid < linuxreclen) {
443 error = EINVAL;
444 goto out;
445 }
446
447 linux_dirent = (struct l_dirent*)lbuf;
448 linux_dirent->d_ino = bdp->d_fileno;
449 linux_dirent->d_off = bdp->d_off;
450 linux_dirent->d_reclen = linuxreclen;
451 /*
452 * Copy d_type to last byte of l_dirent buffer
453 */
454 lbuf[linuxreclen - 1] = bdp->d_type;
455 strlcpy(linux_dirent->d_name, bdp->d_name,
456 linuxreclen - offsetof(struct l_dirent, d_name)-1);
457 error = copyout(linux_dirent, outp, linuxreclen);
458 if (error != 0)
459 goto out;
460
461 inp += reclen;
462 base += reclen;
463 len -= reclen;
464
465 retval += linuxreclen;
466 outp += linuxreclen;
467 resid -= linuxreclen;
468 }
469 td->td_retval[0] = retval;
470
471 out:
472 free(lbuf, M_LINUX);
473 out1:
474 free(buf, M_LINUX);
475 return (error);
476 }
477 #endif
478
479 int
linux_getdents64(struct thread * td,struct linux_getdents64_args * args)480 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
481 {
482 struct dirent *bdp;
483 caddr_t inp, buf; /* BSD-format */
484 int len, reclen; /* BSD-format */
485 caddr_t outp; /* Linux-format */
486 int resid, linuxreclen; /* Linux-format */
487 off_t base;
488 struct l_dirent64 *linux_dirent64;
489 int buflen, error;
490 size_t retval;
491
492 buflen = min(args->count, MAXBSIZE);
493 buf = malloc(buflen, M_LINUX, M_WAITOK);
494
495 error = kern_getdirentries(td, args->fd, buf, buflen,
496 &base, NULL, UIO_SYSSPACE);
497 if (error != 0) {
498 error = linux_getdents_error(td, args->fd, error);
499 goto out1;
500 }
501
502 linux_dirent64 = malloc(LINUX_RECLEN64(LINUX_NAME_MAX), M_LINUX,
503 M_WAITOK | M_ZERO);
504
505 len = td->td_retval[0];
506 inp = buf;
507 outp = (caddr_t)args->dirent;
508 resid = args->count;
509 retval = 0;
510
511 while (len > 0) {
512 bdp = (struct dirent *) inp;
513 reclen = bdp->d_reclen;
514 linuxreclen = LINUX_RECLEN64(bdp->d_namlen);
515 /*
516 * No more space in the user supplied dirent buffer.
517 * Return EINVAL.
518 */
519 if (resid < linuxreclen) {
520 error = EINVAL;
521 goto out;
522 }
523
524 linux_dirent64->d_ino = bdp->d_fileno;
525 linux_dirent64->d_off = bdp->d_off;
526 linux_dirent64->d_reclen = linuxreclen;
527 linux_dirent64->d_type = bdp->d_type;
528 strlcpy(linux_dirent64->d_name, bdp->d_name,
529 linuxreclen - offsetof(struct l_dirent64, d_name));
530 error = copyout(linux_dirent64, outp, linuxreclen);
531 if (error != 0)
532 goto out;
533
534 inp += reclen;
535 base += reclen;
536 len -= reclen;
537
538 retval += linuxreclen;
539 outp += linuxreclen;
540 resid -= linuxreclen;
541 }
542 td->td_retval[0] = retval;
543
544 out:
545 free(linux_dirent64, M_LINUX);
546 out1:
547 free(buf, M_LINUX);
548 return (error);
549 }
550
551 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
552 int
linux_readdir(struct thread * td,struct linux_readdir_args * args)553 linux_readdir(struct thread *td, struct linux_readdir_args *args)
554 {
555 struct dirent *bdp;
556 caddr_t buf; /* BSD-format */
557 int linuxreclen; /* Linux-format */
558 off_t base;
559 struct l_dirent *linux_dirent; /* Linux-format */
560 int buflen, error;
561
562 buflen = sizeof(*bdp);
563 buf = malloc(buflen, M_LINUX, M_WAITOK);
564
565 error = kern_getdirentries(td, args->fd, buf, buflen,
566 &base, NULL, UIO_SYSSPACE);
567 if (error != 0) {
568 error = linux_getdents_error(td, args->fd, error);
569 goto out;
570 }
571 if (td->td_retval[0] == 0)
572 goto out;
573
574 linux_dirent = malloc(LINUX_RECLEN(LINUX_NAME_MAX), M_LINUX,
575 M_WAITOK | M_ZERO);
576
577 bdp = (struct dirent *) buf;
578 linuxreclen = LINUX_RECLEN(bdp->d_namlen);
579
580 linux_dirent->d_ino = bdp->d_fileno;
581 linux_dirent->d_off = bdp->d_off;
582 linux_dirent->d_reclen = bdp->d_namlen;
583 strlcpy(linux_dirent->d_name, bdp->d_name,
584 linuxreclen - offsetof(struct l_dirent, d_name));
585 error = copyout(linux_dirent, args->dent, linuxreclen);
586 if (error == 0)
587 td->td_retval[0] = linuxreclen;
588
589 free(linux_dirent, M_LINUX);
590 out:
591 free(buf, M_LINUX);
592 return (error);
593 }
594 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
595
596 /*
597 * These exist mainly for hooks for doing /compat/linux translation.
598 */
599
600 #ifdef LINUX_LEGACY_SYSCALLS
601 int
linux_access(struct thread * td,struct linux_access_args * args)602 linux_access(struct thread *td, struct linux_access_args *args)
603 {
604
605 /* Linux convention. */
606 if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
607 return (EINVAL);
608
609 return (kern_accessat(td, AT_FDCWD, args->path, UIO_USERSPACE, 0,
610 args->amode));
611 }
612 #endif
613
614 static int
linux_do_accessat(struct thread * td,int ldfd,const char * filename,int amode,int flags)615 linux_do_accessat(struct thread *td, int ldfd, const char *filename,
616 int amode, int flags)
617 {
618 int dfd;
619
620 /* Linux convention. */
621 if (amode & ~(F_OK | X_OK | W_OK | R_OK))
622 return (EINVAL);
623
624 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd;
625 return (kern_accessat(td, dfd, filename, UIO_USERSPACE, flags, amode));
626 }
627
628 int
linux_faccessat(struct thread * td,struct linux_faccessat_args * args)629 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
630 {
631
632 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
633 0));
634 }
635
636 int
linux_faccessat2(struct thread * td,struct linux_faccessat2_args * args)637 linux_faccessat2(struct thread *td, struct linux_faccessat2_args *args)
638 {
639 int flags, unsupported;
640
641 unsupported = args->flags & ~(LINUX_AT_EACCESS | LINUX_AT_EMPTY_PATH |
642 LINUX_AT_SYMLINK_NOFOLLOW);
643 if (unsupported != 0) {
644 linux_msg(td, "faccessat2 unsupported flag 0x%x", unsupported);
645 return (EINVAL);
646 }
647
648 flags = (args->flags & LINUX_AT_EACCESS) == 0 ? 0 :
649 AT_EACCESS;
650 flags |= (args->flags & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
651 AT_EMPTY_PATH;
652 flags |= (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
653 AT_SYMLINK_NOFOLLOW;
654 return (linux_do_accessat(td, args->dfd, args->filename, args->amode,
655 flags));
656 }
657
658
659 #ifdef LINUX_LEGACY_SYSCALLS
660 int
linux_unlink(struct thread * td,struct linux_unlink_args * args)661 linux_unlink(struct thread *td, struct linux_unlink_args *args)
662 {
663 int error;
664 struct stat st;
665
666 error = kern_funlinkat(td, AT_FDCWD, args->path, FD_NONE,
667 UIO_USERSPACE, 0, 0);
668 if (error == EPERM) {
669 /* Introduce POSIX noncompliant behaviour of Linux */
670 if (kern_statat(td, 0, AT_FDCWD, args->path,
671 UIO_USERSPACE, &st) == 0) {
672 if (S_ISDIR(st.st_mode))
673 error = EISDIR;
674 }
675 }
676
677 return (error);
678 }
679 #endif
680
681 static int
linux_unlinkat_impl(struct thread * td,enum uio_seg pathseg,const char * path,int dfd,struct linux_unlinkat_args * args)682 linux_unlinkat_impl(struct thread *td, enum uio_seg pathseg, const char *path,
683 int dfd, struct linux_unlinkat_args *args)
684 {
685 struct stat st;
686 int error;
687
688 if (args->flag & LINUX_AT_REMOVEDIR)
689 error = kern_frmdirat(td, dfd, path, FD_NONE, pathseg, 0);
690 else
691 error = kern_funlinkat(td, dfd, path, FD_NONE, pathseg, 0, 0);
692 if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
693 /* Introduce POSIX noncompliant behaviour of Linux */
694 if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
695 pathseg, &st) == 0 && S_ISDIR(st.st_mode))
696 error = EISDIR;
697 }
698 return (error);
699 }
700
701 int
linux_unlinkat(struct thread * td,struct linux_unlinkat_args * args)702 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
703 {
704 int dfd;
705
706 if (args->flag & ~LINUX_AT_REMOVEDIR)
707 return (EINVAL);
708 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
709 return (linux_unlinkat_impl(td, UIO_USERSPACE, args->pathname,
710 dfd, args));
711 }
712
713 int
linux_chdir(struct thread * td,struct linux_chdir_args * args)714 linux_chdir(struct thread *td, struct linux_chdir_args *args)
715 {
716
717 return (kern_chdir(td, args->path, UIO_USERSPACE));
718 }
719
720 #ifdef LINUX_LEGACY_SYSCALLS
721 int
linux_chmod(struct thread * td,struct linux_chmod_args * args)722 linux_chmod(struct thread *td, struct linux_chmod_args *args)
723 {
724
725 return (kern_fchmodat(td, AT_FDCWD, args->path, UIO_USERSPACE,
726 args->mode, 0));
727 }
728 #endif
729
730 int
linux_fchmodat(struct thread * td,struct linux_fchmodat_args * args)731 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
732 {
733 int dfd;
734
735 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
736 return (kern_fchmodat(td, dfd, args->filename, UIO_USERSPACE,
737 args->mode, 0));
738 }
739
740 #ifdef LINUX_LEGACY_SYSCALLS
741 int
linux_mkdir(struct thread * td,struct linux_mkdir_args * args)742 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
743 {
744
745 return (kern_mkdirat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->mode));
746 }
747 #endif
748
749 int
linux_mkdirat(struct thread * td,struct linux_mkdirat_args * args)750 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
751 {
752 int dfd;
753
754 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
755 return (kern_mkdirat(td, dfd, args->pathname, UIO_USERSPACE, args->mode));
756 }
757
758 #ifdef LINUX_LEGACY_SYSCALLS
759 int
linux_rmdir(struct thread * td,struct linux_rmdir_args * args)760 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
761 {
762
763 return (kern_frmdirat(td, AT_FDCWD, args->path, FD_NONE,
764 UIO_USERSPACE, 0));
765 }
766
767 int
linux_rename(struct thread * td,struct linux_rename_args * args)768 linux_rename(struct thread *td, struct linux_rename_args *args)
769 {
770
771 return (kern_renameat(td, AT_FDCWD, args->from, AT_FDCWD,
772 args->to, UIO_USERSPACE));
773 }
774 #endif
775
776 int
linux_renameat(struct thread * td,struct linux_renameat_args * args)777 linux_renameat(struct thread *td, struct linux_renameat_args *args)
778 {
779 struct linux_renameat2_args renameat2_args = {
780 .olddfd = args->olddfd,
781 .oldname = args->oldname,
782 .newdfd = args->newdfd,
783 .newname = args->newname,
784 .flags = 0
785 };
786
787 return (linux_renameat2(td, &renameat2_args));
788 }
789
790 int
linux_renameat2(struct thread * td,struct linux_renameat2_args * args)791 linux_renameat2(struct thread *td, struct linux_renameat2_args *args)
792 {
793 int olddfd, newdfd;
794
795 if (args->flags != 0) {
796 if (args->flags & ~(LINUX_RENAME_EXCHANGE |
797 LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
798 return (EINVAL);
799 if (args->flags & LINUX_RENAME_EXCHANGE &&
800 args->flags & (LINUX_RENAME_NOREPLACE |
801 LINUX_RENAME_WHITEOUT))
802 return (EINVAL);
803 #if 0
804 /*
805 * This spams the console on Ubuntu Focal.
806 *
807 * What's needed here is a general mechanism to let users know
808 * about missing features without hogging the system.
809 */
810 linux_msg(td, "renameat2 unsupported flags 0x%x",
811 args->flags);
812 #endif
813 return (EINVAL);
814 }
815
816 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
817 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
818 return (kern_renameat(td, olddfd, args->oldname, newdfd,
819 args->newname, UIO_USERSPACE));
820 }
821
822 #ifdef LINUX_LEGACY_SYSCALLS
823 int
linux_symlink(struct thread * td,struct linux_symlink_args * args)824 linux_symlink(struct thread *td, struct linux_symlink_args *args)
825 {
826
827 return (kern_symlinkat(td, args->path, AT_FDCWD, args->to,
828 UIO_USERSPACE));
829 }
830 #endif
831
832 int
linux_symlinkat(struct thread * td,struct linux_symlinkat_args * args)833 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
834 {
835 int dfd;
836
837 dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
838 return (kern_symlinkat(td, args->oldname, dfd, args->newname,
839 UIO_USERSPACE));
840 }
841
842 #ifdef LINUX_LEGACY_SYSCALLS
843 int
linux_readlink(struct thread * td,struct linux_readlink_args * args)844 linux_readlink(struct thread *td, struct linux_readlink_args *args)
845 {
846
847 if (args->count <= 0)
848 return (EINVAL);
849
850 return (kern_readlinkat(td, AT_FDCWD, args->name, UIO_USERSPACE,
851 args->buf, UIO_USERSPACE, args->count));
852 }
853 #endif
854
855 int
linux_readlinkat(struct thread * td,struct linux_readlinkat_args * args)856 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
857 {
858 int dfd;
859
860 if (args->bufsiz <= 0)
861 return (EINVAL);
862
863 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
864 return (kern_readlinkat(td, dfd, args->path, UIO_USERSPACE,
865 args->buf, UIO_USERSPACE, args->bufsiz));
866 }
867
868 int
linux_truncate(struct thread * td,struct linux_truncate_args * args)869 linux_truncate(struct thread *td, struct linux_truncate_args *args)
870 {
871
872 return (kern_truncate(td, args->path, UIO_USERSPACE, args->length));
873 }
874
875 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
876 int
linux_truncate64(struct thread * td,struct linux_truncate64_args * args)877 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
878 {
879 off_t length;
880
881 #if defined(__amd64__) && defined(COMPAT_LINUX32)
882 length = PAIR32TO64(off_t, args->length);
883 #else
884 length = args->length;
885 #endif
886
887 return (kern_truncate(td, args->path, UIO_USERSPACE, length));
888 }
889 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
890
891 int
linux_ftruncate(struct thread * td,struct linux_ftruncate_args * args)892 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
893 {
894
895 return (kern_ftruncate(td, args->fd, args->length));
896 }
897
898 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
899 int
linux_ftruncate64(struct thread * td,struct linux_ftruncate64_args * args)900 linux_ftruncate64(struct thread *td, struct linux_ftruncate64_args *args)
901 {
902 off_t length;
903
904 #if defined(__amd64__) && defined(COMPAT_LINUX32)
905 length = PAIR32TO64(off_t, args->length);
906 #else
907 length = args->length;
908 #endif
909
910 return (kern_ftruncate(td, args->fd, length));
911 }
912 #endif
913
914 #ifdef LINUX_LEGACY_SYSCALLS
915 int
linux_link(struct thread * td,struct linux_link_args * args)916 linux_link(struct thread *td, struct linux_link_args *args)
917 {
918
919 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, args->path, args->to,
920 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
921 }
922 #endif
923
924 int
linux_linkat(struct thread * td,struct linux_linkat_args * args)925 linux_linkat(struct thread *td, struct linux_linkat_args *args)
926 {
927 int olddfd, newdfd, flag;
928
929 if (args->flag & ~(LINUX_AT_SYMLINK_FOLLOW | LINUX_AT_EMPTY_PATH))
930 return (EINVAL);
931
932 flag = (args->flag & LINUX_AT_SYMLINK_FOLLOW) != 0 ? AT_SYMLINK_FOLLOW :
933 0;
934 flag |= (args->flag & LINUX_AT_EMPTY_PATH) != 0 ? AT_EMPTY_PATH : 0;
935
936 olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
937 newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
938 return (kern_linkat(td, olddfd, newdfd, args->oldname,
939 args->newname, UIO_USERSPACE, flag));
940 }
941
942 int
linux_fdatasync(struct thread * td,struct linux_fdatasync_args * uap)943 linux_fdatasync(struct thread *td, struct linux_fdatasync_args *uap)
944 {
945
946 return (kern_fsync(td, uap->fd, false));
947 }
948
949 int
linux_sync_file_range(struct thread * td,struct linux_sync_file_range_args * uap)950 linux_sync_file_range(struct thread *td, struct linux_sync_file_range_args *uap)
951 {
952 off_t nbytes, offset;
953
954 #if defined(__amd64__) && defined(COMPAT_LINUX32)
955 nbytes = PAIR32TO64(off_t, uap->nbytes);
956 offset = PAIR32TO64(off_t, uap->offset);
957 #else
958 nbytes = uap->nbytes;
959 offset = uap->offset;
960 #endif
961
962 if (offset < 0 || nbytes < 0 ||
963 (uap->flags & ~(LINUX_SYNC_FILE_RANGE_WAIT_BEFORE |
964 LINUX_SYNC_FILE_RANGE_WRITE |
965 LINUX_SYNC_FILE_RANGE_WAIT_AFTER)) != 0) {
966 return (EINVAL);
967 }
968
969 return (kern_fsync(td, uap->fd, false));
970 }
971
972 int
linux_pread(struct thread * td,struct linux_pread_args * uap)973 linux_pread(struct thread *td, struct linux_pread_args *uap)
974 {
975 struct vnode *vp;
976 off_t offset;
977 int error;
978
979 #if defined(__amd64__) && defined(COMPAT_LINUX32)
980 offset = PAIR32TO64(off_t, uap->offset);
981 #else
982 offset = uap->offset;
983 #endif
984
985 error = kern_pread(td, uap->fd, uap->buf, uap->nbyte, offset);
986 if (error == 0) {
987 /* This seems to violate POSIX but Linux does it. */
988 error = fgetvp(td, uap->fd, &cap_pread_rights, &vp);
989 if (error != 0)
990 return (error);
991 if (vp->v_type == VDIR)
992 error = EISDIR;
993 vrele(vp);
994 }
995 return (error);
996 }
997
998 int
linux_pwrite(struct thread * td,struct linux_pwrite_args * uap)999 linux_pwrite(struct thread *td, struct linux_pwrite_args *uap)
1000 {
1001 off_t offset;
1002
1003 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1004 offset = PAIR32TO64(off_t, uap->offset);
1005 #else
1006 offset = uap->offset;
1007 #endif
1008
1009 return (linux_enobufs2eagain(td, uap->fd,
1010 kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, offset)));
1011 }
1012
1013 #define HALF_LONG_BITS ((sizeof(l_long) * NBBY / 2))
1014
1015 static inline off_t
pos_from_hilo(unsigned long high,unsigned long low)1016 pos_from_hilo(unsigned long high, unsigned long low)
1017 {
1018
1019 return (((off_t)high << HALF_LONG_BITS) << HALF_LONG_BITS) | low;
1020 }
1021
1022 int
linux_preadv(struct thread * td,struct linux_preadv_args * uap)1023 linux_preadv(struct thread *td, struct linux_preadv_args *uap)
1024 {
1025 struct uio *auio;
1026 int error;
1027 off_t offset;
1028
1029 /*
1030 * According http://man7.org/linux/man-pages/man2/preadv.2.html#NOTES
1031 * pos_l and pos_h, respectively, contain the
1032 * low order and high order 32 bits of offset.
1033 */
1034 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1035 if (offset < 0)
1036 return (EINVAL);
1037 #ifdef COMPAT_LINUX32
1038 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1039 #else
1040 error = copyinuio(uap->vec, uap->vlen, &auio);
1041 #endif
1042 if (error != 0)
1043 return (error);
1044 error = kern_preadv(td, uap->fd, auio, offset);
1045 freeuio(auio);
1046 return (error);
1047 }
1048
1049 int
linux_pwritev(struct thread * td,struct linux_pwritev_args * uap)1050 linux_pwritev(struct thread *td, struct linux_pwritev_args *uap)
1051 {
1052 struct uio *auio;
1053 int error;
1054 off_t offset;
1055
1056 /*
1057 * According http://man7.org/linux/man-pages/man2/pwritev.2.html#NOTES
1058 * pos_l and pos_h, respectively, contain the
1059 * low order and high order 32 bits of offset.
1060 */
1061 offset = pos_from_hilo(uap->pos_h, uap->pos_l);
1062 if (offset < 0)
1063 return (EINVAL);
1064 #ifdef COMPAT_LINUX32
1065 error = freebsd32_copyinuio(PTRIN(uap->vec), uap->vlen, &auio);
1066 #else
1067 error = copyinuio(uap->vec, uap->vlen, &auio);
1068 #endif
1069 if (error != 0)
1070 return (error);
1071 error = kern_pwritev(td, uap->fd, auio, offset);
1072 freeuio(auio);
1073 return (linux_enobufs2eagain(td, uap->fd, error));
1074 }
1075
1076 int
linux_mount(struct thread * td,struct linux_mount_args * args)1077 linux_mount(struct thread *td, struct linux_mount_args *args)
1078 {
1079 struct mntarg *ma = NULL;
1080 char *fstypename, *mntonname, *mntfromname, *data;
1081 int error, fsflags;
1082
1083 fstypename = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1084 mntonname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1085 mntfromname = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1086 data = NULL;
1087 error = copyinstr(args->filesystemtype, fstypename, MNAMELEN - 1,
1088 NULL);
1089 if (error != 0)
1090 goto out;
1091 if (args->specialfile != NULL) {
1092 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1093 if (error != 0)
1094 goto out;
1095 } else {
1096 mntfromname[0] = '\0';
1097 }
1098 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1099 if (error != 0)
1100 goto out;
1101
1102 if (strcmp(fstypename, "ext2") == 0) {
1103 strcpy(fstypename, "ext2fs");
1104 } else if (strcmp(fstypename, "proc") == 0) {
1105 strcpy(fstypename, "linprocfs");
1106 } else if (strcmp(fstypename, "vfat") == 0) {
1107 strcpy(fstypename, "msdosfs");
1108 } else if (strcmp(fstypename, "fuse") == 0 ||
1109 strncmp(fstypename, "fuse.", 5) == 0) {
1110 char *fuse_options, *fuse_option, *fuse_name;
1111
1112 strcpy(mntfromname, "/dev/fuse");
1113 strcpy(fstypename, "fusefs");
1114 data = malloc(MNAMELEN, M_TEMP, M_WAITOK);
1115 error = copyinstr(args->data, data, MNAMELEN - 1, NULL);
1116 if (error != 0)
1117 goto out;
1118
1119 fuse_options = data;
1120 while ((fuse_option = strsep(&fuse_options, ",")) != NULL) {
1121 fuse_name = strsep(&fuse_option, "=");
1122 if (fuse_name == NULL || fuse_option == NULL)
1123 goto out;
1124 ma = mount_arg(ma, fuse_name, fuse_option, -1);
1125 }
1126
1127 /*
1128 * The FUSE server uses Linux errno values instead of FreeBSD
1129 * ones; add a flag to tell fuse(4) to do errno translation.
1130 */
1131 ma = mount_arg(ma, "linux_errnos", "1", -1);
1132 }
1133
1134 fsflags = 0;
1135
1136 /*
1137 * Linux SYNC flag is not included; the closest equivalent
1138 * FreeBSD has is !ASYNC, which is our default.
1139 */
1140 if (args->rwflag & LINUX_MS_RDONLY)
1141 fsflags |= MNT_RDONLY;
1142 if (args->rwflag & LINUX_MS_NOSUID)
1143 fsflags |= MNT_NOSUID;
1144 if (args->rwflag & LINUX_MS_NOEXEC)
1145 fsflags |= MNT_NOEXEC;
1146 if (args->rwflag & LINUX_MS_REMOUNT)
1147 fsflags |= MNT_UPDATE;
1148
1149 ma = mount_arg(ma, "fstype", fstypename, -1);
1150 ma = mount_arg(ma, "fspath", mntonname, -1);
1151 ma = mount_arg(ma, "from", mntfromname, -1);
1152 error = kernel_mount(ma, fsflags);
1153 out:
1154 free(fstypename, M_TEMP);
1155 free(mntonname, M_TEMP);
1156 free(mntfromname, M_TEMP);
1157 free(data, M_TEMP);
1158 return (error);
1159 }
1160
1161 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1162 int
linux_oldumount(struct thread * td,struct linux_oldumount_args * args)1163 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1164 {
1165
1166 return (kern_unmount(td, args->path, 0));
1167 }
1168 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1169
1170 #ifdef LINUX_LEGACY_SYSCALLS
1171 int
linux_umount(struct thread * td,struct linux_umount_args * args)1172 linux_umount(struct thread *td, struct linux_umount_args *args)
1173 {
1174 int flags;
1175
1176 flags = 0;
1177 if ((args->flags & LINUX_MNT_FORCE) != 0) {
1178 args->flags &= ~LINUX_MNT_FORCE;
1179 flags |= MNT_FORCE;
1180 }
1181 if (args->flags != 0) {
1182 linux_msg(td, "unsupported umount2 flags %#x", args->flags);
1183 return (EINVAL);
1184 }
1185
1186 return (kern_unmount(td, args->path, flags));
1187 }
1188 #endif
1189
1190 /*
1191 * fcntl family of syscalls
1192 */
1193
1194 struct l_flock {
1195 l_short l_type;
1196 l_short l_whence;
1197 l_off_t l_start;
1198 l_off_t l_len;
1199 l_pid_t l_pid;
1200 }
1201 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1202 __packed
1203 #endif
1204 ;
1205
1206 static void
linux_to_bsd_flock(struct l_flock * linux_flock,struct flock * bsd_flock)1207 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1208 {
1209 switch (linux_flock->l_type) {
1210 case LINUX_F_RDLCK:
1211 bsd_flock->l_type = F_RDLCK;
1212 break;
1213 case LINUX_F_WRLCK:
1214 bsd_flock->l_type = F_WRLCK;
1215 break;
1216 case LINUX_F_UNLCK:
1217 bsd_flock->l_type = F_UNLCK;
1218 break;
1219 default:
1220 bsd_flock->l_type = -1;
1221 break;
1222 }
1223 bsd_flock->l_whence = linux_flock->l_whence;
1224 bsd_flock->l_start = (off_t)linux_flock->l_start;
1225 bsd_flock->l_len = (off_t)linux_flock->l_len;
1226 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1227 bsd_flock->l_sysid = 0;
1228 }
1229
1230 static void
bsd_to_linux_flock(struct flock * bsd_flock,struct l_flock * linux_flock)1231 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1232 {
1233 switch (bsd_flock->l_type) {
1234 case F_RDLCK:
1235 linux_flock->l_type = LINUX_F_RDLCK;
1236 break;
1237 case F_WRLCK:
1238 linux_flock->l_type = LINUX_F_WRLCK;
1239 break;
1240 case F_UNLCK:
1241 linux_flock->l_type = LINUX_F_UNLCK;
1242 break;
1243 }
1244 linux_flock->l_whence = bsd_flock->l_whence;
1245 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1246 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1247 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1248 }
1249
1250 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1251 struct l_flock64 {
1252 l_short l_type;
1253 l_short l_whence;
1254 l_loff_t l_start;
1255 l_loff_t l_len;
1256 l_pid_t l_pid;
1257 }
1258 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1259 __packed
1260 #endif
1261 ;
1262
1263 static void
linux_to_bsd_flock64(struct l_flock64 * linux_flock,struct flock * bsd_flock)1264 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1265 {
1266 switch (linux_flock->l_type) {
1267 case LINUX_F_RDLCK:
1268 bsd_flock->l_type = F_RDLCK;
1269 break;
1270 case LINUX_F_WRLCK:
1271 bsd_flock->l_type = F_WRLCK;
1272 break;
1273 case LINUX_F_UNLCK:
1274 bsd_flock->l_type = F_UNLCK;
1275 break;
1276 default:
1277 bsd_flock->l_type = -1;
1278 break;
1279 }
1280 bsd_flock->l_whence = linux_flock->l_whence;
1281 bsd_flock->l_start = (off_t)linux_flock->l_start;
1282 bsd_flock->l_len = (off_t)linux_flock->l_len;
1283 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1284 bsd_flock->l_sysid = 0;
1285 }
1286
1287 static void
bsd_to_linux_flock64(struct flock * bsd_flock,struct l_flock64 * linux_flock)1288 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1289 {
1290 switch (bsd_flock->l_type) {
1291 case F_RDLCK:
1292 linux_flock->l_type = LINUX_F_RDLCK;
1293 break;
1294 case F_WRLCK:
1295 linux_flock->l_type = LINUX_F_WRLCK;
1296 break;
1297 case F_UNLCK:
1298 linux_flock->l_type = LINUX_F_UNLCK;
1299 break;
1300 }
1301 linux_flock->l_whence = bsd_flock->l_whence;
1302 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1303 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1304 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1305 }
1306 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1307
1308 static int
fcntl_common(struct thread * td,struct linux_fcntl_args * args)1309 fcntl_common(struct thread *td, struct linux_fcntl_args *args)
1310 {
1311 struct l_flock linux_flock;
1312 struct flock bsd_flock;
1313 struct pipe *fpipe;
1314 struct file *fp;
1315 long arg;
1316 int error, result;
1317
1318 switch (args->cmd) {
1319 case LINUX_F_DUPFD:
1320 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1321
1322 case LINUX_F_GETFD:
1323 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1324
1325 case LINUX_F_SETFD:
1326 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1327
1328 case LINUX_F_GETFL:
1329 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1330 result = td->td_retval[0];
1331 td->td_retval[0] = 0;
1332 if (result & O_RDONLY)
1333 td->td_retval[0] |= LINUX_O_RDONLY;
1334 if (result & O_WRONLY)
1335 td->td_retval[0] |= LINUX_O_WRONLY;
1336 if (result & O_RDWR)
1337 td->td_retval[0] |= LINUX_O_RDWR;
1338 if (result & O_NDELAY)
1339 td->td_retval[0] |= LINUX_O_NONBLOCK;
1340 if (result & O_APPEND)
1341 td->td_retval[0] |= LINUX_O_APPEND;
1342 if (result & O_FSYNC)
1343 td->td_retval[0] |= LINUX_O_SYNC;
1344 if (result & O_ASYNC)
1345 td->td_retval[0] |= LINUX_O_ASYNC;
1346 #ifdef LINUX_O_NOFOLLOW
1347 if (result & O_NOFOLLOW)
1348 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1349 #endif
1350 #ifdef LINUX_O_DIRECT
1351 if (result & O_DIRECT)
1352 td->td_retval[0] |= LINUX_O_DIRECT;
1353 #endif
1354 return (error);
1355
1356 case LINUX_F_SETFL:
1357 arg = 0;
1358 if (args->arg & LINUX_O_NDELAY)
1359 arg |= O_NONBLOCK;
1360 if (args->arg & LINUX_O_APPEND)
1361 arg |= O_APPEND;
1362 if (args->arg & LINUX_O_SYNC)
1363 arg |= O_FSYNC;
1364 if (args->arg & LINUX_O_ASYNC)
1365 arg |= O_ASYNC;
1366 #ifdef LINUX_O_NOFOLLOW
1367 if (args->arg & LINUX_O_NOFOLLOW)
1368 arg |= O_NOFOLLOW;
1369 #endif
1370 #ifdef LINUX_O_DIRECT
1371 if (args->arg & LINUX_O_DIRECT)
1372 arg |= O_DIRECT;
1373 #endif
1374 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1375
1376 case LINUX_F_GETLK:
1377 error = copyin((void *)args->arg, &linux_flock,
1378 sizeof(linux_flock));
1379 if (error)
1380 return (error);
1381 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1382 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1383 if (error)
1384 return (error);
1385 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1386 return (copyout(&linux_flock, (void *)args->arg,
1387 sizeof(linux_flock)));
1388
1389 case LINUX_F_SETLK:
1390 error = copyin((void *)args->arg, &linux_flock,
1391 sizeof(linux_flock));
1392 if (error)
1393 return (error);
1394 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1395 return (kern_fcntl(td, args->fd, F_SETLK,
1396 (intptr_t)&bsd_flock));
1397
1398 case LINUX_F_SETLKW:
1399 error = copyin((void *)args->arg, &linux_flock,
1400 sizeof(linux_flock));
1401 if (error)
1402 return (error);
1403 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1404 return (kern_fcntl(td, args->fd, F_SETLKW,
1405 (intptr_t)&bsd_flock));
1406
1407 case LINUX_F_GETOWN:
1408 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1409
1410 case LINUX_F_SETOWN:
1411 /*
1412 * XXX some Linux applications depend on F_SETOWN having no
1413 * significant effect for pipes (SIGIO is not delivered for
1414 * pipes under Linux-2.2.35 at least).
1415 */
1416 error = fget(td, args->fd,
1417 &cap_fcntl_rights, &fp);
1418 if (error)
1419 return (error);
1420 if (fp->f_type == DTYPE_PIPE) {
1421 fdrop(fp, td);
1422 return (EINVAL);
1423 }
1424 fdrop(fp, td);
1425
1426 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1427
1428 case LINUX_F_DUPFD_CLOEXEC:
1429 return (kern_fcntl(td, args->fd, F_DUPFD_CLOEXEC, args->arg));
1430 /*
1431 * Our F_SEAL_* values match Linux one for maximum compatibility. So we
1432 * only needed to account for different values for fcntl(2) commands.
1433 */
1434 case LINUX_F_GET_SEALS:
1435 error = kern_fcntl(td, args->fd, F_GET_SEALS, 0);
1436 if (error != 0)
1437 return (error);
1438 td->td_retval[0] = bsd_to_linux_bits(td->td_retval[0],
1439 seal_bitmap, 0);
1440 return (0);
1441
1442 case LINUX_F_ADD_SEALS:
1443 return (kern_fcntl(td, args->fd, F_ADD_SEALS,
1444 linux_to_bsd_bits(args->arg, seal_bitmap, 0)));
1445
1446 case LINUX_F_GETPIPE_SZ:
1447 error = fget(td, args->fd,
1448 &cap_fcntl_rights, &fp);
1449 if (error != 0)
1450 return (error);
1451 if (fp->f_type != DTYPE_PIPE) {
1452 fdrop(fp, td);
1453 return (EINVAL);
1454 }
1455 fpipe = fp->f_data;
1456 td->td_retval[0] = fpipe->pipe_buffer.size;
1457 fdrop(fp, td);
1458 return (0);
1459
1460 default:
1461 linux_msg(td, "unsupported fcntl cmd %d", args->cmd);
1462 return (EINVAL);
1463 }
1464 }
1465
1466 int
linux_fcntl(struct thread * td,struct linux_fcntl_args * args)1467 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1468 {
1469
1470 return (fcntl_common(td, args));
1471 }
1472
1473 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1474 int
linux_fcntl64(struct thread * td,struct linux_fcntl64_args * args)1475 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1476 {
1477 struct l_flock64 linux_flock;
1478 struct flock bsd_flock;
1479 struct linux_fcntl_args fcntl_args;
1480 int error;
1481
1482 switch (args->cmd) {
1483 case LINUX_F_GETLK64:
1484 error = copyin((void *)args->arg, &linux_flock,
1485 sizeof(linux_flock));
1486 if (error)
1487 return (error);
1488 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1489 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1490 if (error)
1491 return (error);
1492 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1493 return (copyout(&linux_flock, (void *)args->arg,
1494 sizeof(linux_flock)));
1495
1496 case LINUX_F_SETLK64:
1497 error = copyin((void *)args->arg, &linux_flock,
1498 sizeof(linux_flock));
1499 if (error)
1500 return (error);
1501 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1502 return (kern_fcntl(td, args->fd, F_SETLK,
1503 (intptr_t)&bsd_flock));
1504
1505 case LINUX_F_SETLKW64:
1506 error = copyin((void *)args->arg, &linux_flock,
1507 sizeof(linux_flock));
1508 if (error)
1509 return (error);
1510 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1511 return (kern_fcntl(td, args->fd, F_SETLKW,
1512 (intptr_t)&bsd_flock));
1513 }
1514
1515 fcntl_args.fd = args->fd;
1516 fcntl_args.cmd = args->cmd;
1517 fcntl_args.arg = args->arg;
1518 return (fcntl_common(td, &fcntl_args));
1519 }
1520 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1521
1522 #ifdef LINUX_LEGACY_SYSCALLS
1523 int
linux_chown(struct thread * td,struct linux_chown_args * args)1524 linux_chown(struct thread *td, struct linux_chown_args *args)
1525 {
1526
1527 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE,
1528 args->uid, args->gid, 0));
1529 }
1530 #endif
1531
1532 int
linux_fchownat(struct thread * td,struct linux_fchownat_args * args)1533 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1534 {
1535 int dfd, flag, unsupported;
1536
1537 unsupported = args->flag & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH);
1538 if (unsupported != 0) {
1539 linux_msg(td, "fchownat unsupported flag 0x%x", unsupported);
1540 return (EINVAL);
1541 }
1542
1543 flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1544 AT_SYMLINK_NOFOLLOW;
1545 flag |= (args->flag & LINUX_AT_EMPTY_PATH) == 0 ? 0 :
1546 AT_EMPTY_PATH;
1547
1548 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
1549 return (kern_fchownat(td, dfd, args->filename, UIO_USERSPACE,
1550 args->uid, args->gid, flag));
1551 }
1552
1553 #ifdef LINUX_LEGACY_SYSCALLS
1554 int
linux_lchown(struct thread * td,struct linux_lchown_args * args)1555 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1556 {
1557
1558 return (kern_fchownat(td, AT_FDCWD, args->path, UIO_USERSPACE, args->uid,
1559 args->gid, AT_SYMLINK_NOFOLLOW));
1560 }
1561 #endif
1562
1563 static int
convert_fadvice(int advice)1564 convert_fadvice(int advice)
1565 {
1566 switch (advice) {
1567 case LINUX_POSIX_FADV_NORMAL:
1568 return (POSIX_FADV_NORMAL);
1569 case LINUX_POSIX_FADV_RANDOM:
1570 return (POSIX_FADV_RANDOM);
1571 case LINUX_POSIX_FADV_SEQUENTIAL:
1572 return (POSIX_FADV_SEQUENTIAL);
1573 case LINUX_POSIX_FADV_WILLNEED:
1574 return (POSIX_FADV_WILLNEED);
1575 case LINUX_POSIX_FADV_DONTNEED:
1576 return (POSIX_FADV_DONTNEED);
1577 case LINUX_POSIX_FADV_NOREUSE:
1578 return (POSIX_FADV_NOREUSE);
1579 default:
1580 return (-1);
1581 }
1582 }
1583
1584 int
linux_fadvise64(struct thread * td,struct linux_fadvise64_args * args)1585 linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1586 {
1587 off_t offset;
1588 int advice;
1589
1590 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1591 offset = PAIR32TO64(off_t, args->offset);
1592 #else
1593 offset = args->offset;
1594 #endif
1595
1596 advice = convert_fadvice(args->advice);
1597 if (advice == -1)
1598 return (EINVAL);
1599 return (kern_posix_fadvise(td, args->fd, offset, args->len, advice));
1600 }
1601
1602 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1603 int
linux_fadvise64_64(struct thread * td,struct linux_fadvise64_64_args * args)1604 linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1605 {
1606 off_t len, offset;
1607 int advice;
1608
1609 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1610 len = PAIR32TO64(off_t, args->len);
1611 offset = PAIR32TO64(off_t, args->offset);
1612 #else
1613 len = args->len;
1614 offset = args->offset;
1615 #endif
1616
1617 advice = convert_fadvice(args->advice);
1618 if (advice == -1)
1619 return (EINVAL);
1620 return (kern_posix_fadvise(td, args->fd, offset, len, advice));
1621 }
1622 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1623
1624 #ifdef LINUX_LEGACY_SYSCALLS
1625 int
linux_pipe(struct thread * td,struct linux_pipe_args * args)1626 linux_pipe(struct thread *td, struct linux_pipe_args *args)
1627 {
1628 int fildes[2];
1629 int error;
1630
1631 error = kern_pipe(td, fildes, 0, NULL, NULL);
1632 if (error != 0)
1633 return (error);
1634
1635 error = copyout(fildes, args->pipefds, sizeof(fildes));
1636 if (error != 0) {
1637 (void)kern_close(td, fildes[0]);
1638 (void)kern_close(td, fildes[1]);
1639 }
1640
1641 return (error);
1642 }
1643 #endif
1644
1645 int
linux_pipe2(struct thread * td,struct linux_pipe2_args * args)1646 linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1647 {
1648 int fildes[2];
1649 int error, flags;
1650
1651 if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1652 return (EINVAL);
1653
1654 flags = 0;
1655 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1656 flags |= O_NONBLOCK;
1657 if ((args->flags & LINUX_O_CLOEXEC) != 0)
1658 flags |= O_CLOEXEC;
1659 error = kern_pipe(td, fildes, flags, NULL, NULL);
1660 if (error != 0)
1661 return (error);
1662
1663 error = copyout(fildes, args->pipefds, sizeof(fildes));
1664 if (error != 0) {
1665 (void)kern_close(td, fildes[0]);
1666 (void)kern_close(td, fildes[1]);
1667 }
1668
1669 return (error);
1670 }
1671
1672 int
linux_dup3(struct thread * td,struct linux_dup3_args * args)1673 linux_dup3(struct thread *td, struct linux_dup3_args *args)
1674 {
1675 int cmd;
1676 intptr_t newfd;
1677
1678 if (args->oldfd == args->newfd)
1679 return (EINVAL);
1680 if ((args->flags & ~LINUX_O_CLOEXEC) != 0)
1681 return (EINVAL);
1682 if (args->flags & LINUX_O_CLOEXEC)
1683 cmd = F_DUP2FD_CLOEXEC;
1684 else
1685 cmd = F_DUP2FD;
1686
1687 newfd = args->newfd;
1688 return (kern_fcntl(td, args->oldfd, cmd, newfd));
1689 }
1690
1691 int
linux_fallocate(struct thread * td,struct linux_fallocate_args * args)1692 linux_fallocate(struct thread *td, struct linux_fallocate_args *args)
1693 {
1694 off_t len, offset;
1695
1696 /*
1697 * We emulate only posix_fallocate system call for which
1698 * mode should be 0.
1699 */
1700 if (args->mode != 0)
1701 return (EOPNOTSUPP);
1702
1703 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1704 len = PAIR32TO64(off_t, args->len);
1705 offset = PAIR32TO64(off_t, args->offset);
1706 #else
1707 len = args->len;
1708 offset = args->offset;
1709 #endif
1710
1711 return (kern_posix_fallocate(td, args->fd, offset, len));
1712 }
1713
1714 int
linux_copy_file_range(struct thread * td,struct linux_copy_file_range_args * args)1715 linux_copy_file_range(struct thread *td, struct linux_copy_file_range_args
1716 *args)
1717 {
1718 l_loff_t inoff, outoff, *inoffp, *outoffp;
1719 int error, flags;
1720
1721 /*
1722 * copy_file_range(2) on Linux doesn't define any flags (yet), so is
1723 * the native implementation. Enforce it.
1724 */
1725 if (args->flags != 0) {
1726 linux_msg(td, "copy_file_range unsupported flags 0x%x",
1727 args->flags);
1728 return (EINVAL);
1729 }
1730 flags = 0;
1731 inoffp = outoffp = NULL;
1732 if (args->off_in != NULL) {
1733 error = copyin(args->off_in, &inoff, sizeof(l_loff_t));
1734 if (error != 0)
1735 return (error);
1736 inoffp = &inoff;
1737 }
1738 if (args->off_out != NULL) {
1739 error = copyin(args->off_out, &outoff, sizeof(l_loff_t));
1740 if (error != 0)
1741 return (error);
1742 outoffp = &outoff;
1743 }
1744
1745 error = kern_copy_file_range(td, args->fd_in, inoffp, args->fd_out,
1746 outoffp, args->len, flags);
1747 if (error == 0 && args->off_in != NULL)
1748 error = copyout(inoffp, args->off_in, sizeof(l_loff_t));
1749 if (error == 0 && args->off_out != NULL)
1750 error = copyout(outoffp, args->off_out, sizeof(l_loff_t));
1751 return (error);
1752 }
1753
1754 #define LINUX_MEMFD_PREFIX "memfd:"
1755
1756 int
linux_memfd_create(struct thread * td,struct linux_memfd_create_args * args)1757 linux_memfd_create(struct thread *td, struct linux_memfd_create_args *args)
1758 {
1759 char memfd_name[LINUX_NAME_MAX + 1];
1760 int error, flags, shmflags, oflags;
1761
1762 /*
1763 * This is our clever trick to avoid the heap allocation to copy in the
1764 * uname. We don't really need to go this far out of our way, but it
1765 * does keep the rest of this function fairly clean as they don't have
1766 * to worry about cleanup on the way out.
1767 */
1768 error = copyinstr(args->uname_ptr,
1769 memfd_name + sizeof(LINUX_MEMFD_PREFIX) - 1,
1770 LINUX_NAME_MAX - sizeof(LINUX_MEMFD_PREFIX) - 1, NULL);
1771 if (error != 0) {
1772 if (error == ENAMETOOLONG)
1773 error = EINVAL;
1774 return (error);
1775 }
1776
1777 memcpy(memfd_name, LINUX_MEMFD_PREFIX, sizeof(LINUX_MEMFD_PREFIX) - 1);
1778 flags = linux_to_bsd_bits(args->flags, mfd_bitmap, 0);
1779 if ((flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB |
1780 MFD_HUGE_MASK)) != 0)
1781 return (EINVAL);
1782 /* Size specified but no HUGETLB. */
1783 if ((flags & MFD_HUGE_MASK) != 0 && (flags & MFD_HUGETLB) == 0)
1784 return (EINVAL);
1785 /* We don't actually support HUGETLB. */
1786 if ((flags & MFD_HUGETLB) != 0)
1787 return (ENOSYS);
1788 oflags = O_RDWR;
1789 shmflags = SHM_GROW_ON_WRITE;
1790 if ((flags & MFD_CLOEXEC) != 0)
1791 oflags |= O_CLOEXEC;
1792 if ((flags & MFD_ALLOW_SEALING) != 0)
1793 shmflags |= SHM_ALLOW_SEALING;
1794 return (kern_shm_open2(td, SHM_ANON, oflags, 0, shmflags, NULL,
1795 memfd_name));
1796 }
1797
1798 int
linux_splice(struct thread * td,struct linux_splice_args * args)1799 linux_splice(struct thread *td, struct linux_splice_args *args)
1800 {
1801
1802 linux_msg(td, "syscall splice not really implemented");
1803
1804 /*
1805 * splice(2) is documented to return EINVAL in various circumstances;
1806 * returning it instead of ENOSYS should hint the caller to use fallback
1807 * instead.
1808 */
1809 return (EINVAL);
1810 }
1811
1812 int
linux_close_range(struct thread * td,struct linux_close_range_args * args)1813 linux_close_range(struct thread *td, struct linux_close_range_args *args)
1814 {
1815 u_int flags = 0;
1816
1817 /*
1818 * Implementing close_range(CLOSE_RANGE_UNSHARE) allows Linux to
1819 * unshare filedesc table of the calling thread from others threads
1820 * in a thread group (i.e., process in the FreeBSD) or others processes,
1821 * which shares the same table, before closing the files. FreeBSD does
1822 * not have compatible unsharing mechanism due to the fact that sharing
1823 * process resources, including filedesc table, is at thread level in the
1824 * Linux, while in the FreeBSD it is at the process level.
1825 * Return EINVAL for now if the CLOSE_RANGE_UNSHARE flag is specified
1826 * until this new Linux API stabilizes.
1827 */
1828
1829 if ((args->flags & ~(LINUX_CLOSE_RANGE_CLOEXEC)) != 0)
1830 return (EINVAL);
1831 if (args->first > args->last)
1832 return (EINVAL);
1833 if ((args->flags & LINUX_CLOSE_RANGE_CLOEXEC) != 0)
1834 flags |= CLOSE_RANGE_CLOEXEC;
1835 return (kern_close_range(td, flags, args->first, args->last));
1836 }
1837
1838 int
linux_enobufs2eagain(struct thread * td,int fd,int error)1839 linux_enobufs2eagain(struct thread *td, int fd, int error)
1840 {
1841 struct file *fp;
1842
1843 if (error != ENOBUFS)
1844 return (error);
1845 if (fget(td, fd, &cap_no_rights, &fp) != 0)
1846 return (error);
1847 if (fp->f_type == DTYPE_SOCKET && (fp->f_flag & FNONBLOCK) != 0)
1848 error = EAGAIN;
1849 fdrop(fp, td);
1850 return (error);
1851 }
1852
1853 int
linux_write(struct thread * td,struct linux_write_args * args)1854 linux_write(struct thread *td, struct linux_write_args *args)
1855 {
1856 struct write_args bargs = {
1857 .fd = args->fd,
1858 .buf = args->buf,
1859 .nbyte = args->nbyte,
1860 };
1861
1862 return (linux_enobufs2eagain(td, args->fd, sys_write(td, &bargs)));
1863 }
1864
1865 int
linux_writev(struct thread * td,struct linux_writev_args * args)1866 linux_writev(struct thread *td, struct linux_writev_args *args)
1867 {
1868 struct uio *auio;
1869 int error;
1870
1871 #ifdef COMPAT_LINUX32
1872 error = freebsd32_copyinuio(PTRIN(args->iovp), args->iovcnt, &auio);
1873 #else
1874 error = copyinuio(args->iovp, args->iovcnt, &auio);
1875 #endif
1876 if (error != 0)
1877 return (error);
1878 error = kern_writev(td, args->fd, auio);
1879 freeuio(auio);
1880 return (linux_enobufs2eagain(td, args->fd, error));
1881 }
1882
1883 static int
linux_inotify_init_flags(int l_flags)1884 linux_inotify_init_flags(int l_flags)
1885 {
1886 int bsd_flags;
1887
1888 if ((l_flags & ~(LINUX_IN_CLOEXEC | LINUX_IN_NONBLOCK)) != 0)
1889 linux_msg(NULL, "inotify_init1 unsupported flags 0x%x",
1890 l_flags);
1891
1892 bsd_flags = 0;
1893 if ((l_flags & LINUX_IN_CLOEXEC) != 0)
1894 bsd_flags |= O_CLOEXEC;
1895 if ((l_flags & LINUX_IN_NONBLOCK) != 0)
1896 bsd_flags |= O_NONBLOCK;
1897 return (bsd_flags);
1898 }
1899
1900 static int
inotify_init_common(struct thread * td,int flags)1901 inotify_init_common(struct thread *td, int flags)
1902 {
1903 struct specialfd_inotify si;
1904
1905 si.flags = linux_inotify_init_flags(flags);
1906 return (kern_specialfd(td, SPECIALFD_INOTIFY, &si));
1907 }
1908
1909 #if defined(__i386__) || defined(__amd64__)
1910 int
linux_inotify_init(struct thread * td,struct linux_inotify_init_args * args)1911 linux_inotify_init(struct thread *td, struct linux_inotify_init_args *args)
1912 {
1913 return (inotify_init_common(td, 0));
1914 }
1915 #endif
1916
1917 int
linux_inotify_init1(struct thread * td,struct linux_inotify_init1_args * args)1918 linux_inotify_init1(struct thread *td, struct linux_inotify_init1_args *args)
1919 {
1920 return (inotify_init_common(td, args->flags));
1921 }
1922
1923 /*
1924 * The native implementation uses the same values for inotify events as
1925 * libinotify, which gives us binary compatibility with Linux. This simplifies
1926 * the shim implementation a lot, as otherwise we would have to handle read(2)
1927 * calls on inotify descriptors and translate events to Linux's ABI.
1928 */
1929 _Static_assert(LINUX_IN_ACCESS == IN_ACCESS,
1930 "IN_ACCESS mismatch");
1931 _Static_assert(LINUX_IN_MODIFY == IN_MODIFY,
1932 "IN_MODIFY mismatch");
1933 _Static_assert(LINUX_IN_ATTRIB == IN_ATTRIB,
1934 "IN_ATTRIB mismatch");
1935 _Static_assert(LINUX_IN_CLOSE_WRITE == IN_CLOSE_WRITE,
1936 "IN_CLOSE_WRITE mismatch");
1937 _Static_assert(LINUX_IN_CLOSE_NOWRITE == IN_CLOSE_NOWRITE,
1938 "IN_CLOSE_NOWRITE mismatch");
1939 _Static_assert(LINUX_IN_OPEN == IN_OPEN,
1940 "IN_OPEN mismatch");
1941 _Static_assert(LINUX_IN_MOVED_FROM == IN_MOVED_FROM,
1942 "IN_MOVED_FROM mismatch");
1943 _Static_assert(LINUX_IN_MOVED_TO == IN_MOVED_TO,
1944 "IN_MOVED_TO mismatch");
1945 _Static_assert(LINUX_IN_CREATE == IN_CREATE,
1946 "IN_CREATE mismatch");
1947 _Static_assert(LINUX_IN_DELETE == IN_DELETE,
1948 "IN_DELETE mismatch");
1949 _Static_assert(LINUX_IN_DELETE_SELF == IN_DELETE_SELF,
1950 "IN_DELETE_SELF mismatch");
1951 _Static_assert(LINUX_IN_MOVE_SELF == IN_MOVE_SELF,
1952 "IN_MOVE_SELF mismatch");
1953
1954 _Static_assert(LINUX_IN_UNMOUNT == IN_UNMOUNT,
1955 "IN_UNMOUNT mismatch");
1956 _Static_assert(LINUX_IN_Q_OVERFLOW == IN_Q_OVERFLOW,
1957 "IN_Q_OVERFLOW mismatch");
1958 _Static_assert(LINUX_IN_IGNORED == IN_IGNORED,
1959 "IN_IGNORED mismatch");
1960
1961 _Static_assert(LINUX_IN_ISDIR == IN_ISDIR,
1962 "IN_ISDIR mismatch");
1963 _Static_assert(LINUX_IN_ONLYDIR == IN_ONLYDIR,
1964 "IN_ONLYDIR mismatch");
1965 _Static_assert(LINUX_IN_DONT_FOLLOW == IN_DONT_FOLLOW,
1966 "IN_DONT_FOLLOW mismatch");
1967 _Static_assert(LINUX_IN_MASK_CREATE == IN_MASK_CREATE,
1968 "IN_MASK_CREATE mismatch");
1969 _Static_assert(LINUX_IN_MASK_ADD == IN_MASK_ADD,
1970 "IN_MASK_ADD mismatch");
1971 _Static_assert(LINUX_IN_ONESHOT == IN_ONESHOT,
1972 "IN_ONESHOT mismatch");
1973 _Static_assert(LINUX_IN_EXCL_UNLINK == IN_EXCL_UNLINK,
1974 "IN_EXCL_UNLINK mismatch");
1975
1976 static int
linux_inotify_watch_flags(int l_flags)1977 linux_inotify_watch_flags(int l_flags)
1978 {
1979 if ((l_flags & ~(LINUX_IN_ALL_EVENTS | LINUX_IN_ALL_FLAGS)) != 0) {
1980 linux_msg(NULL, "inotify_add_watch unsupported flags 0x%x",
1981 l_flags);
1982 }
1983
1984 return (l_flags);
1985 }
1986
1987 int
linux_inotify_add_watch(struct thread * td,struct linux_inotify_add_watch_args * args)1988 linux_inotify_add_watch(struct thread *td,
1989 struct linux_inotify_add_watch_args *args)
1990 {
1991 return (kern_inotify_add_watch(args->fd, AT_FDCWD, args->pathname,
1992 linux_inotify_watch_flags(args->mask), td));
1993 }
1994
1995 int
linux_inotify_rm_watch(struct thread * td,struct linux_inotify_rm_watch_args * args)1996 linux_inotify_rm_watch(struct thread *td,
1997 struct linux_inotify_rm_watch_args *args)
1998 {
1999 return (kern_inotify_rm_watch(args->fd, args->wd, td));
2000 }
2001