xref: /netbsd-src/sys/compat/linux/common/linux_file.c (revision 0df165c04d0a9ca1adde9ed2b890344c937954a6)
1 /*	$NetBSD: linux_file.c,v 1.86 2007/11/26 19:01:31 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Functions in multiarch:
41  *	linux_sys_llseek	: linux_llseek.c
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.86 2007/11/26 19:01:31 pooka Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/filedesc.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/mount.h>
57 #include <sys/malloc.h>
58 #include <sys/namei.h>
59 #include <sys/vnode.h>
60 #include <sys/tty.h>
61 #include <sys/socketvar.h>
62 #include <sys/conf.h>
63 #include <sys/pipe.h>
64 
65 #include <sys/syscallargs.h>
66 #include <sys/vfs_syscalls.h>
67 
68 #include <compat/linux/common/linux_types.h>
69 #include <compat/linux/common/linux_signal.h>
70 #include <compat/linux/common/linux_fcntl.h>
71 #include <compat/linux/common/linux_util.h>
72 #include <compat/linux/common/linux_machdep.h>
73 #include <compat/linux/common/linux_ipc.h>
74 #include <compat/linux/common/linux_sem.h>
75 
76 #include <compat/linux/linux_syscallargs.h>
77 
78 static int linux_to_bsd_ioflags __P((int));
79 static int bsd_to_linux_ioflags __P((int));
80 static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
81 static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
82 #ifndef __amd64__
83 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
84 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
85 #endif
86 
87 /*
88  * Some file-related calls are handled here. The usual flag conversion
89  * an structure conversion is done, and alternate emul path searching.
90  */
91 
92 /*
93  * The next two functions convert between the Linux and NetBSD values
94  * of the flags used in open(2) and fcntl(2).
95  */
96 static int
97 linux_to_bsd_ioflags(lflags)
98 	int lflags;
99 {
100 	int res = 0;
101 
102 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
103 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
104 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
105 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
106 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
107 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
108 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
109 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
110 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
111 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
112 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
113 
114 	return res;
115 }
116 
117 static int
118 bsd_to_linux_ioflags(bflags)
119 	int bflags;
120 {
121 	int res = 0;
122 
123 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
124 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
125 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
126 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
127 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
128 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
129 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
130 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
131 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
132 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
133 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
134 
135 	return res;
136 }
137 
138 /*
139  * creat(2) is an obsolete function, but it's present as a Linux
140  * system call, so let's deal with it.
141  *
142  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
143  * in syscalls.master anyway so this doesn't have to be special cased.
144  *
145  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
146  */
147 int
148 linux_sys_creat(l, v, retval)
149 	struct lwp *l;
150 	void *v;
151 	register_t *retval;
152 {
153 	struct linux_sys_creat_args /* {
154 		syscallarg(const char *) path;
155 		syscallarg(int) mode;
156 	} */ *uap = v;
157 	struct sys_open_args oa;
158 
159 	SCARG(&oa, path) = SCARG(uap, path);
160 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
161 	SCARG(&oa, mode) = SCARG(uap, mode);
162 
163 	return sys_open(l, &oa, retval);
164 }
165 
166 /*
167  * open(2). Take care of the different flag values, and let the
168  * NetBSD syscall do the real work. See if this operation
169  * gives the current process a controlling terminal.
170  * (XXX is this necessary?)
171  */
172 int
173 linux_sys_open(l, v, retval)
174 	struct lwp *l;
175 	void *v;
176 	register_t *retval;
177 {
178 	struct linux_sys_open_args /* {
179 		syscallarg(const char *) path;
180 		syscallarg(int) flags;
181 		syscallarg(int) mode;
182 	} */ *uap = v;
183 	struct proc *p = l->l_proc;
184 	int error, fl;
185 	struct sys_open_args boa;
186 
187 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
188 
189 	SCARG(&boa, path) = SCARG(uap, path);
190 	SCARG(&boa, flags) = fl;
191 	SCARG(&boa, mode) = SCARG(uap, mode);
192 
193 	if ((error = sys_open(l, &boa, retval)))
194 		return error;
195 
196 	/*
197 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
198 	 * If we are a session leader, and we don't have a controlling
199 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
200 	 * this the controlling terminal.
201 	 */
202         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
203                 struct filedesc *fdp = p->p_fd;
204                 struct file     *fp;
205 
206 		fp = fd_getfile(fdp, *retval);
207 
208                 /* ignore any error, just give it a try */
209                 if (fp != NULL) {
210 			FILE_USE(fp);
211 			if (fp->f_type == DTYPE_VNODE) {
212 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
213 				    (void *) 0, l);
214 			}
215 			FILE_UNUSE(fp, l);
216 		}
217         }
218 	return 0;
219 }
220 
221 /*
222  * The next two functions take care of converting the flock
223  * structure back and forth between Linux and NetBSD format.
224  * The only difference in the structures is the order of
225  * the fields, and the 'whence' value.
226  */
227 static void
228 bsd_to_linux_flock(bfp, lfp)
229 	struct flock *bfp;
230 	struct linux_flock *lfp;
231 {
232 
233 	lfp->l_start = bfp->l_start;
234 	lfp->l_len = bfp->l_len;
235 	lfp->l_pid = bfp->l_pid;
236 	lfp->l_whence = bfp->l_whence;
237 	switch (bfp->l_type) {
238 	case F_RDLCK:
239 		lfp->l_type = LINUX_F_RDLCK;
240 		break;
241 	case F_UNLCK:
242 		lfp->l_type = LINUX_F_UNLCK;
243 		break;
244 	case F_WRLCK:
245 		lfp->l_type = LINUX_F_WRLCK;
246 		break;
247 	}
248 }
249 
250 static void
251 linux_to_bsd_flock(lfp, bfp)
252 	struct linux_flock *lfp;
253 	struct flock *bfp;
254 {
255 
256 	bfp->l_start = lfp->l_start;
257 	bfp->l_len = lfp->l_len;
258 	bfp->l_pid = lfp->l_pid;
259 	bfp->l_whence = lfp->l_whence;
260 	switch (lfp->l_type) {
261 	case LINUX_F_RDLCK:
262 		bfp->l_type = F_RDLCK;
263 		break;
264 	case LINUX_F_UNLCK:
265 		bfp->l_type = F_UNLCK;
266 		break;
267 	case LINUX_F_WRLCK:
268 		bfp->l_type = F_WRLCK;
269 		break;
270 	}
271 }
272 
273 /*
274  * Most actions in the fcntl() call are straightforward; simply
275  * pass control to the NetBSD system call. A few commands need
276  * conversions after the actual system call has done its work,
277  * because the flag values and lock structure are different.
278  */
279 int
280 linux_sys_fcntl(l, v, retval)
281 	struct lwp *l;
282 	void *v;
283 	register_t *retval;
284 {
285 	struct linux_sys_fcntl_args /* {
286 		syscallarg(int) fd;
287 		syscallarg(int) cmd;
288 		syscallarg(void *) arg;
289 	} */ *uap = v;
290 	struct proc *p = l->l_proc;
291 	int fd, cmd, error;
292 	u_long val;
293 	void *arg;
294 	struct linux_flock lfl;
295 	struct flock bfl;
296 	struct sys_fcntl_args fca;
297 	struct filedesc *fdp;
298 	struct file *fp;
299 	struct vnode *vp;
300 	struct vattr va;
301 	const struct cdevsw *cdev;
302 	long pgid;
303 	struct pgrp *pgrp;
304 	struct tty *tp, *(*d_tty) __P((dev_t));
305 
306 	fd = SCARG(uap, fd);
307 	cmd = SCARG(uap, cmd);
308 	arg = (void *) SCARG(uap, arg);
309 
310 	switch (cmd) {
311 	case LINUX_F_DUPFD:
312 		cmd = F_DUPFD;
313 		break;
314 	case LINUX_F_GETFD:
315 		cmd = F_GETFD;
316 		break;
317 	case LINUX_F_SETFD:
318 		cmd = F_SETFD;
319 		break;
320 	case LINUX_F_GETFL:
321 		SCARG(&fca, fd) = fd;
322 		SCARG(&fca, cmd) = F_GETFL;
323 		SCARG(&fca, arg) = arg;
324 		if ((error = sys_fcntl(l, &fca, retval)))
325 			return error;
326 		retval[0] = bsd_to_linux_ioflags(retval[0]);
327 		return 0;
328 	case LINUX_F_SETFL: {
329 		struct file	*fp1 = NULL;
330 
331 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
332 		/*
333 		 * Linux seems to have same semantics for sending SIGIO to the
334 		 * read side of socket, but slightly different semantics
335 		 * for SIGIO to the write side.  Rather than sending the SIGIO
336 		 * every time it's possible to write (directly) more data, it
337 		 * only sends SIGIO if last write(2) failed due to insufficient
338 		 * memory to hold the data. This is compatible enough
339 		 * with NetBSD semantics to not do anything about the
340 		 * difference.
341 		 *
342 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
343 		 * ones and DTYPE_PIPE ones. For these, we don't set
344 		 * the underlying flags (we don't pass O_ASYNC flag down
345 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
346 		 * so that F_GETFL would report the ASYNC i/o is on.
347 		 */
348 		if (val & O_ASYNC) {
349 			if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
350 			    return (EBADF);
351 
352 			FILE_USE(fp1);
353 
354 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
355 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
356 			    || (fp1->f_type == DTYPE_PIPE))
357 				val &= ~O_ASYNC;
358 			else {
359 				/* not a pipe, do not modify anything */
360 				FILE_UNUSE(fp1, l);
361 				fp1 = NULL;
362 			}
363 		}
364 
365 		SCARG(&fca, fd) = fd;
366 		SCARG(&fca, cmd) = F_SETFL;
367 		SCARG(&fca, arg) = (void *) val;
368 
369 		error = sys_fcntl(l, &fca, retval);
370 
371 		/* Now set the FASYNC flag for pipes */
372 		if (fp1) {
373 			if (!error)
374 				fp1->f_flag |= FASYNC;
375 			FILE_UNUSE(fp1, l);
376 		}
377 
378 		return (error);
379 	    }
380 	case LINUX_F_GETLK:
381 		if ((error = copyin(arg, &lfl, sizeof lfl)))
382 			return error;
383 		linux_to_bsd_flock(&lfl, &bfl);
384 		error = do_fcntl_lock(l, fd, F_GETLK, &bfl);
385 		if (error)
386 			return error;
387 		bsd_to_linux_flock(&bfl, &lfl);
388 		return copyout(&lfl, arg, sizeof lfl);
389 
390 	case LINUX_F_SETLK:
391 	case LINUX_F_SETLKW:
392 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
393 		if ((error = copyin(arg, &lfl, sizeof lfl)))
394 			return error;
395 		linux_to_bsd_flock(&lfl, &bfl);
396 		return do_fcntl_lock(l, fd, cmd, &bfl);
397 
398 	case LINUX_F_SETOWN:
399 	case LINUX_F_GETOWN:
400 		/*
401 		 * We need to route fcntl() for tty descriptors around normal
402 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
403 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
404 		 * this is not a problem.
405 		 */
406 		fdp = p->p_fd;
407 		if ((fp = fd_getfile(fdp, fd)) == NULL)
408 			return EBADF;
409 		FILE_USE(fp);
410 
411 		/* Check it's a character device vnode */
412 		if (fp->f_type != DTYPE_VNODE
413 		    || (vp = (struct vnode *)fp->f_data) == NULL
414 		    || vp->v_type != VCHR) {
415 			FILE_UNUSE(fp, l);
416 
417 	    not_tty:
418 			/* Not a tty, proceed with common fcntl() */
419 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
420 			break;
421 		}
422 
423 		error = VOP_GETATTR(vp, &va, l->l_cred);
424 
425 		FILE_UNUSE(fp, l);
426 
427 		if (error)
428 			return error;
429 
430 		cdev = cdevsw_lookup(va.va_rdev);
431 		if (cdev == NULL)
432 			return (ENXIO);
433 		d_tty = cdev->d_tty;
434 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
435 			goto not_tty;
436 
437 		/* set tty pg_id appropriately */
438 		if (cmd == LINUX_F_GETOWN) {
439 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
440 			return 0;
441 		}
442 		mutex_enter(&proclist_lock);
443 		if ((long)arg <= 0) {
444 			pgid = -(long)arg;
445 		} else {
446 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
447 			if (p1 == NULL)
448 				return (ESRCH);
449 			pgid = (long)p1->p_pgrp->pg_id;
450 		}
451 		pgrp = pg_find(pgid, PFIND_LOCKED);
452 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
453 			mutex_exit(&proclist_lock);
454 			return EPERM;
455 		}
456 		tp->t_pgrp = pgrp;
457 		mutex_exit(&proclist_lock);
458 		return 0;
459 
460 	default:
461 		return EOPNOTSUPP;
462 	}
463 
464 	SCARG(&fca, fd) = fd;
465 	SCARG(&fca, cmd) = cmd;
466 	SCARG(&fca, arg) = arg;
467 
468 	return sys_fcntl(l, &fca, retval);
469 }
470 
471 #if !defined(__amd64__)
472 /*
473  * Convert a NetBSD stat structure to a Linux stat structure.
474  * Only the order of the fields and the padding in the structure
475  * is different. linux_fakedev is a machine-dependent function
476  * which optionally converts device driver major/minor numbers
477  * (XXX horrible, but what can you do against code that compares
478  * things against constant major device numbers? sigh)
479  */
480 static void
481 bsd_to_linux_stat(bsp, lsp)
482 	struct stat *bsp;
483 	struct linux_stat *lsp;
484 {
485 
486 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
487 	lsp->lst_ino     = bsp->st_ino;
488 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
489 	if (bsp->st_nlink >= (1 << 15))
490 		lsp->lst_nlink = (1 << 15) - 1;
491 	else
492 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
493 	lsp->lst_uid     = bsp->st_uid;
494 	lsp->lst_gid     = bsp->st_gid;
495 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
496 	lsp->lst_size    = bsp->st_size;
497 	lsp->lst_blksize = bsp->st_blksize;
498 	lsp->lst_blocks  = bsp->st_blocks;
499 	lsp->lst_atime   = bsp->st_atime;
500 	lsp->lst_mtime   = bsp->st_mtime;
501 	lsp->lst_ctime   = bsp->st_ctime;
502 #ifdef LINUX_STAT_HAS_NSEC
503 	lsp->lst_atime_nsec   = bsp->st_atimensec;
504 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
505 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
506 #endif
507 }
508 
509 /*
510  * The stat functions below are plain sailing. stat and lstat are handled
511  * by one function to avoid code duplication.
512  */
513 int
514 linux_sys_fstat(l, v, retval)
515 	struct lwp *l;
516 	void *v;
517 	register_t *retval;
518 {
519 	struct linux_sys_fstat_args /* {
520 		syscallarg(int) fd;
521 		syscallarg(linux_stat *) sp;
522 	} */ *uap = v;
523 	struct linux_stat tmplst;
524 	struct stat tmpst;
525 	int error;
526 
527 	error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
528 	if (error != 0)
529 		return error;
530 	bsd_to_linux_stat(&tmpst, &tmplst);
531 
532 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
533 }
534 
535 static int
536 linux_stat1(l, v, retval, flags)
537 	struct lwp *l;
538 	void *v;
539 	register_t *retval;
540 	int flags;
541 {
542 	struct linux_stat tmplst;
543 	struct stat tmpst;
544 	int error;
545 	struct linux_sys_stat_args *uap = v;
546 
547 	error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
548 	if (error != 0)
549 		return error;
550 
551 	bsd_to_linux_stat(&tmpst, &tmplst);
552 
553 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
554 }
555 
556 int
557 linux_sys_stat(l, v, retval)
558 	struct lwp *l;
559 	void *v;
560 	register_t *retval;
561 {
562 	struct linux_sys_stat_args /* {
563 		syscallarg(const char *) path;
564 		syscallarg(struct linux_stat *) sp;
565 	} */ *uap = v;
566 
567 	return linux_stat1(l, uap, retval, FOLLOW);
568 }
569 
570 /* Note: this is "newlstat" in the Linux sources */
571 /*	(we don't bother with the old lstat currently) */
572 int
573 linux_sys_lstat(l, v, retval)
574 	struct lwp *l;
575 	void *v;
576 	register_t *retval;
577 {
578 	struct linux_sys_lstat_args /* {
579 		syscallarg(const char *) path;
580 		syscallarg(struct linux_stat *) sp;
581 	} */ *uap = v;
582 
583 	return linux_stat1(l, uap, retval, NOFOLLOW);
584 }
585 #endif /* !__amd64__ */
586 
587 /*
588  * The following syscalls are mostly here because of the alternate path check.
589  */
590 int
591 linux_sys_unlink(l, v, retval)
592 	struct lwp *l;
593 	void *v;
594 	register_t *retval;
595 
596 {
597 	struct linux_sys_unlink_args /* {
598 		syscallarg(const char *) path;
599 	} */ *uap = v;
600 	int error;
601 	struct nameidata nd;
602 
603 	error = sys_unlink(l, uap, retval);
604 	if (error != EPERM)
605 		return (error);
606 
607 	/*
608 	 * Linux returns EISDIR if unlink(2) is called on a directory.
609 	 * We return EPERM in such cases. To emulate correct behaviour,
610 	 * check if the path points to directory and return EISDIR if this
611 	 * is the case.
612 	 */
613 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
614 	    SCARG(uap, path), l);
615 	if (namei(&nd) == 0) {
616 		struct stat sb;
617 
618 		if (vn_stat(nd.ni_vp, &sb, l) == 0
619 		    && S_ISDIR(sb.st_mode))
620 			error = EISDIR;
621 
622 		vput(nd.ni_vp);
623 	}
624 
625 	return (error);
626 }
627 
628 int
629 linux_sys_mknod(l, v, retval)
630 	struct lwp *l;
631 	void *v;
632 	register_t *retval;
633 {
634 	struct linux_sys_mknod_args /* {
635 		syscallarg(const char *) path;
636 		syscallarg(int) mode;
637 		syscallarg(int) dev;
638 	} */ *uap = v;
639 
640 	/*
641 	 * BSD handles FIFOs separately
642 	 */
643 	if (S_ISFIFO(SCARG(uap, mode))) {
644 		struct sys_mkfifo_args bma;
645 
646 		SCARG(&bma, path) = SCARG(uap, path);
647 		SCARG(&bma, mode) = SCARG(uap, mode);
648 		return sys_mkfifo(l, &bma, retval);
649 	} else {
650 		struct sys_mknod_args bma;
651 
652 		SCARG(&bma, path) = SCARG(uap, path);
653 		SCARG(&bma, mode) = SCARG(uap, mode);
654 		/*
655 		 * Linux device numbers uses 8 bits for minor and 8 bits
656 		 * for major. Due to how we map our major and minor,
657 		 * this just fits into our dev_t. Just mask off the
658 		 * upper 16bit to remove any random junk.
659 		 */
660 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
661 		return sys_mknod(l, &bma, retval);
662 	}
663 }
664 
665 #if defined(__i386__) || defined(__m68k__) || \
666     defined(__arm__)
667 int
668 linux_sys_chown16(l, v, retval)
669 	struct lwp *l;
670 	void *v;
671 	register_t *retval;
672 {
673 	struct linux_sys_chown16_args /* {
674 		syscallarg(const char *) path;
675 		syscallarg(int) uid;
676 		syscallarg(int) gid;
677 	} */ *uap = v;
678 	struct sys___posix_chown_args bca;
679 
680 	SCARG(&bca, path) = SCARG(uap, path);
681 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
682 		(uid_t)-1 : SCARG(uap, uid);
683 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
684 		(gid_t)-1 : SCARG(uap, gid);
685 
686 	return sys___posix_chown(l, &bca, retval);
687 }
688 
689 int
690 linux_sys_fchown16(l, v, retval)
691 	struct lwp *l;
692 	void *v;
693 	register_t *retval;
694 {
695 	struct linux_sys_fchown16_args /* {
696 		syscallarg(int) fd;
697 		syscallarg(int) uid;
698 		syscallarg(int) gid;
699 	} */ *uap = v;
700 	struct sys___posix_fchown_args bfa;
701 
702 	SCARG(&bfa, fd) = SCARG(uap, fd);
703 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
704 		(uid_t)-1 : SCARG(uap, uid);
705 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
706 		(gid_t)-1 : SCARG(uap, gid);
707 
708 	return sys___posix_fchown(l, &bfa, retval);
709 }
710 
711 int
712 linux_sys_lchown16(l, v, retval)
713 	struct lwp *l;
714 	void *v;
715 	register_t *retval;
716 {
717 	struct linux_sys_lchown16_args /* {
718 		syscallarg(char *) path;
719 		syscallarg(int) uid;
720 		syscallarg(int) gid;
721 	} */ *uap = v;
722 	struct sys___posix_lchown_args bla;
723 
724 	SCARG(&bla, path) = SCARG(uap, path);
725 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
726 		(uid_t)-1 : SCARG(uap, uid);
727 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
728 		(gid_t)-1 : SCARG(uap, gid);
729 
730 	return sys___posix_lchown(l, &bla, retval);
731 }
732 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
733 
734 /*
735  * This is just fsync() for now (just as it is in the Linux kernel)
736  * Note: this is not implemented under Linux on Alpha and Arm
737  *	but should still be defined in our syscalls.master.
738  *	(syscall #148 on the arm)
739  */
740 int
741 linux_sys_fdatasync(l, v, retval)
742 	struct lwp *l;
743 	void *v;
744 	register_t *retval;
745 {
746 #ifdef notdef
747 	struct linux_sys_fdatasync_args /* {
748 		syscallarg(int) fd;
749 	} */ *uap = v;
750 #endif
751 	return sys_fsync(l, v, retval);
752 }
753 
754 /*
755  * pread(2).
756  */
757 int
758 linux_sys_pread(l, v, retval)
759 	struct lwp *l;
760 	void *v;
761 	register_t *retval;
762 {
763 	struct linux_sys_pread_args /* {
764 		syscallarg(int) fd;
765 		syscallarg(void *) buf;
766 		syscallarg(size_t) nbyte;
767 		syscallarg(linux_off_t) offset;
768 	} */ *uap = v;
769 	struct sys_pread_args pra;
770 
771 	SCARG(&pra, fd) = SCARG(uap, fd);
772 	SCARG(&pra, buf) = SCARG(uap, buf);
773 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
774 	SCARG(&pra, offset) = SCARG(uap, offset);
775 
776 	return sys_pread(l, &pra, retval);
777 }
778 
779 /*
780  * pwrite(2).
781  */
782 int
783 linux_sys_pwrite(l, v, retval)
784 	struct lwp *l;
785 	void *v;
786 	register_t *retval;
787 {
788 	struct linux_sys_pwrite_args /* {
789 		syscallarg(int) fd;
790 		syscallarg(void *) buf;
791 		syscallarg(size_t) nbyte;
792 		syscallarg(linux_off_t) offset;
793 	} */ *uap = v;
794 	struct sys_pwrite_args pra;
795 
796 	SCARG(&pra, fd) = SCARG(uap, fd);
797 	SCARG(&pra, buf) = SCARG(uap, buf);
798 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
799 	SCARG(&pra, offset) = SCARG(uap, offset);
800 
801 	return sys_pwrite(l, &pra, retval);
802 }
803 
804 #define LINUX_NOT_SUPPORTED(fun) \
805 int \
806 fun(struct lwp *l, void *v, register_t *retval) \
807 { \
808 	return EOPNOTSUPP; \
809 }
810 
811 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
812 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
813 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
814 
815 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
816 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
817 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
818 
819 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
820 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
821 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
822 
823 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
824 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
825 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
826