xref: /netbsd-src/sys/compat/linux/common/linux_file.c (revision bfb6cb13d599546df69c7e4d20d70e22e15a549d)
1 /*	$NetBSD: linux_file.c,v 1.83 2007/05/12 23:02:49 dsl 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.83 2007/05/12 23:02:49 dsl 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 
74 #include <compat/linux/linux_syscallargs.h>
75 
76 static int linux_to_bsd_ioflags __P((int));
77 static int bsd_to_linux_ioflags __P((int));
78 static void bsd_to_linux_flock __P((struct flock *, struct linux_flock *));
79 static void linux_to_bsd_flock __P((struct linux_flock *, struct flock *));
80 #ifndef __amd64__
81 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat *));
82 static int linux_stat1 __P((struct lwp *, void *, register_t *, int));
83 #endif
84 
85 /*
86  * Some file-related calls are handled here. The usual flag conversion
87  * an structure conversion is done, and alternate emul path searching.
88  */
89 
90 /*
91  * The next two functions convert between the Linux and NetBSD values
92  * of the flags used in open(2) and fcntl(2).
93  */
94 static int
95 linux_to_bsd_ioflags(lflags)
96 	int lflags;
97 {
98 	int res = 0;
99 
100 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
101 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
102 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
103 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
104 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
105 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
106 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
107 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
108 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
109 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
110 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
111 
112 	return res;
113 }
114 
115 static int
116 bsd_to_linux_ioflags(bflags)
117 	int bflags;
118 {
119 	int res = 0;
120 
121 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
122 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
123 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
124 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
125 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
126 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
127 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
128 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
129 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
130 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
131 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
132 
133 	return res;
134 }
135 
136 /*
137  * creat(2) is an obsolete function, but it's present as a Linux
138  * system call, so let's deal with it.
139  *
140  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
141  * in syscalls.master anyway so this doesn't have to be special cased.
142  *
143  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
144  */
145 int
146 linux_sys_creat(l, v, retval)
147 	struct lwp *l;
148 	void *v;
149 	register_t *retval;
150 {
151 	struct linux_sys_creat_args /* {
152 		syscallarg(const char *) path;
153 		syscallarg(int) mode;
154 	} */ *uap = v;
155 	struct sys_open_args oa;
156 
157 	SCARG(&oa, path) = SCARG(uap, path);
158 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
159 	SCARG(&oa, mode) = SCARG(uap, mode);
160 
161 	return sys_open(l, &oa, retval);
162 }
163 
164 /*
165  * open(2). Take care of the different flag values, and let the
166  * NetBSD syscall do the real work. See if this operation
167  * gives the current process a controlling terminal.
168  * (XXX is this necessary?)
169  */
170 int
171 linux_sys_open(l, v, retval)
172 	struct lwp *l;
173 	void *v;
174 	register_t *retval;
175 {
176 	struct linux_sys_open_args /* {
177 		syscallarg(const char *) path;
178 		syscallarg(int) flags;
179 		syscallarg(int) mode;
180 	} */ *uap = v;
181 	struct proc *p = l->l_proc;
182 	int error, fl;
183 	struct sys_open_args boa;
184 
185 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
186 
187 	SCARG(&boa, path) = SCARG(uap, path);
188 	SCARG(&boa, flags) = fl;
189 	SCARG(&boa, mode) = SCARG(uap, mode);
190 
191 	if ((error = sys_open(l, &boa, retval)))
192 		return error;
193 
194 	/*
195 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
196 	 * If we are a session leader, and we don't have a controlling
197 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
198 	 * this the controlling terminal.
199 	 */
200         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
201                 struct filedesc *fdp = p->p_fd;
202                 struct file     *fp;
203 
204 		fp = fd_getfile(fdp, *retval);
205 
206                 /* ignore any error, just give it a try */
207                 if (fp != NULL) {
208 			FILE_USE(fp);
209 			if (fp->f_type == DTYPE_VNODE) {
210 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
211 				    (void *) 0, l);
212 			}
213 			FILE_UNUSE(fp, l);
214 		}
215         }
216 	return 0;
217 }
218 
219 /*
220  * The next two functions take care of converting the flock
221  * structure back and forth between Linux and NetBSD format.
222  * The only difference in the structures is the order of
223  * the fields, and the 'whence' value.
224  */
225 static void
226 bsd_to_linux_flock(bfp, lfp)
227 	struct flock *bfp;
228 	struct linux_flock *lfp;
229 {
230 
231 	lfp->l_start = bfp->l_start;
232 	lfp->l_len = bfp->l_len;
233 	lfp->l_pid = bfp->l_pid;
234 	lfp->l_whence = bfp->l_whence;
235 	switch (bfp->l_type) {
236 	case F_RDLCK:
237 		lfp->l_type = LINUX_F_RDLCK;
238 		break;
239 	case F_UNLCK:
240 		lfp->l_type = LINUX_F_UNLCK;
241 		break;
242 	case F_WRLCK:
243 		lfp->l_type = LINUX_F_WRLCK;
244 		break;
245 	}
246 }
247 
248 static void
249 linux_to_bsd_flock(lfp, bfp)
250 	struct linux_flock *lfp;
251 	struct flock *bfp;
252 {
253 
254 	bfp->l_start = lfp->l_start;
255 	bfp->l_len = lfp->l_len;
256 	bfp->l_pid = lfp->l_pid;
257 	bfp->l_whence = lfp->l_whence;
258 	switch (lfp->l_type) {
259 	case LINUX_F_RDLCK:
260 		bfp->l_type = F_RDLCK;
261 		break;
262 	case LINUX_F_UNLCK:
263 		bfp->l_type = F_UNLCK;
264 		break;
265 	case LINUX_F_WRLCK:
266 		bfp->l_type = F_WRLCK;
267 		break;
268 	}
269 }
270 
271 /*
272  * Most actions in the fcntl() call are straightforward; simply
273  * pass control to the NetBSD system call. A few commands need
274  * conversions after the actual system call has done its work,
275  * because the flag values and lock structure are different.
276  */
277 int
278 linux_sys_fcntl(l, v, retval)
279 	struct lwp *l;
280 	void *v;
281 	register_t *retval;
282 {
283 	struct linux_sys_fcntl_args /* {
284 		syscallarg(int) fd;
285 		syscallarg(int) cmd;
286 		syscallarg(void *) arg;
287 	} */ *uap = v;
288 	struct proc *p = l->l_proc;
289 	int fd, cmd, error;
290 	u_long val;
291 	void *arg;
292 	struct linux_flock lfl;
293 	struct flock bfl;
294 	struct sys_fcntl_args fca;
295 	struct filedesc *fdp;
296 	struct file *fp;
297 	struct vnode *vp;
298 	struct vattr va;
299 	const struct cdevsw *cdev;
300 	long pgid;
301 	struct pgrp *pgrp;
302 	struct tty *tp, *(*d_tty) __P((dev_t));
303 
304 	fd = SCARG(uap, fd);
305 	cmd = SCARG(uap, cmd);
306 	arg = (void *) SCARG(uap, arg);
307 
308 	switch (cmd) {
309 	case LINUX_F_DUPFD:
310 		cmd = F_DUPFD;
311 		break;
312 	case LINUX_F_GETFD:
313 		cmd = F_GETFD;
314 		break;
315 	case LINUX_F_SETFD:
316 		cmd = F_SETFD;
317 		break;
318 	case LINUX_F_GETFL:
319 		SCARG(&fca, fd) = fd;
320 		SCARG(&fca, cmd) = F_GETFL;
321 		SCARG(&fca, arg) = arg;
322 		if ((error = sys_fcntl(l, &fca, retval)))
323 			return error;
324 		retval[0] = bsd_to_linux_ioflags(retval[0]);
325 		return 0;
326 	case LINUX_F_SETFL: {
327 		struct file	*fp1 = NULL;
328 
329 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
330 		/*
331 		 * Linux seems to have same semantics for sending SIGIO to the
332 		 * read side of socket, but slightly different semantics
333 		 * for SIGIO to the write side.  Rather than sending the SIGIO
334 		 * every time it's possible to write (directly) more data, it
335 		 * only sends SIGIO if last write(2) failed due to insufficient
336 		 * memory to hold the data. This is compatible enough
337 		 * with NetBSD semantics to not do anything about the
338 		 * difference.
339 		 *
340 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
341 		 * ones and DTYPE_PIPE ones. For these, we don't set
342 		 * the underlying flags (we don't pass O_ASYNC flag down
343 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
344 		 * so that F_GETFL would report the ASYNC i/o is on.
345 		 */
346 		if (val & O_ASYNC) {
347 			if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
348 			    return (EBADF);
349 
350 			FILE_USE(fp1);
351 
352 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
353 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
354 			    || (fp1->f_type == DTYPE_PIPE))
355 				val &= ~O_ASYNC;
356 			else {
357 				/* not a pipe, do not modify anything */
358 				FILE_UNUSE(fp1, l);
359 				fp1 = NULL;
360 			}
361 		}
362 
363 		SCARG(&fca, fd) = fd;
364 		SCARG(&fca, cmd) = F_SETFL;
365 		SCARG(&fca, arg) = (void *) val;
366 
367 		error = sys_fcntl(l, &fca, retval);
368 
369 		/* Now set the FASYNC flag for pipes */
370 		if (fp1) {
371 			if (!error)
372 				fp1->f_flag |= FASYNC;
373 			FILE_UNUSE(fp1, l);
374 		}
375 
376 		return (error);
377 	    }
378 	case LINUX_F_GETLK:
379 		if ((error = copyin(arg, &lfl, sizeof lfl)))
380 			return error;
381 		linux_to_bsd_flock(&lfl, &bfl);
382 		error = do_fcntl_lock(l, fd, F_GETLK, &bfl);
383 		if (error)
384 			return error;
385 		bsd_to_linux_flock(&bfl, &lfl);
386 		return copyout(&lfl, arg, sizeof lfl);
387 
388 	case LINUX_F_SETLK:
389 	case LINUX_F_SETLKW:
390 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
391 		if ((error = copyin(arg, &lfl, sizeof lfl)))
392 			return error;
393 		linux_to_bsd_flock(&lfl, &bfl);
394 		return do_fcntl_lock(l, fd, cmd, &bfl);
395 
396 	case LINUX_F_SETOWN:
397 	case LINUX_F_GETOWN:
398 		/*
399 		 * We need to route fcntl() for tty descriptors around normal
400 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
401 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
402 		 * this is not a problem.
403 		 */
404 		fdp = p->p_fd;
405 		if ((fp = fd_getfile(fdp, fd)) == NULL)
406 			return EBADF;
407 		FILE_USE(fp);
408 
409 		/* Check it's a character device vnode */
410 		if (fp->f_type != DTYPE_VNODE
411 		    || (vp = (struct vnode *)fp->f_data) == NULL
412 		    || vp->v_type != VCHR) {
413 			FILE_UNUSE(fp, l);
414 
415 	    not_tty:
416 			/* Not a tty, proceed with common fcntl() */
417 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
418 			break;
419 		}
420 
421 		error = VOP_GETATTR(vp, &va, l->l_cred, l);
422 
423 		FILE_UNUSE(fp, l);
424 
425 		if (error)
426 			return error;
427 
428 		cdev = cdevsw_lookup(va.va_rdev);
429 		if (cdev == NULL)
430 			return (ENXIO);
431 		d_tty = cdev->d_tty;
432 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
433 			goto not_tty;
434 
435 		/* set tty pg_id appropriately */
436 		if (cmd == LINUX_F_GETOWN) {
437 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
438 			return 0;
439 		}
440 		mutex_enter(&proclist_lock);
441 		if ((long)arg <= 0) {
442 			pgid = -(long)arg;
443 		} else {
444 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
445 			if (p1 == NULL)
446 				return (ESRCH);
447 			pgid = (long)p1->p_pgrp->pg_id;
448 		}
449 		pgrp = pg_find(pgid, PFIND_LOCKED);
450 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
451 			mutex_exit(&proclist_lock);
452 			return EPERM;
453 		}
454 		tp->t_pgrp = pgrp;
455 		mutex_exit(&proclist_lock);
456 		return 0;
457 
458 	default:
459 		return EOPNOTSUPP;
460 	}
461 
462 	SCARG(&fca, fd) = fd;
463 	SCARG(&fca, cmd) = cmd;
464 	SCARG(&fca, arg) = arg;
465 
466 	return sys_fcntl(l, &fca, retval);
467 }
468 
469 #if !defined(__amd64__)
470 /*
471  * Convert a NetBSD stat structure to a Linux stat structure.
472  * Only the order of the fields and the padding in the structure
473  * is different. linux_fakedev is a machine-dependent function
474  * which optionally converts device driver major/minor numbers
475  * (XXX horrible, but what can you do against code that compares
476  * things against constant major device numbers? sigh)
477  */
478 static void
479 bsd_to_linux_stat(bsp, lsp)
480 	struct stat *bsp;
481 	struct linux_stat *lsp;
482 {
483 
484 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
485 	lsp->lst_ino     = bsp->st_ino;
486 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
487 	if (bsp->st_nlink >= (1 << 15))
488 		lsp->lst_nlink = (1 << 15) - 1;
489 	else
490 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
491 	lsp->lst_uid     = bsp->st_uid;
492 	lsp->lst_gid     = bsp->st_gid;
493 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
494 	lsp->lst_size    = bsp->st_size;
495 	lsp->lst_blksize = bsp->st_blksize;
496 	lsp->lst_blocks  = bsp->st_blocks;
497 	lsp->lst_atime   = bsp->st_atime;
498 	lsp->lst_mtime   = bsp->st_mtime;
499 	lsp->lst_ctime   = bsp->st_ctime;
500 #ifdef LINUX_STAT_HAS_NSEC
501 	lsp->lst_atime_nsec   = bsp->st_atimensec;
502 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
503 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
504 #endif
505 }
506 
507 /*
508  * The stat functions below are plain sailing. stat and lstat are handled
509  * by one function to avoid code duplication.
510  */
511 int
512 linux_sys_fstat(l, v, retval)
513 	struct lwp *l;
514 	void *v;
515 	register_t *retval;
516 {
517 	struct linux_sys_fstat_args /* {
518 		syscallarg(int) fd;
519 		syscallarg(linux_stat *) sp;
520 	} */ *uap = v;
521 	struct linux_stat tmplst;
522 	struct stat tmpst;
523 	int error;
524 
525 	error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
526 	if (error != 0)
527 		return error;
528 	bsd_to_linux_stat(&tmpst, &tmplst);
529 
530 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
531 }
532 
533 static int
534 linux_stat1(l, v, retval, flags)
535 	struct lwp *l;
536 	void *v;
537 	register_t *retval;
538 	int flags;
539 {
540 	struct linux_stat tmplst;
541 	struct stat tmpst;
542 	int error;
543 	struct linux_sys_stat_args *uap = v;
544 
545 	error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
546 	if (error != 0)
547 		return error;
548 
549 	bsd_to_linux_stat(&tmpst, &tmplst);
550 
551 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
552 }
553 
554 int
555 linux_sys_stat(l, v, retval)
556 	struct lwp *l;
557 	void *v;
558 	register_t *retval;
559 {
560 	struct linux_sys_stat_args /* {
561 		syscallarg(const char *) path;
562 		syscallarg(struct linux_stat *) sp;
563 	} */ *uap = v;
564 
565 	return linux_stat1(l, uap, retval, FOLLOW);
566 }
567 
568 /* Note: this is "newlstat" in the Linux sources */
569 /*	(we don't bother with the old lstat currently) */
570 int
571 linux_sys_lstat(l, v, retval)
572 	struct lwp *l;
573 	void *v;
574 	register_t *retval;
575 {
576 	struct linux_sys_lstat_args /* {
577 		syscallarg(const char *) path;
578 		syscallarg(struct linux_stat *) sp;
579 	} */ *uap = v;
580 
581 	return linux_stat1(l, uap, retval, NOFOLLOW);
582 }
583 #endif /* !__amd64__ */
584 
585 /*
586  * The following syscalls are mostly here because of the alternate path check.
587  */
588 int
589 linux_sys_access(l, v, retval)
590 	struct lwp *l;
591 	void *v;
592 	register_t *retval;
593 {
594 	struct linux_sys_access_args /* {
595 		syscallarg(const char *) path;
596 		syscallarg(int) flags;
597 	} */ *uap = v;
598 
599 	return sys_access(l, uap, retval);
600 }
601 
602 int
603 linux_sys_unlink(l, v, retval)
604 	struct lwp *l;
605 	void *v;
606 	register_t *retval;
607 
608 {
609 	struct linux_sys_unlink_args /* {
610 		syscallarg(const char *) path;
611 	} */ *uap = v;
612 	int error;
613 	struct nameidata nd;
614 
615 	error = sys_unlink(l, uap, retval);
616 	if (error != EPERM)
617 		return (error);
618 
619 	/*
620 	 * Linux returns EISDIR if unlink(2) is called on a directory.
621 	 * We return EPERM in such cases. To emulate correct behaviour,
622 	 * check if the path points to directory and return EISDIR if this
623 	 * is the case.
624 	 */
625 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
626 	    SCARG(uap, path), l);
627 	if (namei(&nd) == 0) {
628 		struct stat sb;
629 
630 		if (vn_stat(nd.ni_vp, &sb, l) == 0
631 		    && S_ISDIR(sb.st_mode))
632 			error = EISDIR;
633 
634 		vput(nd.ni_vp);
635 	}
636 
637 	return (error);
638 }
639 
640 int
641 linux_sys_chdir(l, v, retval)
642 	struct lwp *l;
643 	void *v;
644 	register_t *retval;
645 {
646 	struct linux_sys_chdir_args /* {
647 		syscallarg(const char *) path;
648 	} */ *uap = v;
649 
650 	return sys_chdir(l, uap, retval);
651 }
652 
653 int
654 linux_sys_mknod(l, v, retval)
655 	struct lwp *l;
656 	void *v;
657 	register_t *retval;
658 {
659 	struct linux_sys_mknod_args /* {
660 		syscallarg(const char *) path;
661 		syscallarg(int) mode;
662 		syscallarg(int) dev;
663 	} */ *uap = v;
664 
665 	/*
666 	 * BSD handles FIFOs separately
667 	 */
668 	if (S_ISFIFO(SCARG(uap, mode))) {
669 		struct sys_mkfifo_args bma;
670 
671 		SCARG(&bma, path) = SCARG(uap, path);
672 		SCARG(&bma, mode) = SCARG(uap, mode);
673 		return sys_mkfifo(l, &bma, retval);
674 	} else {
675 		struct sys_mknod_args bma;
676 
677 		SCARG(&bma, path) = SCARG(uap, path);
678 		SCARG(&bma, mode) = SCARG(uap, mode);
679 		/*
680 		 * Linux device numbers uses 8 bits for minor and 8 bits
681 		 * for major. Due to how we map our major and minor,
682 		 * this just fits into our dev_t. Just mask off the
683 		 * upper 16bit to remove any random junk.
684 		 */
685 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
686 		return sys_mknod(l, &bma, retval);
687 	}
688 }
689 
690 int
691 linux_sys_chmod(l, v, retval)
692 	struct lwp *l;
693 	void *v;
694 	register_t *retval;
695 {
696 	struct linux_sys_chmod_args /* {
697 		syscallarg(const char *) path;
698 		syscallarg(int) mode;
699 	} */ *uap = v;
700 
701 	return sys_chmod(l, uap, retval);
702 }
703 
704 #if defined(__i386__) || defined(__m68k__) || \
705     defined(__arm__)
706 int
707 linux_sys_chown16(l, v, retval)
708 	struct lwp *l;
709 	void *v;
710 	register_t *retval;
711 {
712 	struct linux_sys_chown16_args /* {
713 		syscallarg(const char *) path;
714 		syscallarg(int) uid;
715 		syscallarg(int) gid;
716 	} */ *uap = v;
717 	struct sys___posix_chown_args bca;
718 
719 	SCARG(&bca, path) = SCARG(uap, path);
720 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
721 		(uid_t)-1 : SCARG(uap, uid);
722 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
723 		(gid_t)-1 : SCARG(uap, gid);
724 
725 	return sys___posix_chown(l, &bca, retval);
726 }
727 
728 int
729 linux_sys_fchown16(l, v, retval)
730 	struct lwp *l;
731 	void *v;
732 	register_t *retval;
733 {
734 	struct linux_sys_fchown16_args /* {
735 		syscallarg(int) fd;
736 		syscallarg(int) uid;
737 		syscallarg(int) gid;
738 	} */ *uap = v;
739 	struct sys___posix_fchown_args bfa;
740 
741 	SCARG(&bfa, fd) = SCARG(uap, fd);
742 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
743 		(uid_t)-1 : SCARG(uap, uid);
744 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
745 		(gid_t)-1 : SCARG(uap, gid);
746 
747 	return sys___posix_fchown(l, &bfa, retval);
748 }
749 
750 int
751 linux_sys_lchown16(l, v, retval)
752 	struct lwp *l;
753 	void *v;
754 	register_t *retval;
755 {
756 	struct linux_sys_lchown16_args /* {
757 		syscallarg(char *) path;
758 		syscallarg(int) uid;
759 		syscallarg(int) gid;
760 	} */ *uap = v;
761 	struct sys___posix_lchown_args bla;
762 
763 	SCARG(&bla, path) = SCARG(uap, path);
764 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
765 		(uid_t)-1 : SCARG(uap, uid);
766 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
767 		(gid_t)-1 : SCARG(uap, gid);
768 
769 	return sys___posix_lchown(l, &bla, retval);
770 }
771 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
772 #if defined (__i386__) || defined (__m68k__) || defined(__amd64__) || \
773     defined (__powerpc__) || defined (__mips__) || defined (__arm__)
774 int
775 linux_sys_chown(l, v, retval)
776 	struct lwp *l;
777 	void *v;
778 	register_t *retval;
779 {
780 	struct linux_sys_chown_args /* {
781 		syscallarg(char *) path;
782 		syscallarg(int) uid;
783 		syscallarg(int) gid;
784 	} */ *uap = v;
785 
786 	return sys___posix_chown(l, uap, retval);
787 }
788 
789 int
790 linux_sys_lchown(l, v, retval)
791 	struct lwp *l;
792 	void *v;
793 	register_t *retval;
794 {
795 	struct linux_sys_lchown_args /* {
796 		syscallarg(char *) path;
797 		syscallarg(int) uid;
798 		syscallarg(int) gid;
799 	} */ *uap = v;
800 
801 	return sys___posix_lchown(l, uap, retval);
802 }
803 #endif /* __i386__||__m68k__||__powerpc__||__mips__||__arm__ ||__amd64__ */
804 
805 int
806 linux_sys_rename(l, v, retval)
807 	struct lwp *l;
808 	void *v;
809 	register_t *retval;
810 {
811 	struct linux_sys_rename_args /* {
812 		syscallarg(const char *) from;
813 		syscallarg(const char *) to;
814 	} */ *uap = v;
815 
816 	return sys___posix_rename(l, uap, retval);
817 }
818 
819 int
820 linux_sys_mkdir(l, v, retval)
821 	struct lwp *l;
822 	void *v;
823 	register_t *retval;
824 {
825 	struct linux_sys_mkdir_args /* {
826 		syscallarg(const char *) path;
827 		syscallarg(int) mode;
828 	} */ *uap = v;
829 
830 	return sys_mkdir(l, uap, retval);
831 }
832 
833 int
834 linux_sys_rmdir(l, v, retval)
835 	struct lwp *l;
836 	void *v;
837 	register_t *retval;
838 {
839 	struct linux_sys_rmdir_args /* {
840 		syscallarg(const char *) path;
841 	} */ *uap = v;
842 
843 	return sys_rmdir(l, uap, retval);
844 }
845 
846 int
847 linux_sys_symlink(l, v, retval)
848 	struct lwp *l;
849 	void *v;
850 	register_t *retval;
851 {
852 	struct linux_sys_symlink_args /* {
853 		syscallarg(const char *) path;
854 		syscallarg(const char *) to;
855 	} */ *uap = v;
856 
857 	return sys_symlink(l, uap, retval);
858 }
859 
860 int
861 linux_sys_link(l, v, retval)
862 	struct lwp *l;
863 	void *v;
864 	register_t *retval;
865 {
866 	struct linux_sys_link_args /* {
867 		syscallarg(const char *) path;
868 		syscallarg(const char *) link;
869 	} */ *uap = v;
870 
871 	return sys_link(l, uap, retval);
872 }
873 
874 int
875 linux_sys_readlink(l, v, retval)
876 	struct lwp *l;
877 	void *v;
878 	register_t *retval;
879 {
880 	struct linux_sys_readlink_args /* {
881 		syscallarg(const char *) name;
882 		syscallarg(char *) buf;
883 		syscallarg(int) count;
884 	} */ *uap = v;
885 
886 	return sys_readlink(l, uap, retval);
887 }
888 
889 #if !defined(__amd64__)
890 int
891 linux_sys_truncate(l, v, retval)
892 	struct lwp *l;
893 	void *v;
894 	register_t *retval;
895 {
896 	struct linux_sys_truncate_args /* {
897 		syscallarg(const char *) path;
898 		syscallarg(long) length;
899 	} */ *uap = v;
900 
901 	return compat_43_sys_truncate(l, uap, retval);
902 }
903 #endif /* !__amd64__ */
904 
905 /*
906  * This is just fsync() for now (just as it is in the Linux kernel)
907  * Note: this is not implemented under Linux on Alpha and Arm
908  *	but should still be defined in our syscalls.master.
909  *	(syscall #148 on the arm)
910  */
911 int
912 linux_sys_fdatasync(l, v, retval)
913 	struct lwp *l;
914 	void *v;
915 	register_t *retval;
916 {
917 #ifdef notdef
918 	struct linux_sys_fdatasync_args /* {
919 		syscallarg(int) fd;
920 	} */ *uap = v;
921 #endif
922 	return sys_fsync(l, v, retval);
923 }
924 
925 /*
926  * pread(2).
927  */
928 int
929 linux_sys_pread(l, v, retval)
930 	struct lwp *l;
931 	void *v;
932 	register_t *retval;
933 {
934 	struct linux_sys_pread_args /* {
935 		syscallarg(int) fd;
936 		syscallarg(void *) buf;
937 		syscallarg(size_t) nbyte;
938 		syscallarg(linux_off_t) offset;
939 	} */ *uap = v;
940 	struct sys_pread_args pra;
941 
942 	SCARG(&pra, fd) = SCARG(uap, fd);
943 	SCARG(&pra, buf) = SCARG(uap, buf);
944 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
945 	SCARG(&pra, offset) = SCARG(uap, offset);
946 
947 	return sys_pread(l, &pra, retval);
948 }
949 
950 /*
951  * pwrite(2).
952  */
953 int
954 linux_sys_pwrite(l, v, retval)
955 	struct lwp *l;
956 	void *v;
957 	register_t *retval;
958 {
959 	struct linux_sys_pwrite_args /* {
960 		syscallarg(int) fd;
961 		syscallarg(void *) buf;
962 		syscallarg(size_t) nbyte;
963 		syscallarg(linux_off_t) offset;
964 	} */ *uap = v;
965 	struct sys_pwrite_args pra;
966 
967 	SCARG(&pra, fd) = SCARG(uap, fd);
968 	SCARG(&pra, buf) = SCARG(uap, buf);
969 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
970 	SCARG(&pra, offset) = SCARG(uap, offset);
971 
972 	return sys_pwrite(l, &pra, retval);
973 }
974 
975 #define LINUX_NOT_SUPPORTED(fun) \
976 int \
977 fun(struct lwp *l, void *v, register_t *retval) \
978 { \
979 	return EOPNOTSUPP; \
980 }
981 
982 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
983 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
984 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
985 
986 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
987 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
988 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
989 
990 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
991 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
992 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
993 
994 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
995 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
996 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
997