Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Make updating a file's reference and use count MP-safe.
details: https://anonhg.NetBSD.org/src/rev/f7895e025b39
branches: trunk
changeset: 543334:f7895e025b39
user: pk <pk%NetBSD.org@localhost>
date: Sun Feb 23 14:37:32 2003 +0000
description:
Make updating a file's reference and use count MP-safe.
diffstat:
sys/compat/sunos/sunos_ioctl.c | 8 +++--
sys/compat/sunos/sunos_misc.c | 5 ++-
sys/compat/svr4/svr4_fcntl.c | 7 +++-
sys/compat/svr4/svr4_ioctl.c | 6 +++-
sys/compat/svr4/svr4_stream.c | 8 ++++-
sys/kern/kern_descrip.c | 50 ++++++++++++++++++++++++++++++++------
sys/kern/kern_event.c | 9 +++++-
sys/kern/kern_ktrace.c | 6 +++-
sys/kern/sys_generic.c | 20 ++++++++++----
sys/kern/uipc_usrreq.c | 27 ++++++++++++++-------
sys/kern/vfs_syscalls.c | 20 ++++++++++----
sys/miscfs/fdesc/fdesc_vnops.c | 9 ++++--
sys/miscfs/portal/portal_vfsops.c | 10 +++++--
sys/sys/file.h | 13 +++++++++-
sys/uvm/uvm_mmap.c | 6 +++-
15 files changed, 151 insertions(+), 53 deletions(-)
diffs (truncated from 782 to 300 lines):
diff -r 81e62792e2a1 -r f7895e025b39 sys/compat/sunos/sunos_ioctl.c
--- a/sys/compat/sunos/sunos_ioctl.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/compat/sunos/sunos_ioctl.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sunos_ioctl.c,v 1.43 2003/01/18 08:36:15 thorpej Exp $ */
+/* $NetBSD: sunos_ioctl.c,v 1.44 2003/02/23 14:37:32 pk Exp $ */
/*
* Copyright (c) 1993 Markus Wild.
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sunos_ioctl.c,v 1.43 2003/01/18 08:36:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos_ioctl.c,v 1.44 2003/02/23 14:37:32 pk Exp $");
#if defined(_KERNEL_OPT)
#include "opt_execfmt.h"
@@ -422,8 +422,10 @@
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
- if ((fp->f_flag & (FREAD|FWRITE)) == 0)
+ if ((fp->f_flag & (FREAD|FWRITE)) == 0) {
+ simple_unlock(&fp->f_slock);
return EBADF;
+ }
ctl = fp->f_ops->fo_ioctl;
diff -r 81e62792e2a1 -r f7895e025b39 sys/compat/sunos/sunos_misc.c
--- a/sys/compat/sunos/sunos_misc.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/compat/sunos/sunos_misc.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sunos_misc.c,v 1.119 2003/01/29 07:00:37 atatat Exp $ */
+/* $NetBSD: sunos_misc.c,v 1.120 2003/02/23 14:37:32 pk Exp $ */
/*
* Copyright (c) 1992, 1993
@@ -54,7 +54,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.119 2003/01/29 07:00:37 atatat Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.120 2003/02/23 14:37:32 pk Exp $");
#if defined(_KERNEL_OPT)
#include "opt_nfsserver.h"
@@ -877,6 +877,7 @@
struct file *fp;
fp = fd_getfile(fdp, *retval);
+ simple_unlock(&fp->f_slock);
/* ignore any error, just give it a try */
if (fp != NULL && fp->f_type == DTYPE_VNODE)
diff -r 81e62792e2a1 -r f7895e025b39 sys/compat/svr4/svr4_fcntl.c
--- a/sys/compat/svr4/svr4_fcntl.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/compat/svr4/svr4_fcntl.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: svr4_fcntl.c,v 1.42 2003/01/18 08:44:26 thorpej Exp $ */
+/* $NetBSD: svr4_fcntl.c,v 1.43 2003/02/23 14:37:33 pk Exp $ */
/*-
* Copyright (c) 1994, 1997 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_fcntl.c,v 1.42 2003/01/18 08:44:26 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_fcntl.c,v 1.43 2003/02/23 14:37:33 pk Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -273,6 +273,7 @@
if ((fp = fd_getfile(fdp, fd)) == NULL)
return EBADF;
+ simple_unlock(&fp->f_slock);
if (fp->f_type != DTYPE_VNODE)
return EINVAL;
@@ -320,6 +321,7 @@
if ((fp = fd_getfile(fdp, fd)) == NULL)
return EBADF;
+ simple_unlock(&fp->f_slock);
vp = (struct vnode *)fp->f_data;
if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
return ESPIPE;
@@ -391,6 +393,7 @@
struct file *fp;
fp = fd_getfile(fdp, *retval);
+ simple_unlock(&fp->f_slock);
/* ignore any error, just give it a try */
if (fp != NULL && fp->f_type == DTYPE_VNODE)
diff -r 81e62792e2a1 -r f7895e025b39 sys/compat/svr4/svr4_ioctl.c
--- a/sys/compat/svr4/svr4_ioctl.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/compat/svr4/svr4_ioctl.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: svr4_ioctl.c,v 1.23 2003/01/18 08:44:26 thorpej Exp $ */
+/* $NetBSD: svr4_ioctl.c,v 1.24 2003/02/23 14:37:33 pk Exp $ */
/*-
* Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_ioctl.c,v 1.23 2003/01/18 08:44:26 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_ioctl.c,v 1.24 2003/02/23 14:37:33 pk Exp $");
#include <sys/param.h>
#include <sys/proc.h>
@@ -127,6 +127,8 @@
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
+ simple_unlock(&fp->f_slock);
+
if ((fp->f_flag & (FREAD | FWRITE)) == 0)
return EBADF;
diff -r 81e62792e2a1 -r f7895e025b39 sys/compat/svr4/svr4_stream.c
--- a/sys/compat/svr4/svr4_stream.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/compat/svr4/svr4_stream.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: svr4_stream.c,v 1.49 2003/01/18 08:44:27 thorpej Exp $ */
+/* $NetBSD: svr4_stream.c,v 1.50 2003/02/23 14:37:33 pk Exp $ */
/*-
* Copyright (c) 1994 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: svr4_stream.c,v 1.49 2003/01/18 08:44:27 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: svr4_stream.c,v 1.50 2003/02/23 14:37:33 pk Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
@@ -1524,6 +1524,8 @@
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
+ simple_unlock(&fp->f_slock);
+
if (SCARG(uap, ctl) != NULL) {
if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0)
return error;
@@ -1696,6 +1698,8 @@
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return EBADF;
+ simple_unlock(&fp->f_slock);
+
if (SCARG(uap, ctl) != NULL) {
if ((error = copyin(SCARG(uap, ctl), &ctl, sizeof(ctl))) != 0)
return error;
diff -r 81e62792e2a1 -r f7895e025b39 sys/kern/kern_descrip.c
--- a/sys/kern/kern_descrip.c Sun Feb 23 13:04:37 2003 +0000
+++ b/sys/kern/kern_descrip.c Sun Feb 23 14:37:32 2003 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kern_descrip.c,v 1.102 2003/02/14 21:50:10 pk Exp $ */
+/* $NetBSD: kern_descrip.c,v 1.103 2003/02/23 14:37:33 pk Exp $ */
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1993
@@ -41,7 +41,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.102 2003/02/14 21:50:10 pk Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.103 2003/02/23 14:37:33 pk Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -122,6 +122,14 @@
}
}
+/*
+ * Lookup the file structure corresponding to a file descriptor
+ * and return it locked.
+ * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
+ * The locking strategy has been optimised for this case, i.e.
+ * fd_getfile() returns the file locked while FILE_USE() will increment
+ * the file's use count and unlock.
+ */
struct file *
fd_getfile(struct filedesc *fdp, int fd)
{
@@ -130,8 +138,11 @@
if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
return (NULL);
- if (FILE_IS_USABLE(fp) == 0)
+ simple_lock(&fp->f_slock);
+ if (FILE_IS_USABLE(fp) == 0) {
+ simple_unlock(&fp->f_slock);
return (NULL);
+ }
return (fp);
}
@@ -205,10 +216,13 @@
return (EBADF);
if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
- (u_int)new >= maxfiles)
+ (u_int)new >= maxfiles) {
+ simple_unlock(&fp->f_slock);
return (EBADF);
+ }
if (old == new) {
+ simple_unlock(&fp->f_slock);
*retval = new;
return (0);
}
@@ -477,6 +491,7 @@
FILE_UNUSE(fp, p);
if (delfp != NULL) {
+ simple_lock(&delfp->f_slock);
FILE_USE(delfp);
if (new < fdp->fd_knlistsize)
knote_fdclose(p, new);
@@ -505,6 +520,12 @@
if (fp == NULL)
return (EBADF);
+ simple_lock(&fp->f_slock);
+ if (!FILE_IS_USABLE(fp)) {
+ simple_unlock(&fp->f_slock);
+ return (EBADF);
+ }
+
FILE_USE(fp);
*fpp = NULL;
@@ -533,8 +554,12 @@
fd = SCARG(uap, fd);
fdp = p->p_fd;
+ if ((u_int) fd >= fdp->fd_nfiles)
+ return (NULL);
+#if 0
if (fd_getfile(fdp, fd) == NULL)
return (EBADF);
+#endif
return (fdrelease(p, fd));
}
@@ -785,11 +810,12 @@
}
simple_unlock(&filelist_slock);
p->p_fd->fd_ofiles[i] = fp;
+ simple_lock_init(&fp->f_slock);
fp->f_count = 1;
fp->f_cred = p->p_ucred;
crhold(fp->f_cred);
if (resultfp) {
- FILE_USE(fp);
+ fp->f_usecount = 1;
*resultfp = fp;
}
if (resultfd)
@@ -813,7 +839,7 @@
LIST_REMOVE(fp, f_list);
crfree(fp->f_cred);
#ifdef DIAGNOSTIC
- fp->f_count = 0;
+ fp->f_count = 0; /* What's the point? */
#endif
nfiles--;
simple_unlock(&filelist_slock);
@@ -1043,6 +1069,7 @@
fp = *fpp;
if (fp != NULL) {
*fpp = NULL;
+ simple_lock(&fp->f_slock);
FILE_USE(fp);
if (i < fdp->fd_knlistsize)
knote_fdclose(p, fdp->fd_lastfile - i);
@@ -1101,6 +1128,7 @@
* happen if a filedesc structure is shared by multiple
* processes.
*/
+ simple_lock(&fp->f_slock);
if (fp->f_iflags & FIF_WANTCLOSE) {
/*
* Another user of the file is already closing, and is
@@ -1116,6 +1144,7 @@
#endif
if (--fp->f_usecount == 1)
wakeup(&fp->f_usecount);
+ simple_unlock(&fp->f_slock);
Home |
Main Index |
Thread Index |
Old Index