@@ -395,8 +395,10 @@ pg_fdatasync(int fd)
395
395
* flushed.
396
396
*/
397
397
void
398
- pg_flush_data (int fd , off_t offset , off_t nbytes )
398
+ pg_flush_data (int fd , off_t offset , off_t nbytes , bool isdir )
399
399
{
400
+ (void ) isdir ; /* this can be unused on some archs */
401
+
400
402
/*
401
403
* Right now file flushing is primarily used to avoid making later
402
404
* fsync()/fdatasync() calls have a less impact. Thus don't trigger
@@ -452,15 +454,26 @@ pg_flush_data(int fd, off_t offset, off_t nbytes)
452
454
* (msync()), and then remove the mapping again (munmap()).
453
455
*/
454
456
457
+ /* mmap will not work with dirs */
458
+ if (isdir )
459
+ return ;
460
+
455
461
/* mmap() need exact length when we want to map whole file */
456
462
if ((offset == 0 ) && (nbytes == 0 ))
457
463
{
458
- int pagesize = getpagesize ();
459
- nbytes = (lseek (fd , offset , SEEK_END )/pagesize + 1 )* pagesize ;
464
+ int pagesize = sysconf (_SC_PAGESIZE );
465
+
466
+ nbytes = lseek (fd , 0 , SEEK_END );
460
467
if (nbytes < 0 )
461
468
ereport (WARNING ,
462
469
(errcode_for_file_access (),
463
470
errmsg ("could not determine dirty data size: %m" )));
471
+
472
+ /* aling to pagesize with underestimation */
473
+ nbytes = (nbytes /pagesize )* pagesize ;
474
+
475
+ if (nbytes == 0 )
476
+ return ;
464
477
}
465
478
466
479
p = mmap (NULL , nbytes ,
@@ -1526,7 +1539,7 @@ FileWriteback(File file, off_t offset, int amount)
1526
1539
if (returnCode < 0 )
1527
1540
return ;
1528
1541
1529
- pg_flush_data (VfdCache [file ].fd , offset , amount );
1542
+ pg_flush_data (VfdCache [file ].fd , offset , amount , false );
1530
1543
}
1531
1544
1532
1545
int
@@ -2932,7 +2945,7 @@ pre_sync_fname(const char *fname, bool isdir, int elevel)
2932
2945
* pg_flush_data() ignores errors, which is ok because this is only a
2933
2946
* hint.
2934
2947
*/
2935
- pg_flush_data (fd , 0 , 0 );
2948
+ pg_flush_data (fd , 0 , 0 , isdir );
2936
2949
2937
2950
(void ) CloseTransientFile (fd );
2938
2951
}
0 commit comments