linux_file.c revision 127059
19313Ssos/*-
29313Ssos * Copyright (c) 1994-1995 S�ren Schmidt
39313Ssos * All rights reserved.
49313Ssos *
59313Ssos * Redistribution and use in source and binary forms, with or without
69313Ssos * modification, are permitted provided that the following conditions
79313Ssos * are met:
89313Ssos * 1. Redistributions of source code must retain the above copyright
9111798Sdes *    notice, this list of conditions and the following disclaimer
109313Ssos *    in this position and unchanged.
119313Ssos * 2. Redistributions in binary form must reproduce the above copyright
129313Ssos *    notice, this list of conditions and the following disclaimer in the
139313Ssos *    documentation and/or other materials provided with the distribution.
149313Ssos * 3. The name of the author may not be used to endorse or promote products
1597748Sschweikh *    derived from this software without specific prior written permission
169313Ssos *
179313Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189313Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199313Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209313Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219313Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229313Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239313Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249313Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259313Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269313Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279313Ssos */
289313Ssos
29116173Sobrien#include <sys/cdefs.h>
30116173Sobrien__FBSDID("$FreeBSD: head/sys/compat/linux/linux_file.c 127059 2004-03-16 09:05:56Z tjr $");
31116173Sobrien
3231784Seivind#include "opt_compat.h"
33101189Srwatson#include "opt_mac.h"
3431784Seivind
359313Ssos#include <sys/param.h>
369313Ssos#include <sys/systm.h>
3776166Smarkm#include <sys/conf.h>
3876166Smarkm#include <sys/dirent.h>
399313Ssos#include <sys/fcntl.h>
409313Ssos#include <sys/file.h>
419313Ssos#include <sys/filedesc.h>
4231561Sbde#include <sys/lock.h>
43101189Srwatson#include <sys/mac.h>
449313Ssos#include <sys/malloc.h>
4572538Sjlemon#include <sys/mount.h>
4676166Smarkm#include <sys/mutex.h>
4776166Smarkm#include <sys/proc.h>
48102814Siedowse#include <sys/syscallsubr.h>
4976166Smarkm#include <sys/sysproto.h>
5014331Speter#include <sys/tty.h>
5176166Smarkm#include <sys/vnode.h>
5212458Sbde
5372538Sjlemon#include <ufs/ufs/extattr.h>
5472538Sjlemon#include <ufs/ufs/quota.h>
5572538Sjlemon#include <ufs/ufs/ufsmount.h>
5672538Sjlemon
5764905Smarcel#include <machine/../linux/linux.h>
5868583Smarcel#include <machine/../linux/linux_proto.h>
5964905Smarcel#include <compat/linux/linux_util.h>
609313Ssos
6168201Sobrien#ifndef __alpha__
629313Ssosint
6383366Sjulianlinux_creat(struct thread *td, struct linux_creat_args *args)
649313Ssos{
65102814Siedowse    char *path;
66102814Siedowse    int error;
679313Ssos
68102814Siedowse    LCONVPATHEXIST(td, args->path, &path);
6914331Speter
709313Ssos#ifdef DEBUG
7172543Sjlemon	if (ldebug(creat))
72102814Siedowse		printf(ARGS(creat, "%s, %d"), path, args->mode);
739313Ssos#endif
74102814Siedowse    error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
75102814Siedowse	args->mode);
76102814Siedowse    LFREEPATH(path);
77102814Siedowse    return (error);
789313Ssos}
7968201Sobrien#endif /*!__alpha__*/
809313Ssos
819313Ssosint
8283366Sjulianlinux_open(struct thread *td, struct linux_open_args *args)
839313Ssos{
8483382Sjhb    struct proc *p = td->td_proc;
85102814Siedowse    char *path;
86102814Siedowse    int bsd_flags, error;
8714331Speter
8814331Speter    if (args->flags & LINUX_O_CREAT)
89102814Siedowse	LCONVPATHCREAT(td, args->path, &path);
9014331Speter    else
91102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
9214331Speter
939313Ssos#ifdef DEBUG
9472543Sjlemon	if (ldebug(open))
9572543Sjlemon		printf(ARGS(open, "%s, 0x%x, 0x%x"),
96102814Siedowse		    path, args->flags, args->mode);
979313Ssos#endif
98102814Siedowse    bsd_flags = 0;
999313Ssos    if (args->flags & LINUX_O_RDONLY)
100102814Siedowse	bsd_flags |= O_RDONLY;
101111798Sdes    if (args->flags & LINUX_O_WRONLY)
102102814Siedowse	bsd_flags |= O_WRONLY;
1039313Ssos    if (args->flags & LINUX_O_RDWR)
104102814Siedowse	bsd_flags |= O_RDWR;
1059313Ssos    if (args->flags & LINUX_O_NDELAY)
106102814Siedowse	bsd_flags |= O_NONBLOCK;
1079313Ssos    if (args->flags & LINUX_O_APPEND)
108102814Siedowse	bsd_flags |= O_APPEND;
1099313Ssos    if (args->flags & LINUX_O_SYNC)
110102814Siedowse	bsd_flags |= O_FSYNC;
1119313Ssos    if (args->flags & LINUX_O_NONBLOCK)
112102814Siedowse	bsd_flags |= O_NONBLOCK;
1139313Ssos    if (args->flags & LINUX_FASYNC)
114102814Siedowse	bsd_flags |= O_ASYNC;
1159313Ssos    if (args->flags & LINUX_O_CREAT)
116102814Siedowse	bsd_flags |= O_CREAT;
1179313Ssos    if (args->flags & LINUX_O_TRUNC)
118102814Siedowse	bsd_flags |= O_TRUNC;
1199313Ssos    if (args->flags & LINUX_O_EXCL)
120102814Siedowse	bsd_flags |= O_EXCL;
1219313Ssos    if (args->flags & LINUX_O_NOCTTY)
122102814Siedowse	bsd_flags |= O_NOCTTY;
1239313Ssos
124102814Siedowse    error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
12570061Sjhb    PROC_LOCK(p);
126102814Siedowse    if (!error && !(bsd_flags & O_NOCTTY) &&
1279313Ssos	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
12889306Salfred	struct file *fp;
1299313Ssos
130113917Sjhb	PROC_UNLOCK(p);
13189319Salfred	error = fget(td, td->td_retval[0], &fp);
13289319Salfred	if (!error) {
13389319Salfred		if (fp->f_type == DTYPE_VNODE)
134102003Srwatson			fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
135102003Srwatson			    td);
13689319Salfred	    fdrop(fp, td);
13789319Salfred	}
13891140Stanimura    } else {
13970061Sjhb	PROC_UNLOCK(p);
14014331Speter#ifdef DEBUG
14172543Sjlemon	if (ldebug(open))
14272543Sjlemon		printf(LMSG("open returns error %d"), error);
14314331Speter#endif
14491140Stanimura    }
145102814Siedowse    LFREEPATH(path);
1469313Ssos    return error;
1479313Ssos}
1489313Ssos
1499313Ssosint
15083366Sjulianlinux_lseek(struct thread *td, struct linux_lseek_args *args)
1519313Ssos{
1529313Ssos
15312858Speter    struct lseek_args /* {
15412858Speter	int fd;
1559313Ssos	int pad;
15612858Speter	off_t offset;
1579313Ssos	int whence;
15812858Speter    } */ tmp_args;
1599313Ssos    int error;
1609313Ssos
1619313Ssos#ifdef DEBUG
16272543Sjlemon	if (ldebug(lseek))
16372543Sjlemon		printf(ARGS(lseek, "%d, %ld, %d"),
16483221Smarcel		    args->fdes, (long)args->off, args->whence);
1659313Ssos#endif
16612858Speter    tmp_args.fd = args->fdes;
16712858Speter    tmp_args.offset = (off_t)args->off;
1689313Ssos    tmp_args.whence = args->whence;
16983366Sjulian    error = lseek(td, &tmp_args);
1709313Ssos    return error;
1719313Ssos}
1729313Ssos
17368201Sobrien#ifndef __alpha__
17414331Speterint
17583366Sjulianlinux_llseek(struct thread *td, struct linux_llseek_args *args)
17614331Speter{
17714331Speter	struct lseek_args bsd_args;
17814331Speter	int error;
17914331Speter	off_t off;
18014331Speter
18114331Speter#ifdef DEBUG
18272543Sjlemon	if (ldebug(llseek))
18372543Sjlemon		printf(ARGS(llseek, "%d, %d:%d, %d"),
18472543Sjlemon		    args->fd, args->ohigh, args->olow, args->whence);
18514331Speter#endif
18614331Speter	off = (args->olow) | (((off_t) args->ohigh) << 32);
18714331Speter
18814331Speter	bsd_args.fd = args->fd;
18914331Speter	bsd_args.offset = off;
19014331Speter	bsd_args.whence = args->whence;
19114331Speter
19283366Sjulian	if ((error = lseek(td, &bsd_args)))
19314331Speter		return error;
19414331Speter
195111797Sdes	if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
19614331Speter		return error;
19714331Speter
19883366Sjulian	td->td_retval[0] = 0;
19914331Speter	return 0;
20014331Speter}
20168201Sobrien#endif /*!__alpha__*/
20214331Speter
20368201Sobrien#ifndef __alpha__
2049313Ssosint
20583366Sjulianlinux_readdir(struct thread *td, struct linux_readdir_args *args)
2069313Ssos{
20714331Speter	struct linux_getdents_args lda;
20814331Speter
20914331Speter	lda.fd = args->fd;
21014331Speter	lda.dent = args->dent;
21114331Speter	lda.count = 1;
21283366Sjulian	return linux_getdents(td, &lda);
21314331Speter}
21468201Sobrien#endif /*!__alpha__*/
21514331Speter
21683221Smarcel/*
21783221Smarcel * Note that linux_getdents(2) and linux_getdents64(2) have the same
21883221Smarcel * arguments. They only differ in the definition of struct dirent they
21983221Smarcel * operate on. We use this to common the code, with the exception of
22083221Smarcel * accessing struct dirent. Note that linux_readdir(2) is implemented
22183221Smarcel * by means of linux_getdents(2). In this case we never operate on
22283221Smarcel * struct dirent64 and thus don't need to handle it...
22383221Smarcel */
22483221Smarcel
22583221Smarcelstruct l_dirent {
22683221Smarcel	l_long		d_ino;
22783221Smarcel	l_off_t		d_off;
22883221Smarcel	l_ushort	d_reclen;
22983221Smarcel	char		d_name[LINUX_NAME_MAX + 1];
23083221Smarcel};
23183221Smarcel
23283221Smarcelstruct l_dirent64 {
23383221Smarcel	uint64_t	d_ino;
23483221Smarcel	int64_t		d_off;
23583221Smarcel	l_ushort	d_reclen;
23683221Smarcel	u_char		d_type;
23783221Smarcel	char		d_name[LINUX_NAME_MAX + 1];
23883221Smarcel};
23983221Smarcel
24083221Smarcel#define LINUX_RECLEN(de,namlen) \
24183221Smarcel    ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
24283221Smarcel
24383221Smarcel#define	LINUX_DIRBLKSIZ		512
24483221Smarcel
24583221Smarcelstatic int
24683366Sjuliangetdents_common(struct thread *td, struct linux_getdents64_args *args,
24783221Smarcel    int is64bit)
24814331Speter{
249111798Sdes	struct dirent *bdp;
25083221Smarcel	struct vnode *vp;
25183221Smarcel	caddr_t inp, buf;		/* BSD-format */
25283221Smarcel	int len, reclen;		/* BSD-format */
25383221Smarcel	caddr_t outp;			/* Linux-format */
25483221Smarcel	int resid, linuxreclen=0;	/* Linux-format */
25583221Smarcel	struct file *fp;
25683221Smarcel	struct uio auio;
25783221Smarcel	struct iovec aiov;
25883221Smarcel	off_t off;
25983221Smarcel	struct l_dirent linux_dirent;
26083221Smarcel	struct l_dirent64 linux_dirent64;
26183221Smarcel	int buflen, error, eofflag, nbytes, justone;
26283221Smarcel	u_long *cookies = NULL, *cookiep;
26383221Smarcel	int ncookies;
2649313Ssos
26583366Sjulian	if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
26683221Smarcel		return (error);
2679313Ssos
26889306Salfred	if ((fp->f_flag & FREAD) == 0) {
26989306Salfred		fdrop(fp, td);
27083221Smarcel		return (EBADF);
27189306Salfred	}
2729313Ssos
273116678Sphk	vp = fp->f_vnode;
27489306Salfred	if (vp->v_type != VDIR) {
27589306Salfred		fdrop(fp, td);
27683221Smarcel		return (EINVAL);
27789306Salfred	}
2789313Ssos
27983221Smarcel	nbytes = args->count;
28083221Smarcel	if (nbytes == 1) {
28183221Smarcel		/* readdir(2) case. Always struct dirent. */
28289306Salfred		if (is64bit) {
28389306Salfred			fdrop(fp, td);
28483221Smarcel			return (EINVAL);
28589306Salfred		}
28683221Smarcel		nbytes = sizeof(linux_dirent);
28783221Smarcel		justone = 1;
28883221Smarcel	} else
28983221Smarcel		justone = 0;
2909313Ssos
29183221Smarcel	off = fp->f_offset;
2929313Ssos
29383221Smarcel	buflen = max(LINUX_DIRBLKSIZ, nbytes);
29483221Smarcel	buflen = min(buflen, MAXBSIZE);
295111119Simp	buf = malloc(buflen, M_TEMP, M_WAITOK);
29683366Sjulian	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
29783221Smarcel
2989313Ssosagain:
29983221Smarcel	aiov.iov_base = buf;
30083221Smarcel	aiov.iov_len = buflen;
30183221Smarcel	auio.uio_iov = &aiov;
30283221Smarcel	auio.uio_iovcnt = 1;
30383221Smarcel	auio.uio_rw = UIO_READ;
30483221Smarcel	auio.uio_segflg = UIO_SYSSPACE;
30583366Sjulian	auio.uio_td = td;
30683221Smarcel	auio.uio_resid = buflen;
30783221Smarcel	auio.uio_offset = off;
3089313Ssos
30983221Smarcel	if (cookies) {
31083221Smarcel		free(cookies, M_TEMP);
31183221Smarcel		cookies = NULL;
31283221Smarcel	}
31324654Sdfr
314101189Srwatson#ifdef MAC
315101189Srwatson	/*
316101189Srwatson	 * Do directory search MAC check using non-cached credentials.
317101189Srwatson	 */
318112451Sjhb	if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
319101189Srwatson		goto out;
320101189Srwatson#endif /* MAC */
32183221Smarcel	if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
32283221Smarcel		 &cookies)))
32383221Smarcel		goto out;
3249313Ssos
32583221Smarcel	inp = buf;
32683221Smarcel	outp = (caddr_t)args->dirent;
32783221Smarcel	resid = nbytes;
32883221Smarcel	if ((len = buflen - auio.uio_resid) <= 0)
32983221Smarcel		goto eof;
3309313Ssos
33183221Smarcel	cookiep = cookies;
33224654Sdfr
33383221Smarcel	if (cookies) {
33483221Smarcel		/*
33583221Smarcel		 * When using cookies, the vfs has the option of reading from
33683221Smarcel		 * a different offset than that supplied (UFS truncates the
33783221Smarcel		 * offset to a block boundary to make sure that it never reads
33883221Smarcel		 * partway through a directory entry, even if the directory
33983221Smarcel		 * has been compacted).
34083221Smarcel		 */
34183221Smarcel		while (len > 0 && ncookies > 0 && *cookiep <= off) {
34283221Smarcel			bdp = (struct dirent *) inp;
34383221Smarcel			len -= bdp->d_reclen;
34483221Smarcel			inp += bdp->d_reclen;
34583221Smarcel			cookiep++;
34683221Smarcel			ncookies--;
34783221Smarcel		}
34824654Sdfr	}
34924654Sdfr
35083221Smarcel	while (len > 0) {
35183221Smarcel		if (cookiep && ncookies == 0)
35283221Smarcel			break;
35383221Smarcel		bdp = (struct dirent *) inp;
35483221Smarcel		reclen = bdp->d_reclen;
35583221Smarcel		if (reclen & 3) {
35683221Smarcel			error = EFAULT;
35783221Smarcel			goto out;
35883221Smarcel		}
35983221Smarcel
36083221Smarcel		if (bdp->d_fileno == 0) {
36183221Smarcel			inp += reclen;
36283221Smarcel			if (cookiep) {
36383221Smarcel				off = *cookiep++;
36483221Smarcel				ncookies--;
36583221Smarcel			} else
36683221Smarcel				off += reclen;
36783221Smarcel
36883221Smarcel			len -= reclen;
36983221Smarcel			continue;
37083221Smarcel		}
37183221Smarcel
37283221Smarcel		linuxreclen = (is64bit)
37383221Smarcel		    ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
37483221Smarcel		    : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
37583221Smarcel
37683221Smarcel		if (reclen > len || resid < linuxreclen) {
37783221Smarcel			outp++;
37883221Smarcel			break;
37983221Smarcel		}
38083221Smarcel
38183221Smarcel		if (justone) {
38283221Smarcel			/* readdir(2) case. */
38383221Smarcel			linux_dirent.d_ino = (l_long)bdp->d_fileno;
38483221Smarcel			linux_dirent.d_off = (l_off_t)linuxreclen;
38583221Smarcel			linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
38683221Smarcel			strcpy(linux_dirent.d_name, bdp->d_name);
38783221Smarcel			error = copyout(&linux_dirent, outp, linuxreclen);
38883221Smarcel		} else {
38983221Smarcel			if (is64bit) {
39083221Smarcel				linux_dirent64.d_ino = bdp->d_fileno;
39183221Smarcel				linux_dirent64.d_off = (cookiep)
39283221Smarcel				    ? (l_off_t)*cookiep
39383221Smarcel				    : (l_off_t)(off + reclen);
39483221Smarcel				linux_dirent64.d_reclen =
39583221Smarcel				    (l_ushort)linuxreclen;
39683221Smarcel				linux_dirent64.d_type = bdp->d_type;
39783221Smarcel				strcpy(linux_dirent64.d_name, bdp->d_name);
39883221Smarcel				error = copyout(&linux_dirent64, outp,
39983221Smarcel				    linuxreclen);
40083221Smarcel			} else {
40183221Smarcel				linux_dirent.d_ino = bdp->d_fileno;
40283221Smarcel				linux_dirent.d_off = (cookiep)
40383221Smarcel				    ? (l_off_t)*cookiep
40483221Smarcel				    : (l_off_t)(off + reclen);
40583221Smarcel				linux_dirent.d_reclen = (l_ushort)linuxreclen;
40683221Smarcel				strcpy(linux_dirent.d_name, bdp->d_name);
40783221Smarcel				error = copyout(&linux_dirent, outp,
40883221Smarcel				    linuxreclen);
40983221Smarcel			}
41083221Smarcel		}
41183221Smarcel		if (error)
41283221Smarcel			goto out;
41383221Smarcel
41483221Smarcel		inp += reclen;
41583221Smarcel		if (cookiep) {
41683221Smarcel			off = *cookiep++;
41783221Smarcel			ncookies--;
41883221Smarcel		} else
41983221Smarcel			off += reclen;
42083221Smarcel
42183221Smarcel		outp += linuxreclen;
42283221Smarcel		resid -= linuxreclen;
42383221Smarcel		len -= reclen;
42483221Smarcel		if (justone)
42583221Smarcel			break;
42610355Sswallace	}
4279313Ssos
42883221Smarcel	if (outp == (caddr_t)args->dirent)
42983221Smarcel		goto again;
4309313Ssos
43183221Smarcel	fp->f_offset = off;
43283221Smarcel	if (justone)
43383221Smarcel		nbytes = resid + linuxreclen;
43410355Sswallace
4359313Ssoseof:
43683366Sjulian	td->td_retval[0] = nbytes - resid;
43783221Smarcel
4389313Ssosout:
43983221Smarcel	if (cookies)
44083221Smarcel		free(cookies, M_TEMP);
44183221Smarcel
44283366Sjulian	VOP_UNLOCK(vp, 0, td);
44389306Salfred	fdrop(fp, td);
44483221Smarcel	free(buf, M_TEMP);
44583221Smarcel	return (error);
4469313Ssos}
44714331Speter
44883221Smarcelint
44983366Sjulianlinux_getdents(struct thread *td, struct linux_getdents_args *args)
45083221Smarcel{
45183221Smarcel
45283221Smarcel#ifdef DEBUG
45383221Smarcel	if (ldebug(getdents))
45483221Smarcel		printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
45583221Smarcel#endif
45683221Smarcel
45783366Sjulian	return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
45883221Smarcel}
45983221Smarcel
46083221Smarcelint
46183366Sjulianlinux_getdents64(struct thread *td, struct linux_getdents64_args *args)
46283221Smarcel{
46383221Smarcel
46483221Smarcel#ifdef DEBUG
46583221Smarcel	if (ldebug(getdents64))
46683221Smarcel		printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
46783221Smarcel#endif
46883221Smarcel
46983366Sjulian	return (getdents_common(td, args, 1));
47083221Smarcel}
47183221Smarcel
47214331Speter/*
47314331Speter * These exist mainly for hooks for doing /compat/linux translation.
47414331Speter */
47514331Speter
47614331Speterint
47783366Sjulianlinux_access(struct thread *td, struct linux_access_args *args)
47814331Speter{
479102814Siedowse	char *path;
480102814Siedowse	int error;
48114331Speter
482102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
48314331Speter
48414331Speter#ifdef DEBUG
48572543Sjlemon	if (ldebug(access))
486102814Siedowse		printf(ARGS(access, "%s, %d"), path, args->flags);
48714331Speter#endif
488102814Siedowse	error = kern_access(td, path, UIO_SYSSPACE, args->flags);
489102814Siedowse	LFREEPATH(path);
490102814Siedowse	return (error);
49114331Speter}
49214331Speter
49314331Speterint
49483366Sjulianlinux_unlink(struct thread *td, struct linux_unlink_args *args)
49514331Speter{
496102814Siedowse	char *path;
497102814Siedowse	int error;
49814331Speter
499102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
50014331Speter
50114331Speter#ifdef DEBUG
50272543Sjlemon	if (ldebug(unlink))
503102814Siedowse		printf(ARGS(unlink, "%s"), path);
50414331Speter#endif
50514331Speter
506102814Siedowse	error = kern_unlink(td, path, UIO_SYSSPACE);
507102814Siedowse	LFREEPATH(path);
508102814Siedowse	return (error);
50914331Speter}
51014331Speter
51114331Speterint
51283366Sjulianlinux_chdir(struct thread *td, struct linux_chdir_args *args)
51314331Speter{
514102814Siedowse	char *path;
515102814Siedowse	int error;
51614331Speter
517102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
51814331Speter
51914331Speter#ifdef DEBUG
52072543Sjlemon	if (ldebug(chdir))
521102814Siedowse		printf(ARGS(chdir, "%s"), path);
52214331Speter#endif
523102814Siedowse	error = kern_chdir(td, path, UIO_SYSSPACE);
524102814Siedowse	LFREEPATH(path);
525102814Siedowse	return (error);
52614331Speter}
52714331Speter
52814331Speterint
52983366Sjulianlinux_chmod(struct thread *td, struct linux_chmod_args *args)
53014331Speter{
531102814Siedowse	char *path;
532102814Siedowse	int error;
53314331Speter
534102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
53514331Speter
53614331Speter#ifdef DEBUG
53772543Sjlemon	if (ldebug(chmod))
538102814Siedowse		printf(ARGS(chmod, "%s, %d"), path, args->mode);
53914331Speter#endif
540102814Siedowse	error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
541102814Siedowse	LFREEPATH(path);
542102814Siedowse	return (error);
54314331Speter}
54414331Speter
54514331Speterint
54683366Sjulianlinux_mkdir(struct thread *td, struct linux_mkdir_args *args)
54714331Speter{
548102814Siedowse	char *path;
549102814Siedowse	int error;
55014331Speter
551102814Siedowse	LCONVPATHCREAT(td, args->path, &path);
55214331Speter
55314331Speter#ifdef DEBUG
55472543Sjlemon	if (ldebug(mkdir))
555102814Siedowse		printf(ARGS(mkdir, "%s, %d"), path, args->mode);
55614331Speter#endif
557102814Siedowse	error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
558102814Siedowse	LFREEPATH(path);
559102814Siedowse	return (error);
56014331Speter}
56114331Speter
56214331Speterint
56383366Sjulianlinux_rmdir(struct thread *td, struct linux_rmdir_args *args)
56414331Speter{
565102814Siedowse	char *path;
566102814Siedowse	int error;
56714331Speter
568102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
56914331Speter
57014331Speter#ifdef DEBUG
57172543Sjlemon	if (ldebug(rmdir))
572102814Siedowse		printf(ARGS(rmdir, "%s"), path);
57314331Speter#endif
574102814Siedowse	error = kern_rmdir(td, path, UIO_SYSSPACE);
575102814Siedowse	LFREEPATH(path);
576102814Siedowse	return (error);
57714331Speter}
57814331Speter
57914331Speterint
58083366Sjulianlinux_rename(struct thread *td, struct linux_rename_args *args)
58114331Speter{
582102814Siedowse	char *from, *to;
583102814Siedowse	int error;
58414331Speter
585102814Siedowse	LCONVPATHEXIST(td, args->from, &from);
586102814Siedowse	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
587102814Siedowse	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
588102814Siedowse	if (to == NULL) {
589102814Siedowse		LFREEPATH(from);
590102814Siedowse		return (error);
591102814Siedowse	}
59214331Speter
59314331Speter#ifdef DEBUG
59472543Sjlemon	if (ldebug(rename))
595102814Siedowse		printf(ARGS(rename, "%s, %s"), from, to);
59614331Speter#endif
597102814Siedowse	error = kern_rename(td, from, to, UIO_SYSSPACE);
598102814Siedowse	LFREEPATH(from);
599102814Siedowse	LFREEPATH(to);
600102814Siedowse	return (error);
60114331Speter}
60214331Speter
60314331Speterint
60483366Sjulianlinux_symlink(struct thread *td, struct linux_symlink_args *args)
60514331Speter{
606102814Siedowse	char *path, *to;
607102814Siedowse	int error;
60814331Speter
609102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
610102814Siedowse	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
611102814Siedowse	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
612102814Siedowse	if (to == NULL) {
613102814Siedowse		LFREEPATH(path);
614102814Siedowse		return (error);
615102814Siedowse	}
61614331Speter
61714331Speter#ifdef DEBUG
61872543Sjlemon	if (ldebug(symlink))
619102814Siedowse		printf(ARGS(symlink, "%s, %s"), path, to);
62014331Speter#endif
621102814Siedowse	error = kern_symlink(td, path, to, UIO_SYSSPACE);
622102814Siedowse	LFREEPATH(path);
623102814Siedowse	LFREEPATH(to);
624102814Siedowse	return (error);
62514331Speter}
62614331Speter
62714331Speterint
62883366Sjulianlinux_readlink(struct thread *td, struct linux_readlink_args *args)
62914331Speter{
630102814Siedowse	char *name;
631102814Siedowse	int error;
63214331Speter
633102814Siedowse	LCONVPATHEXIST(td, args->name, &name);
63414331Speter
63514331Speter#ifdef DEBUG
63672543Sjlemon	if (ldebug(readlink))
637102814Siedowse		printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
638102814Siedowse		    args->count);
63914331Speter#endif
640102814Siedowse	error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
641102814Siedowse	    args->count);
642102814Siedowse	LFREEPATH(name);
643102814Siedowse	return (error);
64414331Speter}
64514331Speter
64614331Speterint
64783366Sjulianlinux_truncate(struct thread *td, struct linux_truncate_args *args)
64814331Speter{
649102814Siedowse	char *path;
650102814Siedowse	int error;
65114331Speter
652102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
65314331Speter
65414331Speter#ifdef DEBUG
65572543Sjlemon	if (ldebug(truncate))
656102814Siedowse		printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
65714331Speter#endif
65814331Speter
659102814Siedowse	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
660102814Siedowse	LFREEPATH(path);
661102814Siedowse	return (error);
66214331Speter}
66314331Speter
66449662Smarcelint
66583366Sjulianlinux_link(struct thread *td, struct linux_link_args *args)
66649662Smarcel{
667102814Siedowse	char *path, *to;
668102814Siedowse	int error;
66949662Smarcel
670102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
671102814Siedowse	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
672102814Siedowse	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
673102814Siedowse	if (to == NULL) {
674102814Siedowse		LFREEPATH(path);
675102814Siedowse		return (error);
676102814Siedowse	}
67749662Smarcel
67849662Smarcel#ifdef DEBUG
67972543Sjlemon	if (ldebug(link))
680102814Siedowse		printf(ARGS(link, "%s, %s"), path, to);
68149662Smarcel#endif
682102814Siedowse	error = kern_link(td, path, to, UIO_SYSSPACE);
683102814Siedowse	LFREEPATH(path);
684102814Siedowse	LFREEPATH(to);
685102814Siedowse	return (error);
68649662Smarcel}
68749788Smarcel
68868201Sobrien#ifndef __alpha__
68953713Smarcelint
69083366Sjulianlinux_fdatasync(td, uap)
69183366Sjulian	struct thread *td;
69253713Smarcel	struct linux_fdatasync_args *uap;
69353713Smarcel{
69453713Smarcel	struct fsync_args bsd;
69553713Smarcel
69653713Smarcel	bsd.fd = uap->fd;
69783366Sjulian	return fsync(td, &bsd);
69853713Smarcel}
69968201Sobrien#endif /*!__alpha__*/
70063285Smarcel
70163285Smarcelint
70283366Sjulianlinux_pread(td, uap)
70383366Sjulian	struct thread *td;
70463285Smarcel	struct linux_pread_args *uap;
70563285Smarcel{
70663285Smarcel	struct pread_args bsd;
70763285Smarcel
70863285Smarcel	bsd.fd = uap->fd;
70963285Smarcel	bsd.buf = uap->buf;
71063285Smarcel	bsd.nbyte = uap->nbyte;
71163285Smarcel	bsd.offset = uap->offset;
71283366Sjulian	return pread(td, &bsd);
71363285Smarcel}
71463285Smarcel
71563285Smarcelint
71683366Sjulianlinux_pwrite(td, uap)
71783366Sjulian	struct thread *td;
71863285Smarcel	struct linux_pwrite_args *uap;
71963285Smarcel{
72063285Smarcel	struct pwrite_args bsd;
72163285Smarcel
72263285Smarcel	bsd.fd = uap->fd;
72363285Smarcel	bsd.buf = uap->buf;
72463285Smarcel	bsd.nbyte = uap->nbyte;
72563285Smarcel	bsd.offset = uap->offset;
72683366Sjulian	return pwrite(td, &bsd);
72763285Smarcel}
72872538Sjlemon
72972538Sjlemonint
73083366Sjulianlinux_mount(struct thread *td, struct linux_mount_args *args)
73172538Sjlemon{
73272538Sjlemon	struct ufs_args ufs;
733111798Sdes	char fstypename[MFSNAMELEN];
734111798Sdes	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
735127059Stjr	struct uio auio;
736127059Stjr	struct iovec iov[4];
73773286Sadrian	int error;
73873286Sadrian	int fsflags;
73973286Sadrian	void *fsdata;
74072538Sjlemon
741111798Sdes	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
74273286Sadrian	    NULL);
74372538Sjlemon	if (error)
744111798Sdes		return (error);
745127057Stjr	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
74672538Sjlemon	if (error)
747111798Sdes		return (error);
748127057Stjr	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
74972538Sjlemon	if (error)
750111798Sdes		return (error);
75172538Sjlemon
75272538Sjlemon#ifdef DEBUG
75372538Sjlemon	if (ldebug(mount))
75472538Sjlemon		printf(ARGS(mount, "%s, %s, %s"),
75572538Sjlemon		    fstypename, mntfromname, mntonname);
75672538Sjlemon#endif
75772538Sjlemon
75872538Sjlemon	if (strcmp(fstypename, "ext2") == 0) {
759127059Stjr		strcpy(fstypename, "ext2fs");
76073286Sadrian		fsdata = &ufs;
76172538Sjlemon		ufs.fspec = mntfromname;
76272538Sjlemon#define DEFAULT_ROOTID		-2
76372538Sjlemon		ufs.export.ex_root = DEFAULT_ROOTID;
76472538Sjlemon		ufs.export.ex_flags =
76572538Sjlemon		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
76672538Sjlemon	} else if (strcmp(fstypename, "proc") == 0) {
767127059Stjr		strcpy(fstypename, "linprocfs");
76873286Sadrian		fsdata = NULL;
76972538Sjlemon	} else {
77072538Sjlemon		return (ENODEV);
77172538Sjlemon	}
77272538Sjlemon
77373286Sadrian	fsflags = 0;
77472538Sjlemon
77572538Sjlemon	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
77672538Sjlemon		/*
77772538Sjlemon		 * Linux SYNC flag is not included; the closest equivalent
77872538Sjlemon		 * FreeBSD has is !ASYNC, which is our default.
77972538Sjlemon		 */
78072538Sjlemon		if (args->rwflag & LINUX_MS_RDONLY)
781111798Sdes			fsflags |= MNT_RDONLY;
78272538Sjlemon		if (args->rwflag & LINUX_MS_NOSUID)
783111798Sdes			fsflags |= MNT_NOSUID;
78472538Sjlemon		if (args->rwflag & LINUX_MS_NODEV)
785111798Sdes			fsflags |= MNT_NODEV;
78672538Sjlemon		if (args->rwflag & LINUX_MS_NOEXEC)
787111798Sdes			fsflags |= MNT_NOEXEC;
78872538Sjlemon		if (args->rwflag & LINUX_MS_REMOUNT)
789111798Sdes			fsflags |= MNT_UPDATE;
79072538Sjlemon	}
79172538Sjlemon
792127059Stjr	if (strcmp(fstypename, "linprocfs") == 0) {
793127059Stjr		bzero(&auio, sizeof(auio));
794127059Stjr		auio.uio_iov = iov;
795127059Stjr		auio.uio_iovcnt = sizeof(iov) / sizeof(*iov);
796127059Stjr		auio.uio_segflg = UIO_SYSSPACE;
797127059Stjr		iov[0].iov_base = "fstype";
798127059Stjr		iov[0].iov_len = sizeof("fstype");
799127059Stjr		iov[1].iov_base = fstypename;
800127059Stjr		iov[1].iov_len = strlen(fstypename) + 1;
801127059Stjr		iov[2].iov_base = "fspath";
802127059Stjr		iov[2].iov_len = sizeof("fspath");
803127059Stjr		iov[3].iov_base = mntonname;
804127059Stjr		iov[3].iov_len = strlen(mntonname) + 1;
805127059Stjr		error = vfs_nmount(td, fsflags, &auio);
806127059Stjr	} else
807127059Stjr		error = vfs_mount(td, fstypename, mntonname, fsflags, fsdata);
808127059Stjr	return (error);
80972538Sjlemon}
81072538Sjlemon
81172538Sjlemonint
81283366Sjulianlinux_oldumount(struct thread *td, struct linux_oldumount_args *args)
81372538Sjlemon{
81483221Smarcel	struct linux_umount_args args2;
81572538Sjlemon
81672538Sjlemon	args2.path = args->path;
81772538Sjlemon	args2.flags = 0;
81883366Sjulian	return (linux_umount(td, &args2));
81972538Sjlemon}
82072538Sjlemon
82172538Sjlemonint
82283366Sjulianlinux_umount(struct thread *td, struct linux_umount_args *args)
82372538Sjlemon{
82472538Sjlemon	struct unmount_args bsd;
82572538Sjlemon
82672538Sjlemon	bsd.path = args->path;
82772538Sjlemon	bsd.flags = args->flags;	/* XXX correct? */
82883366Sjulian	return (unmount(td, &bsd));
82972538Sjlemon}
83083221Smarcel
83183221Smarcel/*
83283221Smarcel * fcntl family of syscalls
83383221Smarcel */
83483221Smarcel
83583221Smarcelstruct l_flock {
83683221Smarcel	l_short		l_type;
83783221Smarcel	l_short		l_whence;
83883221Smarcel	l_off_t		l_start;
83983221Smarcel	l_off_t		l_len;
84083221Smarcel	l_pid_t		l_pid;
84183221Smarcel};
84283221Smarcel
84383221Smarcelstatic void
84483221Smarcellinux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
84583221Smarcel{
84683221Smarcel	switch (linux_flock->l_type) {
84783221Smarcel	case LINUX_F_RDLCK:
84883221Smarcel		bsd_flock->l_type = F_RDLCK;
84983221Smarcel		break;
85083221Smarcel	case LINUX_F_WRLCK:
85183221Smarcel		bsd_flock->l_type = F_WRLCK;
85283221Smarcel		break;
85383221Smarcel	case LINUX_F_UNLCK:
85483221Smarcel		bsd_flock->l_type = F_UNLCK;
85583221Smarcel		break;
85683221Smarcel	default:
85783221Smarcel		bsd_flock->l_type = -1;
85883221Smarcel		break;
85983221Smarcel	}
86083221Smarcel	bsd_flock->l_whence = linux_flock->l_whence;
86183221Smarcel	bsd_flock->l_start = (off_t)linux_flock->l_start;
86283221Smarcel	bsd_flock->l_len = (off_t)linux_flock->l_len;
86383221Smarcel	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
86483221Smarcel}
86583221Smarcel
86683221Smarcelstatic void
86783221Smarcelbsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
86883221Smarcel{
86983221Smarcel	switch (bsd_flock->l_type) {
87083221Smarcel	case F_RDLCK:
87183221Smarcel		linux_flock->l_type = LINUX_F_RDLCK;
87283221Smarcel		break;
87383221Smarcel	case F_WRLCK:
87483221Smarcel		linux_flock->l_type = LINUX_F_WRLCK;
87583221Smarcel		break;
87683221Smarcel	case F_UNLCK:
87783221Smarcel		linux_flock->l_type = LINUX_F_UNLCK;
87883221Smarcel		break;
87983221Smarcel	}
88083221Smarcel	linux_flock->l_whence = bsd_flock->l_whence;
88183221Smarcel	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
88283221Smarcel	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
88383221Smarcel	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
88483221Smarcel}
88583221Smarcel
88683221Smarcel#if defined(__i386__)
88783221Smarcelstruct l_flock64 {
88883221Smarcel	l_short		l_type;
88983221Smarcel	l_short		l_whence;
89083221Smarcel	l_loff_t	l_start;
89183221Smarcel	l_loff_t	l_len;
89283221Smarcel	l_pid_t		l_pid;
89383221Smarcel};
89483221Smarcel
89583221Smarcelstatic void
89683221Smarcellinux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
89783221Smarcel{
89883221Smarcel	switch (linux_flock->l_type) {
89983221Smarcel	case LINUX_F_RDLCK:
90083221Smarcel		bsd_flock->l_type = F_RDLCK;
90183221Smarcel		break;
90283221Smarcel	case LINUX_F_WRLCK:
90383221Smarcel		bsd_flock->l_type = F_WRLCK;
90483221Smarcel		break;
90583221Smarcel	case LINUX_F_UNLCK:
90683221Smarcel		bsd_flock->l_type = F_UNLCK;
90783221Smarcel		break;
90883221Smarcel	default:
90983221Smarcel		bsd_flock->l_type = -1;
91083221Smarcel		break;
91183221Smarcel	}
91283221Smarcel	bsd_flock->l_whence = linux_flock->l_whence;
91383221Smarcel	bsd_flock->l_start = (off_t)linux_flock->l_start;
91483221Smarcel	bsd_flock->l_len = (off_t)linux_flock->l_len;
91583221Smarcel	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
91683221Smarcel}
91783221Smarcel
91883221Smarcelstatic void
91983221Smarcelbsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
92083221Smarcel{
92183221Smarcel	switch (bsd_flock->l_type) {
92283221Smarcel	case F_RDLCK:
92383221Smarcel		linux_flock->l_type = LINUX_F_RDLCK;
92483221Smarcel		break;
92583221Smarcel	case F_WRLCK:
92683221Smarcel		linux_flock->l_type = LINUX_F_WRLCK;
92783221Smarcel		break;
92883221Smarcel	case F_UNLCK:
92983221Smarcel		linux_flock->l_type = LINUX_F_UNLCK;
93083221Smarcel		break;
93183221Smarcel	}
93283221Smarcel	linux_flock->l_whence = bsd_flock->l_whence;
93383221Smarcel	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
93483221Smarcel	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
93583221Smarcel	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
93683221Smarcel}
93783221Smarcel#endif /* __i386__ */
93883221Smarcel
93983221Smarcel#if defined(__alpha__)
94083221Smarcel#define	linux_fcntl64_args	linux_fcntl_args
94183221Smarcel#endif
94283221Smarcel
94383221Smarcelstatic int
94483366Sjulianfcntl_common(struct thread *td, struct linux_fcntl64_args *args)
94583221Smarcel{
946107680Siedowse	struct l_flock linux_flock;
947107680Siedowse	struct flock bsd_flock;
94883221Smarcel	struct file *fp;
949102872Siedowse	long arg;
95083221Smarcel	int error, result;
95183221Smarcel
95283221Smarcel	switch (args->cmd) {
95383221Smarcel	case LINUX_F_DUPFD:
954102872Siedowse		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
95583221Smarcel
95683221Smarcel	case LINUX_F_GETFD:
957102872Siedowse		return (kern_fcntl(td, args->fd, F_GETFD, 0));
95883221Smarcel
95983221Smarcel	case LINUX_F_SETFD:
960102872Siedowse		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
96183221Smarcel
96283221Smarcel	case LINUX_F_GETFL:
963102872Siedowse		error = kern_fcntl(td, args->fd, F_GETFL, 0);
96483366Sjulian		result = td->td_retval[0];
96583366Sjulian		td->td_retval[0] = 0;
96683221Smarcel		if (result & O_RDONLY)
96783366Sjulian			td->td_retval[0] |= LINUX_O_RDONLY;
96883221Smarcel		if (result & O_WRONLY)
96983366Sjulian			td->td_retval[0] |= LINUX_O_WRONLY;
97083221Smarcel		if (result & O_RDWR)
97183366Sjulian			td->td_retval[0] |= LINUX_O_RDWR;
97283221Smarcel		if (result & O_NDELAY)
97383366Sjulian			td->td_retval[0] |= LINUX_O_NONBLOCK;
97483221Smarcel		if (result & O_APPEND)
97583366Sjulian			td->td_retval[0] |= LINUX_O_APPEND;
97683221Smarcel		if (result & O_FSYNC)
97783366Sjulian			td->td_retval[0] |= LINUX_O_SYNC;
97883221Smarcel		if (result & O_ASYNC)
97983366Sjulian			td->td_retval[0] |= LINUX_FASYNC;
98083221Smarcel		return (error);
98183221Smarcel
98283221Smarcel	case LINUX_F_SETFL:
983102872Siedowse		arg = 0;
98483221Smarcel		if (args->arg & LINUX_O_NDELAY)
985102872Siedowse			arg |= O_NONBLOCK;
98683221Smarcel		if (args->arg & LINUX_O_APPEND)
987102872Siedowse			arg |= O_APPEND;
98883221Smarcel		if (args->arg & LINUX_O_SYNC)
989102872Siedowse			arg |= O_FSYNC;
99083221Smarcel		if (args->arg & LINUX_FASYNC)
991102872Siedowse			arg |= O_ASYNC;
992102872Siedowse		return (kern_fcntl(td, args->fd, F_SETFL, arg));
99383221Smarcel
994107680Siedowse	case LINUX_F_GETLK:
995111797Sdes		error = copyin((void *)args->arg, &linux_flock,
996107680Siedowse		    sizeof(linux_flock));
997107680Siedowse		if (error)
998107680Siedowse			return (error);
999107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1000107680Siedowse		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1001107680Siedowse		if (error)
1002107680Siedowse			return (error);
1003107680Siedowse		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1004111797Sdes		return (copyout(&linux_flock, (void *)args->arg,
1005107680Siedowse		    sizeof(linux_flock)));
1006107680Siedowse
1007107680Siedowse	case LINUX_F_SETLK:
1008111797Sdes		error = copyin((void *)args->arg, &linux_flock,
1009107680Siedowse		    sizeof(linux_flock));
1010107680Siedowse		if (error)
1011107680Siedowse			return (error);
1012107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1013107680Siedowse		return (kern_fcntl(td, args->fd, F_SETLK,
1014107680Siedowse		    (intptr_t)&bsd_flock));
1015107680Siedowse
1016107680Siedowse	case LINUX_F_SETLKW:
1017111797Sdes		error = copyin((void *)args->arg, &linux_flock,
1018107680Siedowse		    sizeof(linux_flock));
1019107680Siedowse		if (error)
1020107680Siedowse			return (error);
1021107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1022107680Siedowse		return (kern_fcntl(td, args->fd, F_SETLKW,
1023107680Siedowse		     (intptr_t)&bsd_flock));
1024107680Siedowse
102583221Smarcel	case LINUX_F_GETOWN:
1026102872Siedowse		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
102783221Smarcel
102883221Smarcel	case LINUX_F_SETOWN:
102983221Smarcel		/*
103083221Smarcel		 * XXX some Linux applications depend on F_SETOWN having no
103183221Smarcel		 * significant effect for pipes (SIGIO is not delivered for
103283221Smarcel		 * pipes under Linux-2.2.35 at least).
103383221Smarcel		 */
103489319Salfred		error = fget(td, args->fd, &fp);
103589319Salfred		if (error)
103689319Salfred			return (error);
103789306Salfred		if (fp->f_type == DTYPE_PIPE) {
103889306Salfred			fdrop(fp, td);
103983221Smarcel			return (EINVAL);
104089306Salfred		}
104189306Salfred		fdrop(fp, td);
104283221Smarcel
1043102872Siedowse		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
104483221Smarcel	}
104583221Smarcel
104683221Smarcel	return (EINVAL);
104783221Smarcel}
104883221Smarcel
104983221Smarcelint
105083366Sjulianlinux_fcntl(struct thread *td, struct linux_fcntl_args *args)
105183221Smarcel{
105283221Smarcel	struct linux_fcntl64_args args64;
105383221Smarcel
105483221Smarcel#ifdef DEBUG
105583221Smarcel	if (ldebug(fcntl))
105683221Smarcel		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
105783221Smarcel#endif
105883221Smarcel
105983221Smarcel	args64.fd = args->fd;
106083221Smarcel	args64.cmd = args->cmd;
106183221Smarcel	args64.arg = args->arg;
106283366Sjulian	return (fcntl_common(td, &args64));
106383221Smarcel}
106483221Smarcel
106583221Smarcel#if defined(__i386__)
106683221Smarcelint
106783366Sjulianlinux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
106883221Smarcel{
106983221Smarcel	struct l_flock64 linux_flock;
1070102872Siedowse	struct flock bsd_flock;
107183221Smarcel	int error;
107283221Smarcel
107383221Smarcel#ifdef DEBUG
107483221Smarcel	if (ldebug(fcntl64))
107583221Smarcel		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
107683221Smarcel#endif
107783221Smarcel
107883221Smarcel	switch (args->cmd) {
107999687Srobert	case LINUX_F_GETLK64:
1080111797Sdes		error = copyin((void *)args->arg, &linux_flock,
108183221Smarcel		    sizeof(linux_flock));
108283221Smarcel		if (error)
108383221Smarcel			return (error);
1084102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1085102872Siedowse		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
108683221Smarcel		if (error)
108783221Smarcel			return (error);
1088102872Siedowse		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1089111797Sdes		return (copyout(&linux_flock, (void *)args->arg,
1090111797Sdes			    sizeof(linux_flock)));
109183221Smarcel
109299687Srobert	case LINUX_F_SETLK64:
1093111797Sdes		error = copyin((void *)args->arg, &linux_flock,
109483221Smarcel		    sizeof(linux_flock));
109583221Smarcel		if (error)
109683221Smarcel			return (error);
1097102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1098102872Siedowse		return (kern_fcntl(td, args->fd, F_SETLK,
1099102872Siedowse		    (intptr_t)&bsd_flock));
110083221Smarcel
110199687Srobert	case LINUX_F_SETLKW64:
1102111797Sdes		error = copyin((void *)args->arg, &linux_flock,
110383221Smarcel		    sizeof(linux_flock));
110483221Smarcel		if (error)
110583221Smarcel			return (error);
1106102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1107102872Siedowse		return (kern_fcntl(td, args->fd, F_SETLKW,
1108102872Siedowse		    (intptr_t)&bsd_flock));
110983221Smarcel	}
111083221Smarcel
111183366Sjulian	return (fcntl_common(td, args));
111283221Smarcel}
111383221Smarcel#endif /* __i386__ */
111485022Smarcel
111585022Smarcelint
111685022Smarcellinux_chown(struct thread *td, struct linux_chown_args *args)
111785022Smarcel{
1118102814Siedowse	char *path;
1119102814Siedowse	int error;
112085022Smarcel
1121102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
112285022Smarcel
112385022Smarcel#ifdef DEBUG
112485022Smarcel	if (ldebug(chown))
1125102814Siedowse		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
112685022Smarcel#endif
1127102814Siedowse	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1128102814Siedowse	LFREEPATH(path);
1129102814Siedowse	return (error);
113085022Smarcel}
113185022Smarcel
113285022Smarcelint
113385022Smarcellinux_lchown(struct thread *td, struct linux_lchown_args *args)
113485022Smarcel{
1135102814Siedowse	char *path;
1136102814Siedowse	int error;
113785022Smarcel
1138102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
113985022Smarcel
114085022Smarcel#ifdef DEBUG
114185022Smarcel	if (ldebug(lchown))
1142102814Siedowse		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
114385022Smarcel#endif
1144102814Siedowse	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1145102814Siedowse	LFREEPATH(path);
1146102814Siedowse	return (error);
114785022Smarcel}
1148