2 #define _FILE_OFFSET_BITS 64 10 #include <sys/param.h> 12 # include <sys/statvfs.h> 13 # include <sys/ucred.h> 14 # include <sys/mount.h> 24 #pragma GCC diagnostic ignored "-Wformat" // shut warning on '%z' 66 static const char procfs[] =
"/proc/filesystems";
67 FILE *fs = fopen(procfs,
"r");
72 <<
"Cannot read '" << procfs <<
"': " 73 << strerror(nerr) <<
" (error " << nerr <<
")";
86 if ((nread = getdelim(&type, &len,
'\t', fs)) == -1 && ! feof(fs))
88 fprintf(stderr,
"%s:%d: %s (%zd; 1)\n",
89 procfs, line, strerror(errno), nread);
95 if ((nread = getdelim(&fstype, &len,
'\n', fs)) == -1 && ! feof(fs))
97 fprintf(stderr,
"%s:%d: %s (%zd; 2)\n",
98 procfs, line, strerror(errno), nread);
112 if (! strcmp(type,
"nodev\t")
113 || ! strcmp(fstype,
"lustre\n")
114 || ! strncmp(fstype,
"fuse", 4))
146 struct statfs *
m =
static_cast<struct statfs *
>(
arg);
147 size_t infolen =
sizeof(
struct FSInfo);
148 size_t fslen = strlen(m->f_mntfromname) + 1;
149 size_t dirlen = strlen(m->f_mntonname) + 1;
150 size_t typelen = strlen(m->f_fstypename) + 1;
151 size_t totlen = infolen + fslen + dirlen + typelen;
153 char *
p = (
char *) i;
154 i->
fsname = strncpy(p += infolen, m->f_mntfromname, fslen);
155 i->
type = strncpy(p += fslen, m->f_fstypename, typelen);
156 i->
dir = strncpy(p += typelen, m->f_mntonname, dirlen);
157 i->
dev = m->f_fsid.val[0];
166 i->
freespc /= 1024. * 1024. * 1024.;
174 i->
local = ((m->f_flags & MNT_LOCAL) ? 1 : 0);
179 mntent *m =
static_cast<mntent *
>(
arg);
180 size_t infolen =
sizeof(
struct FSInfo);
181 size_t fslen = strlen(m->mnt_fsname) + 1;
182 size_t dirlen = strlen(m->mnt_dir) + 1;
183 size_t typelen = strlen(m->mnt_type) + 1;
184 size_t originlen = strlen(m->mnt_fsname) + 1;
185 size_t totlen = infolen + fslen + dirlen + typelen + originlen;
187 char *p = (
char *) i;
188 i->
fsname =
static_cast<char*
>(memcpy(p += infolen, m->mnt_fsname, fslen));
189 i->
type =
static_cast<char*
>(memcpy(p += fslen, m->mnt_type, typelen));
190 i->
dir =
static_cast<char*
>(memcpy(p += typelen, m->mnt_dir, dirlen));
191 i->
origin =
static_cast<char*
>(memcpy(p += dirlen, m->mnt_fsname, originlen));
197 i->
bind = strstr(m->mnt_opts,
"bind") !=
nullptr;
218 struct statfs *mtab = 0;
219 if ((rc = getmntinfo(&mtab, MNT_NOWAIT)) < 0)
223 <<
"getmntinfo() failed: " << strerror(nerr)
224 <<
" (error " << nerr <<
")";
229 for (
int ix = 0; ix < rc; ++ix)
234 const char *
const _PATH_MOUNTED_LINUX =
"/proc/self/mounts";
236 FILE *mtab = setmntent(_PATH_MOUNTED_LINUX,
"r");
241 <<
"Cannot read '" << _PATH_MOUNTED_LINUX <<
"': " 242 << strerror(nerr) <<
" (error " << nerr <<
")";
247 while ((m = getmntent(mtab)))
273 if (lstat(i->
dir, &s) < 0)
278 if (nerr != ENOENT && nerr != EACCES)
280 <<
"Cannot lstat('" << i->
dir <<
"'): " 281 << strerror(nerr) <<
" (error " << nerr <<
")";
285 if (statfs(i->
dir, &sfs) < 0)
290 <<
"Cannot statfs('" << i->
dir <<
"'): " 291 << strerror(nerr) <<
" (error " << nerr <<
")";
301 i->
freespc /= 1024. * 1024. * 1024.;
330 for (
const auto & old_path : prev_paths)
332 if (!strcmp(old_path.c_str(),
path))
335 <<
"Found a loop in bind mounts; stopping evaluation.";
342 size_t len = strlen(path);
343 for (
size_t i = 0;
i <
fs_.size(); ++
i)
353 size_t fslen = strlen(
fs_[
i]->
dir);
354 if (! strncmp(
fs_[
i]->
dir, path, fslen)
355 && ((fslen == 1 &&
fs_[
i]->
dir[0] ==
'/')
356 || len == fslen || path[fslen] ==
'/')
357 && (! best || fslen > bestlen || (fslen == bestlen && !best->
local)))
368 if (
fs_[i]->
dev != s->st_dev ||
fs_[i]->fstype != sfs->f_type)
384 char *fullpath = realpath(best->
origin,
nullptr);
387 fullpath = strdup(best->
origin);
389 if (lstat(fullpath, &s2) < 0)
393 <<
"Cannot lstat('" << fullpath <<
"' alias '" 394 << path <<
"'): " << strerror(nerr) <<
" (error " 400 if (statfs(fullpath, &sfs2) < 0)
404 <<
"Cannot statfs('" << fullpath <<
"' alias '" 405 << path <<
"'): " << strerror(nerr) <<
" (error " 411 prev_paths.push_back(path);
413 return new_best ? new_best : best;
433 char *fullpath = realpath(path.c_str(),
nullptr);
436 fullpath = strdup(path.c_str());
438 if (lstat(fullpath, &s) < 0)
442 <<
"Cannot lstat('" << fullpath <<
"' alias '" 443 << path <<
"'): " << strerror(nerr) <<
" (error " 449 if (statfs(fullpath, &sfs) < 0)
453 <<
"Cannot statfs('" << fullpath <<
"' alias '" 454 << path <<
"'): " << strerror(nerr) <<
" (error " 460 std::vector<std::string> prev_paths;
464 return m ? m->
local :
false;
485 std::pair<std::string, std::string>
487 double minFreeSpace)
const 491 std::ostringstream warningst;
492 warningst <<
"Cannot use lazy-download because:\n";
494 for (
size_t i = 0,
e = paths.size();
i <
e; ++
i)
497 const char *inpath = paths[
i].c_str();
498 const char *
path = inpath;
502 char *
p = getenv(path+1);
505 else if (! strcmp(path,
"$TMPDIR"))
509 if (! (fullpath = realpath(path,
nullptr)))
510 fullpath = strdup(path);
514 <<
"Checking if '" << fullpath <<
"', from '" 515 << inpath <<
"' is valid cache path with " 516 << minFreeSpace <<
" free space" << std::endl;
519 if (lstat(fullpath, &s) < 0)
522 if (nerr != ENOENT && nerr != EACCES)
524 <<
"Cannot lstat('" << fullpath <<
"', from '" 525 << inpath <<
"'): " << strerror(nerr) <<
" (error " 531 if (statfs(fullpath, &sfs) < 0)
535 <<
"Cannot statfs('" << fullpath <<
"', from '" 536 << inpath <<
"'): " << strerror(nerr) <<
" (error " 542 std::vector<std::string> prev_paths;
546 <<
"Candidate '" << fullpath <<
"': " 547 <<
"found=" << (m ? 1 : 0)
548 <<
" local=" << (m && m->
local)
549 <<
" free=" << (m ? m->
freespc : 0)
550 <<
" access=" << access(fullpath, W_OK)
557 && access(fullpath, W_OK) == 0)
567 warningst <<
"- The mount " << fullpath <<
" is not local.\n";
569 else if (m->
freespc < minFreeSpace)
571 warningst <<
" - The mount at " << fullpath <<
" has only " << m->
freespc <<
" GB free; a minumum of " << minFreeSpace <<
" GB is required.\n";
573 else if (access(fullpath, W_OK))
575 warningst <<
" - The process has no permission to write into " << fullpath <<
"\n";
583 if (!warning_str.empty())
585 warning_str = warning_str.substr(0, warning_str.size()-2);
604 for (
size_t i = 0,
e =
fs_.size();
i <
e; ++
i)
int statFSInfo(FSInfo *i) const
Information about file systems on this node.
std::vector< std::string > fstypes_
FSInfo * initFSInfo(void *p)
bool isLocalPath(const std::string &path) const
std::atomic< bool > checked
std::pair< std::string, std::string > findCachePath(const std::vector< std::string > &paths, double minFreeSpace) const
std::vector< FSInfo * > fs_
FSInfo * findMount(const char *path, struct statfs *sfs, struct stat *s, std::vector< std::string > &) const