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