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