xref: /netbsd-src/sys/compat/linux/common/linux_file.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: linux_file.c,v 1.58 2003/03/19 11:36:35 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.58 2003/03/19 11:36:35 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/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() not needed here */
431 		if (fp->f_type != DTYPE_VNODE) {
432 	    not_tty:
433 			/* Not a tty, proceed with common fcntl() */
434 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
435 			break;
436 		}
437 
438 		/* check that the vnode is a tty */
439 		vp = (struct vnode *)fp->f_data;
440 		if (vp->v_type != VCHR)
441 			goto not_tty;
442 		if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
443 			return error;
444 		cdev = cdevsw_lookup(va.va_rdev);
445 		if (cdev == NULL)
446 			return (ENXIO);
447 		d_tty = cdev->d_tty;
448 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
449 			goto not_tty;
450 
451 		/* set tty pg_id appropriately */
452 		if (cmd == LINUX_F_GETOWN) {
453 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
454 			return 0;
455 		}
456 		if ((long)arg <= 0) {
457 			pgid = -(long)arg;
458 		} else {
459 			struct proc *p1 = pfind((long)arg);
460 			if (p1 == NULL)
461 				return (ESRCH);
462 			pgid = (long)p1->p_pgrp->pg_id;
463 		}
464 		pgrp = pgfind(pgid);
465 		if (pgrp == NULL || pgrp->pg_session != p->p_session)
466 			return EPERM;
467 		tp->t_pgrp = pgrp;
468 		return 0;
469 
470 	default:
471 		return EOPNOTSUPP;
472 	}
473 
474 	SCARG(&fca, fd) = fd;
475 	SCARG(&fca, cmd) = cmd;
476 	SCARG(&fca, arg) = arg;
477 
478 	return sys_fcntl(l, &fca, retval);
479 }
480 
481 /*
482  * Convert a NetBSD stat structure to a Linux stat structure.
483  * Only the order of the fields and the padding in the structure
484  * is different. linux_fakedev is a machine-dependent function
485  * which optionally converts device driver major/minor numbers
486  * (XXX horrible, but what can you do against code that compares
487  * things against constant major device numbers? sigh)
488  */
489 static void
490 bsd_to_linux_stat(bsp, lsp)
491 	struct stat *bsp;
492 	struct linux_stat *lsp;
493 {
494 
495 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
496 	lsp->lst_ino     = bsp->st_ino;
497 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
498 	if (bsp->st_nlink >= (1 << 15))
499 		lsp->lst_nlink = (1 << 15) - 1;
500 	else
501 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
502 	lsp->lst_uid     = bsp->st_uid;
503 	lsp->lst_gid     = bsp->st_gid;
504 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
505 	lsp->lst_size    = bsp->st_size;
506 	lsp->lst_blksize = bsp->st_blksize;
507 	lsp->lst_blocks  = bsp->st_blocks;
508 	lsp->lst_atime   = bsp->st_atime;
509 	lsp->lst_mtime   = bsp->st_mtime;
510 	lsp->lst_ctime   = bsp->st_ctime;
511 }
512 
513 /*
514  * The stat functions below are plain sailing. stat and lstat are handled
515  * by one function to avoid code duplication.
516  */
517 int
518 linux_sys_fstat(l, v, retval)
519 	struct lwp *l;
520 	void *v;
521 	register_t *retval;
522 {
523 	struct linux_sys_fstat_args /* {
524 		syscallarg(int) fd;
525 		syscallarg(linux_stat *) sp;
526 	} */ *uap = v;
527 	struct proc *p = l->l_proc;
528 	struct sys___fstat13_args fsa;
529 	struct linux_stat tmplst;
530 	struct stat *st,tmpst;
531 	caddr_t sg;
532 	int error;
533 
534 	sg = stackgap_init(p, 0);
535 
536 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
537 
538 	SCARG(&fsa, fd) = SCARG(uap, fd);
539 	SCARG(&fsa, sb) = st;
540 
541 	if ((error = sys___fstat13(l, &fsa, retval)))
542 		return error;
543 
544 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
545 		return error;
546 
547 	bsd_to_linux_stat(&tmpst, &tmplst);
548 
549 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
550 		return error;
551 
552 	return 0;
553 }
554 
555 static int
556 linux_stat1(l, v, retval, dolstat)
557 	struct lwp *l;
558 	void *v;
559 	register_t *retval;
560 	int dolstat;
561 {
562 	struct sys___stat13_args sa;
563 	struct linux_stat tmplst;
564 	struct stat *st, tmpst;
565 	struct proc *p = l->l_proc;
566 	caddr_t sg;
567 	int error;
568 	struct linux_sys_stat_args *uap = v;
569 
570 	sg = stackgap_init(p, 0);
571 	st = stackgap_alloc(p, &sg, sizeof (struct stat));
572 	if (dolstat)
573 		CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
574 	else
575 		CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
576 
577 	SCARG(&sa, ub) = st;
578 	SCARG(&sa, path) = SCARG(uap, path);
579 
580 	if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
581 				sys___stat13(l, &sa, retval))))
582 		return error;
583 
584 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
585 		return error;
586 
587 	bsd_to_linux_stat(&tmpst, &tmplst);
588 
589 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
590 		return error;
591 
592 	return 0;
593 }
594 
595 int
596 linux_sys_stat(l, v, retval)
597 	struct lwp *l;
598 	void *v;
599 	register_t *retval;
600 {
601 	struct linux_sys_stat_args /* {
602 		syscallarg(const char *) path;
603 		syscallarg(struct linux_stat *) sp;
604 	} */ *uap = v;
605 
606 	return linux_stat1(l, uap, retval, 0);
607 }
608 
609 /* Note: this is "newlstat" in the Linux sources */
610 /*	(we don't bother with the old lstat currently) */
611 int
612 linux_sys_lstat(l, v, retval)
613 	struct lwp *l;
614 	void *v;
615 	register_t *retval;
616 {
617 	struct linux_sys_lstat_args /* {
618 		syscallarg(const char *) path;
619 		syscallarg(struct linux_stat *) sp;
620 	} */ *uap = v;
621 
622 	return linux_stat1(l, uap, retval, 1);
623 }
624 
625 /*
626  * The following syscalls are mostly here because of the alternate path check.
627  */
628 int
629 linux_sys_access(l, v, retval)
630 	struct lwp *l;
631 	void *v;
632 	register_t *retval;
633 {
634 	struct linux_sys_access_args /* {
635 		syscallarg(const char *) path;
636 		syscallarg(int) flags;
637 	} */ *uap = v;
638 	struct proc *p = l->l_proc;
639 	caddr_t sg = stackgap_init(p, 0);
640 
641 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
642 
643 	return sys_access(l, uap, retval);
644 }
645 
646 int
647 linux_sys_unlink(l, v, retval)
648 	struct lwp *l;
649 	void *v;
650 	register_t *retval;
651 
652 {
653 	struct linux_sys_unlink_args /* {
654 		syscallarg(const char *) path;
655 	} */ *uap = v;
656 	struct proc *p = l->l_proc;
657 	caddr_t sg = stackgap_init(p, 0);
658 
659 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
660 
661 	return sys_unlink(l, uap, retval);
662 }
663 
664 int
665 linux_sys_chdir(l, v, retval)
666 	struct lwp *l;
667 	void *v;
668 	register_t *retval;
669 {
670 	struct linux_sys_chdir_args /* {
671 		syscallarg(const char *) path;
672 	} */ *uap = v;
673 	struct proc *p = l->l_proc;
674 	caddr_t sg = stackgap_init(p, 0);
675 
676 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
677 
678 	return sys_chdir(l, uap, retval);
679 }
680 
681 int
682 linux_sys_mknod(l, v, retval)
683 	struct lwp *l;
684 	void *v;
685 	register_t *retval;
686 {
687 	struct linux_sys_mknod_args /* {
688 		syscallarg(const char *) path;
689 		syscallarg(int) mode;
690 		syscallarg(int) dev;
691 	} */ *uap = v;
692 	struct proc *p = l->l_proc;
693 	caddr_t sg = stackgap_init(p, 0);
694 
695 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
696 
697 	/*
698 	 * BSD handles FIFOs separately
699 	 */
700 	if (SCARG(uap, mode) & S_IFIFO) {
701 		struct sys_mkfifo_args bma;
702 
703 		SCARG(&bma, path) = SCARG(uap, path);
704 		SCARG(&bma, mode) = SCARG(uap, mode);
705 		return sys_mkfifo(l, &bma, retval);
706 	} else {
707 		struct sys_mknod_args bma;
708 
709 		SCARG(&bma, path) = SCARG(uap, path);
710 		SCARG(&bma, mode) = SCARG(uap, mode);
711 		/*
712 		 * Linux device numbers uses 8 bits for minor and 8 bits
713 		 * for major. Due to how we map our major and minor,
714 		 * this just fints into our dev_t. Just mask off the
715 		 * upper 16bit to remove any random junk.
716 		 */
717 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
718 		return sys_mknod(l, &bma, retval);
719 	}
720 }
721 
722 int
723 linux_sys_chmod(l, v, retval)
724 	struct lwp *l;
725 	void *v;
726 	register_t *retval;
727 {
728 	struct linux_sys_chmod_args /* {
729 		syscallarg(const char *) path;
730 		syscallarg(int) mode;
731 	} */ *uap = v;
732 	struct proc *p = l->l_proc;
733 	caddr_t sg = stackgap_init(p, 0);
734 
735 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
736 
737 	return sys_chmod(l, uap, retval);
738 }
739 
740 #if defined(__i386__) || defined(__m68k__) || defined(__arm__)
741 int
742 linux_sys_chown16(l, v, retval)
743 	struct lwp *l;
744 	void *v;
745 	register_t *retval;
746 {
747 	struct linux_sys_chown16_args /* {
748 		syscallarg(const char *) path;
749 		syscallarg(int) uid;
750 		syscallarg(int) gid;
751 	} */ *uap = v;
752 	struct proc *p = l->l_proc;
753 	struct sys___posix_chown_args bca;
754 	caddr_t sg = stackgap_init(p, 0);
755 
756 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
757 
758 	SCARG(&bca, path) = SCARG(uap, path);
759 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
760 		(uid_t)-1 : SCARG(uap, uid);
761 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
762 		(gid_t)-1 : SCARG(uap, gid);
763 
764 	return sys___posix_chown(l, &bca, retval);
765 }
766 
767 int
768 linux_sys_fchown16(l, v, retval)
769 	struct lwp *l;
770 	void *v;
771 	register_t *retval;
772 {
773 	struct linux_sys_fchown16_args /* {
774 		syscallarg(int) fd;
775 		syscallarg(int) uid;
776 		syscallarg(int) gid;
777 	} */ *uap = v;
778 	struct sys___posix_fchown_args bfa;
779 
780 	SCARG(&bfa, fd) = SCARG(uap, fd);
781 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
782 		(uid_t)-1 : SCARG(uap, uid);
783 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
784 		(gid_t)-1 : SCARG(uap, gid);
785 
786 	return sys___posix_fchown(l, &bfa, retval);
787 }
788 
789 int
790 linux_sys_lchown16(l, v, retval)
791 	struct lwp *l;
792 	void *v;
793 	register_t *retval;
794 {
795 	struct linux_sys_lchown16_args /* {
796 		syscallarg(char *) path;
797 		syscallarg(int) uid;
798 		syscallarg(int) gid;
799 	} */ *uap = v;
800 	struct proc *p = l->l_proc;
801 	struct sys___posix_lchown_args bla;
802 	caddr_t sg = stackgap_init(p, 0);
803 
804 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
805 
806 	SCARG(&bla, path) = SCARG(uap, path);
807 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
808 		(uid_t)-1 : SCARG(uap, uid);
809 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
810 		(gid_t)-1 : SCARG(uap, gid);
811 
812 	return sys___posix_lchown(l, &bla, retval);
813 }
814 #endif /* __i386__ || __m68k__ || __arm__ */
815 #if defined (__i386__) || defined (__m68k__) || \
816     defined (__powerpc__) || defined (__mips__) || defined(__arm__)
817 int
818 linux_sys_chown(l, v, retval)
819 	struct lwp *l;
820 	void *v;
821 	register_t *retval;
822 {
823 	struct linux_sys_chown_args /* {
824 		syscallarg(char *) path;
825 		syscallarg(int) uid;
826 		syscallarg(int) gid;
827 	} */ *uap = v;
828 	struct proc *p = l->l_proc;
829 	caddr_t sg = stackgap_init(p, 0);
830 
831 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
832 
833 	return sys___posix_chown(l, uap, retval);
834 }
835 
836 int
837 linux_sys_lchown(l, v, retval)
838 	struct lwp *l;
839 	void *v;
840 	register_t *retval;
841 {
842 	struct linux_sys_lchown_args /* {
843 		syscallarg(char *) path;
844 		syscallarg(int) uid;
845 		syscallarg(int) gid;
846 	} */ *uap = v;
847 	struct proc *p = l->l_proc;
848 	caddr_t sg = stackgap_init(p, 0);
849 
850 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, path));
851 
852 	return sys___posix_lchown(l, uap, retval);
853 }
854 #endif /* __i386__ || __m68k__ || __powerpc__ || __mips__ || __arm__ */
855 
856 int
857 linux_sys_rename(l, v, retval)
858 	struct lwp *l;
859 	void *v;
860 	register_t *retval;
861 {
862 	struct linux_sys_rename_args /* {
863 		syscallarg(const char *) from;
864 		syscallarg(const char *) to;
865 	} */ *uap = v;
866 	struct proc *p = l->l_proc;
867 	caddr_t sg = stackgap_init(p, 0);
868 
869 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
870 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
871 
872 	return sys___posix_rename(l, uap, retval);
873 }
874 
875 int
876 linux_sys_mkdir(l, v, retval)
877 	struct lwp *l;
878 	void *v;
879 	register_t *retval;
880 {
881 	struct linux_sys_mkdir_args /* {
882 		syscallarg(const char *) path;
883 		syscallarg(int) mode;
884 	} */ *uap = v;
885 	struct proc *p = l->l_proc;
886 	caddr_t sg = stackgap_init(p, 0);
887 
888 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
889 
890 	return sys_mkdir(l, uap, retval);
891 }
892 
893 int
894 linux_sys_rmdir(l, v, retval)
895 	struct lwp *l;
896 	void *v;
897 	register_t *retval;
898 {
899 	struct linux_sys_rmdir_args /* {
900 		syscallarg(const char *) path;
901 	} */ *uap = v;
902 	struct proc *p = l->l_proc;
903 	caddr_t sg = stackgap_init(p, 0);
904 
905 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
906 
907 	return sys_rmdir(l, uap, retval);
908 }
909 
910 int
911 linux_sys_symlink(l, v, retval)
912 	struct lwp *l;
913 	void *v;
914 	register_t *retval;
915 {
916 	struct linux_sys_symlink_args /* {
917 		syscallarg(const char *) path;
918 		syscallarg(const char *) to;
919 	} */ *uap = v;
920 	struct proc *p = l->l_proc;
921 	caddr_t sg = stackgap_init(p, 0);
922 
923 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
924 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
925 
926 	return sys_symlink(l, uap, retval);
927 }
928 
929 int
930 linux_sys_link(l, v, retval)
931 	struct lwp *l;
932 	void *v;
933 	register_t *retval;
934 {
935 	struct linux_sys_link_args /* {
936 		syscallarg(const char *) path;
937 		syscallarg(const char *) link;
938 	} */ *uap = v;
939 	struct proc *p = l->l_proc;
940 	caddr_t sg = stackgap_init(p, 0);
941 
942 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
943 	CHECK_ALT_CREAT(p, &sg, SCARG(uap, link));
944 
945 	return sys_link(l, uap, retval);
946 }
947 
948 int
949 linux_sys_readlink(l, v, retval)
950 	struct lwp *l;
951 	void *v;
952 	register_t *retval;
953 {
954 	struct linux_sys_readlink_args /* {
955 		syscallarg(const char *) name;
956 		syscallarg(char *) buf;
957 		syscallarg(int) count;
958 	} */ *uap = v;
959 	struct proc *p = l->l_proc;
960 	caddr_t sg = stackgap_init(p, 0);
961 
962 	CHECK_ALT_SYMLINK(p, &sg, SCARG(uap, name));
963 
964 	return sys_readlink(l, uap, retval);
965 }
966 
967 int
968 linux_sys_truncate(l, v, retval)
969 	struct lwp *l;
970 	void *v;
971 	register_t *retval;
972 {
973 	struct linux_sys_truncate_args /* {
974 		syscallarg(const char *) path;
975 		syscallarg(long) length;
976 	} */ *uap = v;
977 	struct proc *p = l->l_proc;
978 	caddr_t sg = stackgap_init(p, 0);
979 
980 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
981 
982 	return compat_43_sys_truncate(l, uap, retval);
983 }
984 
985 /*
986  * This is just fsync() for now (just as it is in the Linux kernel)
987  * Note: this is not implemented under Linux on Alpha and Arm
988  *	but should still be defined in our syscalls.master.
989  *	(syscall #148 on the arm)
990  */
991 int
992 linux_sys_fdatasync(l, v, retval)
993 	struct lwp *l;
994 	void *v;
995 	register_t *retval;
996 {
997 #ifdef notdef
998 	struct linux_sys_fdatasync_args /* {
999 		syscallarg(int) fd;
1000 	} */ *uap = v;
1001 #endif
1002 	return sys_fsync(l, v, retval);
1003 }
1004 
1005 /*
1006  * pread(2).
1007  */
1008 int
1009 linux_sys_pread(l, v, retval)
1010 	struct lwp *l;
1011 	void *v;
1012 	register_t *retval;
1013 {
1014 	struct linux_sys_pread_args /* {
1015 		syscallarg(int) fd;
1016 		syscallarg(void *) buf;
1017 		syscallarg(size_t) nbyte;
1018 		syscallarg(linux_off_t) offset;
1019 	} */ *uap = v;
1020 	struct sys_pread_args pra;
1021 
1022 	SCARG(&pra, fd) = SCARG(uap, fd);
1023 	SCARG(&pra, buf) = SCARG(uap, buf);
1024 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1025 	SCARG(&pra, offset) = SCARG(uap, offset);
1026 
1027 	return sys_read(l, &pra, retval);
1028 }
1029 
1030 /*
1031  * pwrite(2).
1032  */
1033 int
1034 linux_sys_pwrite(l, v, retval)
1035 	struct lwp *l;
1036 	void *v;
1037 	register_t *retval;
1038 {
1039 	struct linux_sys_pwrite_args /* {
1040 		syscallarg(int) fd;
1041 		syscallarg(void *) buf;
1042 		syscallarg(size_t) nbyte;
1043 		syscallarg(linux_off_t) offset;
1044 	} */ *uap = v;
1045 	struct sys_pwrite_args pra;
1046 
1047 	SCARG(&pra, fd) = SCARG(uap, fd);
1048 	SCARG(&pra, buf) = SCARG(uap, buf);
1049 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
1050 	SCARG(&pra, offset) = SCARG(uap, offset);
1051 
1052 	return sys_write(l, &pra, retval);
1053 }
1054