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