From d6bcf78bdbc99f5411dcde1163b12ff573f66ca9 Mon Sep 17 00:00:00 2001 From: mpi Date: Thu, 30 Apr 2015 09:20:51 +0000 Subject: [PATCH] Indroduce fd_getfile_mode() and use it were fd_getfile() is directly followed by a mode check. This will simplify the ref/unref dance as soon as fd_getfile() will increment fp's reference counter. Idea from and ok guenther@, ok millert@ --- sys/compat/linux/linux_cdrom.c | 9 ++------- sys/compat/linux/linux_termios.c | 9 ++------- sys/kern/kern_descrip.c | 17 ++++++++++++++++- sys/kern/sys_generic.c | 25 ++++++------------------- sys/kern/vfs_syscalls.c | 18 +++++------------- sys/sys/filedesc.h | 5 +++-- 6 files changed, 34 insertions(+), 49 deletions(-) diff --git a/sys/compat/linux/linux_cdrom.c b/sys/compat/linux/linux_cdrom.c index b1ca8e16af0..033a94bd8bb 100644 --- a/sys/compat/linux/linux_cdrom.c +++ b/sys/compat/linux/linux_cdrom.c @@ -1,5 +1,5 @@ -/* $OpenBSD: linux_cdrom.c,v 1.11 2014/03/26 05:23:42 guenther Exp $ */ +/* $OpenBSD: linux_cdrom.c,v 1.12 2015/04/30 09:20:51 mpi Exp $ */ /* * Copyright 1997 Niels Provos * All rights reserved. @@ -108,15 +108,10 @@ linux_ioctl_cdrom(p, v, retval) fdp = p->p_fd; - if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) + if ((fp = fd_getfile_mode(fdp, SCARG(uap, fd), FREAD|FWRITE)) == NULL) return (EBADF); FREF(fp); - if ((fp->f_flag & (FREAD | FWRITE)) == 0) { - error = EBADF; - goto out; - } - com = SCARG(uap, com); retval[0] = 0; diff --git a/sys/compat/linux/linux_termios.c b/sys/compat/linux/linux_termios.c index 1baae7e2f6d..9b555947c6b 100644 --- a/sys/compat/linux/linux_termios.c +++ b/sys/compat/linux/linux_termios.c @@ -1,4 +1,4 @@ -/* $OpenBSD: linux_termios.c,v 1.17 2014/03/26 05:23:42 guenther Exp $ */ +/* $OpenBSD: linux_termios.c,v 1.18 2015/04/30 09:20:51 mpi Exp $ */ /* $NetBSD: linux_termios.c,v 1.3 1996/04/05 00:01:54 christos Exp $ */ /* @@ -461,15 +461,10 @@ linux_ioctl_termios(p, v, retval) int error = 0; fdp = p->p_fd; - if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) + if ((fp = fd_getfile_mode(fdp, SCARG(uap, fd), FREAD|FWRITE)) == NULL) return (EBADF); FREF(fp); - if ((fp->f_flag & (FREAD | FWRITE)) == 0) { - error = EBADF; - goto out; - } - com = SCARG(uap, com); retval[0] = 0; diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 2b55a32e7c1..2c8955c4d01 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_descrip.c,v 1.117 2015/03/14 03:38:50 jsg Exp $ */ +/* $OpenBSD: kern_descrip.c,v 1.118 2015/04/30 09:20:51 mpi Exp $ */ /* $NetBSD: kern_descrip.c,v 1.42 1996/03/30 22:24:38 christos Exp $ */ /* @@ -192,6 +192,21 @@ fd_getfile(struct filedesc *fdp, int fd) return (fp); } +struct file * +fd_getfile_mode(struct filedesc *fdp, int fd, int mode) +{ + struct file *fp; + + KASSERT(mode != 0); + + fp = fd_getfile(fdp, fd); + + if ((fp->f_flag & mode) == 0) + return (NULL); + + return (fp); +} + /* * System calls on descriptors. */ diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index 9c0f30be523..57b1813f080 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_generic.c,v 1.96 2015/02/12 22:27:04 millert Exp $ */ +/* $OpenBSD: sys_generic.c,v 1.97 2015/04/30 09:20:51 mpi Exp $ */ /* $NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $ */ /* @@ -87,9 +87,7 @@ sys_read(struct proc *p, void *v, register_t *retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FREAD)) == NULL) return (EBADF); iov.iov_base = SCARG(uap, buf); @@ -116,11 +114,8 @@ sys_readv(struct proc *p, void *v, register_t *retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FREAD)) == NULL) return (EBADF); - FREF(fp); /* dofilereadv() will FRELE the descriptor for us */ @@ -240,9 +235,7 @@ sys_write(struct proc *p, void *v, register_t *retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FWRITE)) == NULL) return (EBADF); iov.iov_base = (void *)SCARG(uap, buf); @@ -269,11 +262,8 @@ sys_writev(struct proc *p, void *v, register_t *retval) struct file *fp; struct filedesc *fdp = p->p_fd; - if ((fp = fd_getfile(fdp, fd)) == NULL) + if ((fp = fd_getfile_mode(fdp, fd, FWRITE)) == NULL) return (EBADF); - if ((fp->f_flag & FWRITE) == 0) - return (EBADF); - FREF(fp); /* dofilewritev() will FRELE the descriptor for us */ @@ -403,10 +393,7 @@ sys_ioctl(struct proc *p, void *v, register_t *retval) long long stkbuf[STK_PARAMS / sizeof(long long)]; fdp = p->p_fd; - if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL) - return (EBADF); - - if ((fp->f_flag & (FREAD | FWRITE)) == 0) + if ((fp = fd_getfile_mode(fdp, SCARG(uap, fd), FREAD|FWRITE)) == NULL) return (EBADF); switch (com = SCARG(uap, com)) { diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index 7cac013b16b..81667eed88f 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vfs_syscalls.c,v 1.218 2015/04/17 04:43:20 guenther Exp $ */ +/* $OpenBSD: vfs_syscalls.c,v 1.219 2015/04/30 09:20:51 mpi Exp $ */ /* $NetBSD: vfs_syscalls.c,v 1.71 1996/04/23 10:29:02 mycroft Exp $ */ /* @@ -2824,9 +2824,7 @@ sys_pread(struct proc *p, void *v, register_t *retval) off_t offset; int fd = SCARG(uap, fd); - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FREAD)) == NULL) return (EBADF); vp = (struct vnode *)fp->f_data; @@ -2867,9 +2865,7 @@ sys_preadv(struct proc *p, void *v, register_t *retval) off_t offset; int fd = SCARG(uap, fd); - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FREAD) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FREAD)) == NULL) return (EBADF); vp = (struct vnode *)fp->f_data; @@ -2909,9 +2905,7 @@ sys_pwrite(struct proc *p, void *v, register_t *retval) off_t offset; int fd = SCARG(uap, fd); - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FWRITE)) == NULL) return (EBADF); vp = (struct vnode *)fp->f_data; @@ -2952,9 +2946,7 @@ sys_pwritev(struct proc *p, void *v, register_t *retval) off_t offset; int fd = SCARG(uap, fd); - if ((fp = fd_getfile(fdp, fd)) == NULL) - return (EBADF); - if ((fp->f_flag & FWRITE) == 0) + if ((fp = fd_getfile_mode(fdp, fd, FWRITE)) == NULL) return (EBADF); vp = (struct vnode *)fp->f_data; diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index b383a38351e..2016441326b 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: filedesc.h,v 1.28 2014/05/15 03:52:25 guenther Exp $ */ +/* $OpenBSD: filedesc.h,v 1.29 2015/04/30 09:20:51 mpi Exp $ */ /* $NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $ */ /* @@ -132,7 +132,8 @@ void fdfree(struct proc *p); int fdrelease(struct proc *p, int); void fdremove(struct filedesc *, int); void fdcloseexec(struct proc *); -struct file *fd_getfile(struct filedesc *, int fd); +struct file *fd_getfile(struct filedesc *, int); +struct file *fd_getfile_mode(struct filedesc *, int, int); int closef(struct file *, struct proc *); int getsock(struct filedesc *, int, struct file **); -- 2.20.1