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