xref: /freebsd/sys/compat/linux/linux_file.c (revision 3157ba21)
1 /*-
2  * Copyright (c) 1994-1995 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/dirent.h>
38 #include <sys/fcntl.h>
39 #include <sys/file.h>
40 #include <sys/filedesc.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/mutex.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/stat.h>
48 #include <sys/sx.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.h>
51 #include <sys/tty.h>
52 #include <sys/unistd.h>
53 #include <sys/vnode.h>
54 
55 #include <security/mac/mac_framework.h>
56 
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/ufsmount.h>
60 
61 #ifdef COMPAT_LINUX32
62 #include <machine/../linux32/linux.h>
63 #include <machine/../linux32/linux32_proto.h>
64 #else
65 #include <machine/../linux/linux.h>
66 #include <machine/../linux/linux_proto.h>
67 #endif
68 #include <compat/linux/linux_util.h>
69 #include <compat/linux/linux_file.h>
70 
71 int
72 linux_creat(struct thread *td, struct linux_creat_args *args)
73 {
74     char *path;
75     int error;
76 
77     LCONVPATHEXIST(td, args->path, &path);
78 
79 #ifdef DEBUG
80 	if (ldebug(creat))
81 		printf(ARGS(creat, "%s, %d"), path, args->mode);
82 #endif
83     error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
84 	args->mode);
85     LFREEPATH(path);
86     return (error);
87 }
88 
89 
90 static int
91 linux_common_open(struct thread *td, int dirfd, char *path, int l_flags, int mode)
92 {
93     struct proc *p = td->td_proc;
94     struct file *fp;
95     int fd;
96     int bsd_flags, error;
97 
98     bsd_flags = 0;
99     switch (l_flags & LINUX_O_ACCMODE) {
100     case LINUX_O_WRONLY:
101 	bsd_flags |= O_WRONLY;
102 	break;
103     case LINUX_O_RDWR:
104 	bsd_flags |= O_RDWR;
105 	break;
106     default:
107 	bsd_flags |= O_RDONLY;
108     }
109     if (l_flags & LINUX_O_NDELAY)
110 	bsd_flags |= O_NONBLOCK;
111     if (l_flags & LINUX_O_APPEND)
112 	bsd_flags |= O_APPEND;
113     if (l_flags & LINUX_O_SYNC)
114 	bsd_flags |= O_FSYNC;
115     if (l_flags & LINUX_O_NONBLOCK)
116 	bsd_flags |= O_NONBLOCK;
117     if (l_flags & LINUX_FASYNC)
118 	bsd_flags |= O_ASYNC;
119     if (l_flags & LINUX_O_CREAT)
120 	bsd_flags |= O_CREAT;
121     if (l_flags & LINUX_O_TRUNC)
122 	bsd_flags |= O_TRUNC;
123     if (l_flags & LINUX_O_EXCL)
124 	bsd_flags |= O_EXCL;
125     if (l_flags & LINUX_O_NOCTTY)
126 	bsd_flags |= O_NOCTTY;
127     if (l_flags & LINUX_O_DIRECT)
128 	bsd_flags |= O_DIRECT;
129     if (l_flags & LINUX_O_NOFOLLOW)
130 	bsd_flags |= O_NOFOLLOW;
131     if (l_flags & LINUX_O_DIRECTORY)
132 	bsd_flags |= O_DIRECTORY;
133     /* XXX LINUX_O_NOATIME: unable to be easily implemented. */
134 
135     error = kern_openat(td, dirfd, path, UIO_SYSSPACE, bsd_flags, mode);
136 
137     if (!error) {
138 	    fd = td->td_retval[0];
139 	    /*
140 	     * XXX In between kern_open() and fget(), another process
141 	     * having the same filedesc could use that fd without
142 	     * checking below.
143 	     */
144 	    error = fget(td, fd, &fp);
145 	    if (!error) {
146 		    sx_slock(&proctree_lock);
147 		    PROC_LOCK(p);
148 		    if (!(bsd_flags & O_NOCTTY) &&
149 			SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
150 			    PROC_UNLOCK(p);
151 			    sx_unlock(&proctree_lock);
152 			    if (fp->f_type == DTYPE_VNODE)
153 				    (void) fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0,
154 					     td->td_ucred, td);
155 		    } else {
156 			    PROC_UNLOCK(p);
157 			    sx_sunlock(&proctree_lock);
158 		    }
159 		    fdrop(fp, td);
160 		    /*
161 		     * XXX as above, fdrop()/kern_close() pair is racy.
162 		     */
163 		    if (error)
164 			    kern_close(td, fd);
165 	    }
166     }
167 
168 #ifdef DEBUG
169     if (ldebug(open))
170 	    printf(LMSG("open returns error %d"), error);
171 #endif
172     LFREEPATH(path);
173     return (error);
174 }
175 
176 int
177 linux_openat(struct thread *td, struct linux_openat_args *args)
178 {
179 	char *path;
180 	int dfd;
181 
182 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
183 	if (args->flags & LINUX_O_CREAT)
184 		LCONVPATH_AT(td, args->filename, &path, 1, dfd);
185 	else
186 		LCONVPATH_AT(td, args->filename, &path, 0, dfd);
187 #ifdef DEBUG
188 	if (ldebug(openat))
189 		printf(ARGS(openat, "%i, %s, 0x%x, 0x%x"), args->dfd,
190 		    path, args->flags, args->mode);
191 #endif
192 	return (linux_common_open(td, dfd, path, args->flags, args->mode));
193 }
194 
195 int
196 linux_open(struct thread *td, struct linux_open_args *args)
197 {
198     char *path;
199 
200     if (args->flags & LINUX_O_CREAT)
201 	LCONVPATHCREAT(td, args->path, &path);
202     else
203 	LCONVPATHEXIST(td, args->path, &path);
204 
205 #ifdef DEBUG
206 	if (ldebug(open))
207 		printf(ARGS(open, "%s, 0x%x, 0x%x"),
208 		    path, args->flags, args->mode);
209 #endif
210 
211 	return (linux_common_open(td, AT_FDCWD, path, args->flags, args->mode));
212 }
213 
214 int
215 linux_lseek(struct thread *td, struct linux_lseek_args *args)
216 {
217 
218     struct lseek_args /* {
219 	int fd;
220 	int pad;
221 	off_t offset;
222 	int whence;
223     } */ tmp_args;
224     int error;
225 
226 #ifdef DEBUG
227 	if (ldebug(lseek))
228 		printf(ARGS(lseek, "%d, %ld, %d"),
229 		    args->fdes, (long)args->off, args->whence);
230 #endif
231     tmp_args.fd = args->fdes;
232     tmp_args.offset = (off_t)args->off;
233     tmp_args.whence = args->whence;
234     error = lseek(td, &tmp_args);
235     return error;
236 }
237 
238 int
239 linux_llseek(struct thread *td, struct linux_llseek_args *args)
240 {
241 	struct lseek_args bsd_args;
242 	int error;
243 	off_t off;
244 
245 #ifdef DEBUG
246 	if (ldebug(llseek))
247 		printf(ARGS(llseek, "%d, %d:%d, %d"),
248 		    args->fd, args->ohigh, args->olow, args->whence);
249 #endif
250 	off = (args->olow) | (((off_t) args->ohigh) << 32);
251 
252 	bsd_args.fd = args->fd;
253 	bsd_args.offset = off;
254 	bsd_args.whence = args->whence;
255 
256 	if ((error = lseek(td, &bsd_args)))
257 		return error;
258 
259 	if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
260 		return error;
261 
262 	td->td_retval[0] = 0;
263 	return 0;
264 }
265 
266 int
267 linux_readdir(struct thread *td, struct linux_readdir_args *args)
268 {
269 	struct linux_getdents_args lda;
270 
271 	lda.fd = args->fd;
272 	lda.dent = args->dent;
273 	lda.count = 1;
274 	return linux_getdents(td, &lda);
275 }
276 
277 /*
278  * Note that linux_getdents(2) and linux_getdents64(2) have the same
279  * arguments. They only differ in the definition of struct dirent they
280  * operate on. We use this to common the code, with the exception of
281  * accessing struct dirent. Note that linux_readdir(2) is implemented
282  * by means of linux_getdents(2). In this case we never operate on
283  * struct dirent64 and thus don't need to handle it...
284  */
285 
286 struct l_dirent {
287 	l_ulong		d_ino;
288 	l_off_t		d_off;
289 	l_ushort	d_reclen;
290 	char		d_name[LINUX_NAME_MAX + 1];
291 };
292 
293 struct l_dirent64 {
294 	uint64_t	d_ino;
295 	int64_t		d_off;
296 	l_ushort	d_reclen;
297 	u_char		d_type;
298 	char		d_name[LINUX_NAME_MAX + 1];
299 };
300 
301 /*
302  * Linux uses the last byte in the dirent buffer to store d_type,
303  * at least glibc-2.7 requires it. That is why l_dirent is padded with 2 bytes.
304  */
305 #define LINUX_RECLEN(namlen)						\
306     roundup((offsetof(struct l_dirent, d_name) + (namlen) + 2),		\
307     sizeof(l_ulong))
308 
309 #define LINUX_RECLEN64(namlen)						\
310     roundup((offsetof(struct l_dirent64, d_name) + (namlen) + 1),	\
311     sizeof(uint64_t))
312 
313 #define LINUX_MAXRECLEN		max(LINUX_RECLEN(LINUX_NAME_MAX),	\
314 				    LINUX_RECLEN64(LINUX_NAME_MAX))
315 #define	LINUX_DIRBLKSIZ		512
316 
317 static int
318 getdents_common(struct thread *td, struct linux_getdents64_args *args,
319     int is64bit)
320 {
321 	struct dirent *bdp;
322 	struct vnode *vp;
323 	caddr_t inp, buf;		/* BSD-format */
324 	int len, reclen;		/* BSD-format */
325 	caddr_t outp;			/* Linux-format */
326 	int resid, linuxreclen=0;	/* Linux-format */
327 	caddr_t lbuf;			/* Linux-format */
328 	struct file *fp;
329 	struct uio auio;
330 	struct iovec aiov;
331 	off_t off;
332 	struct l_dirent *linux_dirent;
333 	struct l_dirent64 *linux_dirent64;
334 	int buflen, error, eofflag, nbytes, justone;
335 	u_long *cookies = NULL, *cookiep;
336 	int ncookies, vfslocked;
337 
338 	nbytes = args->count;
339 	if (nbytes == 1) {
340 		/* readdir(2) case. Always struct dirent. */
341 		if (is64bit)
342 			return (EINVAL);
343 		nbytes = sizeof(*linux_dirent);
344 		justone = 1;
345 	} else
346 		justone = 0;
347 
348 	if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
349 		return (error);
350 
351 	if ((fp->f_flag & FREAD) == 0) {
352 		fdrop(fp, td);
353 		return (EBADF);
354 	}
355 
356 	vp = fp->f_vnode;
357 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
358 	if (vp->v_type != VDIR) {
359 		VFS_UNLOCK_GIANT(vfslocked);
360 		fdrop(fp, td);
361 		return (EINVAL);
362 	}
363 
364 	off = fp->f_offset;
365 
366 	buflen = max(LINUX_DIRBLKSIZ, nbytes);
367 	buflen = min(buflen, MAXBSIZE);
368 	buf = malloc(buflen, M_TEMP, M_WAITOK);
369 	lbuf = malloc(LINUX_MAXRECLEN, M_TEMP, M_WAITOK | M_ZERO);
370 	vn_lock(vp, LK_SHARED | LK_RETRY);
371 
372 again:
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 		goto again;
511 
512 	fp->f_offset = off;
513 	if (justone)
514 		nbytes = resid + linuxreclen;
515 
516 eof:
517 	td->td_retval[0] = nbytes - resid;
518 
519 out:
520 	if (cookies)
521 		free(cookies, M_TEMP);
522 
523 	VOP_UNLOCK(vp, 0);
524 	VFS_UNLOCK_GIANT(vfslocked);
525 	fdrop(fp, td);
526 	free(buf, M_TEMP);
527 	free(lbuf, M_TEMP);
528 	return (error);
529 }
530 
531 int
532 linux_getdents(struct thread *td, struct linux_getdents_args *args)
533 {
534 
535 #ifdef DEBUG
536 	if (ldebug(getdents))
537 		printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
538 #endif
539 
540 	return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
541 }
542 
543 int
544 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
545 {
546 
547 #ifdef DEBUG
548 	if (ldebug(getdents64))
549 		printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
550 #endif
551 
552 	return (getdents_common(td, args, 1));
553 }
554 
555 /*
556  * These exist mainly for hooks for doing /compat/linux translation.
557  */
558 
559 int
560 linux_access(struct thread *td, struct linux_access_args *args)
561 {
562 	char *path;
563 	int error;
564 
565 	/* linux convention */
566 	if (args->flags & ~(F_OK | X_OK | W_OK | R_OK))
567 		return (EINVAL);
568 
569 	LCONVPATHEXIST(td, args->path, &path);
570 
571 #ifdef DEBUG
572 	if (ldebug(access))
573 		printf(ARGS(access, "%s, %d"), path, args->flags);
574 #endif
575 	error = kern_access(td, path, UIO_SYSSPACE, args->flags);
576 	LFREEPATH(path);
577 
578 	return (error);
579 }
580 
581 int
582 linux_faccessat(struct thread *td, struct linux_faccessat_args *args)
583 {
584 	char *path;
585 	int error, dfd;
586 
587 	/* linux convention */
588 	if (args->mode & ~(F_OK | X_OK | W_OK | R_OK))
589 		return (EINVAL);
590 
591 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
592 	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
593 
594 #ifdef DEBUG
595 	if (ldebug(access))
596 		printf(ARGS(access, "%s, %d"), path, args->mode);
597 #endif
598 
599 	error = kern_accessat(td, dfd, path, UIO_SYSSPACE, 0 /* XXX */,
600 	    args->mode);
601 	LFREEPATH(path);
602 
603 	return (error);
604 }
605 
606 int
607 linux_unlink(struct thread *td, struct linux_unlink_args *args)
608 {
609 	char *path;
610 	int error;
611 	struct stat st;
612 
613 	LCONVPATHEXIST(td, args->path, &path);
614 
615 #ifdef DEBUG
616 	if (ldebug(unlink))
617 		printf(ARGS(unlink, "%s"), path);
618 #endif
619 
620 	error = kern_unlink(td, path, UIO_SYSSPACE);
621 	if (error == EPERM)
622 		/* Introduce POSIX noncompliant behaviour of Linux */
623 		if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
624 			if (S_ISDIR(st.st_mode))
625 				error = EISDIR;
626 	LFREEPATH(path);
627 	return (error);
628 }
629 
630 int
631 linux_unlinkat(struct thread *td, struct linux_unlinkat_args *args)
632 {
633 	char *path;
634 	int error, dfd;
635 	struct stat st;
636 
637 	if (args->flag & ~LINUX_AT_REMOVEDIR)
638 		return (EINVAL);
639 
640 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
641 	LCONVPATHEXIST_AT(td, args->pathname, &path, dfd);
642 
643 #ifdef DEBUG
644 	if (ldebug(unlinkat))
645 		printf(ARGS(unlinkat, "%s"), path);
646 #endif
647 
648 	if (args->flag & LINUX_AT_REMOVEDIR)
649 		error = kern_rmdirat(td, dfd, path, UIO_SYSSPACE);
650 	else
651 		error = kern_unlinkat(td, dfd, path, UIO_SYSSPACE, 0);
652 	if (error == EPERM && !(args->flag & LINUX_AT_REMOVEDIR)) {
653 		/* Introduce POSIX noncompliant behaviour of Linux */
654 		if (kern_statat(td, AT_SYMLINK_NOFOLLOW, dfd, path,
655 		    UIO_SYSSPACE, &st) == 0 && S_ISDIR(st.st_mode))
656 			error = EISDIR;
657 	}
658 	LFREEPATH(path);
659 	return (error);
660 }
661 int
662 linux_chdir(struct thread *td, struct linux_chdir_args *args)
663 {
664 	char *path;
665 	int error;
666 
667 	LCONVPATHEXIST(td, args->path, &path);
668 
669 #ifdef DEBUG
670 	if (ldebug(chdir))
671 		printf(ARGS(chdir, "%s"), path);
672 #endif
673 	error = kern_chdir(td, path, UIO_SYSSPACE);
674 	LFREEPATH(path);
675 	return (error);
676 }
677 
678 int
679 linux_chmod(struct thread *td, struct linux_chmod_args *args)
680 {
681 	char *path;
682 	int error;
683 
684 	LCONVPATHEXIST(td, args->path, &path);
685 
686 #ifdef DEBUG
687 	if (ldebug(chmod))
688 		printf(ARGS(chmod, "%s, %d"), path, args->mode);
689 #endif
690 	error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
691 	LFREEPATH(path);
692 	return (error);
693 }
694 
695 int
696 linux_fchmodat(struct thread *td, struct linux_fchmodat_args *args)
697 {
698 	char *path;
699 	int error, dfd;
700 
701 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
702 	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
703 
704 #ifdef DEBUG
705 	if (ldebug(fchmodat))
706 		printf(ARGS(fchmodat, "%s, %d"), path, args->mode);
707 #endif
708 
709 	error = kern_fchmodat(td, dfd, path, UIO_SYSSPACE, args->mode, 0);
710 	LFREEPATH(path);
711 	return (error);
712 }
713 
714 int
715 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
716 {
717 	char *path;
718 	int error;
719 
720 	LCONVPATHCREAT(td, args->path, &path);
721 
722 #ifdef DEBUG
723 	if (ldebug(mkdir))
724 		printf(ARGS(mkdir, "%s, %d"), path, args->mode);
725 #endif
726 	error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
727 	LFREEPATH(path);
728 	return (error);
729 }
730 
731 int
732 linux_mkdirat(struct thread *td, struct linux_mkdirat_args *args)
733 {
734 	char *path;
735 	int error, dfd;
736 
737 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
738 	LCONVPATHCREAT_AT(td, args->pathname, &path, dfd);
739 
740 #ifdef DEBUG
741 	if (ldebug(mkdirat))
742 		printf(ARGS(mkdirat, "%s, %d"), path, args->mode);
743 #endif
744 	error = kern_mkdirat(td, dfd, path, UIO_SYSSPACE, args->mode);
745 	LFREEPATH(path);
746 	return (error);
747 }
748 
749 int
750 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
751 {
752 	char *path;
753 	int error;
754 
755 	LCONVPATHEXIST(td, args->path, &path);
756 
757 #ifdef DEBUG
758 	if (ldebug(rmdir))
759 		printf(ARGS(rmdir, "%s"), path);
760 #endif
761 	error = kern_rmdir(td, path, UIO_SYSSPACE);
762 	LFREEPATH(path);
763 	return (error);
764 }
765 
766 int
767 linux_rename(struct thread *td, struct linux_rename_args *args)
768 {
769 	char *from, *to;
770 	int error;
771 
772 	LCONVPATHEXIST(td, args->from, &from);
773 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
774 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
775 	if (to == NULL) {
776 		LFREEPATH(from);
777 		return (error);
778 	}
779 
780 #ifdef DEBUG
781 	if (ldebug(rename))
782 		printf(ARGS(rename, "%s, %s"), from, to);
783 #endif
784 	error = kern_rename(td, from, to, UIO_SYSSPACE);
785 	LFREEPATH(from);
786 	LFREEPATH(to);
787 	return (error);
788 }
789 
790 int
791 linux_renameat(struct thread *td, struct linux_renameat_args *args)
792 {
793 	char *from, *to;
794 	int error, olddfd, newdfd;
795 
796 	olddfd = (args->olddfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->olddfd;
797 	newdfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
798 	LCONVPATHEXIST_AT(td, args->oldname, &from, olddfd);
799 	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
800 	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, newdfd);
801 	if (to == NULL) {
802 		LFREEPATH(from);
803 		return (error);
804 	}
805 
806 #ifdef DEBUG
807 	if (ldebug(renameat))
808 		printf(ARGS(renameat, "%s, %s"), from, to);
809 #endif
810 	error = kern_renameat(td, olddfd, from, newdfd, to, UIO_SYSSPACE);
811 	LFREEPATH(from);
812 	LFREEPATH(to);
813 	return (error);
814 }
815 
816 int
817 linux_symlink(struct thread *td, struct linux_symlink_args *args)
818 {
819 	char *path, *to;
820 	int error;
821 
822 	LCONVPATHEXIST(td, args->path, &path);
823 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
824 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
825 	if (to == NULL) {
826 		LFREEPATH(path);
827 		return (error);
828 	}
829 
830 #ifdef DEBUG
831 	if (ldebug(symlink))
832 		printf(ARGS(symlink, "%s, %s"), path, to);
833 #endif
834 	error = kern_symlink(td, path, to, UIO_SYSSPACE);
835 	LFREEPATH(path);
836 	LFREEPATH(to);
837 	return (error);
838 }
839 
840 int
841 linux_symlinkat(struct thread *td, struct linux_symlinkat_args *args)
842 {
843 	char *path, *to;
844 	int error, dfd;
845 
846 	dfd = (args->newdfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->newdfd;
847 	LCONVPATHEXIST_AT(td, args->oldname, &path, dfd);
848 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
849 	error = linux_emul_convpath(td, args->newname, UIO_USERSPACE, &to, 1, dfd);
850 	if (to == NULL) {
851 		LFREEPATH(path);
852 		return (error);
853 	}
854 
855 #ifdef DEBUG
856 	if (ldebug(symlinkat))
857 		printf(ARGS(symlinkat, "%s, %s"), path, to);
858 #endif
859 
860 	error = kern_symlinkat(td, path, dfd, to, UIO_SYSSPACE);
861 	LFREEPATH(path);
862 	LFREEPATH(to);
863 	return (error);
864 }
865 
866 int
867 linux_readlink(struct thread *td, struct linux_readlink_args *args)
868 {
869 	char *name;
870 	int error;
871 
872 	LCONVPATHEXIST(td, args->name, &name);
873 
874 #ifdef DEBUG
875 	if (ldebug(readlink))
876 		printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
877 		    args->count);
878 #endif
879 	error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
880 	    args->count);
881 	LFREEPATH(name);
882 	return (error);
883 }
884 
885 int
886 linux_readlinkat(struct thread *td, struct linux_readlinkat_args *args)
887 {
888 	char *name;
889 	int error, dfd;
890 
891 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd;
892 	LCONVPATHEXIST_AT(td, args->path, &name, dfd);
893 
894 #ifdef DEBUG
895 	if (ldebug(readlinkat))
896 		printf(ARGS(readlinkat, "%s, %p, %d"), name, (void *)args->buf,
897 		    args->bufsiz);
898 #endif
899 
900 	error = kern_readlinkat(td, dfd, name, UIO_SYSSPACE, args->buf,
901 	    UIO_USERSPACE, args->bufsiz);
902 	LFREEPATH(name);
903 	return (error);
904 }
905 
906 int
907 linux_truncate(struct thread *td, struct linux_truncate_args *args)
908 {
909 	char *path;
910 	int error;
911 
912 	LCONVPATHEXIST(td, args->path, &path);
913 
914 #ifdef DEBUG
915 	if (ldebug(truncate))
916 		printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
917 #endif
918 
919 	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
920 	LFREEPATH(path);
921 	return (error);
922 }
923 
924 int
925 linux_truncate64(struct thread *td, struct linux_truncate64_args *args)
926 {
927 	char *path;
928 	int error;
929 
930 	LCONVPATHEXIST(td, args->path, &path);
931 
932 #ifdef DEBUG
933 	if (ldebug(truncate64))
934 		printf(ARGS(truncate64, "%s, %jd"), path, args->length);
935 #endif
936 
937 	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
938 	LFREEPATH(path);
939 	return (error);
940 }
941 int
942 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
943 {
944 	struct ftruncate_args /* {
945 		int fd;
946 		int pad;
947 		off_t length;
948 		} */ nuap;
949 
950 	nuap.fd = args->fd;
951 	nuap.length = args->length;
952 	return (ftruncate(td, &nuap));
953 }
954 
955 int
956 linux_link(struct thread *td, struct linux_link_args *args)
957 {
958 	char *path, *to;
959 	int error;
960 
961 	LCONVPATHEXIST(td, args->path, &path);
962 	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
963 	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1, AT_FDCWD);
964 	if (to == NULL) {
965 		LFREEPATH(path);
966 		return (error);
967 	}
968 
969 #ifdef DEBUG
970 	if (ldebug(link))
971 		printf(ARGS(link, "%s, %s"), path, to);
972 #endif
973 	error = kern_link(td, path, to, UIO_SYSSPACE);
974 	LFREEPATH(path);
975 	LFREEPATH(to);
976 	return (error);
977 }
978 
979 int
980 linux_linkat(struct thread *td, struct linux_linkat_args *args)
981 {
982 	char *path, *to;
983 	int error, olddfd, newdfd;
984 
985 	/*
986 	 * They really introduced flags argument which is forbidden to
987 	 * use.
988 	 */
989 	if (args->flags != 0)
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->flags);
1006 #endif
1007 
1008 	error = kern_linkat(td, olddfd, newdfd, path, to, UIO_SYSSPACE, FOLLOW);
1009 	LFREEPATH(path);
1010 	LFREEPATH(to);
1011 	return (error);
1012 }
1013 
1014 int
1015 linux_fdatasync(td, uap)
1016 	struct thread *td;
1017 	struct linux_fdatasync_args *uap;
1018 {
1019 	struct fsync_args bsd;
1020 
1021 	bsd.fd = uap->fd;
1022 	return fsync(td, &bsd);
1023 }
1024 
1025 int
1026 linux_pread(td, uap)
1027 	struct thread *td;
1028 	struct linux_pread_args *uap;
1029 {
1030 	struct pread_args bsd;
1031 	struct vnode *vp;
1032 	int error;
1033 
1034 	bsd.fd = uap->fd;
1035 	bsd.buf = uap->buf;
1036 	bsd.nbyte = uap->nbyte;
1037 	bsd.offset = uap->offset;
1038 
1039 	error = pread(td, &bsd);
1040 
1041 	if (error == 0) {
1042    	   	/* This seems to violate POSIX but linux does it */
1043    	   	if ((error = fgetvp(td, uap->fd, &vp)) != 0)
1044    		   	return (error);
1045 		if (vp->v_type == VDIR) {
1046    		   	vrele(vp);
1047 			return (EISDIR);
1048 		}
1049 		vrele(vp);
1050 	}
1051 
1052 	return (error);
1053 }
1054 
1055 int
1056 linux_pwrite(td, uap)
1057 	struct thread *td;
1058 	struct linux_pwrite_args *uap;
1059 {
1060 	struct pwrite_args bsd;
1061 
1062 	bsd.fd = uap->fd;
1063 	bsd.buf = uap->buf;
1064 	bsd.nbyte = uap->nbyte;
1065 	bsd.offset = uap->offset;
1066 	return pwrite(td, &bsd);
1067 }
1068 
1069 int
1070 linux_mount(struct thread *td, struct linux_mount_args *args)
1071 {
1072 	struct ufs_args ufs;
1073 	char fstypename[MFSNAMELEN];
1074 	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
1075 	int error;
1076 	int fsflags;
1077 	void *fsdata;
1078 
1079 	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
1080 	    NULL);
1081 	if (error)
1082 		return (error);
1083 	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
1084 	if (error)
1085 		return (error);
1086 	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
1087 	if (error)
1088 		return (error);
1089 
1090 #ifdef DEBUG
1091 	if (ldebug(mount))
1092 		printf(ARGS(mount, "%s, %s, %s"),
1093 		    fstypename, mntfromname, mntonname);
1094 #endif
1095 
1096 	if (strcmp(fstypename, "ext2") == 0) {
1097 		strcpy(fstypename, "ext2fs");
1098 		fsdata = &ufs;
1099 		ufs.fspec = mntfromname;
1100 #define DEFAULT_ROOTID		-2
1101 		ufs.export.ex_root = DEFAULT_ROOTID;
1102 		ufs.export.ex_flags =
1103 		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
1104 	} else if (strcmp(fstypename, "proc") == 0) {
1105 		strcpy(fstypename, "linprocfs");
1106 		fsdata = NULL;
1107 	} else if (strcmp(fstypename, "vfat") == 0) {
1108 		strcpy(fstypename, "msdosfs");
1109 		fsdata = NULL;
1110 	} else {
1111 		return (ENODEV);
1112 	}
1113 
1114 	fsflags = 0;
1115 
1116 	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
1117 		/*
1118 		 * Linux SYNC flag is not included; the closest equivalent
1119 		 * FreeBSD has is !ASYNC, which is our default.
1120 		 */
1121 		if (args->rwflag & LINUX_MS_RDONLY)
1122 			fsflags |= MNT_RDONLY;
1123 		if (args->rwflag & LINUX_MS_NOSUID)
1124 			fsflags |= MNT_NOSUID;
1125 		if (args->rwflag & LINUX_MS_NOEXEC)
1126 			fsflags |= MNT_NOEXEC;
1127 		if (args->rwflag & LINUX_MS_REMOUNT)
1128 			fsflags |= MNT_UPDATE;
1129 	}
1130 
1131 	if (strcmp(fstypename, "linprocfs") == 0) {
1132 		error = kernel_vmount(fsflags,
1133 			"fstype", fstypename,
1134 			"fspath", mntonname,
1135 			NULL);
1136 	} else if (strcmp(fstypename, "msdosfs") == 0) {
1137 		error = kernel_vmount(fsflags,
1138 			"fstype", fstypename,
1139 			"fspath", mntonname,
1140 			"from", mntfromname,
1141 			NULL);
1142 	} else
1143 		error = EOPNOTSUPP;
1144 	return (error);
1145 }
1146 
1147 int
1148 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
1149 {
1150 	struct linux_umount_args args2;
1151 
1152 	args2.path = args->path;
1153 	args2.flags = 0;
1154 	return (linux_umount(td, &args2));
1155 }
1156 
1157 int
1158 linux_umount(struct thread *td, struct linux_umount_args *args)
1159 {
1160 	struct unmount_args bsd;
1161 
1162 	bsd.path = args->path;
1163 	bsd.flags = args->flags;	/* XXX correct? */
1164 	return (unmount(td, &bsd));
1165 }
1166 
1167 /*
1168  * fcntl family of syscalls
1169  */
1170 
1171 struct l_flock {
1172 	l_short		l_type;
1173 	l_short		l_whence;
1174 	l_off_t		l_start;
1175 	l_off_t		l_len;
1176 	l_pid_t		l_pid;
1177 }
1178 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1179 __packed
1180 #endif
1181 ;
1182 
1183 static void
1184 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
1185 {
1186 	switch (linux_flock->l_type) {
1187 	case LINUX_F_RDLCK:
1188 		bsd_flock->l_type = F_RDLCK;
1189 		break;
1190 	case LINUX_F_WRLCK:
1191 		bsd_flock->l_type = F_WRLCK;
1192 		break;
1193 	case LINUX_F_UNLCK:
1194 		bsd_flock->l_type = F_UNLCK;
1195 		break;
1196 	default:
1197 		bsd_flock->l_type = -1;
1198 		break;
1199 	}
1200 	bsd_flock->l_whence = linux_flock->l_whence;
1201 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1202 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1203 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1204 	bsd_flock->l_sysid = 0;
1205 }
1206 
1207 static void
1208 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
1209 {
1210 	switch (bsd_flock->l_type) {
1211 	case F_RDLCK:
1212 		linux_flock->l_type = LINUX_F_RDLCK;
1213 		break;
1214 	case F_WRLCK:
1215 		linux_flock->l_type = LINUX_F_WRLCK;
1216 		break;
1217 	case F_UNLCK:
1218 		linux_flock->l_type = LINUX_F_UNLCK;
1219 		break;
1220 	}
1221 	linux_flock->l_whence = bsd_flock->l_whence;
1222 	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
1223 	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
1224 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1225 }
1226 
1227 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1228 struct l_flock64 {
1229 	l_short		l_type;
1230 	l_short		l_whence;
1231 	l_loff_t	l_start;
1232 	l_loff_t	l_len;
1233 	l_pid_t		l_pid;
1234 }
1235 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1236 __packed
1237 #endif
1238 ;
1239 
1240 static void
1241 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
1242 {
1243 	switch (linux_flock->l_type) {
1244 	case LINUX_F_RDLCK:
1245 		bsd_flock->l_type = F_RDLCK;
1246 		break;
1247 	case LINUX_F_WRLCK:
1248 		bsd_flock->l_type = F_WRLCK;
1249 		break;
1250 	case LINUX_F_UNLCK:
1251 		bsd_flock->l_type = F_UNLCK;
1252 		break;
1253 	default:
1254 		bsd_flock->l_type = -1;
1255 		break;
1256 	}
1257 	bsd_flock->l_whence = linux_flock->l_whence;
1258 	bsd_flock->l_start = (off_t)linux_flock->l_start;
1259 	bsd_flock->l_len = (off_t)linux_flock->l_len;
1260 	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
1261 	bsd_flock->l_sysid = 0;
1262 }
1263 
1264 static void
1265 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
1266 {
1267 	switch (bsd_flock->l_type) {
1268 	case F_RDLCK:
1269 		linux_flock->l_type = LINUX_F_RDLCK;
1270 		break;
1271 	case F_WRLCK:
1272 		linux_flock->l_type = LINUX_F_WRLCK;
1273 		break;
1274 	case F_UNLCK:
1275 		linux_flock->l_type = LINUX_F_UNLCK;
1276 		break;
1277 	}
1278 	linux_flock->l_whence = bsd_flock->l_whence;
1279 	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
1280 	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
1281 	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
1282 }
1283 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1284 
1285 static int
1286 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
1287 {
1288 	struct l_flock linux_flock;
1289 	struct flock bsd_flock;
1290 	struct file *fp;
1291 	long arg;
1292 	int error, result;
1293 
1294 	switch (args->cmd) {
1295 	case LINUX_F_DUPFD:
1296 		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1297 
1298 	case LINUX_F_GETFD:
1299 		return (kern_fcntl(td, args->fd, F_GETFD, 0));
1300 
1301 	case LINUX_F_SETFD:
1302 		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1303 
1304 	case LINUX_F_GETFL:
1305 		error = kern_fcntl(td, args->fd, F_GETFL, 0);
1306 		result = td->td_retval[0];
1307 		td->td_retval[0] = 0;
1308 		if (result & O_RDONLY)
1309 			td->td_retval[0] |= LINUX_O_RDONLY;
1310 		if (result & O_WRONLY)
1311 			td->td_retval[0] |= LINUX_O_WRONLY;
1312 		if (result & O_RDWR)
1313 			td->td_retval[0] |= LINUX_O_RDWR;
1314 		if (result & O_NDELAY)
1315 			td->td_retval[0] |= LINUX_O_NONBLOCK;
1316 		if (result & O_APPEND)
1317 			td->td_retval[0] |= LINUX_O_APPEND;
1318 		if (result & O_FSYNC)
1319 			td->td_retval[0] |= LINUX_O_SYNC;
1320 		if (result & O_ASYNC)
1321 			td->td_retval[0] |= LINUX_FASYNC;
1322 #ifdef LINUX_O_NOFOLLOW
1323 		if (result & O_NOFOLLOW)
1324 			td->td_retval[0] |= LINUX_O_NOFOLLOW;
1325 #endif
1326 #ifdef LINUX_O_DIRECT
1327 		if (result & O_DIRECT)
1328 			td->td_retval[0] |= LINUX_O_DIRECT;
1329 #endif
1330 		return (error);
1331 
1332 	case LINUX_F_SETFL:
1333 		arg = 0;
1334 		if (args->arg & LINUX_O_NDELAY)
1335 			arg |= O_NONBLOCK;
1336 		if (args->arg & LINUX_O_APPEND)
1337 			arg |= O_APPEND;
1338 		if (args->arg & LINUX_O_SYNC)
1339 			arg |= O_FSYNC;
1340 		if (args->arg & LINUX_FASYNC)
1341 			arg |= O_ASYNC;
1342 #ifdef LINUX_O_NOFOLLOW
1343 		if (args->arg & LINUX_O_NOFOLLOW)
1344 			arg |= O_NOFOLLOW;
1345 #endif
1346 #ifdef LINUX_O_DIRECT
1347 		if (args->arg & LINUX_O_DIRECT)
1348 			arg |= O_DIRECT;
1349 #endif
1350 		return (kern_fcntl(td, args->fd, F_SETFL, arg));
1351 
1352 	case LINUX_F_GETLK:
1353 		error = copyin((void *)args->arg, &linux_flock,
1354 		    sizeof(linux_flock));
1355 		if (error)
1356 			return (error);
1357 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1358 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1359 		if (error)
1360 			return (error);
1361 		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1362 		return (copyout(&linux_flock, (void *)args->arg,
1363 		    sizeof(linux_flock)));
1364 
1365 	case LINUX_F_SETLK:
1366 		error = copyin((void *)args->arg, &linux_flock,
1367 		    sizeof(linux_flock));
1368 		if (error)
1369 			return (error);
1370 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1371 		return (kern_fcntl(td, args->fd, F_SETLK,
1372 		    (intptr_t)&bsd_flock));
1373 
1374 	case LINUX_F_SETLKW:
1375 		error = copyin((void *)args->arg, &linux_flock,
1376 		    sizeof(linux_flock));
1377 		if (error)
1378 			return (error);
1379 		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1380 		return (kern_fcntl(td, args->fd, F_SETLKW,
1381 		     (intptr_t)&bsd_flock));
1382 
1383 	case LINUX_F_GETOWN:
1384 		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1385 
1386 	case LINUX_F_SETOWN:
1387 		/*
1388 		 * XXX some Linux applications depend on F_SETOWN having no
1389 		 * significant effect for pipes (SIGIO is not delivered for
1390 		 * pipes under Linux-2.2.35 at least).
1391 		 */
1392 		error = fget(td, args->fd, &fp);
1393 		if (error)
1394 			return (error);
1395 		if (fp->f_type == DTYPE_PIPE) {
1396 			fdrop(fp, td);
1397 			return (EINVAL);
1398 		}
1399 		fdrop(fp, td);
1400 
1401 		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1402 	}
1403 
1404 	return (EINVAL);
1405 }
1406 
1407 int
1408 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1409 {
1410 	struct linux_fcntl64_args args64;
1411 
1412 #ifdef DEBUG
1413 	if (ldebug(fcntl))
1414 		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1415 #endif
1416 
1417 	args64.fd = args->fd;
1418 	args64.cmd = args->cmd;
1419 	args64.arg = args->arg;
1420 	return (fcntl_common(td, &args64));
1421 }
1422 
1423 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1424 int
1425 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1426 {
1427 	struct l_flock64 linux_flock;
1428 	struct flock bsd_flock;
1429 	int error;
1430 
1431 #ifdef DEBUG
1432 	if (ldebug(fcntl64))
1433 		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1434 #endif
1435 
1436 	switch (args->cmd) {
1437 	case LINUX_F_GETLK64:
1438 		error = copyin((void *)args->arg, &linux_flock,
1439 		    sizeof(linux_flock));
1440 		if (error)
1441 			return (error);
1442 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1443 		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1444 		if (error)
1445 			return (error);
1446 		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1447 		return (copyout(&linux_flock, (void *)args->arg,
1448 			    sizeof(linux_flock)));
1449 
1450 	case LINUX_F_SETLK64:
1451 		error = copyin((void *)args->arg, &linux_flock,
1452 		    sizeof(linux_flock));
1453 		if (error)
1454 			return (error);
1455 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1456 		return (kern_fcntl(td, args->fd, F_SETLK,
1457 		    (intptr_t)&bsd_flock));
1458 
1459 	case LINUX_F_SETLKW64:
1460 		error = copyin((void *)args->arg, &linux_flock,
1461 		    sizeof(linux_flock));
1462 		if (error)
1463 			return (error);
1464 		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1465 		return (kern_fcntl(td, args->fd, F_SETLKW,
1466 		    (intptr_t)&bsd_flock));
1467 	}
1468 
1469 	return (fcntl_common(td, args));
1470 }
1471 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1472 
1473 int
1474 linux_chown(struct thread *td, struct linux_chown_args *args)
1475 {
1476 	char *path;
1477 	int error;
1478 
1479 	LCONVPATHEXIST(td, args->path, &path);
1480 
1481 #ifdef DEBUG
1482 	if (ldebug(chown))
1483 		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1484 #endif
1485 	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1486 	LFREEPATH(path);
1487 	return (error);
1488 }
1489 
1490 int
1491 linux_fchownat(struct thread *td, struct linux_fchownat_args *args)
1492 {
1493 	char *path;
1494 	int error, dfd, follow;
1495 
1496 	if (args->flag & ~LINUX_AT_SYMLINK_NOFOLLOW)
1497 		return (EINVAL);
1498 
1499 	dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD :  args->dfd;
1500 	LCONVPATHEXIST_AT(td, args->filename, &path, dfd);
1501 
1502 #ifdef DEBUG
1503 	if (ldebug(fchownat))
1504 		printf(ARGS(fchownat, "%s, %d, %d"), path, args->uid, args->gid);
1505 #endif
1506 
1507 	follow = (args->flag & LINUX_AT_SYMLINK_NOFOLLOW) == 0 ? 0 :
1508 	    AT_SYMLINK_NOFOLLOW;
1509 	error = kern_fchownat(td, dfd, path, UIO_SYSSPACE, args->uid, args->gid,
1510 	    follow);
1511 	LFREEPATH(path);
1512 	return (error);
1513 }
1514 
1515 int
1516 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1517 {
1518 	char *path;
1519 	int error;
1520 
1521 	LCONVPATHEXIST(td, args->path, &path);
1522 
1523 #ifdef DEBUG
1524 	if (ldebug(lchown))
1525 		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1526 #endif
1527 	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1528 	LFREEPATH(path);
1529 	return (error);
1530 }
1531