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