linux_file.c revision 132708
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 132708 2004-07-27 21:38:42Z phk $");
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];
73573286Sadrian	int error;
73673286Sadrian	int fsflags;
73773286Sadrian	void *fsdata;
73872538Sjlemon
739111798Sdes	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
74073286Sadrian	    NULL);
74172538Sjlemon	if (error)
742111798Sdes		return (error);
743127057Stjr	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
74472538Sjlemon	if (error)
745111798Sdes		return (error);
746127057Stjr	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
74772538Sjlemon	if (error)
748111798Sdes		return (error);
74972538Sjlemon
75072538Sjlemon#ifdef DEBUG
75172538Sjlemon	if (ldebug(mount))
75272538Sjlemon		printf(ARGS(mount, "%s, %s, %s"),
75372538Sjlemon		    fstypename, mntfromname, mntonname);
75472538Sjlemon#endif
75572538Sjlemon
75672538Sjlemon	if (strcmp(fstypename, "ext2") == 0) {
757127059Stjr		strcpy(fstypename, "ext2fs");
75873286Sadrian		fsdata = &ufs;
75972538Sjlemon		ufs.fspec = mntfromname;
76072538Sjlemon#define DEFAULT_ROOTID		-2
76172538Sjlemon		ufs.export.ex_root = DEFAULT_ROOTID;
76272538Sjlemon		ufs.export.ex_flags =
76372538Sjlemon		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
76472538Sjlemon	} else if (strcmp(fstypename, "proc") == 0) {
765127059Stjr		strcpy(fstypename, "linprocfs");
76673286Sadrian		fsdata = NULL;
76772538Sjlemon	} else {
76872538Sjlemon		return (ENODEV);
76972538Sjlemon	}
77072538Sjlemon
77173286Sadrian	fsflags = 0;
77272538Sjlemon
77372538Sjlemon	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
77472538Sjlemon		/*
77572538Sjlemon		 * Linux SYNC flag is not included; the closest equivalent
77672538Sjlemon		 * FreeBSD has is !ASYNC, which is our default.
77772538Sjlemon		 */
77872538Sjlemon		if (args->rwflag & LINUX_MS_RDONLY)
779111798Sdes			fsflags |= MNT_RDONLY;
78072538Sjlemon		if (args->rwflag & LINUX_MS_NOSUID)
781111798Sdes			fsflags |= MNT_NOSUID;
78272538Sjlemon		if (args->rwflag & LINUX_MS_NODEV)
783111798Sdes			fsflags |= MNT_NODEV;
78472538Sjlemon		if (args->rwflag & LINUX_MS_NOEXEC)
785111798Sdes			fsflags |= MNT_NOEXEC;
78672538Sjlemon		if (args->rwflag & LINUX_MS_REMOUNT)
787111798Sdes			fsflags |= MNT_UPDATE;
78872538Sjlemon	}
78972538Sjlemon
790127059Stjr	if (strcmp(fstypename, "linprocfs") == 0) {
791132708Sphk		error = kernel_vmount(fsflags,
792132708Sphk			"fstype", fstypename,
793132708Sphk			"fspath", mntonname,
794132708Sphk			NULL);
795127059Stjr	} else
796127059Stjr		error = vfs_mount(td, fstypename, mntonname, fsflags, fsdata);
797127059Stjr	return (error);
79872538Sjlemon}
79972538Sjlemon
80072538Sjlemonint
80183366Sjulianlinux_oldumount(struct thread *td, struct linux_oldumount_args *args)
80272538Sjlemon{
80383221Smarcel	struct linux_umount_args args2;
80472538Sjlemon
80572538Sjlemon	args2.path = args->path;
80672538Sjlemon	args2.flags = 0;
80783366Sjulian	return (linux_umount(td, &args2));
80872538Sjlemon}
80972538Sjlemon
81072538Sjlemonint
81183366Sjulianlinux_umount(struct thread *td, struct linux_umount_args *args)
81272538Sjlemon{
81372538Sjlemon	struct unmount_args bsd;
81472538Sjlemon
81572538Sjlemon	bsd.path = args->path;
81672538Sjlemon	bsd.flags = args->flags;	/* XXX correct? */
81783366Sjulian	return (unmount(td, &bsd));
81872538Sjlemon}
81983221Smarcel
82083221Smarcel/*
82183221Smarcel * fcntl family of syscalls
82283221Smarcel */
82383221Smarcel
82483221Smarcelstruct l_flock {
82583221Smarcel	l_short		l_type;
82683221Smarcel	l_short		l_whence;
82783221Smarcel	l_off_t		l_start;
82883221Smarcel	l_off_t		l_len;
82983221Smarcel	l_pid_t		l_pid;
83083221Smarcel};
83183221Smarcel
83283221Smarcelstatic void
83383221Smarcellinux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
83483221Smarcel{
83583221Smarcel	switch (linux_flock->l_type) {
83683221Smarcel	case LINUX_F_RDLCK:
83783221Smarcel		bsd_flock->l_type = F_RDLCK;
83883221Smarcel		break;
83983221Smarcel	case LINUX_F_WRLCK:
84083221Smarcel		bsd_flock->l_type = F_WRLCK;
84183221Smarcel		break;
84283221Smarcel	case LINUX_F_UNLCK:
84383221Smarcel		bsd_flock->l_type = F_UNLCK;
84483221Smarcel		break;
84583221Smarcel	default:
84683221Smarcel		bsd_flock->l_type = -1;
84783221Smarcel		break;
84883221Smarcel	}
84983221Smarcel	bsd_flock->l_whence = linux_flock->l_whence;
85083221Smarcel	bsd_flock->l_start = (off_t)linux_flock->l_start;
85183221Smarcel	bsd_flock->l_len = (off_t)linux_flock->l_len;
85283221Smarcel	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
85383221Smarcel}
85483221Smarcel
85583221Smarcelstatic void
85683221Smarcelbsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
85783221Smarcel{
85883221Smarcel	switch (bsd_flock->l_type) {
85983221Smarcel	case F_RDLCK:
86083221Smarcel		linux_flock->l_type = LINUX_F_RDLCK;
86183221Smarcel		break;
86283221Smarcel	case F_WRLCK:
86383221Smarcel		linux_flock->l_type = LINUX_F_WRLCK;
86483221Smarcel		break;
86583221Smarcel	case F_UNLCK:
86683221Smarcel		linux_flock->l_type = LINUX_F_UNLCK;
86783221Smarcel		break;
86883221Smarcel	}
86983221Smarcel	linux_flock->l_whence = bsd_flock->l_whence;
87083221Smarcel	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
87183221Smarcel	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
87283221Smarcel	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
87383221Smarcel}
87483221Smarcel
87583221Smarcel#if defined(__i386__)
87683221Smarcelstruct l_flock64 {
87783221Smarcel	l_short		l_type;
87883221Smarcel	l_short		l_whence;
87983221Smarcel	l_loff_t	l_start;
88083221Smarcel	l_loff_t	l_len;
88183221Smarcel	l_pid_t		l_pid;
88283221Smarcel};
88383221Smarcel
88483221Smarcelstatic void
88583221Smarcellinux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
88683221Smarcel{
88783221Smarcel	switch (linux_flock->l_type) {
88883221Smarcel	case LINUX_F_RDLCK:
88983221Smarcel		bsd_flock->l_type = F_RDLCK;
89083221Smarcel		break;
89183221Smarcel	case LINUX_F_WRLCK:
89283221Smarcel		bsd_flock->l_type = F_WRLCK;
89383221Smarcel		break;
89483221Smarcel	case LINUX_F_UNLCK:
89583221Smarcel		bsd_flock->l_type = F_UNLCK;
89683221Smarcel		break;
89783221Smarcel	default:
89883221Smarcel		bsd_flock->l_type = -1;
89983221Smarcel		break;
90083221Smarcel	}
90183221Smarcel	bsd_flock->l_whence = linux_flock->l_whence;
90283221Smarcel	bsd_flock->l_start = (off_t)linux_flock->l_start;
90383221Smarcel	bsd_flock->l_len = (off_t)linux_flock->l_len;
90483221Smarcel	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
90583221Smarcel}
90683221Smarcel
90783221Smarcelstatic void
90883221Smarcelbsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
90983221Smarcel{
91083221Smarcel	switch (bsd_flock->l_type) {
91183221Smarcel	case F_RDLCK:
91283221Smarcel		linux_flock->l_type = LINUX_F_RDLCK;
91383221Smarcel		break;
91483221Smarcel	case F_WRLCK:
91583221Smarcel		linux_flock->l_type = LINUX_F_WRLCK;
91683221Smarcel		break;
91783221Smarcel	case F_UNLCK:
91883221Smarcel		linux_flock->l_type = LINUX_F_UNLCK;
91983221Smarcel		break;
92083221Smarcel	}
92183221Smarcel	linux_flock->l_whence = bsd_flock->l_whence;
92283221Smarcel	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
92383221Smarcel	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
92483221Smarcel	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
92583221Smarcel}
92683221Smarcel#endif /* __i386__ */
92783221Smarcel
92883221Smarcel#if defined(__alpha__)
92983221Smarcel#define	linux_fcntl64_args	linux_fcntl_args
93083221Smarcel#endif
93183221Smarcel
93283221Smarcelstatic int
93383366Sjulianfcntl_common(struct thread *td, struct linux_fcntl64_args *args)
93483221Smarcel{
935107680Siedowse	struct l_flock linux_flock;
936107680Siedowse	struct flock bsd_flock;
93783221Smarcel	struct file *fp;
938102872Siedowse	long arg;
93983221Smarcel	int error, result;
94083221Smarcel
94183221Smarcel	switch (args->cmd) {
94283221Smarcel	case LINUX_F_DUPFD:
943102872Siedowse		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
94483221Smarcel
94583221Smarcel	case LINUX_F_GETFD:
946102872Siedowse		return (kern_fcntl(td, args->fd, F_GETFD, 0));
94783221Smarcel
94883221Smarcel	case LINUX_F_SETFD:
949102872Siedowse		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
95083221Smarcel
95183221Smarcel	case LINUX_F_GETFL:
952102872Siedowse		error = kern_fcntl(td, args->fd, F_GETFL, 0);
95383366Sjulian		result = td->td_retval[0];
95483366Sjulian		td->td_retval[0] = 0;
95583221Smarcel		if (result & O_RDONLY)
95683366Sjulian			td->td_retval[0] |= LINUX_O_RDONLY;
95783221Smarcel		if (result & O_WRONLY)
95883366Sjulian			td->td_retval[0] |= LINUX_O_WRONLY;
95983221Smarcel		if (result & O_RDWR)
96083366Sjulian			td->td_retval[0] |= LINUX_O_RDWR;
96183221Smarcel		if (result & O_NDELAY)
96283366Sjulian			td->td_retval[0] |= LINUX_O_NONBLOCK;
96383221Smarcel		if (result & O_APPEND)
96483366Sjulian			td->td_retval[0] |= LINUX_O_APPEND;
96583221Smarcel		if (result & O_FSYNC)
96683366Sjulian			td->td_retval[0] |= LINUX_O_SYNC;
96783221Smarcel		if (result & O_ASYNC)
96883366Sjulian			td->td_retval[0] |= LINUX_FASYNC;
96983221Smarcel		return (error);
97083221Smarcel
97183221Smarcel	case LINUX_F_SETFL:
972102872Siedowse		arg = 0;
97383221Smarcel		if (args->arg & LINUX_O_NDELAY)
974102872Siedowse			arg |= O_NONBLOCK;
97583221Smarcel		if (args->arg & LINUX_O_APPEND)
976102872Siedowse			arg |= O_APPEND;
97783221Smarcel		if (args->arg & LINUX_O_SYNC)
978102872Siedowse			arg |= O_FSYNC;
97983221Smarcel		if (args->arg & LINUX_FASYNC)
980102872Siedowse			arg |= O_ASYNC;
981102872Siedowse		return (kern_fcntl(td, args->fd, F_SETFL, arg));
98283221Smarcel
983107680Siedowse	case LINUX_F_GETLK:
984111797Sdes		error = copyin((void *)args->arg, &linux_flock,
985107680Siedowse		    sizeof(linux_flock));
986107680Siedowse		if (error)
987107680Siedowse			return (error);
988107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
989107680Siedowse		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
990107680Siedowse		if (error)
991107680Siedowse			return (error);
992107680Siedowse		bsd_to_linux_flock(&bsd_flock, &linux_flock);
993111797Sdes		return (copyout(&linux_flock, (void *)args->arg,
994107680Siedowse		    sizeof(linux_flock)));
995107680Siedowse
996107680Siedowse	case LINUX_F_SETLK:
997111797Sdes		error = copyin((void *)args->arg, &linux_flock,
998107680Siedowse		    sizeof(linux_flock));
999107680Siedowse		if (error)
1000107680Siedowse			return (error);
1001107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1002107680Siedowse		return (kern_fcntl(td, args->fd, F_SETLK,
1003107680Siedowse		    (intptr_t)&bsd_flock));
1004107680Siedowse
1005107680Siedowse	case LINUX_F_SETLKW:
1006111797Sdes		error = copyin((void *)args->arg, &linux_flock,
1007107680Siedowse		    sizeof(linux_flock));
1008107680Siedowse		if (error)
1009107680Siedowse			return (error);
1010107680Siedowse		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1011107680Siedowse		return (kern_fcntl(td, args->fd, F_SETLKW,
1012107680Siedowse		     (intptr_t)&bsd_flock));
1013107680Siedowse
101483221Smarcel	case LINUX_F_GETOWN:
1015102872Siedowse		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
101683221Smarcel
101783221Smarcel	case LINUX_F_SETOWN:
101883221Smarcel		/*
101983221Smarcel		 * XXX some Linux applications depend on F_SETOWN having no
102083221Smarcel		 * significant effect for pipes (SIGIO is not delivered for
102183221Smarcel		 * pipes under Linux-2.2.35 at least).
102283221Smarcel		 */
102389319Salfred		error = fget(td, args->fd, &fp);
102489319Salfred		if (error)
102589319Salfred			return (error);
102689306Salfred		if (fp->f_type == DTYPE_PIPE) {
102789306Salfred			fdrop(fp, td);
102883221Smarcel			return (EINVAL);
102989306Salfred		}
103089306Salfred		fdrop(fp, td);
103183221Smarcel
1032102872Siedowse		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
103383221Smarcel	}
103483221Smarcel
103583221Smarcel	return (EINVAL);
103683221Smarcel}
103783221Smarcel
103883221Smarcelint
103983366Sjulianlinux_fcntl(struct thread *td, struct linux_fcntl_args *args)
104083221Smarcel{
104183221Smarcel	struct linux_fcntl64_args args64;
104283221Smarcel
104383221Smarcel#ifdef DEBUG
104483221Smarcel	if (ldebug(fcntl))
104583221Smarcel		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
104683221Smarcel#endif
104783221Smarcel
104883221Smarcel	args64.fd = args->fd;
104983221Smarcel	args64.cmd = args->cmd;
105083221Smarcel	args64.arg = args->arg;
105183366Sjulian	return (fcntl_common(td, &args64));
105283221Smarcel}
105383221Smarcel
105483221Smarcel#if defined(__i386__)
105583221Smarcelint
105683366Sjulianlinux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
105783221Smarcel{
105883221Smarcel	struct l_flock64 linux_flock;
1059102872Siedowse	struct flock bsd_flock;
106083221Smarcel	int error;
106183221Smarcel
106283221Smarcel#ifdef DEBUG
106383221Smarcel	if (ldebug(fcntl64))
106483221Smarcel		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
106583221Smarcel#endif
106683221Smarcel
106783221Smarcel	switch (args->cmd) {
106899687Srobert	case LINUX_F_GETLK64:
1069111797Sdes		error = copyin((void *)args->arg, &linux_flock,
107083221Smarcel		    sizeof(linux_flock));
107183221Smarcel		if (error)
107283221Smarcel			return (error);
1073102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1074102872Siedowse		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
107583221Smarcel		if (error)
107683221Smarcel			return (error);
1077102872Siedowse		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1078111797Sdes		return (copyout(&linux_flock, (void *)args->arg,
1079111797Sdes			    sizeof(linux_flock)));
108083221Smarcel
108199687Srobert	case LINUX_F_SETLK64:
1082111797Sdes		error = copyin((void *)args->arg, &linux_flock,
108383221Smarcel		    sizeof(linux_flock));
108483221Smarcel		if (error)
108583221Smarcel			return (error);
1086102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1087102872Siedowse		return (kern_fcntl(td, args->fd, F_SETLK,
1088102872Siedowse		    (intptr_t)&bsd_flock));
108983221Smarcel
109099687Srobert	case LINUX_F_SETLKW64:
1091111797Sdes		error = copyin((void *)args->arg, &linux_flock,
109283221Smarcel		    sizeof(linux_flock));
109383221Smarcel		if (error)
109483221Smarcel			return (error);
1095102872Siedowse		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1096102872Siedowse		return (kern_fcntl(td, args->fd, F_SETLKW,
1097102872Siedowse		    (intptr_t)&bsd_flock));
109883221Smarcel	}
109983221Smarcel
110083366Sjulian	return (fcntl_common(td, args));
110183221Smarcel}
110283221Smarcel#endif /* __i386__ */
110385022Smarcel
110485022Smarcelint
110585022Smarcellinux_chown(struct thread *td, struct linux_chown_args *args)
110685022Smarcel{
1107102814Siedowse	char *path;
1108102814Siedowse	int error;
110985022Smarcel
1110102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
111185022Smarcel
111285022Smarcel#ifdef DEBUG
111385022Smarcel	if (ldebug(chown))
1114102814Siedowse		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
111585022Smarcel#endif
1116102814Siedowse	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1117102814Siedowse	LFREEPATH(path);
1118102814Siedowse	return (error);
111985022Smarcel}
112085022Smarcel
112185022Smarcelint
112285022Smarcellinux_lchown(struct thread *td, struct linux_lchown_args *args)
112385022Smarcel{
1124102814Siedowse	char *path;
1125102814Siedowse	int error;
112685022Smarcel
1127102814Siedowse	LCONVPATHEXIST(td, args->path, &path);
112885022Smarcel
112985022Smarcel#ifdef DEBUG
113085022Smarcel	if (ldebug(lchown))
1131102814Siedowse		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
113285022Smarcel#endif
1133102814Siedowse	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1134102814Siedowse	LFREEPATH(path);
1135102814Siedowse	return (error);
113685022Smarcel}
1137