Skip to content

Commit a5186c7

Browse files
authored
Merge pull request #1365 from ggtakec/check_lseek
Dynamically determine whether lseek extended options are supported
2 parents 4580e6f + 55cb892 commit a5186c7

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/fdcache.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ using namespace std;
5555
static const int MAX_MULTIPART_CNT = 10 * 1000; // S3 multipart max count
5656
static const int CHECK_CACHEFILE_PART_SIZE = 1024 * 16; // Buffer size in PageList::CheckZeroAreaInFile()
5757

58+
//
59+
// [NOTE]
60+
// If the following symbols in lseek whence are undefined, define them.
61+
// If it is not supported by lseek, s3fs judges by the processing result of lseek.
62+
//
63+
#ifndef SEEK_DATA
64+
#define SEEK_DATA 3
65+
#endif
66+
#ifndef SEEK_HOLE
67+
#define SEEK_HOLE 4
68+
#endif
69+
#define TMPFILE_FOR_CHECK_HOLE "/tmp/.s3fs_hole_check.tmp"
70+
5871
//
5972
// For cache directory top path
6073
//
@@ -2719,6 +2732,8 @@ string FdManager::cache_dir;
27192732
bool FdManager::check_cache_dir_exist(false);
27202733
off_t FdManager::free_disk_space = 0;
27212734
std::string FdManager::check_cache_output;
2735+
bool FdManager::checked_lseek(false);
2736+
bool FdManager::have_lseek_hole(false);
27222737

27232738
//------------------------------------------------
27242739
// FdManager class methods
@@ -2928,6 +2943,43 @@ bool FdManager::IsSafeDiskSpace(const char* path, off_t size)
29282943
return size + FdManager::GetEnsureFreeDiskSpace() <= fsize;
29292944
}
29302945

2946+
bool FdManager::HaveLseekHole(void)
2947+
{
2948+
if(FdManager::checked_lseek){
2949+
return FdManager::have_lseek_hole;
2950+
}
2951+
2952+
// create tempolary file
2953+
int fd;
2954+
if(-1 == (fd = open(TMPFILE_FOR_CHECK_HOLE, O_CREAT|O_RDWR, 0600))){
2955+
S3FS_PRN_ERR("failed to open tempolary file(%s) - errno(%d)", TMPFILE_FOR_CHECK_HOLE, errno);
2956+
FdManager::checked_lseek = true;
2957+
FdManager::have_lseek_hole = false;
2958+
return FdManager::have_lseek_hole;
2959+
}
2960+
2961+
// check SEEK_DATA/SEEK_HOLE options
2962+
bool result = true;
2963+
if(-1 == lseek(fd, 0, SEEK_DATA)){
2964+
if(EINVAL == errno){
2965+
S3FS_PRN_ERR("lseek does not support SEEK_DATA");
2966+
result = false;
2967+
}
2968+
}
2969+
if(result && -1 == lseek(fd, 0, SEEK_HOLE)){
2970+
if(EINVAL == errno){
2971+
S3FS_PRN_ERR("lseek does not support SEEK_HOLE");
2972+
result = false;
2973+
}
2974+
}
2975+
close(fd);
2976+
unlink(TMPFILE_FOR_CHECK_HOLE);
2977+
2978+
FdManager::checked_lseek = true;
2979+
FdManager::have_lseek_hole = result;
2980+
return FdManager::have_lseek_hole;
2981+
}
2982+
29312983
//------------------------------------------------
29322984
// FdManager methods
29332985
//------------------------------------------------
@@ -3470,6 +3522,11 @@ bool FdManager::RawCheckAllCache(FILE* fp, const char* cache_stat_top_dir, const
34703522

34713523
bool FdManager::CheckAllCache()
34723524
{
3525+
if(!FdManager::HaveLseekHole()){
3526+
S3FS_PRN_ERR("lseek does not support SEEK_DATA/SEEK_HOLE, then could not check cache.");
3527+
return false;
3528+
}
3529+
34733530
FILE* fp;
34743531
if(FdManager::check_cache_output.empty()){
34753532
fp = stdout;

src/fdcache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ class FdManager
232232
static bool check_cache_dir_exist;
233233
static off_t free_disk_space; // limit free disk space
234234
static std::string check_cache_output;
235+
static bool checked_lseek;
236+
static bool have_lseek_hole;
235237

236238
fdent_map_t fent;
237239

@@ -265,6 +267,7 @@ class FdManager
265267
static bool IsSafeDiskSpace(const char* path, off_t size);
266268
static void FreeReservedDiskSpace(off_t size);
267269
static bool ReserveDiskSpace(off_t size);
270+
static bool HaveLseekHole(void);
268271

269272
// Return FdEntity associated with path, returning NULL on error. This operation increments the reference count; callers must decrement via Close after use.
270273
FdEntity* GetFdEntity(const char* path, int existfd = -1);

src/sighandlers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ void S3fsSignals::HandlerUSR1(int sig)
9393

9494
bool S3fsSignals::SetUsr1Handler(const char* path)
9595
{
96+
if(!FdManager::HaveLseekHole()){
97+
S3FS_PRN_ERR("Could not set SIGUSR1 for checking cache, because this system does not support SEEK_DATA/SEEK_HOLE in lseek function.");
98+
return false;
99+
}
100+
96101
// set output file
97102
if(!FdManager::SetCacheCheckOutput(path)){
98103
S3FS_PRN_ERR("Could not set output file(%s) for checking cache.", path ? path : "null(stdout)");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy