]> granicus.if.org Git - postgresql/blob - src/backend/storage/file/fd.c
The heralded `Grand Unified Configuration scheme' (GUC)
[postgresql] / src / backend / storage / file / fd.c
1 /*-------------------------------------------------------------------------
2  *
3  * fd.c
4  *        Virtual file descriptor code.
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.57 2000/05/31 00:28:27 petere Exp $
11  *
12  * NOTES:
13  *
14  * This code manages a cache of 'virtual' file descriptors (VFDs).
15  * The server opens many file descriptors for a variety of reasons,
16  * including base tables, scratch files (e.g., sort and hash spool
17  * files), and random calls to C library routines like system(3); it
18  * is quite easy to exceed system limits on the number of open files a
19  * single process can have.  (This is around 256 on many modern
20  * operating systems, but can be as low as 32 on others.)
21  *
22  * VFDs are managed as an LRU pool, with actual OS file descriptors
23  * being opened and closed as needed.  Obviously, if a routine is
24  * opened using these interfaces, all subsequent operations must also
25  * be through these interfaces (the File type is not a real file
26  * descriptor).
27  *
28  * For this scheme to work, most (if not all) routines throughout the
29  * server should use these interfaces instead of calling the C library
30  * routines (e.g., open(2) and fopen(3)) themselves.  Otherwise, we
31  * may find ourselves short of real file descriptors anyway.
32  *
33  * This file used to contain a bunch of stuff to support RAID levels 0
34  * (jbod), 1 (duplex) and 5 (xor parity).  That stuff is all gone
35  * because the parallel query processing code that called it is all
36  * gone.  If you really need it you could get it from the original
37  * POSTGRES source.
38  *-------------------------------------------------------------------------
39  */
40
41 #include <sys/types.h>
42 #include <sys/file.h>
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48
49 #include "postgres.h"
50 #include "miscadmin.h"
51 #include "storage/fd.h"
52 #include "storage/ipc.h"
53
54 /*
55  * Problem: Postgres does a system(ld...) to do dynamic loading.
56  * This will open several extra files in addition to those used by
57  * Postgres.  We need to guarantee that there are file descriptors free
58  * for ld to use.
59  *
60  * The current solution is to limit the number of file descriptors
61  * that this code will allocate at one time: it leaves RESERVE_FOR_LD free.
62  *
63  * (Even though most dynamic loaders now use dlopen(3) or the
64  * equivalent, the OS must still open several files to perform the
65  * dynamic loading.  Keep this here.)
66  */
67 #ifndef RESERVE_FOR_LD
68 #define RESERVE_FOR_LD  10
69 #endif
70
71 /*
72  * We need to ensure that we have at least some file descriptors
73  * available to postgreSQL after we've reserved the ones for LD,
74  * so we set that value here.
75  *
76  * I think 10 is an appropriate value so that's what it'll be
77  * for now.
78  */
79 #ifndef FD_MINFREE
80 #define FD_MINFREE 10
81 #endif
82
83 /* Debugging.... */
84
85 #ifdef FDDEBUG
86 #define DO_DB(A) A
87 #else
88 #define DO_DB(A)                                /* A */
89 #endif
90
91 #define VFD_CLOSED (-1)
92
93 #define FileIsValid(file) \
94         ((file) > 0 && (file) < (int) SizeVfdCache && VfdCache[file].fileName != NULL)
95
96 #define FileIsNotOpen(file) (VfdCache[file].fd == VFD_CLOSED)
97
98 typedef struct vfd
99 {
100         signed short fd;                        /* current FD, or VFD_CLOSED if none */
101         unsigned short fdstate;         /* bitflags for VFD's state */
102
103 /* these are the assigned bits in fdstate: */
104 #define FD_DIRTY                (1 << 0)/* written to, but not yet fsync'd */
105 #define FD_TEMPORARY    (1 << 1)/* should be unlinked when closed */
106
107         File            nextFree;               /* link to next free VFD, if in freelist */
108         File            lruMoreRecently;/* doubly linked recency-of-use list */
109         File            lruLessRecently;
110         long            seekPos;                /* current logical file position */
111         char       *fileName;           /* name of file, or NULL for unused VFD */
112         /* NB: fileName is malloc'd, and must be free'd when closing the VFD */
113         int                     fileFlags;              /* open(2) flags for opening the file */
114         int                     fileMode;               /* mode to pass to open(2) */
115 } Vfd;
116
117 /*
118  * Virtual File Descriptor array pointer and size.      This grows as
119  * needed.      'File' values are indexes into this array.
120  * Note that VfdCache[0] is not a usable VFD, just a list header.
121  */
122 static Vfd *VfdCache;
123 static Size SizeVfdCache = 0;
124
125 /*
126  * Number of file descriptors known to be in use by VFD entries.
127  */
128 static int      nfile = 0;
129
130 /*
131  * List of stdio FILEs opened with AllocateFile.
132  *
133  * Since we don't want to encourage heavy use of AllocateFile, it seems
134  * OK to put a pretty small maximum limit on the number of simultaneously
135  * allocated files.
136  */
137 #define MAX_ALLOCATED_FILES  32
138
139 static int      numAllocatedFiles = 0;
140 static FILE *allocatedFiles[MAX_ALLOCATED_FILES];
141
142 /*
143  * Number of temporary files opened during the current transaction;
144  * this is used in generation of tempfile names.
145  */
146 static long tempFileCounter = 0;
147
148
149 /*--------------------
150  *
151  * Private Routines
152  *
153  * Delete                  - delete a file from the Lru ring
154  * LruDelete       - remove a file from the Lru ring and close its FD
155  * Insert                  - put a file at the front of the Lru ring
156  * LruInsert       - put a file at the front of the Lru ring and open it
157  * ReleaseLruFile  - Release an fd by closing the last entry in the Lru ring
158  * AllocateVfd     - grab a free (or new) file record (from VfdArray)
159  * FreeVfd                 - free a file record
160  *
161  * The Least Recently Used ring is a doubly linked list that begins and
162  * ends on element zero.  Element zero is special -- it doesn't represent
163  * a file and its "fd" field always == VFD_CLOSED.      Element zero is just an
164  * anchor that shows us the beginning/end of the ring.
165  * Only VFD elements that are currently really open (have an FD assigned) are
166  * in the Lru ring.  Elements that are "virtually" open can be recognized
167  * by having a non-null fileName field.
168  *
169  * example:
170  *
171  *         /--less----\                            /---------\
172  *         v               \                      v                       \
173  *       #0 --more---> LeastRecentlyUsed --more-\ \
174  *        ^\                                                                    | |
175  *         \\less--> MostRecentlyUsedFile       <---/ |
176  *              \more---/                                        \--less--/
177  *
178  *--------------------
179  */
180 static void Delete(File file);
181 static void LruDelete(File file);
182 static void Insert(File file);
183 static int      LruInsert(File file);
184 static void ReleaseLruFile(void);
185 static File AllocateVfd(void);
186 static void FreeVfd(File file);
187
188 static int      FileAccess(File file);
189 static File fileNameOpenFile(FileName fileName, int fileFlags, int fileMode);
190 static char *filepath(char *filename);
191 static long pg_nofile(void);
192
193 /*
194  * pg_fsync --- same as fsync except does nothing if -F switch was given
195  */
196 int
197 pg_fsync(int fd)
198 {
199         if (enableFsync)
200                 return fsync(fd);
201         else
202                 return 0;
203 }
204
205 /*
206  * pg_nofile: determine number of filedescriptors that fd.c is allowed to use
207  */
208 static long
209 pg_nofile(void)
210 {
211         static long no_files = 0;
212
213         if (no_files == 0)
214         {
215                 /* need do this calculation only once */
216 #ifndef HAVE_SYSCONF
217                 no_files = (long) NOFILE;
218 #else
219                 no_files = sysconf(_SC_OPEN_MAX);
220                 if (no_files == -1)
221                 {
222                         elog(DEBUG, "pg_nofile: Unable to get _SC_OPEN_MAX using sysconf(); using %d", NOFILE);
223                         no_files = (long) NOFILE;
224                 }
225 #endif
226
227                 if ((no_files - RESERVE_FOR_LD) < FD_MINFREE)
228                         elog(FATAL, "pg_nofile: insufficient File Descriptors in postmaster to start backend (%ld).\n"
229                                  "                   O/S allows %ld, Postmaster reserves %d, We need %d (MIN) after that.",
230                                  no_files - RESERVE_FOR_LD, no_files, RESERVE_FOR_LD, FD_MINFREE);
231
232                 no_files -= RESERVE_FOR_LD;
233         }
234
235         return no_files;
236 }
237
238 #if defined(FDDEBUG)
239
240 static void
241 _dump_lru()
242 {
243         int                     mru = VfdCache[0].lruLessRecently;
244         Vfd                *vfdP = &VfdCache[mru];
245         char            buf[2048];
246
247         sprintf(buf, "LRU: MOST %d ", mru);
248         while (mru != 0)
249         {
250                 mru = vfdP->lruLessRecently;
251                 vfdP = &VfdCache[mru];
252                 sprintf(buf + strlen(buf), "%d ", mru);
253         }
254         sprintf(buf + strlen(buf), "LEAST");
255         elog(DEBUG, buf);
256 }
257
258 #endif   /* FDDEBUG */
259
260 static void
261 Delete(File file)
262 {
263         Vfd                *vfdP;
264
265         Assert(file != 0);
266
267         DO_DB(elog(DEBUG, "Delete %d (%s)",
268                            file, VfdCache[file].fileName));
269         DO_DB(_dump_lru());
270
271         vfdP = &VfdCache[file];
272
273         VfdCache[vfdP->lruLessRecently].lruMoreRecently = vfdP->lruMoreRecently;
274         VfdCache[vfdP->lruMoreRecently].lruLessRecently = vfdP->lruLessRecently;
275
276         DO_DB(_dump_lru());
277 }
278
279 static void
280 LruDelete(File file)
281 {
282         Vfd                *vfdP;
283         int                     returnValue;
284
285         Assert(file != 0);
286
287         DO_DB(elog(DEBUG, "LruDelete %d (%s)",
288                            file, VfdCache[file].fileName));
289
290         vfdP = &VfdCache[file];
291
292         /* delete the vfd record from the LRU ring */
293         Delete(file);
294
295         /* save the seek position */
296         vfdP->seekPos = (long) lseek(vfdP->fd, 0L, SEEK_CUR);
297         Assert(vfdP->seekPos != -1);
298
299         /* if we have written to the file, sync it before closing */
300         if (vfdP->fdstate & FD_DIRTY)
301         {
302                 returnValue = pg_fsync(vfdP->fd);
303                 Assert(returnValue != -1);
304                 vfdP->fdstate &= ~FD_DIRTY;
305         }
306
307         /* close the file */
308         returnValue = close(vfdP->fd);
309         Assert(returnValue != -1);
310
311         --nfile;
312         vfdP->fd = VFD_CLOSED;
313 }
314
315 static void
316 Insert(File file)
317 {
318         Vfd                *vfdP;
319
320         Assert(file != 0);
321
322         DO_DB(elog(DEBUG, "Insert %d (%s)",
323                            file, VfdCache[file].fileName));
324         DO_DB(_dump_lru());
325
326         vfdP = &VfdCache[file];
327
328         vfdP->lruMoreRecently = 0;
329         vfdP->lruLessRecently = VfdCache[0].lruLessRecently;
330         VfdCache[0].lruLessRecently = file;
331         VfdCache[vfdP->lruLessRecently].lruMoreRecently = file;
332
333         DO_DB(_dump_lru());
334 }
335
336 static int
337 LruInsert(File file)
338 {
339         Vfd                *vfdP;
340         int                     returnValue;
341
342         Assert(file != 0);
343
344         DO_DB(elog(DEBUG, "LruInsert %d (%s)",
345                            file, VfdCache[file].fileName));
346
347         vfdP = &VfdCache[file];
348
349         if (FileIsNotOpen(file))
350         {
351
352                 while (nfile + numAllocatedFiles >= pg_nofile())
353                         ReleaseLruFile();
354
355                 /*
356                  * The open could still fail for lack of file descriptors, eg due
357                  * to overall system file table being full.  So, be prepared to
358                  * release another FD if necessary...
359                  */
360 tryAgain:
361                 vfdP->fd = open(vfdP->fileName, vfdP->fileFlags, vfdP->fileMode);
362                 if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE))
363                 {
364                         errno = 0;
365                         ReleaseLruFile();
366                         goto tryAgain;
367                 }
368
369                 if (vfdP->fd < 0)
370                 {
371                         DO_DB(elog(DEBUG, "RE_OPEN FAILED: %d",
372                                            errno));
373                         return vfdP->fd;
374                 }
375                 else
376                 {
377                         DO_DB(elog(DEBUG, "RE_OPEN SUCCESS"));
378                         ++nfile;
379                 }
380
381                 /* seek to the right position */
382                 if (vfdP->seekPos != 0L)
383                 {
384                         returnValue = lseek(vfdP->fd, vfdP->seekPos, SEEK_SET);
385                         Assert(returnValue != -1);
386                 }
387         }
388
389         /*
390          * put it at the head of the Lru ring
391          */
392
393         Insert(file);
394
395         return 0;
396 }
397
398 static void
399 ReleaseLruFile()
400 {
401         DO_DB(elog(DEBUG, "ReleaseLruFile. Opened %d", nfile));
402
403         if (nfile <= 0)
404                 elog(ERROR, "ReleaseLruFile: No open files available to be closed");
405
406         /*
407          * There are opened files and so there should be at least one used vfd
408          * in the ring.
409          */
410         Assert(VfdCache[0].lruMoreRecently != 0);
411         LruDelete(VfdCache[0].lruMoreRecently);
412 }
413
414 /*
415  * Force one kernel file descriptor to be released (temporarily).
416  */
417 bool
418 ReleaseDataFile()
419 {
420         DO_DB(elog(DEBUG, "ReleaseDataFile. Opened %d", nfile));
421
422         if (nfile <= 0)
423                 return (false);
424         Assert(VfdCache[0].lruMoreRecently != 0);
425         LruDelete(VfdCache[0].lruMoreRecently);
426
427         return (true);
428 }
429
430 static File
431 AllocateVfd()
432 {
433         Index           i;
434         File            file;
435
436         DO_DB(elog(DEBUG, "AllocateVfd. Size %d", SizeVfdCache));
437
438         if (SizeVfdCache == 0)
439         {
440                 /* initialize header entry first time through */
441                 VfdCache = (Vfd *) malloc(sizeof(Vfd));
442                 Assert(VfdCache != NULL);
443                 MemSet((char *) &(VfdCache[0]), 0, sizeof(Vfd));
444                 VfdCache->fd = VFD_CLOSED;
445
446                 SizeVfdCache = 1;
447
448                 /*
449                  * register proc-exit call to ensure temp files are dropped at
450                  * exit
451                  */
452                 on_proc_exit(AtEOXact_Files, NULL);
453         }
454
455         if (VfdCache[0].nextFree == 0)
456         {
457
458                 /*
459                  * The free list is empty so it is time to increase the size of
460                  * the array.  We choose to double it each time this happens.
461                  * However, there's not much point in starting *real* small.
462                  */
463                 Size            newCacheSize = SizeVfdCache * 2;
464
465                 if (newCacheSize < 32)
466                         newCacheSize = 32;
467
468                 VfdCache = (Vfd *) realloc(VfdCache, sizeof(Vfd) * newCacheSize);
469                 Assert(VfdCache != NULL);
470
471                 /*
472                  * Initialize the new entries and link them into the free list.
473                  */
474
475                 for (i = SizeVfdCache; i < newCacheSize; i++)
476                 {
477                         MemSet((char *) &(VfdCache[i]), 0, sizeof(Vfd));
478                         VfdCache[i].nextFree = i + 1;
479                         VfdCache[i].fd = VFD_CLOSED;
480                 }
481                 VfdCache[newCacheSize - 1].nextFree = 0;
482                 VfdCache[0].nextFree = SizeVfdCache;
483
484                 /*
485                  * Record the new size
486                  */
487
488                 SizeVfdCache = newCacheSize;
489         }
490
491         file = VfdCache[0].nextFree;
492
493         VfdCache[0].nextFree = VfdCache[file].nextFree;
494
495         return file;
496 }
497
498 static void
499 FreeVfd(File file)
500 {
501         Vfd                *vfdP = &VfdCache[file];
502
503         DO_DB(elog(DEBUG, "FreeVfd: %d (%s)",
504                            file, vfdP->fileName ? vfdP->fileName : ""));
505
506         if (vfdP->fileName != NULL)
507         {
508                 free(vfdP->fileName);
509                 vfdP->fileName = NULL;
510         }
511
512         vfdP->nextFree = VfdCache[0].nextFree;
513         VfdCache[0].nextFree = file;
514 }
515
516 /* filepath()
517  * Convert given pathname to absolute.
518  *
519  * (Generally, this isn't actually necessary, considering that we
520  * should be cd'd into the database directory.  Presently it is only
521  * necessary to do it in "bootstrap" mode.      Maybe we should change
522  * bootstrap mode to do the cd, and save a few cycles/bytes here.)
523  */
524 static char *
525 filepath(char *filename)
526 {
527         char       *buf;
528         int                     len;
529
530         /* Not an absolute path name? Then fill in with database path... */
531         if (*filename != SEP_CHAR)
532         {
533                 len = strlen(DatabasePath) + strlen(filename) + 2;
534                 buf = (char *) palloc(len);
535                 sprintf(buf, "%s%c%s", DatabasePath, SEP_CHAR, filename);
536         }
537         else
538         {
539                 buf = (char *) palloc(strlen(filename) + 1);
540                 strcpy(buf, filename);
541         }
542
543 #ifdef FILEDEBUG
544         printf("filepath: path is %s\n", buf);
545 #endif
546
547         return buf;
548 }
549
550 static int
551 FileAccess(File file)
552 {
553         int                     returnValue;
554
555         DO_DB(elog(DEBUG, "FileAccess %d (%s)",
556                            file, VfdCache[file].fileName));
557
558         /*
559          * Is the file open?  If not, open it and put it at the head of the
560          * LRU ring (possibly closing the least recently used file to get an
561          * FD).
562          */
563
564         if (FileIsNotOpen(file))
565         {
566                 returnValue = LruInsert(file);
567                 if (returnValue != 0)
568                         return returnValue;
569         }
570         else if (VfdCache[0].lruLessRecently != file)
571         {
572
573                 /*
574                  * We now know that the file is open and that it is not the last
575                  * one accessed, so we need to move it to the head of the Lru
576                  * ring.
577                  */
578
579                 Delete(file);
580                 Insert(file);
581         }
582
583         return 0;
584 }
585
586 /*
587  *      Called when we get a shared invalidation message on some relation.
588  */
589 #ifdef NOT_USED
590 void
591 FileInvalidate(File file)
592 {
593         Assert(FileIsValid(file));
594         if (!FileIsNotOpen(file))
595                 LruDelete(file);
596 }
597
598 #endif
599
600 static File
601 fileNameOpenFile(FileName fileName,
602                                  int fileFlags,
603                                  int fileMode)
604 {
605         File            file;
606         Vfd                *vfdP;
607
608         if (fileName == NULL)
609                 elog(ERROR, "fileNameOpenFile: NULL fname");
610
611         DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
612                            fileName, fileFlags, fileMode));
613
614         file = AllocateVfd();
615         vfdP = &VfdCache[file];
616
617         while (nfile + numAllocatedFiles >= pg_nofile())
618                 ReleaseLruFile();
619
620 tryAgain:
621         vfdP->fd = open(fileName, fileFlags, fileMode);
622         if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE))
623         {
624                 DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d",
625                                    errno));
626                 errno = 0;
627                 ReleaseLruFile();
628                 goto tryAgain;
629         }
630
631         if (vfdP->fd < 0)
632         {
633                 FreeVfd(file);
634                 return -1;
635         }
636         ++nfile;
637         DO_DB(elog(DEBUG, "fileNameOpenFile: success %d",
638                            vfdP->fd));
639
640         Insert(file);
641
642         vfdP->fileName = malloc(strlen(fileName) + 1);
643         strcpy(vfdP->fileName, fileName);
644
645         vfdP->fileFlags = fileFlags & ~(O_TRUNC | O_EXCL);
646         vfdP->fileMode = fileMode;
647         vfdP->seekPos = 0;
648         vfdP->fdstate = 0x0;
649
650         return file;
651 }
652
653 /*
654  * open a file in the database directory ($PGDATA/base/...)
655  */
656 File
657 FileNameOpenFile(FileName fileName, int fileFlags, int fileMode)
658 {
659         File            fd;
660         char       *fname;
661
662         fname = filepath(fileName);
663         fd = fileNameOpenFile(fname, fileFlags, fileMode);
664         pfree(fname);
665         return fd;
666 }
667
668 /*
669  * open a file in an arbitrary directory
670  */
671 File
672 PathNameOpenFile(FileName fileName, int fileFlags, int fileMode)
673 {
674         return fileNameOpenFile(fileName, fileFlags, fileMode);
675 }
676
677 /*
678  * Open a temporary file that will disappear when we close it.
679  *
680  * This routine takes care of generating an appropriate tempfile name.
681  * There's no need to pass in fileFlags or fileMode either, since only
682  * one setting makes any sense for a temp file.
683  */
684 File
685 OpenTemporaryFile(void)
686 {
687         char            tempfilename[64];
688         File            file;
689
690         /*
691          * Generate a tempfile name that's unique within the current
692          * transaction
693          */
694         snprintf(tempfilename, sizeof(tempfilename),
695                          "pg_sorttemp%d.%ld", MyProcPid, tempFileCounter++);
696
697         /* Open the file */
698 #ifndef __CYGWIN32__
699         file = FileNameOpenFile(tempfilename,
700                                                         O_RDWR | O_CREAT | O_TRUNC, 0600);
701 #else
702         file = FileNameOpenFile(tempfilename,
703                                                         O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0600);
704 #endif
705
706         if (file <= 0)
707                 elog(ERROR, "Failed to create temporary file %s", tempfilename);
708
709         /* Mark it for deletion at close or EOXact */
710         VfdCache[file].fdstate |= FD_TEMPORARY;
711
712         return file;
713 }
714
715 /*
716  * close a file when done with it
717  */
718 void
719 FileClose(File file)
720 {
721         int                     returnValue;
722
723         Assert(FileIsValid(file));
724
725         DO_DB(elog(DEBUG, "FileClose: %d (%s)",
726                            file, VfdCache[file].fileName));
727
728         if (!FileIsNotOpen(file))
729         {
730
731                 /* remove the file from the lru ring */
732                 Delete(file);
733
734                 /* if we did any writes, sync the file before closing */
735                 if (VfdCache[file].fdstate & FD_DIRTY)
736                 {
737                         returnValue = pg_fsync(VfdCache[file].fd);
738                         Assert(returnValue != -1);
739                         VfdCache[file].fdstate &= ~FD_DIRTY;
740                 }
741
742                 /* close the file */
743                 returnValue = close(VfdCache[file].fd);
744                 Assert(returnValue != -1);
745
746                 --nfile;
747                 VfdCache[file].fd = VFD_CLOSED;
748         }
749
750         /*
751          * Delete the file if it was temporary
752          */
753         if (VfdCache[file].fdstate & FD_TEMPORARY)
754                 unlink(VfdCache[file].fileName);
755
756         /*
757          * Return the Vfd slot to the free list
758          */
759         FreeVfd(file);
760 }
761
762 /*
763  * close a file and forcibly delete the underlying Unix file
764  */
765 void
766 FileUnlink(File file)
767 {
768         Assert(FileIsValid(file));
769
770         DO_DB(elog(DEBUG, "FileUnlink: %d (%s)",
771                            file, VfdCache[file].fileName));
772
773         /* force FileClose to delete it */
774         VfdCache[file].fdstate |= FD_TEMPORARY;
775
776         FileClose(file);
777 }
778
779 int
780 FileRead(File file, char *buffer, int amount)
781 {
782         int                     returnCode;
783
784         Assert(FileIsValid(file));
785
786         DO_DB(elog(DEBUG, "FileRead: %d (%s) %d %p",
787                            file, VfdCache[file].fileName, amount, buffer));
788
789         FileAccess(file);
790         returnCode = read(VfdCache[file].fd, buffer, amount);
791         if (returnCode > 0)
792                 VfdCache[file].seekPos += returnCode;
793
794         return returnCode;
795 }
796
797 int
798 FileWrite(File file, char *buffer, int amount)
799 {
800         int                     returnCode;
801
802         Assert(FileIsValid(file));
803
804         DO_DB(elog(DEBUG, "FileWrite: %d (%s) %d %p",
805                            file, VfdCache[file].fileName, amount, buffer));
806
807         FileAccess(file);
808         returnCode = write(VfdCache[file].fd, buffer, amount);
809         if (returnCode > 0)
810                 VfdCache[file].seekPos += returnCode;
811
812         /* mark the file as needing fsync */
813         VfdCache[file].fdstate |= FD_DIRTY;
814
815         return returnCode;
816 }
817
818 long
819 FileSeek(File file, long offset, int whence)
820 {
821         Assert(FileIsValid(file));
822
823         DO_DB(elog(DEBUG, "FileSeek: %d (%s) %ld %d",
824                            file, VfdCache[file].fileName, offset, whence));
825
826         if (FileIsNotOpen(file))
827         {
828                 switch (whence)
829                 {
830                         case SEEK_SET:
831                                 VfdCache[file].seekPos = offset;
832                                 break;
833                         case SEEK_CUR:
834                                 VfdCache[file].seekPos += offset;
835                                 break;
836                         case SEEK_END:
837                                 FileAccess(file);
838                                 VfdCache[file].seekPos = lseek(VfdCache[file].fd, offset, whence);
839                                 break;
840                         default:
841                                 elog(ERROR, "FileSeek: invalid whence: %d", whence);
842                                 break;
843                 }
844         }
845         else
846                 VfdCache[file].seekPos = lseek(VfdCache[file].fd, offset, whence);
847         return VfdCache[file].seekPos;
848 }
849
850 /*
851  * XXX not actually used but here for completeness
852  */
853 #ifdef NOT_USED
854 long
855 FileTell(File file)
856 {
857         Assert(FileIsValid(file));
858         DO_DB(elog(DEBUG, "FileTell %d (%s)",
859                            file, VfdCache[file].fileName));
860         return VfdCache[file].seekPos;
861 }
862
863 #endif
864
865 int
866 FileTruncate(File file, long offset)
867 {
868         int                     returnCode;
869
870         Assert(FileIsValid(file));
871
872         DO_DB(elog(DEBUG, "FileTruncate %d (%s)",
873                            file, VfdCache[file].fileName));
874
875         FileSync(file);
876         FileAccess(file);
877         returnCode = ftruncate(VfdCache[file].fd, (size_t) offset);
878         return returnCode;
879 }
880
881 /*
882  * FileSync --- if a file is marked as dirty, fsync it.
883  *
884  * The FD_DIRTY bit is slightly misnamed: it doesn't mean that we need to
885  * write the file, but that we *have* written it and need to execute an
886  * fsync() to ensure the changes are down on disk before we mark the current
887  * transaction committed.
888  *
889  * FD_DIRTY is set by FileWrite or by an explicit FileMarkDirty() call.
890  * It is cleared after successfully fsync'ing the file.  FileClose() will
891  * fsync a dirty File that is about to be closed, since there will be no
892  * other place to remember the need to fsync after the VFD is gone.
893  *
894  * Note that the DIRTY bit is logically associated with the actual disk file,
895  * not with any particular kernel FD we might have open for it.  We assume
896  * that fsync will force out any dirty buffers for that file, whether or not
897  * they were written through the FD being used for the fsync call --- they
898  * might even have been written by some other backend!
899  *
900  * Note also that LruDelete currently fsyncs a dirty file that it is about
901  * to close the kernel file descriptor for.  The idea there is to avoid
902  * having to re-open the kernel descriptor later.  But it's not real clear
903  * that this is a performance win; we could end up fsyncing the same file
904  * multiple times in a transaction, which would probably cost more time
905  * than is saved by avoiding an open() call.  This should be studied.
906  *
907  * This routine used to think it could skip the fsync if the file is
908  * physically closed, but that is now WRONG; see comments for FileMarkDirty.
909  */
910 int
911 FileSync(File file)
912 {
913         int                     returnCode;
914
915         Assert(FileIsValid(file));
916
917         if (!(VfdCache[file].fdstate & FD_DIRTY))
918         {
919                 /* Need not sync if file is not dirty. */
920                 returnCode = 0;
921         }
922         else if (!enableFsync)
923         {
924                 /* Don't force the file open if pg_fsync isn't gonna sync it. */
925                 returnCode = 0;
926                 VfdCache[file].fdstate &= ~FD_DIRTY;
927         }
928         else
929         {
930
931                 /*
932                  * We don't use FileAccess() because we don't want to force the
933                  * file to the front of the LRU ring; we aren't expecting to
934                  * access it again soon.
935                  */
936                 if (FileIsNotOpen(file))
937                 {
938                         returnCode = LruInsert(file);
939                         if (returnCode != 0)
940                                 return returnCode;
941                 }
942                 returnCode = pg_fsync(VfdCache[file].fd);
943                 if (returnCode == 0)
944                         VfdCache[file].fdstate &= ~FD_DIRTY;
945         }
946
947         return returnCode;
948 }
949
950 /*
951  * FileMarkDirty --- mark a file as needing fsync at transaction commit.
952  *
953  * Since FileWrite marks the file dirty, this routine is not needed in
954  * normal use.  It is called when the buffer manager detects that some other
955  * backend has written out a shared buffer that this backend dirtied (but
956  * didn't write) in the current xact.  In that scenario, we need to fsync
957  * the file before we can commit.  We cannot assume that the other backend
958  * has fsync'd the file yet; we need to do our own fsync to ensure that
959  * (a) the disk page is written and (b) this backend's commit is delayed
960  * until the write is complete.
961  *
962  * Note we are assuming that an fsync issued by this backend will write
963  * kernel disk buffers that were dirtied by another backend.  Furthermore,
964  * it doesn't matter whether we currently have the file physically open;
965  * we must fsync even if we have to re-open the file to do it.
966  */
967 void
968 FileMarkDirty(File file)
969 {
970         Assert(FileIsValid(file));
971
972         DO_DB(elog(DEBUG, "FileMarkDirty: %d (%s)",
973                            file, VfdCache[file].fileName));
974
975         VfdCache[file].fdstate |= FD_DIRTY;
976 }
977
978
979 /*
980  * Routines that want to use stdio (ie, FILE*) should use AllocateFile
981  * rather than plain fopen().  This lets fd.c deal with freeing FDs if
982  * necessary to open the file.  When done, call FreeFile rather than fclose.
983  *
984  * Note that files that will be open for any significant length of time
985  * should NOT be handled this way, since they cannot share kernel file
986  * descriptors with other files; there is grave risk of running out of FDs
987  * if anyone locks down too many FDs.  Most callers of this routine are
988  * simply reading a config file that they will read and close immediately.
989  *
990  * fd.c will automatically close all files opened with AllocateFile at
991  * transaction commit or abort; this prevents FD leakage if a routine
992  * that calls AllocateFile is terminated prematurely by elog(ERROR).
993  */
994
995 FILE *
996 AllocateFile(char *name, char *mode)
997 {
998         FILE       *file;
999
1000         DO_DB(elog(DEBUG, "AllocateFile: Allocated %d.", numAllocatedFiles));
1001
1002         if (numAllocatedFiles >= MAX_ALLOCATED_FILES)
1003                 elog(ERROR, "AllocateFile: too many private FDs demanded");
1004
1005 TryAgain:
1006         if ((file = fopen(name, mode)) == NULL)
1007         {
1008                 if (errno == EMFILE || errno == ENFILE)
1009                 {
1010                         DO_DB(elog(DEBUG, "AllocateFile: not enough descs, retry, er= %d",
1011                                            errno));
1012                         errno = 0;
1013                         ReleaseLruFile();
1014                         goto TryAgain;
1015                 }
1016         }
1017         else
1018                 allocatedFiles[numAllocatedFiles++] = file;
1019         return file;
1020 }
1021
1022 void
1023 FreeFile(FILE *file)
1024 {
1025         int                     i;
1026
1027         DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", numAllocatedFiles));
1028
1029         /* Remove file from list of allocated files, if it's present */
1030         for (i = numAllocatedFiles; --i >= 0;)
1031         {
1032                 if (allocatedFiles[i] == file)
1033                 {
1034                         allocatedFiles[i] = allocatedFiles[--numAllocatedFiles];
1035                         break;
1036                 }
1037         }
1038         if (i < 0)
1039                 elog(NOTICE, "FreeFile: file was not obtained from AllocateFile");
1040
1041         fclose(file);
1042 }
1043
1044 /*
1045  * closeAllVfds
1046  *
1047  * Force all VFDs into the physically-closed state, so that the fewest
1048  * possible number of kernel file descriptors are in use.  There is no
1049  * change in the logical state of the VFDs.
1050  */
1051 void
1052 closeAllVfds()
1053 {
1054         Index           i;
1055
1056         if (SizeVfdCache > 0)
1057         {
1058                 Assert(FileIsNotOpen(0));               /* Make sure ring not corrupted */
1059                 for (i = 1; i < SizeVfdCache; i++)
1060                 {
1061                         if (!FileIsNotOpen(i))
1062                                 LruDelete(i);
1063                 }
1064         }
1065 }
1066
1067 /*
1068  * AtEOXact_Files
1069  *
1070  * This routine is called during transaction commit or abort or backend
1071  * exit (it doesn't particularly care which).  All still-open temporary-file
1072  * VFDs are closed, which also causes the underlying files to be deleted.
1073  * Furthermore, all "allocated" stdio files are closed.
1074  *
1075  * This routine is not involved in fsync'ing non-temporary files at xact
1076  * commit; that is done by FileSync under control of the buffer manager.
1077  * During a commit, that is done *before* control gets here.  If we still
1078  * have any needs-fsync bits set when we get here, we assume this is abort
1079  * and clear them.
1080  */
1081 void
1082 AtEOXact_Files(void)
1083 {
1084         Index           i;
1085
1086         if (SizeVfdCache > 0)
1087         {
1088                 Assert(FileIsNotOpen(0));               /* Make sure ring not corrupted */
1089                 for (i = 1; i < SizeVfdCache; i++)
1090                 {
1091                         if ((VfdCache[i].fdstate & FD_TEMPORARY) &&
1092                                 VfdCache[i].fileName != NULL)
1093                                 FileClose(i);
1094                         else
1095                                 VfdCache[i].fdstate &= ~FD_DIRTY;
1096                 }
1097         }
1098
1099         while (numAllocatedFiles > 0)
1100                 FreeFile(allocatedFiles[0]);
1101
1102         /*
1103          * Reset the tempfile name counter to 0; not really necessary, but
1104          * helps keep the names from growing unreasonably long.
1105          */
1106         tempFileCounter = 0;
1107 }