77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
99 * 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 $
1111 *
1212 * NOTES:
1313 *
@@ -181,7 +181,7 @@ static void Delete(File file);
181181static void LruDelete (File file );
182182static void Insert (File file );
183183static int LruInsert (File file );
184- static void ReleaseLruFile (void );
184+ static bool ReleaseLruFile (void );
185185static File AllocateVfd (void );
186186static void FreeVfd (File file );
187187
@@ -347,7 +347,10 @@ LruInsert(File file)
347347{
348348
349349while (nfile + numAllocatedFiles >=pg_nofile ())
350- ReleaseLruFile ();
350+ {
351+ if (!ReleaseLruFile ())
352+ break ;
353+ }
351354
352355/*
353356 * The open could still fail for lack of file descriptors, eg due
@@ -358,9 +361,12 @@ LruInsert(File file)
358361vfdP -> fd = open (vfdP -> fileName ,vfdP -> fileFlags ,vfdP -> fileMode );
359362if (vfdP -> fd < 0 && (errno == EMFILE || errno == ENFILE ))
360363{
364+ int save_errno = errno ;
365+
361366errno = 0 ;
362- ReleaseLruFile ();
363- gototryAgain ;
367+ if (ReleaseLruFile ())
368+ gototryAgain ;
369+ errno = save_errno ;
364370}
365371
366372if (vfdP -> fd < 0 )
@@ -392,20 +398,22 @@ LruInsert(File file)
392398return 0 ;
393399}
394400
395- static void
401+ static bool
396402ReleaseLruFile ()
397403{
398404DO_DB (elog (DEBUG ,"ReleaseLruFile. Opened %d" ,nfile ));
399405
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 */
409417}
410418
411419/*
@@ -612,17 +620,23 @@ fileNameOpenFile(FileName fileName,
612620vfdP = & VfdCache [file ];
613621
614622while (nfile + numAllocatedFiles >=pg_nofile ())
615- ReleaseLruFile ();
623+ {
624+ if (!ReleaseLruFile ())
625+ break ;
626+ }
616627
617628tryAgain :
618629vfdP -> fd = open (fileName ,fileFlags ,fileMode );
619630if (vfdP -> fd < 0 && (errno == EMFILE || errno == ENFILE ))
620631{
632+ int save_errno = errno ;
633+
621634DO_DB (elog (DEBUG ,"fileNameOpenFile: not enough descs, retry, er= %d" ,
622635errno ));
623636errno = 0 ;
624- ReleaseLruFile ();
625- gototryAgain ;
637+ if (ReleaseLruFile ())
638+ gototryAgain ;
639+ errno = save_errno ;
626640}
627641
628642if (vfdP -> fd < 0 )
@@ -1004,11 +1018,14 @@ AllocateFile(char *name, char *mode)
10041018{
10051019if (errno == EMFILE || errno == ENFILE )
10061020{
1021+ int save_errno = errno ;
1022+
10071023DO_DB (elog (DEBUG ,"AllocateFile: not enough descs, retry, er= %d" ,
10081024errno ));
10091025errno = 0 ;
1010- ReleaseLruFile ();
1011- gotoTryAgain ;
1026+ if (ReleaseLruFile ())
1027+ gotoTryAgain ;
1028+ errno = save_errno ;
10121029}
10131030}
10141031else