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