Home | History | Annotate | Line # | Download | only in kern
sys_descrip.c revision 1.32
      1 /*	$NetBSD: sys_descrip.c,v 1.32 2019/02/03 03:19:28 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  * (c) UNIX System Laboratories, Inc.
     33  * All or some portions of this file are derived from material licensed
     34  * to the University of California by American Telephone and Telegraph
     35  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     36  * the permission of UNIX System Laboratories, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     63  */
     64 
     65 /*
     66  * System calls on descriptors.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.32 2019/02/03 03:19:28 mrg Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/filedesc.h>
     75 #include <sys/kernel.h>
     76 #include <sys/vnode.h>
     77 #include <sys/proc.h>
     78 #include <sys/file.h>
     79 #include <sys/namei.h>
     80 #include <sys/socket.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/stat.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/fcntl.h>
     85 #include <sys/kmem.h>
     86 #include <sys/pool.h>
     87 #include <sys/syslog.h>
     88 #include <sys/unistd.h>
     89 #include <sys/resourcevar.h>
     90 #include <sys/conf.h>
     91 #include <sys/event.h>
     92 #include <sys/kauth.h>
     93 #include <sys/atomic.h>
     94 #include <sys/mount.h>
     95 #include <sys/syscallargs.h>
     96 
     97 #include <uvm/uvm_readahead.h>
     98 
     99 /*
    100  * Duplicate a file descriptor.
    101  */
    102 int
    103 sys_dup(struct lwp *l, const struct sys_dup_args *uap, register_t *retval)
    104 {
    105 	/* {
    106 		syscallarg(int)	fd;
    107 	} */
    108 	int error, newfd, oldfd;
    109 	file_t *fp;
    110 
    111 	oldfd = SCARG(uap, fd);
    112 
    113 	if ((fp = fd_getfile(oldfd)) == NULL) {
    114 		return EBADF;
    115 	}
    116 	error = fd_dup(fp, 0, &newfd, false);
    117 	fd_putfile(oldfd);
    118 	*retval = newfd;
    119 	return error;
    120 }
    121 
    122 /*
    123  * Duplicate a file descriptor to a particular value.
    124  */
    125 int
    126 dodup(struct lwp *l, int from, int to, int flags, register_t *retval)
    127 {
    128 	int error;
    129 	file_t *fp;
    130 
    131 	if ((fp = fd_getfile(from)) == NULL)
    132 		return EBADF;
    133 	mutex_enter(&fp->f_lock);
    134 	fp->f_count++;
    135 	mutex_exit(&fp->f_lock);
    136 	fd_putfile(from);
    137 
    138 	if ((u_int)to >= curproc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    139 	    (u_int)to >= maxfiles)
    140 		error = EBADF;
    141 	else if (from == to)
    142 		error = 0;
    143 	else
    144 		error = fd_dup2(fp, to, flags);
    145 	closef(fp);
    146 	*retval = to;
    147 
    148 	return error;
    149 }
    150 
    151 int
    152 sys_dup3(struct lwp *l, const struct sys_dup3_args *uap, register_t *retval)
    153 {
    154 	/* {
    155 		syscallarg(int)	from;
    156 		syscallarg(int)	to;
    157 		syscallarg(int)	flags;
    158 	} */
    159 	return dodup(l, SCARG(uap, from), SCARG(uap, to), SCARG(uap, flags),
    160 	    retval);
    161 }
    162 
    163 int
    164 sys_dup2(struct lwp *l, const struct sys_dup2_args *uap, register_t *retval)
    165 {
    166 	/* {
    167 		syscallarg(int)	from;
    168 		syscallarg(int)	to;
    169 	} */
    170 	return dodup(l, SCARG(uap, from), SCARG(uap, to), 0, retval);
    171 }
    172 
    173 /*
    174  * fcntl call which is being passed to the file's fs.
    175  */
    176 static int
    177 fcntl_forfs(int fd, file_t *fp, int cmd, void *arg)
    178 {
    179 	int		error;
    180 	u_int		size;
    181 	void		*data, *memp;
    182 #define STK_PARAMS	128
    183 	char		stkbuf[STK_PARAMS];
    184 
    185 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    186 		return (EBADF);
    187 
    188 	/*
    189 	 * Interpret high order word to find amount of data to be
    190 	 * copied to/from the user's address space.
    191 	 */
    192 	size = (size_t)F_PARAM_LEN(cmd);
    193 	if (size > F_PARAM_MAX)
    194 		return (EINVAL);
    195 	memp = NULL;
    196 	if (size > sizeof(stkbuf)) {
    197 		memp = kmem_alloc(size, KM_SLEEP);
    198 		data = memp;
    199 	} else
    200 		data = stkbuf;
    201 	if (cmd & F_FSIN) {
    202 		if (size) {
    203 			error = copyin(arg, data, size);
    204 			if (error) {
    205 				if (memp)
    206 					kmem_free(memp, size);
    207 				return (error);
    208 			}
    209 		} else
    210 			*(void **)data = arg;
    211 	} else if ((cmd & F_FSOUT) != 0 && size != 0) {
    212 		/*
    213 		 * Zero the buffer so the user always
    214 		 * gets back something deterministic.
    215 		 */
    216 		memset(data, 0, size);
    217 	} else if (cmd & F_FSVOID)
    218 		*(void **)data = arg;
    219 
    220 
    221 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data);
    222 
    223 	/*
    224 	 * Copy any data to user, size was
    225 	 * already set and checked above.
    226 	 */
    227 	if (error == 0 && (cmd & F_FSOUT) && size)
    228 		error = copyout(data, arg, size);
    229 	if (memp)
    230 		kmem_free(memp, size);
    231 	return (error);
    232 }
    233 
    234 int
    235 do_fcntl_lock(int fd, int cmd, struct flock *fl)
    236 {
    237 	file_t *fp;
    238 	vnode_t *vp;
    239 	proc_t *p;
    240 	int error, flg;
    241 
    242 	if ((fp = fd_getfile(fd)) == NULL)
    243 		return EBADF;
    244 	if (fp->f_type != DTYPE_VNODE) {
    245 		fd_putfile(fd);
    246 		return EINVAL;
    247 	}
    248 	vp = fp->f_vnode;
    249 	if (fl->l_whence == SEEK_CUR)
    250 		fl->l_start += fp->f_offset;
    251 
    252 	flg = F_POSIX;
    253 	p = curproc;
    254 
    255 	switch (cmd) {
    256 	case F_SETLKW:
    257 		flg |= F_WAIT;
    258 		/* Fall into F_SETLK */
    259 
    260 		/* FALLTHROUGH */
    261 	case F_SETLK:
    262 		switch (fl->l_type) {
    263 		case F_RDLCK:
    264 			if ((fp->f_flag & FREAD) == 0) {
    265 				error = EBADF;
    266 				break;
    267 			}
    268 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    269 				mutex_enter(p->p_lock);
    270 				p->p_flag |= PK_ADVLOCK;
    271 				mutex_exit(p->p_lock);
    272 			}
    273 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    274 			break;
    275 
    276 		case F_WRLCK:
    277 			if ((fp->f_flag & FWRITE) == 0) {
    278 				error = EBADF;
    279 				break;
    280 			}
    281 			if ((p->p_flag & PK_ADVLOCK) == 0) {
    282 				mutex_enter(p->p_lock);
    283 				p->p_flag |= PK_ADVLOCK;
    284 				mutex_exit(p->p_lock);
    285 			}
    286 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    287 			break;
    288 
    289 		case F_UNLCK:
    290 			error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
    291 			break;
    292 
    293 		default:
    294 			error = EINVAL;
    295 			break;
    296 		}
    297 		break;
    298 
    299 	case F_GETLK:
    300 		if (fl->l_type != F_RDLCK &&
    301 		    fl->l_type != F_WRLCK &&
    302 		    fl->l_type != F_UNLCK) {
    303 			error = EINVAL;
    304 			break;
    305 		}
    306 		error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
    307 		break;
    308 
    309 	default:
    310 		error = EINVAL;
    311 		break;
    312 	}
    313 
    314 	fd_putfile(fd);
    315 	return error;
    316 }
    317 
    318 /*
    319  * The file control system call.
    320  */
    321 int
    322 sys_fcntl(struct lwp *l, const struct sys_fcntl_args *uap, register_t *retval)
    323 {
    324 	/* {
    325 		syscallarg(int)		fd;
    326 		syscallarg(int)		cmd;
    327 		syscallarg(void *)	arg;
    328 	} */
    329 	int fd, i, tmp, error, cmd, newmin;
    330 	filedesc_t *fdp;
    331 	file_t *fp;
    332 	struct flock fl;
    333 	bool cloexec = false;
    334 
    335 	fd = SCARG(uap, fd);
    336 	cmd = SCARG(uap, cmd);
    337 	fdp = l->l_fd;
    338 	error = 0;
    339 
    340 	switch (cmd) {
    341 	case F_CLOSEM:
    342 		if (fd < 0)
    343 			return EBADF;
    344 		while ((i = fdp->fd_lastfile) >= fd) {
    345 			if (fd_getfile(i) == NULL) {
    346 				/* Another thread has updated. */
    347 				continue;
    348 			}
    349 			fd_close(i);
    350 		}
    351 		return 0;
    352 
    353 	case F_MAXFD:
    354 		*retval = fdp->fd_lastfile;
    355 		return 0;
    356 
    357 	case F_SETLKW:
    358 	case F_SETLK:
    359 	case F_GETLK:
    360 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    361 		if (error)
    362 			return error;
    363 		error = do_fcntl_lock(fd, cmd, &fl);
    364 		if (cmd == F_GETLK && error == 0)
    365 			error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
    366 		return error;
    367 
    368 	default:
    369 		/* Handled below */
    370 		break;
    371 	}
    372 
    373 	if ((fp = fd_getfile(fd)) == NULL)
    374 		return (EBADF);
    375 
    376 	if ((cmd & F_FSCTL)) {
    377 		error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
    378 		fd_putfile(fd);
    379 		return error;
    380 	}
    381 
    382 	switch (cmd) {
    383 	case F_DUPFD_CLOEXEC:
    384 		cloexec = true;
    385 		/*FALLTHROUGH*/
    386 	case F_DUPFD:
    387 		newmin = (long)SCARG(uap, arg);
    388 		if ((u_int)newmin >=
    389 		    l->l_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    390 		    (u_int)newmin >= maxfiles) {
    391 			fd_putfile(fd);
    392 			return EINVAL;
    393 		}
    394 		error = fd_dup(fp, newmin, &i, cloexec);
    395 		*retval = i;
    396 		break;
    397 
    398 	case F_GETFD:
    399 		*retval = fdp->fd_dt->dt_ff[fd]->ff_exclose;
    400 		break;
    401 
    402 	case F_SETFD:
    403 		fd_set_exclose(l, fd,
    404 		    ((long)SCARG(uap, arg) & FD_CLOEXEC) != 0);
    405 		break;
    406 
    407 	case F_GETNOSIGPIPE:
    408 		*retval = (fp->f_flag & FNOSIGPIPE) != 0;
    409 		break;
    410 
    411 	case F_SETNOSIGPIPE:
    412 		if (SCARG(uap, arg))
    413 			atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
    414 		else
    415 			atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
    416 		*retval = 0;
    417 		break;
    418 
    419 	case F_GETFL:
    420 		*retval = OFLAGS(fp->f_flag);
    421 		break;
    422 
    423 	case F_SETFL:
    424 		/* XXX not guaranteed to be atomic. */
    425 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    426 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp);
    427 		if (error)
    428 			break;
    429 		i = tmp ^ fp->f_flag;
    430 		if (i & FNONBLOCK) {
    431 			int flgs = tmp & FNONBLOCK;
    432 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs);
    433 			if (error) {
    434 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
    435 				    &fp->f_flag);
    436 				break;
    437 			}
    438 		}
    439 		if (i & FASYNC) {
    440 			int flgs = tmp & FASYNC;
    441 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs);
    442 			if (error) {
    443 				if (i & FNONBLOCK) {
    444 					tmp = fp->f_flag & FNONBLOCK;
    445 					(void)(*fp->f_ops->fo_ioctl)(fp,
    446 						FIONBIO, &tmp);
    447 				}
    448 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
    449 				    &fp->f_flag);
    450 				break;
    451 			}
    452 		}
    453 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
    454 		break;
    455 
    456 	case F_GETOWN:
    457 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp);
    458 		*retval = tmp;
    459 		break;
    460 
    461 	case F_SETOWN:
    462 		tmp = (int)(uintptr_t) SCARG(uap, arg);
    463 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp);
    464 		break;
    465 
    466 	default:
    467 		error = EINVAL;
    468 	}
    469 
    470 	fd_putfile(fd);
    471 	return (error);
    472 }
    473 
    474 /*
    475  * Close a file descriptor.
    476  */
    477 int
    478 sys_close(struct lwp *l, const struct sys_close_args *uap, register_t *retval)
    479 {
    480 	/* {
    481 		syscallarg(int)	fd;
    482 	} */
    483 	int error;
    484 
    485 	if (fd_getfile(SCARG(uap, fd)) == NULL) {
    486 		return EBADF;
    487 	}
    488 
    489 	error = fd_close(SCARG(uap, fd));
    490 	if (error == ERESTART) {
    491 #ifdef DIAGNOSTIC
    492 		printf("pid %d: close returned ERESTART\n",
    493 		    (int)l->l_proc->p_pid);
    494 #endif
    495 		error = EINTR;
    496 	}
    497 
    498 	return error;
    499 }
    500 
    501 /*
    502  * Return status information about a file descriptor.
    503  * Common function for compat code.
    504  */
    505 int
    506 do_sys_fstat(int fd, struct stat *sb)
    507 {
    508 	file_t *fp;
    509 	int error;
    510 
    511 	if ((fp = fd_getfile(fd)) == NULL) {
    512 		return EBADF;
    513 	}
    514 	error = (*fp->f_ops->fo_stat)(fp, sb);
    515 	fd_putfile(fd);
    516 
    517 	return error;
    518 }
    519 
    520 /*
    521  * Return status information about a file descriptor.
    522  */
    523 int
    524 sys___fstat50(struct lwp *l, const struct sys___fstat50_args *uap,
    525 	      register_t *retval)
    526 {
    527 	/* {
    528 		syscallarg(int)			fd;
    529 		syscallarg(struct stat *)	sb;
    530 	} */
    531 	struct stat sb;
    532 	int error;
    533 
    534 	error = do_sys_fstat(SCARG(uap, fd), &sb);
    535 	if (error == 0) {
    536 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
    537 	}
    538 	return error;
    539 }
    540 
    541 /*
    542  * Return pathconf information about a file descriptor.
    543  */
    544 int
    545 sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap,
    546 	      register_t *retval)
    547 {
    548 	/* {
    549 		syscallarg(int)	fd;
    550 		syscallarg(int)	name;
    551 	} */
    552 	int fd, error;
    553 	file_t *fp;
    554 
    555 	fd = SCARG(uap, fd);
    556 	error = 0;
    557 
    558 	if ((fp = fd_getfile(fd)) == NULL) {
    559 		return (EBADF);
    560 	}
    561 	switch (fp->f_type) {
    562 	case DTYPE_SOCKET:
    563 	case DTYPE_PIPE:
    564 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    565 			error = EINVAL;
    566 		else
    567 			*retval = PIPE_BUF;
    568 		break;
    569 
    570 	case DTYPE_VNODE:
    571 		error = VOP_PATHCONF(fp->f_vnode, SCARG(uap, name), retval);
    572 		break;
    573 
    574 	case DTYPE_KQUEUE:
    575 		error = EINVAL;
    576 		break;
    577 
    578 	default:
    579 		error = EOPNOTSUPP;
    580 		break;
    581 	}
    582 
    583 	fd_putfile(fd);
    584 	return (error);
    585 }
    586 
    587 /*
    588  * Apply an advisory lock on a file descriptor.
    589  *
    590  * Just attempt to get a record lock of the requested type on
    591  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    592  */
    593 /* ARGSUSED */
    594 int
    595 sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval)
    596 {
    597 	/* {
    598 		syscallarg(int)	fd;
    599 		syscallarg(int)	how;
    600 	} */
    601 	int fd, how, error;
    602 	file_t *fp;
    603 	vnode_t	*vp;
    604 	struct flock lf;
    605 
    606 	fd = SCARG(uap, fd);
    607 	how = SCARG(uap, how);
    608 	error = 0;
    609 
    610 	if ((fp = fd_getfile(fd)) == NULL) {
    611 		return EBADF;
    612 	}
    613 	if (fp->f_type != DTYPE_VNODE) {
    614 		fd_putfile(fd);
    615 		return EOPNOTSUPP;
    616 	}
    617 
    618 	vp = fp->f_vnode;
    619 	lf.l_whence = SEEK_SET;
    620 	lf.l_start = 0;
    621 	lf.l_len = 0;
    622 
    623 	switch (how & ~LOCK_NB) {
    624 	case LOCK_UN:
    625 		lf.l_type = F_UNLCK;
    626 		atomic_and_uint(&fp->f_flag, ~FHASLOCK);
    627 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
    628 		fd_putfile(fd);
    629 		return error;
    630 	case LOCK_EX:
    631 		lf.l_type = F_WRLCK;
    632 		break;
    633 	case LOCK_SH:
    634 		lf.l_type = F_RDLCK;
    635 		break;
    636 	default:
    637 		fd_putfile(fd);
    638 		return EINVAL;
    639 	}
    640 
    641 	atomic_or_uint(&fp->f_flag, FHASLOCK);
    642 	if (how & LOCK_NB) {
    643 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
    644 	} else {
    645 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
    646 	}
    647 	fd_putfile(fd);
    648 	return error;
    649 }
    650 
    651 int
    652 do_posix_fadvise(int fd, off_t offset, off_t len, int advice)
    653 {
    654 	file_t *fp;
    655 	vnode_t *vp;
    656 	off_t endoffset;
    657 	int error;
    658 
    659 	CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
    660 	CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
    661 	CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
    662 
    663 	if (len == 0) {
    664 		endoffset = INT64_MAX;
    665 	} else if (len > 0 && (INT64_MAX - offset) >= len) {
    666 		endoffset = offset + len;
    667 	} else {
    668 		return EINVAL;
    669 	}
    670 	if ((fp = fd_getfile(fd)) == NULL) {
    671 		return EBADF;
    672 	}
    673 	if (fp->f_type != DTYPE_VNODE) {
    674 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
    675 			error = ESPIPE;
    676 		} else {
    677 			error = EOPNOTSUPP;
    678 		}
    679 		fd_putfile(fd);
    680 		return error;
    681 	}
    682 
    683 	switch (advice) {
    684 	case POSIX_FADV_WILLNEED:
    685 	case POSIX_FADV_DONTNEED:
    686 		vp = fp->f_vnode;
    687 		if (vp->v_type != VREG && vp->v_type != VBLK) {
    688 			fd_putfile(fd);
    689 			return 0;
    690 		}
    691 		break;
    692 	}
    693 
    694 	switch (advice) {
    695 	case POSIX_FADV_NORMAL:
    696 	case POSIX_FADV_RANDOM:
    697 	case POSIX_FADV_SEQUENTIAL:
    698 		/*
    699 		 * We ignore offset and size.  Must lock the file to
    700 		 * do this, as f_advice is sub-word sized.
    701 		 */
    702 		mutex_enter(&fp->f_lock);
    703 		fp->f_advice = (u_char)advice;
    704 		mutex_exit(&fp->f_lock);
    705 		error = 0;
    706 		break;
    707 
    708 	case POSIX_FADV_WILLNEED:
    709 		vp = fp->f_vnode;
    710 		error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset);
    711 		break;
    712 
    713 	case POSIX_FADV_DONTNEED:
    714 		vp = fp->f_vnode;
    715 		/*
    716 		 * Align the region to page boundaries as VOP_PUTPAGES expects
    717 		 * by shrinking it.  We shrink instead of expand because we
    718 		 * do not want to deactivate cache outside of the requested
    719 		 * region.  It means that if the specified region is smaller
    720 		 * than PAGE_SIZE, we do nothing.
    721 		 */
    722 		if (round_page(offset) < trunc_page(endoffset) &&
    723 		    offset <= round_page(offset)) {
    724 			mutex_enter(vp->v_interlock);
    725 			error = VOP_PUTPAGES(vp,
    726 			    round_page(offset), trunc_page(endoffset),
    727 			    PGO_DEACTIVATE | PGO_CLEANIT);
    728 		} else {
    729 			error = 0;
    730 		}
    731 		break;
    732 
    733 	case POSIX_FADV_NOREUSE:
    734 		/* Not implemented yet. */
    735 		error = 0;
    736 		break;
    737 	default:
    738 		error = EINVAL;
    739 		break;
    740 	}
    741 
    742 	fd_putfile(fd);
    743 	return error;
    744 }
    745 
    746 int
    747 sys___posix_fadvise50(struct lwp *l,
    748 		      const struct sys___posix_fadvise50_args *uap,
    749 		      register_t *retval)
    750 {
    751 	/* {
    752 		syscallarg(int) fd;
    753 		syscallarg(int) pad;
    754 		syscallarg(off_t) offset;
    755 		syscallarg(off_t) len;
    756 		syscallarg(int) advice;
    757 	} */
    758 
    759 	*retval = do_posix_fadvise(SCARG(uap, fd), SCARG(uap, offset),
    760 	    SCARG(uap, len), SCARG(uap, advice));
    761 
    762 	return 0;
    763 }
    764 
    765 int
    766 sys_pipe(struct lwp *l, const void *v, register_t *retval)
    767 {
    768 	int fd[2], error;
    769 
    770 	if ((error = pipe1(l, fd, 0)) != 0)
    771 		return error;
    772 
    773 	retval[0] = fd[0];
    774 	retval[1] = fd[1];
    775 
    776 	return 0;
    777 }
    778 
    779 int
    780 sys_pipe2(struct lwp *l, const struct sys_pipe2_args *uap, register_t *retval)
    781 {
    782 	/* {
    783 		syscallarg(int[2]) fildes;
    784 		syscallarg(int) flags;
    785 	} */
    786 	int fd[2], error;
    787 
    788 	if ((error = pipe1(l, fd, SCARG(uap, flags))) != 0)
    789 		return error;
    790 
    791 	if ((error = copyout(fd, SCARG(uap, fildes), sizeof(fd))) != 0)
    792 		return error;
    793 	retval[0] = 0;
    794 	return 0;
    795 }
    796