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