xref: /netbsd-src/sys/kern/sys_descrip.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: sys_descrip.c,v 1.33 2019/05/21 18:09:31 christos 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.33 2019/05/21 18:09:31 christos 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 	int fd = SCARG(uap, fd);
485 
486 	if (fd_getfile(fd) == NULL) {
487 		return EBADF;
488 	}
489 
490 	error = fd_close(fd);
491 	if (error == ERESTART) {
492 #ifdef DIAGNOSTIC
493 		printf("%s[%d]: close(%d) returned ERESTART\n",
494 		    l->l_proc->p_comm, (int)l->l_proc->p_pid, fd);
495 #endif
496 		error = EINTR;
497 	}
498 
499 	return error;
500 }
501 
502 /*
503  * Return status information about a file descriptor.
504  * Common function for compat code.
505  */
506 int
507 do_sys_fstat(int fd, struct stat *sb)
508 {
509 	file_t *fp;
510 	int error;
511 
512 	if ((fp = fd_getfile(fd)) == NULL) {
513 		return EBADF;
514 	}
515 	error = (*fp->f_ops->fo_stat)(fp, sb);
516 	fd_putfile(fd);
517 
518 	return error;
519 }
520 
521 /*
522  * Return status information about a file descriptor.
523  */
524 int
525 sys___fstat50(struct lwp *l, const struct sys___fstat50_args *uap,
526 	      register_t *retval)
527 {
528 	/* {
529 		syscallarg(int)			fd;
530 		syscallarg(struct stat *)	sb;
531 	} */
532 	struct stat sb;
533 	int error;
534 
535 	error = do_sys_fstat(SCARG(uap, fd), &sb);
536 	if (error == 0) {
537 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
538 	}
539 	return error;
540 }
541 
542 /*
543  * Return pathconf information about a file descriptor.
544  */
545 int
546 sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap,
547 	      register_t *retval)
548 {
549 	/* {
550 		syscallarg(int)	fd;
551 		syscallarg(int)	name;
552 	} */
553 	int fd, error;
554 	file_t *fp;
555 
556 	fd = SCARG(uap, fd);
557 	error = 0;
558 
559 	if ((fp = fd_getfile(fd)) == NULL) {
560 		return (EBADF);
561 	}
562 	switch (fp->f_type) {
563 	case DTYPE_SOCKET:
564 	case DTYPE_PIPE:
565 		if (SCARG(uap, name) != _PC_PIPE_BUF)
566 			error = EINVAL;
567 		else
568 			*retval = PIPE_BUF;
569 		break;
570 
571 	case DTYPE_VNODE:
572 		error = VOP_PATHCONF(fp->f_vnode, SCARG(uap, name), retval);
573 		break;
574 
575 	case DTYPE_KQUEUE:
576 		error = EINVAL;
577 		break;
578 
579 	default:
580 		error = EOPNOTSUPP;
581 		break;
582 	}
583 
584 	fd_putfile(fd);
585 	return (error);
586 }
587 
588 /*
589  * Apply an advisory lock on a file descriptor.
590  *
591  * Just attempt to get a record lock of the requested type on
592  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
593  */
594 /* ARGSUSED */
595 int
596 sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval)
597 {
598 	/* {
599 		syscallarg(int)	fd;
600 		syscallarg(int)	how;
601 	} */
602 	int fd, how, error;
603 	file_t *fp;
604 	vnode_t	*vp;
605 	struct flock lf;
606 
607 	fd = SCARG(uap, fd);
608 	how = SCARG(uap, how);
609 	error = 0;
610 
611 	if ((fp = fd_getfile(fd)) == NULL) {
612 		return EBADF;
613 	}
614 	if (fp->f_type != DTYPE_VNODE) {
615 		fd_putfile(fd);
616 		return EOPNOTSUPP;
617 	}
618 
619 	vp = fp->f_vnode;
620 	lf.l_whence = SEEK_SET;
621 	lf.l_start = 0;
622 	lf.l_len = 0;
623 
624 	switch (how & ~LOCK_NB) {
625 	case LOCK_UN:
626 		lf.l_type = F_UNLCK;
627 		atomic_and_uint(&fp->f_flag, ~FHASLOCK);
628 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
629 		fd_putfile(fd);
630 		return error;
631 	case LOCK_EX:
632 		lf.l_type = F_WRLCK;
633 		break;
634 	case LOCK_SH:
635 		lf.l_type = F_RDLCK;
636 		break;
637 	default:
638 		fd_putfile(fd);
639 		return EINVAL;
640 	}
641 
642 	atomic_or_uint(&fp->f_flag, FHASLOCK);
643 	if (how & LOCK_NB) {
644 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
645 	} else {
646 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
647 	}
648 	fd_putfile(fd);
649 	return error;
650 }
651 
652 int
653 do_posix_fadvise(int fd, off_t offset, off_t len, int advice)
654 {
655 	file_t *fp;
656 	vnode_t *vp;
657 	off_t endoffset;
658 	int error;
659 
660 	CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
661 	CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
662 	CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
663 
664 	if (len == 0) {
665 		endoffset = INT64_MAX;
666 	} else if (len > 0 && (INT64_MAX - offset) >= len) {
667 		endoffset = offset + len;
668 	} else {
669 		return EINVAL;
670 	}
671 	if ((fp = fd_getfile(fd)) == NULL) {
672 		return EBADF;
673 	}
674 	if (fp->f_type != DTYPE_VNODE) {
675 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
676 			error = ESPIPE;
677 		} else {
678 			error = EOPNOTSUPP;
679 		}
680 		fd_putfile(fd);
681 		return error;
682 	}
683 
684 	switch (advice) {
685 	case POSIX_FADV_WILLNEED:
686 	case POSIX_FADV_DONTNEED:
687 		vp = fp->f_vnode;
688 		if (vp->v_type != VREG && vp->v_type != VBLK) {
689 			fd_putfile(fd);
690 			return 0;
691 		}
692 		break;
693 	}
694 
695 	switch (advice) {
696 	case POSIX_FADV_NORMAL:
697 	case POSIX_FADV_RANDOM:
698 	case POSIX_FADV_SEQUENTIAL:
699 		/*
700 		 * We ignore offset and size.  Must lock the file to
701 		 * do this, as f_advice is sub-word sized.
702 		 */
703 		mutex_enter(&fp->f_lock);
704 		fp->f_advice = (u_char)advice;
705 		mutex_exit(&fp->f_lock);
706 		error = 0;
707 		break;
708 
709 	case POSIX_FADV_WILLNEED:
710 		vp = fp->f_vnode;
711 		error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset);
712 		break;
713 
714 	case POSIX_FADV_DONTNEED:
715 		vp = fp->f_vnode;
716 		/*
717 		 * Align the region to page boundaries as VOP_PUTPAGES expects
718 		 * by shrinking it.  We shrink instead of expand because we
719 		 * do not want to deactivate cache outside of the requested
720 		 * region.  It means that if the specified region is smaller
721 		 * than PAGE_SIZE, we do nothing.
722 		 */
723 		if (round_page(offset) < trunc_page(endoffset) &&
724 		    offset <= round_page(offset)) {
725 			mutex_enter(vp->v_interlock);
726 			error = VOP_PUTPAGES(vp,
727 			    round_page(offset), trunc_page(endoffset),
728 			    PGO_DEACTIVATE | PGO_CLEANIT);
729 		} else {
730 			error = 0;
731 		}
732 		break;
733 
734 	case POSIX_FADV_NOREUSE:
735 		/* Not implemented yet. */
736 		error = 0;
737 		break;
738 	default:
739 		error = EINVAL;
740 		break;
741 	}
742 
743 	fd_putfile(fd);
744 	return error;
745 }
746 
747 int
748 sys___posix_fadvise50(struct lwp *l,
749 		      const struct sys___posix_fadvise50_args *uap,
750 		      register_t *retval)
751 {
752 	/* {
753 		syscallarg(int) fd;
754 		syscallarg(int) pad;
755 		syscallarg(off_t) offset;
756 		syscallarg(off_t) len;
757 		syscallarg(int) advice;
758 	} */
759 
760 	*retval = do_posix_fadvise(SCARG(uap, fd), SCARG(uap, offset),
761 	    SCARG(uap, len), SCARG(uap, advice));
762 
763 	return 0;
764 }
765 
766 int
767 sys_pipe(struct lwp *l, const void *v, register_t *retval)
768 {
769 	int fd[2], error;
770 
771 	if ((error = pipe1(l, fd, 0)) != 0)
772 		return error;
773 
774 	retval[0] = fd[0];
775 	retval[1] = fd[1];
776 
777 	return 0;
778 }
779 
780 int
781 sys_pipe2(struct lwp *l, const struct sys_pipe2_args *uap, register_t *retval)
782 {
783 	/* {
784 		syscallarg(int[2]) fildes;
785 		syscallarg(int) flags;
786 	} */
787 	int fd[2], error;
788 
789 	if ((error = pipe1(l, fd, SCARG(uap, flags))) != 0)
790 		return error;
791 
792 	if ((error = copyout(fd, SCARG(uap, fildes), sizeof(fd))) != 0)
793 		return error;
794 	retval[0] = 0;
795 	return 0;
796 }
797