linux_file.c revision 234352
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: head/sys/compat/linux/linux_file.c 234352 2012-04-16 21:22:02Z jkim $");
31
32#include "opt_compat.h"
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/capability.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_util.h>
70#include <compat/linux/linux_file.h>
71
72/* XXX */
73int	do_pipe(struct thread *td, int fildes[2], int flags);
74
75int
76linux_creat(struct thread *td, struct linux_creat_args *args)
77{
78    char *path;
79    int error;
80
81    LCONVPATHEXIST(td, args->path, &path);
82
83#ifdef DEBUG
84	if (ldebug(creat))
85		printf(ARGS(creat, "%s, %d"), path, args->mode);
86#endif
87    error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
88	args->mode);
89    LFREEPATH(path);
90    return (error);
91}
92
93
94static int
95linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
96{
97    struct proc *p = td->td_proc;
98    struct file *fp;
99    int fd;
100    int bsd_flags, error;
101
102    bsd_flags = 0;
103    switch (l_flags & LINUX_O_ACCMODE) {
104    case LINUX_O_WRONLY:
105	bsd_flags |= O_WRONLY;
106	break;
107    case LINUX_O_RDWR:
108	bsd_flags |= O_RDWR;
109	break;
110    default:
111	bsd_flags |= O_RDONLY;
112    }
113    if (l_flags & LINUX_O_NDELAY)
114	bsd_flags |= O_NONBLOCK;
115    if (l_flags & LINUX_O_APPEND)
116	bsd_flags |= O_APPEND;
117    if (l_flags & LINUX_O_SYNC)
118	bsd_flags |= O_FSYNC;
119    if (l_flags & LINUX_O_NONBLOCK)
120	bsd_flags |= O_NONBLOCK;
121    if (l_flags & LINUX_FASYNC)
122	bsd_flags |= O_ASYNC;
123    if (l_flags & LINUX_O_CREAT)
124	bsd_flags |= O_CREAT;
125    if (l_flags & LINUX_O_TRUNC)
126	bsd_flags |= O_TRUNC;
127    if (l_flags & LINUX_O_EXCL)
128	bsd_flags |= O_EXCL;
129    if (l_flags & LINUX_O_NOCTTY)
130	bsd_flags |= O_NOCTTY;
131    if (l_flags & LINUX_O_DIRECT)
132	bsd_flags |= O_DIRECT;
133    if (l_flags & LINUX_O_NOFOLLOW)
134	bsd_flags |= O_NOFOLLOW;
135    if (l_flags & LINUX_O_DIRECTORY)
136	bsd_flags |= O_DIRECTORY;
137    /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
138
139    error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
140
141    if (!error) {
142	    fd = td->td_retval[0];
143	    /*
144	     * XXX In between kern_open() and fget(), another process
145	     * having the same filedesc could use that fd without
146	     * checking below.
147	     */
148	    error = fget(td, fd, CAP_IOCTL, &fp);
149	    if (!error) {
150		    sx_slock(&proctree_lock);
151		    PROC_LOCK(p);
152		    if (!(bsd_flags & O_NOCTTY) &&
153			SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
154			    PROC_UNLOCK(p);
155			    sx_unlock(&proctree_lock);
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
180int
181linux_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
199int
200linux_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
218int
219linux_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
242int
243linux_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
270int
271linux_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
290struct 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
297struct 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
321static int
322getdents_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	struct file *fp;
333	struct uio auio;
334	struct iovec aiov;
335	off_t off;
336	struct l_dirent *linux_dirent;
337	struct l_dirent64 *linux_dirent64;
338	int buflen, error, eofflag, nbytes, justone;
339	u_long *cookies = NULL, *cookiep;
340	int ncookies, vfslocked;
341
342	nbytes = args->count;
343	if (nbytes == 1) {
344		/* readdir(2) case. Always struct dirent. */
345		if (is64bit)
346			return (EINVAL);
347		nbytes = sizeof(*linux_dirent);
348		justone = 1;
349	} else
350		justone = 0;
351
352	if ((error = getvnode(td->td_proc->p_fd, args->fd, CAP_READ, &fp)) != 0)
353		return (error);
354
355	if ((fp->f_flag & FREAD) == 0) {
356		fdrop(fp, td);
357		return (EBADF);
358	}
359
360	vp = fp->f_vnode;
361	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
362	if (vp->v_type != VDIR) {
363		VFS_UNLOCK_GIANT(vfslocked);
364		fdrop(fp, td);
365		return (EINVAL);
366	}
367
368	off = fp->f_offset;
369
370	buflen = max(LINUX_DIRBLKSIZ, nbytes);
371	buflen = min(buflen, MAXBSIZE);
372	buf = malloc(buflen, M_TEMP, M_WAITOK);
373	lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO);
374	vn_lock(vp, LK_SHARED | LK_RETRY);
375
376	aiov.iov_base = buf;
377	aiov.iov_len = buflen;
378	auio.uio_iov = &aiov;
379	auio.uio_iovcnt = 1;
380	auio.uio_rw = UIO_READ;
381	auio.uio_segflg = UIO_SYSSPACE;
382	auio.uio_td = td;
383	auio.uio_resid = buflen;
384	auio.uio_offset = off;
385
386	if (cookies) {
387		free(cookies, M_TEMP);
388		cookies = NULL;
389	}
390
391#ifdef MAC
392	/*
393	 * Do directory search MAC check using non-cached credentials.
394	 */
395	if ((error = mac_vnode_check_readdir(td->td_ucred, vp)))
396		goto out;
397#endif /* MAC */
398	if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
399		 &cookies)))
400		goto out;
401
402	inp = buf;
403	outp = (caddr_t)args->dirent;
404	resid = nbytes;
405	if ((len = buflen - auio.uio_resid) <= 0)
406		goto eof;
407
408	cookiep = cookies;
409
410	if (cookies) {
411		/*
412		 * When using cookies, the vfs has the option of reading from
413		 * a different offset than that supplied (UFS truncates the
414		 * offset to a block boundary to make sure that it never reads
415		 * partway through a directory entry, even if the directory
416		 * has been compacted).
417		 */
418		while (len > 0 && ncookies > 0 && *cookiep <= off) {
419			bdp = (struct dirent *) inp;
420			len -= bdp->d_reclen;
421			inp += bdp->d_reclen;
422			cookiep++;
423			ncookies--;
424		}
425	}
426
427	while (len > 0) {
428		if (cookiep && ncookies == 0)
429			break;
430		bdp = (struct dirent *) inp;
431		reclen = bdp->d_reclen;
432		if (reclen & 3) {
433			error = EFAULT;
434			goto out;
435		}
436
437		if (bdp->d_fileno == 0) {
438			inp += reclen;
439			if (cookiep) {
440				off = *cookiep++;
441				ncookies--;
442			} else
443				off += reclen;
444
445			len -= reclen;
446			continue;
447		}
448
449		linuxreclen = (is64bit)
450		    ? LINUX_RECLEN64(bdp->d_namlen)
451		    : LINUX_RECLEN(bdp->d_namlen);
452
453		if (reclen > len || resid < linuxreclen) {
454			outp++;
455			break;
456		}
457
458		if (justone) {
459			/* readdir(2) case. */
460			linux_dirent = (struct l_dirent*)lbuf;
461			linux_dirent->d_ino = bdp->d_fileno;
462			linux_dirent->d_off = (l_off_t)linuxreclen;
463			linux_dirent->d_reclen = (l_ushort)bdp->d_namlen;
464			strlcpy(linux_dirent->d_name, bdp->d_name,
465			    linuxreclen - offsetof(struct l_dirent, d_name));
466			error = copyout(linux_dirent, outp, linuxreclen);
467		}
468		if (is64bit) {
469			linux_dirent64 = (struct l_dirent64*)lbuf;
470			linux_dirent64->d_ino = bdp->d_fileno;
471			linux_dirent64->d_off = (cookiep)
472			    ? (l_off_t)*cookiep
473			    : (l_off_t)(off + reclen);
474			linux_dirent64->d_reclen = (l_ushort)linuxreclen;
475			linux_dirent64->d_type = bdp->d_type;
476			strlcpy(linux_dirent64->d_name, bdp->d_name,
477			    linuxreclen - offsetof(struct l_dirent64, d_name));
478			error = copyout(linux_dirent64, outp, linuxreclen);
479		} else if (!justone) {
480			linux_dirent = (struct l_dirent*)lbuf;
481			linux_dirent->d_ino = bdp->d_fileno;
482			linux_dirent->d_off = (cookiep)
483			    ? (l_off_t)*cookiep
484			    : (l_off_t)(off + reclen);
485			linux_dirent->d_reclen = (l_ushort)linuxreclen;
486			/*
487			 * Copy d_type to last byte of l_dirent buffer
488			 */
489			lbuf[linuxreclen-1] = bdp->d_type;
490			strlcpy(linux_dirent->d_name, bdp->d_name,
491			    linuxreclen - offsetof(struct l_dirent, d_name)-1);
492			error = copyout(linux_dirent, outp, linuxreclen);
493		}
494
495		if (error)
496			goto out;
497
498		inp += reclen;
499		if (cookiep) {
500			off = *cookiep++;
501			ncookies--;
502		} else
503			off += reclen;
504
505		outp += linuxreclen;
506		resid -= linuxreclen;
507		len -= reclen;
508		if (justone)
509			break;
510	}
511
512	if (outp == (caddr_t)args->dirent) {
513		nbytes = resid;
514		goto eof;
515	}
516
517	fp->f_offset = off;
518	if (justone)
519		nbytes = resid + linuxreclen;
520
521eof:
522	td->td_retval[0] = nbytes - resid;
523
524out:
525	if (cookies)
526		free(cookies, M_TEMP);
527
528	VOP_UNLOCK(vp, 0);
529	VFS_UNLOCK_GIANT(vfslocked);
530	fdrop(fp, td);
531	free(buf, M_TEMP);
532	free(lbuf, M_TEMP);
533	return (error);
534}
535
536int
537linux_getdents(struct thread *td, struct linux_getdents_args *args)
538{
539
540#ifdef DEBUG
541	if (ldebug(getdents))
542		printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
543#endif
544
545	return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
546}
547
548int
549linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
550{
551
552#ifdef DEBUG
553	if (ldebug(getdents64))
554		printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
555#endif
556
557	return (getdents_common(td, args, 1));
558}
559
560/*
561 * These exist mainly for hooks for doing /compat/linux translation.
562 */
563
564int
565linux_access(struct thread *td, struct linux_access_args *args)
566{
567	char *path;
568	int error;
569
570	/* linux convention */
571	if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
572		return (EINVAL);
573
574	LCONVPATHEXIST(td, args->path, &path);
575
576#ifdef DEBUG
577	if (ldebug(access))
578		printf(ARGS(access, "%s, %d"), path, args->amode);
579#endif
580	error = kern_access(td, path, UIO_SYSSPACE, args->amode);
581	LFREEPATH(path);
582
583	return (error);
584}
585
586int
587linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
588{
589	char *path;
590	int error, dfd, flag;
591
592	if (args->flag & ~LINUX_AT_EACCESS)
593		return (EINVAL);
594	/* linux convention */
595	if (args->amode & ~(F_OK | X_OK | W_OK | R_OK))
596		return (EINVAL);
597
598	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
599	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
600
601#ifdef DEBUG
602	if (ldebug(access))
603		printf(ARGS(access, "%s, %d"), path, args->amode);
604#endif
605
606	flag = (args->flag & LINUX_AT_EACCESS) == 0 ? 0 : AT_EACCESS;
607	error = kern_accessat(td, dfd, path, UIO_SYSSPACE, flag, args->amode);
608	LFREEPATH(path);
609
610	return (error);
611}
612
613int
614linux_unlink(struct thread *td, struct linux_unlink_args *args)
615{
616	char *path;
617	int error;
618	struct stat st;
619
620	LCONVPATHEXIST(td, args->path, &path);
621
622#ifdef DEBUG
623	if (ldebug(unlink))
624		printf(ARGS(unlink, "%s"), path);
625#endif
626
627	error = kern_unlink(td, path, UIO_SYSSPACE);
628	if (error == EPERM)
629		/* Introduce POSIX noncompliant behaviour of Linux */
630		if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
631			if (S_ISDIR(st.st_mode))
632				error = EISDIR;
633	LFREEPATH(path);
634	return (error);
635}
636
637int
638linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
639{
640	char *path;
641	int error, dfd;
642	struct stat st;
643
644	if (args->flag & ~LINUX_AT_REMOVEDIR)
645		return (EINVAL);
646
647	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
648	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
649
650#ifdef DEBUG
651	if (ldebug(unlinkat))
652		printf(ARGS(unlinkat, "%s"), path);
653#endif
654
655	if (args->flag & LINUX_AT_REMOVEDIR)
656		error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
657	else
658		error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
659	if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
660		/* Introduce POSIX noncompliant behaviour of Linux */
661		if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
662		    UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode))
663			error = EISDIR;
664	}
665	LFREEPATH(path);
666	return (error);
667}
668int
669linux_chdir(struct thread *td, struct linux_chdir_args *args)
670{
671	char *path;
672	int error;
673
674	LCONVPATHEXIST(td, args->path, &path);
675
676#ifdef DEBUG
677	if (ldebug(chdir))
678		printf(ARGS(chdir, "%s"), path);
679#endif
680	error = kern_chdir(td, path, UIO_SYSSPACE);
681	LFREEPATH(path);
682	return (error);
683}
684
685int
686linux_chmod(struct thread *td, struct linux_chmod_args *args)
687{
688	char *path;
689	int error;
690
691	LCONVPATHEXIST(td, args->path, &path);
692
693#ifdef DEBUG
694	if (ldebug(chmod))
695		printf(ARGS(chmod, "%s, %d"), path, args->mode);
696#endif
697	error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
698	LFREEPATH(path);
699	return (error);
700}
701
702int
703linux_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
721int
722linux_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_mkdir(td, path, UIO_SYSSPACE, args->mode);
734	LFREEPATH(path);
735	return (error);
736}
737
738int
739linux_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
756int
757linux_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_rmdir(td, path, UIO_SYSSPACE);
769	LFREEPATH(path);
770	return (error);
771}
772
773int
774linux_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_rename(td, from, to, UIO_SYSSPACE);
792	LFREEPATH(from);
793	LFREEPATH(to);
794	return (error);
795}
796
797int
798linux_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
823int
824linux_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_symlink(td, path, to, UIO_SYSSPACE);
842	LFREEPATH(path);
843	LFREEPATH(to);
844	return (error);
845}
846
847int
848linux_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
873int
874linux_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_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
887	    args->count);
888	LFREEPATH(name);
889	return (error);
890}
891
892int
893linux_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
913int
914linux_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
931int
932linux_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}
948int
949linux_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
962int
963linux_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_link(td, path, to, UIO_SYSSPACE);
981	LFREEPATH(path);
982	LFREEPATH(to);
983	return (error);
984}
985
986int
987linux_linkat(struct thread *td, struct linux_linkat_args *args)
988{
989	char *path, *to;
990	int error, olddfd, newdfd, follow;
991
992	if (args->flag & ~LINUX_AT_SYMLINK_FOLLOW)
993		return (EINVAL);
994
995	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
996	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
997	LCONVPATHEXIST_AT(td, args->oldname, &path, olddfd);
998	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
999	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
1000	if (to == NULL) {
1001		LFREEPATH(path);
1002		return (error);
1003	}
1004
1005#ifdef DEBUG
1006	if (ldebug(linkat))
1007		printf(ARGS(linkat, "%i, %s, %i, %s, %i"), args->olddfd, path,
1008			args->newdfd, to, args->flag);
1009#endif
1010
1011	follow = (args->flag & LINUX_AT_SYMLINK_FOLLOW) == 0 ? NOFOLLOW :
1012	    FOLLOW;
1013	error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, follow);
1014	LFREEPATH(path);
1015	LFREEPATH(to);
1016	return (error);
1017}
1018
1019int
1020linux_fdatasync(td, uap)
1021	struct thread *td;
1022	struct linux_fdatasync_args *uap;
1023{
1024	struct fsync_args bsd;
1025
1026	bsd.fd = uap->fd;
1027	return sys_fsync(td, &bsd);
1028}
1029
1030int
1031linux_pread(td, uap)
1032	struct thread *td;
1033	struct linux_pread_args *uap;
1034{
1035	struct pread_args bsd;
1036	struct vnode *vp;
1037	int error;
1038
1039	bsd.fd = uap->fd;
1040	bsd.buf = uap->buf;
1041	bsd.nbyte = uap->nbyte;
1042	bsd.offset = uap->offset;
1043
1044	error = sys_pread(td, &bsd);
1045
1046	if (error == 0) {
1047   	   	/* This seems to violate POSIX but linux does it */
1048		if ((error = fgetvp(td, uap->fd, CAP_READ, &vp)) != 0)
1049   		   	return (error);
1050		if (vp->v_type == VDIR) {
1051   		   	vrele(vp);
1052			return (EISDIR);
1053		}
1054		vrele(vp);
1055	}
1056
1057	return (error);
1058}
1059
1060int
1061linux_pwrite(td, uap)
1062	struct thread *td;
1063	struct linux_pwrite_args *uap;
1064{
1065	struct pwrite_args bsd;
1066
1067	bsd.fd = uap->fd;
1068	bsd.buf = uap->buf;
1069	bsd.nbyte = uap->nbyte;
1070	bsd.offset = uap->offset;
1071	return sys_pwrite(td, &bsd);
1072}
1073
1074int
1075linux_mount(struct thread *td, struct linux_mount_args *args)
1076{
1077	struct ufs_args ufs;
1078	char fstypename[MFSNAMELEN];
1079	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1080	int error;
1081	int fsflags;
1082	void *fsdata;
1083
1084	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1085	    NULL);
1086	if (error)
1087		return (error);
1088	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1089	if (error)
1090		return (error);
1091	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1092	if (error)
1093		return (error);
1094
1095#ifdef DEBUG
1096	if (ldebug(mount))
1097		printf(ARGS(mount, "%s, %s, %s"),
1098		    fstypename, mntfromname, mntonname);
1099#endif
1100
1101	if (strcmp(fstypename, "ext2") == 0) {
1102		strcpy(fstypename, "ext2fs");
1103		fsdata = &ufs;
1104		ufs.fspec = mntfromname;
1105#define DEFAULT_ROOTID		-2
1106		ufs.export.ex_root = DEFAULT_ROOTID;
1107		ufs.export.ex_flags =
1108		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
1109	} else if (strcmp(fstypename, "proc") == 0) {
1110		strcpy(fstypename, "linprocfs");
1111		fsdata = NULL;
1112	} else if (strcmp(fstypename, "vfat") == 0) {
1113		strcpy(fstypename, "msdosfs");
1114		fsdata = NULL;
1115	} else {
1116		return (ENODEV);
1117	}
1118
1119	fsflags = 0;
1120
1121	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1122		/*
1123		 * Linux SYNC flag is not included; the closest equivalent
1124		 * FreeBSD has is !ASYNC, which is our default.
1125		 */
1126		if (args->rwflag & LINUX_MS_RDONLY)
1127			fsflags |= MNT_RDONLY;
1128		if (args->rwflag & LINUX_MS_NOSUID)
1129			fsflags |= MNT_NOSUID;
1130		if (args->rwflag & LINUX_MS_NOEXEC)
1131			fsflags |= MNT_NOEXEC;
1132		if (args->rwflag & LINUX_MS_REMOUNT)
1133			fsflags |= MNT_UPDATE;
1134	}
1135
1136	if (strcmp(fstypename, "linprocfs") == 0) {
1137		error = kernel_vmount(fsflags,
1138			"fstype", fstypename,
1139			"fspath", mntonname,
1140			NULL);
1141	} else if (strcmp(fstypename, "msdosfs") == 0) {
1142		error = kernel_vmount(fsflags,
1143			"fstype", fstypename,
1144			"fspath", mntonname,
1145			"from", mntfromname,
1146			NULL);
1147	} else
1148		error = EOPNOTSUPP;
1149	return (error);
1150}
1151
1152int
1153linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1154{
1155	struct linux_umount_args args2;
1156
1157	args2.path = args->path;
1158	args2.flags = 0;
1159	return (linux_umount(td, &args2));
1160}
1161
1162int
1163linux_umount(struct thread *td, struct linux_umount_args *args)
1164{
1165	struct unmount_args bsd;
1166
1167	bsd.path = args->path;
1168	bsd.flags = args->flags;	/* XXX correct? */
1169	return (sys_unmount(td, &bsd));
1170}
1171
1172/*
1173 * fcntl family of syscalls
1174 */
1175
1176struct l_flock {
1177	l_short		l_type;
1178	l_short		l_whence;
1179	l_off_t		l_start;
1180	l_off_t		l_len;
1181	l_pid_t		l_pid;
1182}
1183#if defined(__amd64__) && defined(COMPAT_LINUX32)
1184__packed
1185#endif
1186;
1187
1188static void
1189linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1190{
1191	switch (linux_flock->l_type) {
1192	case LINUX_F_RDLCK:
1193		bsd_flock->l_type = F_RDLCK;
1194		break;
1195	case LINUX_F_WRLCK:
1196		bsd_flock->l_type = F_WRLCK;
1197		break;
1198	case LINUX_F_UNLCK:
1199		bsd_flock->l_type = F_UNLCK;
1200		break;
1201	default:
1202		bsd_flock->l_type = -1;
1203		break;
1204	}
1205	bsd_flock->l_whence = linux_flock->l_whence;
1206	bsd_flock->l_start = (off_t)linux_flock->l_start;
1207	bsd_flock->l_len = (off_t)linux_flock->l_len;
1208	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1209	bsd_flock->l_sysid = 0;
1210}
1211
1212static void
1213bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1214{
1215	switch (bsd_flock->l_type) {
1216	case F_RDLCK:
1217		linux_flock->l_type = LINUX_F_RDLCK;
1218		break;
1219	case F_WRLCK:
1220		linux_flock->l_type = LINUX_F_WRLCK;
1221		break;
1222	case F_UNLCK:
1223		linux_flock->l_type = LINUX_F_UNLCK;
1224		break;
1225	}
1226	linux_flock->l_whence = bsd_flock->l_whence;
1227	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1228	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1229	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1230}
1231
1232#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1233struct l_flock64 {
1234	l_short		l_type;
1235	l_short		l_whence;
1236	l_loff_t	l_start;
1237	l_loff_t	l_len;
1238	l_pid_t		l_pid;
1239}
1240#if defined(__amd64__) && defined(COMPAT_LINUX32)
1241__packed
1242#endif
1243;
1244
1245static void
1246linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1247{
1248	switch (linux_flock->l_type) {
1249	case LINUX_F_RDLCK:
1250		bsd_flock->l_type = F_RDLCK;
1251		break;
1252	case LINUX_F_WRLCK:
1253		bsd_flock->l_type = F_WRLCK;
1254		break;
1255	case LINUX_F_UNLCK:
1256		bsd_flock->l_type = F_UNLCK;
1257		break;
1258	default:
1259		bsd_flock->l_type = -1;
1260		break;
1261	}
1262	bsd_flock->l_whence = linux_flock->l_whence;
1263	bsd_flock->l_start = (off_t)linux_flock->l_start;
1264	bsd_flock->l_len = (off_t)linux_flock->l_len;
1265	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1266	bsd_flock->l_sysid = 0;
1267}
1268
1269static void
1270bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1271{
1272	switch (bsd_flock->l_type) {
1273	case F_RDLCK:
1274		linux_flock->l_type = LINUX_F_RDLCK;
1275		break;
1276	case F_WRLCK:
1277		linux_flock->l_type = LINUX_F_WRLCK;
1278		break;
1279	case F_UNLCK:
1280		linux_flock->l_type = LINUX_F_UNLCK;
1281		break;
1282	}
1283	linux_flock->l_whence = bsd_flock->l_whence;
1284	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1285	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1286	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1287}
1288#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1289
1290static int
1291fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
1292{
1293	struct l_flock linux_flock;
1294	struct flock bsd_flock;
1295	struct file *fp;
1296	long arg;
1297	int error, result;
1298
1299	switch (args->cmd) {
1300	case LINUX_F_DUPFD:
1301		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1302
1303	case LINUX_F_GETFD:
1304		return (kern_fcntl(td, args->fd, F_GETFD, 0));
1305
1306	case LINUX_F_SETFD:
1307		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1308
1309	case LINUX_F_GETFL:
1310		error = kern_fcntl(td, args->fd, F_GETFL, 0);
1311		result = td->td_retval[0];
1312		td->td_retval[0] = 0;
1313		if (result & O_RDONLY)
1314			td->td_retval[0] |= LINUX_O_RDONLY;
1315		if (result & O_WRONLY)
1316			td->td_retval[0] |= LINUX_O_WRONLY;
1317		if (result & O_RDWR)
1318			td->td_retval[0] |= LINUX_O_RDWR;
1319		if (result & O_NDELAY)
1320			td->td_retval[0] |= LINUX_O_NONBLOCK;
1321		if (result & O_APPEND)
1322			td->td_retval[0] |= LINUX_O_APPEND;
1323		if (result & O_FSYNC)
1324			td->td_retval[0] |= LINUX_O_SYNC;
1325		if (result & O_ASYNC)
1326			td->td_retval[0] |= LINUX_FASYNC;
1327#ifdef LINUX_O_NOFOLLOW
1328		if (result & O_NOFOLLOW)
1329			td->td_retval[0] |= LINUX_O_NOFOLLOW;
1330#endif
1331#ifdef LINUX_O_DIRECT
1332		if (result & O_DIRECT)
1333			td->td_retval[0] |= LINUX_O_DIRECT;
1334#endif
1335		return (error);
1336
1337	case LINUX_F_SETFL:
1338		arg = 0;
1339		if (args->arg & LINUX_O_NDELAY)
1340			arg |= O_NONBLOCK;
1341		if (args->arg & LINUX_O_APPEND)
1342			arg |= O_APPEND;
1343		if (args->arg & LINUX_O_SYNC)
1344			arg |= O_FSYNC;
1345		if (args->arg & LINUX_FASYNC)
1346			arg |= O_ASYNC;
1347#ifdef LINUX_O_NOFOLLOW
1348		if (args->arg & LINUX_O_NOFOLLOW)
1349			arg |= O_NOFOLLOW;
1350#endif
1351#ifdef LINUX_O_DIRECT
1352		if (args->arg & LINUX_O_DIRECT)
1353			arg |= O_DIRECT;
1354#endif
1355		return (kern_fcntl(td, args->fd, F_SETFL, arg));
1356
1357	case LINUX_F_GETLK:
1358		error = copyin((void *)args->arg, &linux_flock,
1359		    sizeof(linux_flock));
1360		if (error)
1361			return (error);
1362		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1363		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1364		if (error)
1365			return (error);
1366		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1367		return (copyout(&linux_flock, (void *)args->arg,
1368		    sizeof(linux_flock)));
1369
1370	case LINUX_F_SETLK:
1371		error = copyin((void *)args->arg, &linux_flock,
1372		    sizeof(linux_flock));
1373		if (error)
1374			return (error);
1375		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1376		return (kern_fcntl(td, args->fd, F_SETLK,
1377		    (intptr_t)&bsd_flock));
1378
1379	case LINUX_F_SETLKW:
1380		error = copyin((void *)args->arg, &linux_flock,
1381		    sizeof(linux_flock));
1382		if (error)
1383			return (error);
1384		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1385		return (kern_fcntl(td, args->fd, F_SETLKW,
1386		     (intptr_t)&bsd_flock));
1387
1388	case LINUX_F_GETOWN:
1389		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1390
1391	case LINUX_F_SETOWN:
1392		/*
1393		 * XXX some Linux applications depend on F_SETOWN having no
1394		 * significant effect for pipes (SIGIO is not delivered for
1395		 * pipes under Linux-2.2.35 at least).
1396		 */
1397		error = fget(td, args->fd, CAP_FCNTL, &fp);
1398		if (error)
1399			return (error);
1400		if (fp->f_type == DTYPE_PIPE) {
1401			fdrop(fp, td);
1402			return (EINVAL);
1403		}
1404		fdrop(fp, td);
1405
1406		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1407	}
1408
1409	return (EINVAL);
1410}
1411
1412int
1413linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1414{
1415	struct linux_fcntl64_args args64;
1416
1417#ifdef DEBUG
1418	if (ldebug(fcntl))
1419		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1420#endif
1421
1422	args64.fd = args->fd;
1423	args64.cmd = args->cmd;
1424	args64.arg = args->arg;
1425	return (fcntl_common(td, &args64));
1426}
1427
1428#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1429int
1430linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1431{
1432	struct l_flock64 linux_flock;
1433	struct flock bsd_flock;
1434	int error;
1435
1436#ifdef DEBUG
1437	if (ldebug(fcntl64))
1438		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1439#endif
1440
1441	switch (args->cmd) {
1442	case LINUX_F_GETLK64:
1443		error = copyin((void *)args->arg, &linux_flock,
1444		    sizeof(linux_flock));
1445		if (error)
1446			return (error);
1447		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1448		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1449		if (error)
1450			return (error);
1451		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1452		return (copyout(&linux_flock, (void *)args->arg,
1453			    sizeof(linux_flock)));
1454
1455	case LINUX_F_SETLK64:
1456		error = copyin((void *)args->arg, &linux_flock,
1457		    sizeof(linux_flock));
1458		if (error)
1459			return (error);
1460		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1461		return (kern_fcntl(td, args->fd, F_SETLK,
1462		    (intptr_t)&bsd_flock));
1463
1464	case LINUX_F_SETLKW64:
1465		error = copyin((void *)args->arg, &linux_flock,
1466		    sizeof(linux_flock));
1467		if (error)
1468			return (error);
1469		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1470		return (kern_fcntl(td, args->fd, F_SETLKW,
1471		    (intptr_t)&bsd_flock));
1472	}
1473
1474	return (fcntl_common(td, args));
1475}
1476#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1477
1478int
1479linux_chown(struct thread *td, struct linux_chown_args *args)
1480{
1481	char *path;
1482	int error;
1483
1484	LCONVPATHEXIST(td, args->path, &path);
1485
1486#ifdef DEBUG
1487	if (ldebug(chown))
1488		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1489#endif
1490	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1491	LFREEPATH(path);
1492	return (error);
1493}
1494
1495int
1496linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1497{
1498	char *path;
1499	int error, dfd, flag;
1500
1501	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1502		return (EINVAL);
1503
1504	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD :  args->dfd;
1505	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1506
1507#ifdef DEBUG
1508	if (ldebug(fchownat))
1509		printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1510#endif
1511
1512	flag = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1513	    AT_SYMLINK_NOFOLLOW;
1514	error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1515	    flag);
1516	LFREEPATH(path);
1517	return (error);
1518}
1519
1520int
1521linux_lchown(struct thread *td, struct linux_lchown_args *args)
1522{
1523	char *path;
1524	int error;
1525
1526	LCONVPATHEXIST(td, args->path, &path);
1527
1528#ifdef DEBUG
1529	if (ldebug(lchown))
1530		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1531#endif
1532	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1533	LFREEPATH(path);
1534	return (error);
1535}
1536
1537static int
1538convert_fadvice(int advice)
1539{
1540	switch (advice) {
1541	case LINUX_POSIX_FADV_NORMAL:
1542		return (POSIX_FADV_NORMAL);
1543	case LINUX_POSIX_FADV_RANDOM:
1544		return (POSIX_FADV_RANDOM);
1545	case LINUX_POSIX_FADV_SEQUENTIAL:
1546		return (POSIX_FADV_SEQUENTIAL);
1547	case LINUX_POSIX_FADV_WILLNEED:
1548		return (POSIX_FADV_WILLNEED);
1549	case LINUX_POSIX_FADV_DONTNEED:
1550		return (POSIX_FADV_DONTNEED);
1551	case LINUX_POSIX_FADV_NOREUSE:
1552		return (POSIX_FADV_NOREUSE);
1553	default:
1554		return (-1);
1555	}
1556}
1557
1558int
1559linux_fadvise64(struct thread *td, struct linux_fadvise64_args *args)
1560{
1561	int advice;
1562
1563	advice = convert_fadvice(args->advice);
1564	if (advice == -1)
1565		return (EINVAL);
1566	return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1567	    advice));
1568}
1569
1570int
1571linux_fadvise64_64(struct thread *td, struct linux_fadvise64_64_args *args)
1572{
1573	int advice;
1574
1575	advice = convert_fadvice(args->advice);
1576	if (advice == -1)
1577		return (EINVAL);
1578	return (kern_posix_fadvise(td, args->fd, args->offset, args->len,
1579	    advice));
1580}
1581
1582int
1583linux_pipe(struct thread *td, struct linux_pipe_args *args)
1584{
1585	int fildes[2];
1586	int error;
1587
1588#ifdef DEBUG
1589	if (ldebug(pipe))
1590		printf(ARGS(pipe, "*"));
1591#endif
1592
1593	error = do_pipe(td, fildes, 0);
1594	if (error)
1595		return (error);
1596
1597	/* XXX: Close descriptors on error. */
1598	return (copyout(fildes, args->pipefds, sizeof(fildes)));
1599}
1600
1601int
1602linux_pipe2(struct thread *td, struct linux_pipe2_args *args)
1603{
1604	int fildes[2];
1605	int error, flags;
1606
1607#ifdef DEBUG
1608	if (ldebug(pipe2))
1609		printf(ARGS(pipe2, "*, %d"), args->flags);
1610#endif
1611
1612	if ((args->flags & ~(LINUX_O_NONBLOCK | LINUX_O_CLOEXEC)) != 0)
1613		return (EINVAL);
1614
1615	flags = 0;
1616	if ((args->flags & LINUX_O_NONBLOCK) != 0)
1617		flags |= O_NONBLOCK;
1618	if ((args->flags & LINUX_O_CLOEXEC) != 0)
1619		flags |= O_CLOEXEC;
1620	error = do_pipe(td, fildes, flags);
1621	if (error)
1622		return (error);
1623
1624	/* XXX: Close descriptors on error. */
1625	return (copyout(fildes, args->pipefds, sizeof(fildes)));
1626}
1627