Sun Sep 25 13:40:37 2011 UTC ()
in fd_allocfile(), free the fd if we fail to allocate a file.


(chs)
diff -r1.216 -r1.217 src/sys/kern/kern_descrip.c

cvs diff -r1.216 -r1.217 src/sys/kern/kern_descrip.c (expand / switch to unified diff)

--- src/sys/kern/kern_descrip.c 2011/07/15 14:50:19 1.216
+++ src/sys/kern/kern_descrip.c 2011/09/25 13:40:37 1.217
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: kern_descrip.c,v 1.216 2011/07/15 14:50:19 christos Exp $ */ 1/* $NetBSD: kern_descrip.c,v 1.217 2011/09/25 13:40:37 chs Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -60,27 +60,27 @@ @@ -60,27 +60,27 @@
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE. 63 * SUCH DAMAGE.
64 * 64 *
65 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95 65 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
66 */ 66 */
67 67
68/* 68/*
69 * File descriptor management. 69 * File descriptor management.
70 */ 70 */
71 71
72#include <sys/cdefs.h> 72#include <sys/cdefs.h>
73__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.216 2011/07/15 14:50:19 christos Exp $"); 73__KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.217 2011/09/25 13:40:37 chs Exp $");
74 74
75#include <sys/param.h> 75#include <sys/param.h>
76#include <sys/systm.h> 76#include <sys/systm.h>
77#include <sys/filedesc.h> 77#include <sys/filedesc.h>
78#include <sys/kernel.h> 78#include <sys/kernel.h>
79#include <sys/proc.h> 79#include <sys/proc.h>
80#include <sys/file.h> 80#include <sys/file.h>
81#include <sys/socket.h> 81#include <sys/socket.h>
82#include <sys/socketvar.h> 82#include <sys/socketvar.h>
83#include <sys/stat.h> 83#include <sys/stat.h>
84#include <sys/ioctl.h> 84#include <sys/ioctl.h>
85#include <sys/fcntl.h> 85#include <sys/fcntl.h>
86#include <sys/pool.h> 86#include <sys/pool.h>
@@ -281,27 +281,27 @@ static inline void @@ -281,27 +281,27 @@ static inline void
281fd_used(filedesc_t *fdp, unsigned fd) 281fd_used(filedesc_t *fdp, unsigned fd)
282{ 282{
283 u_int off = fd >> NDENTRYSHIFT; 283 u_int off = fd >> NDENTRYSHIFT;
284 fdfile_t *ff; 284 fdfile_t *ff;
285 285
286 ff = fdp->fd_dt->dt_ff[fd]; 286 ff = fdp->fd_dt->dt_ff[fd];
287 287
288 KASSERT(mutex_owned(&fdp->fd_lock)); 288 KASSERT(mutex_owned(&fdp->fd_lock));
289 KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0); 289 KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
290 KASSERT(ff != NULL); 290 KASSERT(ff != NULL);
291 KASSERT(ff->ff_file == NULL); 291 KASSERT(ff->ff_file == NULL);
292 KASSERT(!ff->ff_allocated); 292 KASSERT(!ff->ff_allocated);
293 293
294 ff->ff_allocated = 1; 294 ff->ff_allocated = true;
295 fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK); 295 fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
296 if (__predict_false(fdp->fd_lomap[off] == ~0)) { 296 if (__predict_false(fdp->fd_lomap[off] == ~0)) {
297 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] & 297 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
298 (1 << (off & NDENTRYMASK))) == 0); 298 (1 << (off & NDENTRYMASK))) == 0);
299 fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK); 299 fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
300 } 300 }
301 301
302 if ((int)fd > fdp->fd_lastfile) { 302 if ((int)fd > fdp->fd_lastfile) {
303 fdp->fd_lastfile = fd; 303 fdp->fd_lastfile = fd;
304 } 304 }
305 305
306 fd_checkmaps(fdp); 306 fd_checkmaps(fdp);
307} 307}
@@ -327,27 +327,27 @@ fd_unused(filedesc_t *fdp, unsigned fd) @@ -327,27 +327,27 @@ fd_unused(filedesc_t *fdp, unsigned fd)
327 327
328 if (fd < fdp->fd_freefile) { 328 if (fd < fdp->fd_freefile) {
329 fdp->fd_freefile = fd; 329 fdp->fd_freefile = fd;
330 } 330 }
331 331
332 if (fdp->fd_lomap[off] == ~0) { 332 if (fdp->fd_lomap[off] == ~0) {
333 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] & 333 KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
334 (1 << (off & NDENTRYMASK))) != 0); 334 (1 << (off & NDENTRYMASK))) != 0);
335 fdp->fd_himap[off >> NDENTRYSHIFT] &= 335 fdp->fd_himap[off >> NDENTRYSHIFT] &=
336 ~(1 << (off & NDENTRYMASK)); 336 ~(1 << (off & NDENTRYMASK));
337 } 337 }
338 KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0); 338 KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
339 fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK)); 339 fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
340 ff->ff_allocated = 0; 340 ff->ff_allocated = false;
341 341
342 KASSERT(fd <= fdp->fd_lastfile); 342 KASSERT(fd <= fdp->fd_lastfile);
343 if (fd == fdp->fd_lastfile) { 343 if (fd == fdp->fd_lastfile) {
344 fdp->fd_lastfile = fd_last_set(fdp, fd); 344 fdp->fd_lastfile = fd_last_set(fdp, fd);
345 } 345 }
346 fd_checkmaps(fdp); 346 fd_checkmaps(fdp);
347} 347}
348 348
349/* 349/*
350 * Look up the file structure corresponding to a file descriptor 350 * Look up the file structure corresponding to a file descriptor
351 * and return the file, holding a reference on the descriptor. 351 * and return the file, holding a reference on the descriptor.
352 */ 352 */
353file_t * 353file_t *
@@ -1073,26 +1073,27 @@ fd_allocfile(file_t **resultfp, int *res @@ -1073,26 +1073,27 @@ fd_allocfile(file_t **resultfp, int *res
1073 kauth_cred_t cred; 1073 kauth_cred_t cred;
1074 file_t *fp; 1074 file_t *fp;
1075 int error; 1075 int error;
1076 1076
1077 while ((error = fd_alloc(p, 0, resultfd)) != 0) { 1077 while ((error = fd_alloc(p, 0, resultfd)) != 0) {
1078 if (error != ENOSPC) { 1078 if (error != ENOSPC) {
1079 return error; 1079 return error;
1080 } 1080 }
1081 fd_tryexpand(p); 1081 fd_tryexpand(p);
1082 } 1082 }
1083 1083
1084 fp = pool_cache_get(file_cache, PR_WAITOK); 1084 fp = pool_cache_get(file_cache, PR_WAITOK);
1085 if (fp == NULL) { 1085 if (fp == NULL) {
 1086 fd_abort(p, NULL, *resultfd);
1086 return ENFILE; 1087 return ENFILE;
1087 } 1088 }
1088 KASSERT(fp->f_count == 0); 1089 KASSERT(fp->f_count == 0);
1089 KASSERT(fp->f_msgcount == 0); 1090 KASSERT(fp->f_msgcount == 0);
1090 KASSERT(fp->f_unpcount == 0); 1091 KASSERT(fp->f_unpcount == 0);
1091 1092
1092 /* Replace cached credentials if not what we need. */ 1093 /* Replace cached credentials if not what we need. */
1093 cred = curlwp->l_cred; 1094 cred = curlwp->l_cred;
1094 if (__predict_false(cred != fp->f_cred)) { 1095 if (__predict_false(cred != fp->f_cred)) {
1095 kauth_cred_free(fp->f_cred); 1096 kauth_cred_free(fp->f_cred);
1096 kauth_cred_hold(cred); 1097 kauth_cred_hold(cred);
1097 fp->f_cred = cred; 1098 fp->f_cred = cred;
1098 } 1099 }