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