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.56 2000/04/12 17:15:35 momjian Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.56.2.1 2000/09/23 23:31:24 tgl Exp $
11
11
*
12
12
* NOTES:
13
13
*
@@ -181,7 +181,7 @@ static void Delete(File file);
181
181
static void LruDelete (File file );
182
182
static void Insert (File file );
183
183
static int LruInsert (File file );
184
- static void ReleaseLruFile (void );
184
+ static bool ReleaseLruFile (void );
185
185
static File AllocateVfd (void );
186
186
static void FreeVfd (File file );
187
187
@@ -347,7 +347,10 @@ LruInsert(File file)
347
347
{
348
348
349
349
while (nfile + numAllocatedFiles >=pg_nofile ())
350
- ReleaseLruFile ();
350
+ {
351
+ if (!ReleaseLruFile ())
352
+ break ;
353
+ }
351
354
352
355
/*
353
356
* The open could still fail for lack of file descriptors, eg due
@@ -358,9 +361,12 @@ LruInsert(File file)
358
361
vfdP -> fd = open (vfdP -> fileName ,vfdP -> fileFlags ,vfdP -> fileMode );
359
362
if (vfdP -> fd < 0 && (errno == EMFILE || errno == ENFILE ))
360
363
{
364
+ int save_errno = errno ;
365
+
361
366
errno = 0 ;
362
- ReleaseLruFile ();
363
- gototryAgain ;
367
+ if (ReleaseLruFile ())
368
+ gototryAgain ;
369
+ errno = save_errno ;
364
370
}
365
371
366
372
if (vfdP -> fd < 0 )
@@ -392,20 +398,22 @@ LruInsert(File file)
392
398
return 0 ;
393
399
}
394
400
395
- static void
401
+ static bool
396
402
ReleaseLruFile ()
397
403
{
398
404
DO_DB (elog (DEBUG ,"ReleaseLruFile. Opened %d" ,nfile ));
399
405
400
- if (nfile <=0 )
401
- elog (ERROR ,"ReleaseLruFile: No open files available to be closed" );
402
-
403
- /*
404
- * There are opened files and so there should be at least one used vfd
405
- * in the ring.
406
- */
407
- Assert (VfdCache [0 ].lruMoreRecently != 0 );
408
- LruDelete (VfdCache [0 ].lruMoreRecently );
406
+ if (nfile > 0 )
407
+ {
408
+ /*
409
+ * There are opened files and so there should be at least one used
410
+ * vfd in the ring.
411
+ */
412
+ Assert (VfdCache [0 ].lruMoreRecently != 0 );
413
+ LruDelete (VfdCache [0 ].lruMoreRecently );
414
+ return true;/* freed a file */
415
+ }
416
+ return false;/* no files available to free */
409
417
}
410
418
411
419
/*
@@ -612,17 +620,23 @@ fileNameOpenFile(FileName fileName,
612
620
vfdP = & VfdCache [file ];
613
621
614
622
while (nfile + numAllocatedFiles >=pg_nofile ())
615
- ReleaseLruFile ();
623
+ {
624
+ if (!ReleaseLruFile ())
625
+ break ;
626
+ }
616
627
617
628
tryAgain :
618
629
vfdP -> fd = open (fileName ,fileFlags ,fileMode );
619
630
if (vfdP -> fd < 0 && (errno == EMFILE || errno == ENFILE ))
620
631
{
632
+ int save_errno = errno ;
633
+
621
634
DO_DB (elog (DEBUG ,"fileNameOpenFile: not enough descs, retry, er= %d" ,
622
635
errno ));
623
636
errno = 0 ;
624
- ReleaseLruFile ();
625
- gototryAgain ;
637
+ if (ReleaseLruFile ())
638
+ gototryAgain ;
639
+ errno = save_errno ;
626
640
}
627
641
628
642
if (vfdP -> fd < 0 )
@@ -1004,11 +1018,14 @@ AllocateFile(char *name, char *mode)
1004
1018
{
1005
1019
if (errno == EMFILE || errno == ENFILE )
1006
1020
{
1021
+ int save_errno = errno ;
1022
+
1007
1023
DO_DB (elog (DEBUG ,"AllocateFile: not enough descs, retry, er= %d" ,
1008
1024
errno ));
1009
1025
errno = 0 ;
1010
- ReleaseLruFile ();
1011
- gotoTryAgain ;
1026
+ if (ReleaseLruFile ())
1027
+ gotoTryAgain ;
1028
+ errno = save_errno ;
1012
1029
}
1013
1030
}
1014
1031
else