xref: /netbsd-src/sys/kern/sys_descrip.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: sys_descrip.c,v 1.37 2020/02/23 15:46:41 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2020 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.37 2020/02/23 15:46:41 ad 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 ((error = fd_getvnode(fd, &fp)) != 0)
243 		return error;
244 
245 	vp = fp->f_vnode;
246 	if (fl->l_whence == SEEK_CUR)
247 		fl->l_start += fp->f_offset;
248 
249 	flg = F_POSIX;
250 	p = curproc;
251 
252 	switch (cmd) {
253 	case F_SETLKW:
254 		flg |= F_WAIT;
255 		/* Fall into F_SETLK */
256 
257 		/* FALLTHROUGH */
258 	case F_SETLK:
259 		switch (fl->l_type) {
260 		case F_RDLCK:
261 			if ((fp->f_flag & FREAD) == 0) {
262 				error = EBADF;
263 				break;
264 			}
265 			if ((p->p_flag & PK_ADVLOCK) == 0) {
266 				mutex_enter(p->p_lock);
267 				p->p_flag |= PK_ADVLOCK;
268 				mutex_exit(p->p_lock);
269 			}
270 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
271 			break;
272 
273 		case F_WRLCK:
274 			if ((fp->f_flag & FWRITE) == 0) {
275 				error = EBADF;
276 				break;
277 			}
278 			if ((p->p_flag & PK_ADVLOCK) == 0) {
279 				mutex_enter(p->p_lock);
280 				p->p_flag |= PK_ADVLOCK;
281 				mutex_exit(p->p_lock);
282 			}
283 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
284 			break;
285 
286 		case F_UNLCK:
287 			error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
288 			break;
289 
290 		default:
291 			error = EINVAL;
292 			break;
293 		}
294 		break;
295 
296 	case F_GETLK:
297 		if (fl->l_type != F_RDLCK &&
298 		    fl->l_type != F_WRLCK &&
299 		    fl->l_type != F_UNLCK) {
300 			error = EINVAL;
301 			break;
302 		}
303 		error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
304 		break;
305 
306 	default:
307 		error = EINVAL;
308 		break;
309 	}
310 
311 	fd_putfile(fd);
312 	return error;
313 }
314 
315 static int
316 do_fcntl_getpath(struct lwp *l, file_t *fp, char *upath)
317 {
318 	char *kpath;
319 	int error;
320 
321 	if (fp->f_type != DTYPE_VNODE)
322 		return EOPNOTSUPP;
323 
324 	kpath = PNBUF_GET();
325 
326 	error = vnode_to_path(kpath, MAXPATHLEN, fp->f_vnode, l, l->l_proc);
327 	if (!error)
328 		error = copyoutstr(kpath, upath, MAXPATHLEN, NULL);
329 
330 	PNBUF_PUT(kpath);
331 
332 	return error;
333 }
334 
335 /*
336  * The file control system call.
337  */
338 int
339 sys_fcntl(struct lwp *l, const struct sys_fcntl_args *uap, register_t *retval)
340 {
341 	/* {
342 		syscallarg(int)		fd;
343 		syscallarg(int)		cmd;
344 		syscallarg(void *)	arg;
345 	} */
346 	int fd, i, tmp, error, cmd, newmin;
347 	filedesc_t *fdp;
348 	fdtab_t *dt;
349 	file_t *fp;
350 	struct flock fl;
351 	bool cloexec = false;
352 
353 	fd = SCARG(uap, fd);
354 	cmd = SCARG(uap, cmd);
355 	fdp = l->l_fd;
356 	error = 0;
357 
358 	switch (cmd) {
359 	case F_CLOSEM:
360 		if (fd < 0)
361 			return EBADF;
362 		while ((i = fdp->fd_lastfile) >= fd) {
363 			if (fd_getfile(i) == NULL) {
364 				/* Another thread has updated. */
365 				continue;
366 			}
367 			fd_close(i);
368 		}
369 		return 0;
370 
371 	case F_MAXFD:
372 		*retval = fdp->fd_lastfile;
373 		return 0;
374 
375 	case F_SETLKW:
376 	case F_SETLK:
377 	case F_GETLK:
378 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
379 		if (error)
380 			return error;
381 		error = do_fcntl_lock(fd, cmd, &fl);
382 		if (cmd == F_GETLK && error == 0)
383 			error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
384 		return error;
385 
386 	default:
387 		/* Handled below */
388 		break;
389 	}
390 
391 	if ((fp = fd_getfile(fd)) == NULL)
392 		return EBADF;
393 
394 	if ((cmd & F_FSCTL)) {
395 		error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
396 		fd_putfile(fd);
397 		return error;
398 	}
399 
400 	switch (cmd) {
401 	case F_DUPFD_CLOEXEC:
402 		cloexec = true;
403 		/*FALLTHROUGH*/
404 	case F_DUPFD:
405 		newmin = (long)SCARG(uap, arg);
406 		if ((u_int)newmin >=
407 		    l->l_proc->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
408 		    (u_int)newmin >= maxfiles) {
409 			fd_putfile(fd);
410 			return EINVAL;
411 		}
412 		error = fd_dup(fp, newmin, &i, cloexec);
413 		*retval = i;
414 		break;
415 
416 	case F_GETFD:
417 		dt = atomic_load_consume(&fdp->fd_dt);
418 		*retval = dt->dt_ff[fd]->ff_exclose;
419 		break;
420 
421 	case F_SETFD:
422 		fd_set_exclose(l, fd,
423 		    ((long)SCARG(uap, arg) & FD_CLOEXEC) != 0);
424 		break;
425 
426 	case F_GETNOSIGPIPE:
427 		*retval = (fp->f_flag & FNOSIGPIPE) != 0;
428 		break;
429 
430 	case F_SETNOSIGPIPE:
431 		if (SCARG(uap, arg))
432 			atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
433 		else
434 			atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
435 		*retval = 0;
436 		break;
437 
438 	case F_GETFL:
439 		*retval = OFLAGS(fp->f_flag);
440 		break;
441 
442 	case F_SETFL:
443 		/* XXX not guaranteed to be atomic. */
444 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
445 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp);
446 		if (error)
447 			break;
448 		i = tmp ^ fp->f_flag;
449 		if (i & FNONBLOCK) {
450 			int flgs = tmp & FNONBLOCK;
451 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs);
452 			if (error) {
453 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
454 				    &fp->f_flag);
455 				break;
456 			}
457 		}
458 		if (i & FASYNC) {
459 			int flgs = tmp & FASYNC;
460 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs);
461 			if (error) {
462 				if (i & FNONBLOCK) {
463 					tmp = fp->f_flag & FNONBLOCK;
464 					(void)(*fp->f_ops->fo_ioctl)(fp,
465 						FIONBIO, &tmp);
466 				}
467 				(*fp->f_ops->fo_fcntl)(fp, F_SETFL,
468 				    &fp->f_flag);
469 				break;
470 			}
471 		}
472 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
473 		break;
474 
475 	case F_GETOWN:
476 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp);
477 		*retval = tmp;
478 		break;
479 
480 	case F_SETOWN:
481 		tmp = (int)(uintptr_t) SCARG(uap, arg);
482 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp);
483 		break;
484 
485 	case F_GETPATH:
486 		error = do_fcntl_getpath(l, fp, SCARG(uap, arg));
487 		break;
488 
489 	default:
490 		error = EINVAL;
491 	}
492 
493 	fd_putfile(fd);
494 	return (error);
495 }
496 
497 /*
498  * Close a file descriptor.
499  */
500 int
501 sys_close(struct lwp *l, const struct sys_close_args *uap, register_t *retval)
502 {
503 	/* {
504 		syscallarg(int)	fd;
505 	} */
506 	int error;
507 	int fd = SCARG(uap, fd);
508 
509 	if (fd_getfile(fd) == NULL) {
510 		return EBADF;
511 	}
512 
513 	error = fd_close(fd);
514 	if (error == ERESTART) {
515 #ifdef DIAGNOSTIC
516 		printf("%s[%d]: close(%d) returned ERESTART\n",
517 		    l->l_proc->p_comm, (int)l->l_proc->p_pid, fd);
518 #endif
519 		error = EINTR;
520 	}
521 
522 	return error;
523 }
524 
525 /*
526  * Return status information about a file descriptor.
527  * Common function for compat code.
528  */
529 int
530 do_sys_fstat(int fd, struct stat *sb)
531 {
532 	file_t *fp;
533 	int error;
534 
535 	if ((fp = fd_getfile(fd)) == NULL) {
536 		return EBADF;
537 	}
538 	error = (*fp->f_ops->fo_stat)(fp, sb);
539 	fd_putfile(fd);
540 
541 	return error;
542 }
543 
544 /*
545  * Return status information about a file descriptor.
546  */
547 int
548 sys___fstat50(struct lwp *l, const struct sys___fstat50_args *uap,
549 	      register_t *retval)
550 {
551 	/* {
552 		syscallarg(int)			fd;
553 		syscallarg(struct stat *)	sb;
554 	} */
555 	struct stat sb;
556 	int error;
557 
558 	error = do_sys_fstat(SCARG(uap, fd), &sb);
559 	if (error == 0) {
560 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
561 	}
562 	return error;
563 }
564 
565 /*
566  * Return pathconf information about a file descriptor.
567  */
568 int
569 sys_fpathconf(struct lwp *l, const struct sys_fpathconf_args *uap,
570 	      register_t *retval)
571 {
572 	/* {
573 		syscallarg(int)	fd;
574 		syscallarg(int)	name;
575 	} */
576 	int fd, error;
577 	file_t *fp;
578 
579 	fd = SCARG(uap, fd);
580 	error = 0;
581 
582 	if ((fp = fd_getfile(fd)) == NULL) {
583 		return (EBADF);
584 	}
585 	switch (fp->f_type) {
586 	case DTYPE_SOCKET:
587 	case DTYPE_PIPE:
588 		if (SCARG(uap, name) != _PC_PIPE_BUF)
589 			error = EINVAL;
590 		else
591 			*retval = PIPE_BUF;
592 		break;
593 
594 	case DTYPE_VNODE:
595 		error = VOP_PATHCONF(fp->f_vnode, SCARG(uap, name), retval);
596 		break;
597 
598 	case DTYPE_KQUEUE:
599 		error = EINVAL;
600 		break;
601 
602 	default:
603 		error = EOPNOTSUPP;
604 		break;
605 	}
606 
607 	fd_putfile(fd);
608 	return (error);
609 }
610 
611 /*
612  * Apply an advisory lock on a file descriptor.
613  *
614  * Just attempt to get a record lock of the requested type on
615  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
616  */
617 /* ARGSUSED */
618 int
619 sys_flock(struct lwp *l, const struct sys_flock_args *uap, register_t *retval)
620 {
621 	/* {
622 		syscallarg(int)	fd;
623 		syscallarg(int)	how;
624 	} */
625 	int fd, how, error;
626 	file_t *fp;
627 	vnode_t	*vp;
628 	struct flock lf;
629 
630 	fd = SCARG(uap, fd);
631 	how = SCARG(uap, how);
632 
633 	if ((error = fd_getvnode(fd, &fp)) != 0)
634 		return error == EINVAL ? EOPNOTSUPP : error;
635 
636 	vp = fp->f_vnode;
637 	lf.l_whence = SEEK_SET;
638 	lf.l_start = 0;
639 	lf.l_len = 0;
640 
641 	switch (how & ~LOCK_NB) {
642 	case LOCK_UN:
643 		lf.l_type = F_UNLCK;
644 		atomic_and_uint(&fp->f_flag, ~FHASLOCK);
645 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
646 		fd_putfile(fd);
647 		return error;
648 	case LOCK_EX:
649 		lf.l_type = F_WRLCK;
650 		break;
651 	case LOCK_SH:
652 		lf.l_type = F_RDLCK;
653 		break;
654 	default:
655 		fd_putfile(fd);
656 		return EINVAL;
657 	}
658 
659 	atomic_or_uint(&fp->f_flag, FHASLOCK);
660 	if (how & LOCK_NB) {
661 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
662 	} else {
663 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
664 	}
665 	fd_putfile(fd);
666 	return error;
667 }
668 
669 int
670 do_posix_fadvise(int fd, off_t offset, off_t len, int advice)
671 {
672 	file_t *fp;
673 	vnode_t *vp;
674 	off_t endoffset;
675 	int error;
676 
677 	CTASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
678 	CTASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
679 	CTASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
680 
681 	if (offset < 0) {
682 		return EINVAL;
683 	}
684 	if (len == 0) {
685 		endoffset = INT64_MAX;
686 	} else if (len > 0 && (INT64_MAX - offset) >= len) {
687 		endoffset = offset + len;
688 	} else {
689 		return EINVAL;
690 	}
691 	if ((fp = fd_getfile(fd)) == NULL) {
692 		return EBADF;
693 	}
694 	if (fp->f_type != DTYPE_VNODE) {
695 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
696 			error = ESPIPE;
697 		} else {
698 			error = EOPNOTSUPP;
699 		}
700 		fd_putfile(fd);
701 		return error;
702 	}
703 
704 	switch (advice) {
705 	case POSIX_FADV_WILLNEED:
706 	case POSIX_FADV_DONTNEED:
707 		vp = fp->f_vnode;
708 		if (vp->v_type != VREG && vp->v_type != VBLK) {
709 			fd_putfile(fd);
710 			return 0;
711 		}
712 		break;
713 	}
714 
715 	switch (advice) {
716 	case POSIX_FADV_NORMAL:
717 	case POSIX_FADV_RANDOM:
718 	case POSIX_FADV_SEQUENTIAL:
719 		/*
720 		 * We ignore offset and size.  Must lock the file to
721 		 * do this, as f_advice is sub-word sized.
722 		 */
723 		mutex_enter(&fp->f_lock);
724 		fp->f_advice = (u_char)advice;
725 		mutex_exit(&fp->f_lock);
726 		error = 0;
727 		break;
728 
729 	case POSIX_FADV_WILLNEED:
730 		vp = fp->f_vnode;
731 		error = uvm_readahead(&vp->v_uobj, offset, endoffset - offset);
732 		break;
733 
734 	case POSIX_FADV_DONTNEED:
735 		vp = fp->f_vnode;
736 		/*
737 		 * Align the region to page boundaries as VOP_PUTPAGES expects
738 		 * by shrinking it.  We shrink instead of expand because we
739 		 * do not want to deactivate cache outside of the requested
740 		 * region.  It means that if the specified region is smaller
741 		 * than PAGE_SIZE, we do nothing.
742 		 */
743 		if (round_page(offset) < trunc_page(endoffset) &&
744 		    offset <= round_page(offset)) {
745 			rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
746 			error = VOP_PUTPAGES(vp,
747 			    round_page(offset), trunc_page(endoffset),
748 			    PGO_DEACTIVATE | PGO_CLEANIT);
749 		} else {
750 			error = 0;
751 		}
752 		break;
753 
754 	case POSIX_FADV_NOREUSE:
755 		/* Not implemented yet. */
756 		error = 0;
757 		break;
758 	default:
759 		error = EINVAL;
760 		break;
761 	}
762 
763 	fd_putfile(fd);
764 	return error;
765 }
766 
767 int
768 sys___posix_fadvise50(struct lwp *l,
769 		      const struct sys___posix_fadvise50_args *uap,
770 		      register_t *retval)
771 {
772 	/* {
773 		syscallarg(int) fd;
774 		syscallarg(int) pad;
775 		syscallarg(off_t) offset;
776 		syscallarg(off_t) len;
777 		syscallarg(int) advice;
778 	} */
779 
780 	*retval = do_posix_fadvise(SCARG(uap, fd), SCARG(uap, offset),
781 	    SCARG(uap, len), SCARG(uap, advice));
782 
783 	return 0;
784 }
785 
786 int
787 sys_pipe(struct lwp *l, const void *v, register_t *retval)
788 {
789 	int fd[2], error;
790 
791 	if ((error = pipe1(l, fd, 0)) != 0)
792 		return error;
793 
794 	retval[0] = fd[0];
795 	retval[1] = fd[1];
796 
797 	return 0;
798 }
799 
800 int
801 sys_pipe2(struct lwp *l, const struct sys_pipe2_args *uap, register_t *retval)
802 {
803 	/* {
804 		syscallarg(int[2]) fildes;
805 		syscallarg(int) flags;
806 	} */
807 	int fd[2], error;
808 
809 	if ((error = pipe1(l, fd, SCARG(uap, flags))) != 0)
810 		return error;
811 
812 	if ((error = copyout(fd, SCARG(uap, fildes), sizeof(fd))) != 0)
813 		return error;
814 	retval[0] = 0;
815 	return 0;
816 }
817