7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.62 2000/07/05 21:10:05 tgl Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.63 2000/08/27 21:48:00 tgl Exp $
11
11
*
12
12
* NOTES:
13
13
*
@@ -182,7 +182,7 @@ static void Delete(File file);
182
182
static void LruDelete (File file );
183
183
static void Insert (File file );
184
184
static int LruInsert (File file );
185
- static void ReleaseLruFile (void );
185
+ static bool ReleaseLruFile (void );
186
186
static File AllocateVfd (void );
187
187
static void FreeVfd (File file );
188
188
@@ -230,13 +230,16 @@ BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
230
230
if (fd >=0 )
231
231
return fd ;/* success! */
232
232
233
- if (( errno == EMFILE || errno == ENFILE ) && nfile > 0 )
233
+ if (errno == EMFILE || errno == ENFILE )
234
234
{
235
+ int save_errno = errno ;
236
+
235
237
DO_DB (elog (DEBUG ,"BasicOpenFile: not enough descs, retry, er= %d" ,
236
238
errno ));
237
239
errno = 0 ;
238
- ReleaseLruFile ();
239
- gototryAgain ;
240
+ if (ReleaseLruFile ())
241
+ gototryAgain ;
242
+ errno = save_errno ;
240
243
}
241
244
242
245
return -1 ;/* failure */
@@ -278,7 +281,7 @@ pg_nofile(void)
278
281
#if defined(FDDEBUG )
279
282
280
283
static void
281
- _dump_lru ()
284
+ _dump_lru (void )
282
285
{
283
286
int mru = VfdCache [0 ].lruLessRecently ;
284
287
Vfd * vfdP = & VfdCache [mru ];
@@ -389,7 +392,10 @@ LruInsert(File file)
389
392
if (FileIsNotOpen (file ))
390
393
{
391
394
while (nfile + numAllocatedFiles >=pg_nofile ())
392
- ReleaseLruFile ();
395
+ {
396
+ if (!ReleaseLruFile ())
397
+ break ;
398
+ }
393
399
394
400
/*
395
401
* The open could still fail for lack of file descriptors, eg due
@@ -400,8 +406,7 @@ LruInsert(File file)
400
406
vfdP -> fileMode );
401
407
if (vfdP -> fd < 0 )
402
408
{
403
- DO_DB (elog (DEBUG ,"RE_OPEN FAILED: %d" ,
404
- errno ));
409
+ DO_DB (elog (DEBUG ,"RE_OPEN FAILED: %d" ,errno ));
405
410
return vfdP -> fd ;
406
411
}
407
412
else
@@ -427,24 +432,26 @@ LruInsert(File file)
427
432
return 0 ;
428
433
}
429
434
430
- static void
431
- ReleaseLruFile ()
435
+ static bool
436
+ ReleaseLruFile (void )
432
437
{
433
438
DO_DB (elog (DEBUG ,"ReleaseLruFile. Opened %d" ,nfile ));
434
439
435
- if (nfile <=0 )
436
- elog (ERROR ,"ReleaseLruFile: No open files available to be closed" );
437
-
438
- /*
439
- * There are opened files and so there should be at least one used vfd
440
- * in the ring.
441
- */
442
- Assert (VfdCache [0 ].lruMoreRecently != 0 );
443
- LruDelete (VfdCache [0 ].lruMoreRecently );
440
+ if (nfile > 0 )
441
+ {
442
+ /*
443
+ * There are opened files and so there should be at least one used
444
+ * vfd in the ring.
445
+ */
446
+ Assert (VfdCache [0 ].lruMoreRecently != 0 );
447
+ LruDelete (VfdCache [0 ].lruMoreRecently );
448
+ return true;/* freed a file */
449
+ }
450
+ return false;/* no files available to free */
444
451
}
445
452
446
453
static File
447
- AllocateVfd ()
454
+ AllocateVfd (void )
448
455
{
449
456
Index i ;
450
457
File file ;
@@ -631,7 +638,10 @@ fileNameOpenFile(FileName fileName,
631
638
vfdP = & VfdCache [file ];
632
639
633
640
while (nfile + numAllocatedFiles >=pg_nofile ())
634
- ReleaseLruFile ();
641
+ {
642
+ if (!ReleaseLruFile ())
643
+ break ;
644
+ }
635
645
636
646
vfdP -> fd = BasicOpenFile (fileName ,fileFlags ,fileMode );
637
647
@@ -1027,34 +1037,39 @@ AllocateFile(char *name, char *mode)
1027
1037
{
1028
1038
FILE * file ;
1029
1039
1030
- DO_DB (elog (DEBUG ,"AllocateFile: Allocated %d. " ,numAllocatedFiles ));
1040
+ DO_DB (elog (DEBUG ,"AllocateFile: Allocated %d" ,numAllocatedFiles ));
1031
1041
1032
1042
if (numAllocatedFiles >=MAX_ALLOCATED_FILES )
1033
1043
elog (ERROR ,"AllocateFile: too many private FDs demanded" );
1034
1044
1035
1045
TryAgain :
1036
- if ((file = fopen (name ,mode ))= =NULL )
1046
+ if ((file = fopen (name ,mode ))! =NULL )
1037
1047
{
1038
- if ((errno == EMFILE || errno == ENFILE )&& nfile > 0 )
1039
- {
1040
- DO_DB (elog (DEBUG ,"AllocateFile: not enough descs, retry, er= %d" ,
1041
- errno ));
1042
- errno = 0 ;
1043
- ReleaseLruFile ();
1048
+ allocatedFiles [numAllocatedFiles ++ ]= file ;
1049
+ return file ;
1050
+ }
1051
+
1052
+ if (errno == EMFILE || errno == ENFILE )
1053
+ {
1054
+ int save_errno = errno ;
1055
+
1056
+ DO_DB (elog (DEBUG ,"AllocateFile: not enough descs, retry, er= %d" ,
1057
+ errno ));
1058
+ errno = 0 ;
1059
+ if (ReleaseLruFile ())
1044
1060
gotoTryAgain ;
1045
- }
1061
+ errno = save_errno ;
1046
1062
}
1047
- else
1048
- allocatedFiles [numAllocatedFiles ++ ]= file ;
1049
- return file ;
1063
+
1064
+ return NULL ;
1050
1065
}
1051
1066
1052
1067
void
1053
1068
FreeFile (FILE * file )
1054
1069
{
1055
1070
int i ;
1056
1071
1057
- DO_DB (elog (DEBUG ,"FreeFile: Allocated %d. " ,numAllocatedFiles ));
1072
+ DO_DB (elog (DEBUG ,"FreeFile: Allocated %d" ,numAllocatedFiles ));
1058
1073
1059
1074
/* Remove file from list of allocated files, if it's present */
1060
1075
for (i = numAllocatedFiles ;-- i >=0 ;)
@@ -1079,7 +1094,7 @@ FreeFile(FILE *file)
1079
1094
* change in the logical state of the VFDs.
1080
1095
*/
1081
1096
void
1082
- closeAllVfds ()
1097
+ closeAllVfds (void )
1083
1098
{
1084
1099
Index i ;
1085
1100