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>
23 #pragma GCC diagnostic ignored "-Wformat" // shut warning on '%z'
65 static const char procfs[] =
"/proc/filesystems";
66 FILE *fs = fopen(procfs,
"r");
71 <<
"Cannot read '" << procfs <<
"': "
72 << strerror(nerr) <<
" (error " << nerr <<
")";
85 if ((nread = getdelim(&type, &len,
'\t', fs)) == -1 && ! feof(fs))
87 fprintf(stderr,
"%s:%d: %s (%zd; 1)\n",
88 procfs, line, strerror(errno), nread);
94 if ((nread = getdelim(&fstype, &len,
'\n', fs)) == -1 && ! feof(fs))
96 fprintf(stderr,
"%s:%d: %s (%zd; 2)\n",
97 procfs, line, strerror(errno), nread);
111 if (! strcmp(type,
"nodev\t")
112 || ! strcmp(fstype,
"lustre\n")
113 || ! strncmp(fstype,
"fuse", 4))
145 struct statfs *
m =
static_cast<struct statfs *
>(
arg);
146 size_t infolen =
sizeof(
struct FSInfo);
147 size_t fslen = strlen(m->f_mntfromname) + 1;
148 size_t dirlen = strlen(m->f_mntonname) + 1;
149 size_t typelen = strlen(m->f_fstypename) + 1;
150 size_t totlen = infolen + fslen + dirlen + typelen;
152 char *
p = (
char *) i;
153 i->
fsname = strncpy(p += infolen, m->f_mntfromname, fslen);
154 i->
type = strncpy(p += fslen, m->f_fstypename, typelen);
155 i->
dir = strncpy(p += typelen, m->f_mntonname, dirlen);
156 i->
dev = m->f_fsid.val[0];
165 i->
freespc /= 1024. * 1024. * 1024.;
173 i->
local = ((m->f_flags & MNT_LOCAL) ? 1 : 0);
178 mntent *m =
static_cast<mntent *
>(
arg);
179 size_t infolen =
sizeof(
struct FSInfo);
180 size_t fslen = strlen(m->mnt_fsname) + 1;
181 size_t dirlen = strlen(m->mnt_dir) + 1;
182 size_t typelen = strlen(m->mnt_type) + 1;
183 size_t originlen = strlen(m->mnt_fsname) + 1;
184 size_t totlen = infolen + fslen + dirlen + typelen + originlen;
186 char *p = (
char *) i;
187 i->
fsname = strncpy(p += infolen, m->mnt_fsname, fslen);
188 i->
type = strncpy(p += fslen, m->mnt_type, typelen);
189 i->
dir = strncpy(p += typelen, m->mnt_dir, dirlen);
190 i->
origin = strncpy(p += dirlen, m->mnt_fsname, originlen);
196 i->
bind = strstr(m->mnt_opts,
"bind") !=
nullptr;
217 struct statfs *mtab = 0;
218 if ((rc = getmntinfo(&mtab, MNT_NOWAIT)) < 0)
222 <<
"getmntinfo() failed: " << strerror(nerr)
223 <<
" (error " << nerr <<
")";
228 for (
int ix = 0; ix < rc; ++ix)
233 const char *
const _PATH_MOUNTED_LINUX =
"/proc/self/mounts";
235 FILE *mtab = setmntent(_PATH_MOUNTED_LINUX,
"r");
240 <<
"Cannot read '" << _PATH_MOUNTED_LINUX <<
"': "
241 << strerror(nerr) <<
" (error " << nerr <<
")";
246 while ((m = getmntent(mtab)))
273 if (lstat(i->
dir, &s) < 0)
276 if (nerr != ENOENT && nerr != EACCES)
278 <<
"Cannot lstat('" << i->
dir <<
"'): "
279 << strerror(nerr) <<
" (error " << nerr <<
")";
283 if (statfs(i->
dir, &sfs) < 0)
287 <<
"Cannot statfs('" << i->
dir <<
"'): "
288 << strerror(nerr) <<
" (error " << nerr <<
")";
298 i->
freespc /= 1024. * 1024. * 1024.;
326 for (
const auto & old_path : prev_paths)
328 if (!strcmp(old_path.c_str(),
path))
331 <<
"Found a loop in bind mounts; stopping evaluation.";
338 size_t len = strlen(path);
339 for (
size_t i = 0;
i <
fs_.size(); ++
i)
343 size_t fslen = strlen(
fs_[
i]->
dir);
344 if (! strncmp(
fs_[
i]->
dir, path, fslen)
345 && ((fslen == 1 &&
fs_[
i]->
dir[0] ==
'/')
346 || len == fslen || path[fslen] ==
'/')
347 && (! best || fslen > bestlen))
358 if (
fs_[i]->dev != s->st_dev ||
fs_[i]->fstype != sfs->f_type)
374 char *fullpath = realpath(best->
origin, 0);
377 fullpath = strdup(best->
origin);
379 if (lstat(fullpath, &s2) < 0)
383 <<
"Cannot lstat('" << fullpath <<
"' alias '"
384 << path <<
"'): " << strerror(nerr) <<
" (error "
390 if (statfs(fullpath, &sfs2) < 0)
394 <<
"Cannot statfs('" << fullpath <<
"' alias '"
395 << path <<
"'): " << strerror(nerr) <<
" (error "
401 prev_paths.push_back(path);
403 return new_best ? new_best : best;
423 char *fullpath = realpath(path.c_str(), 0);
426 fullpath = strdup(path.c_str());
428 if (lstat(fullpath, &s) < 0)
432 <<
"Cannot lstat('" << fullpath <<
"' alias '"
433 << path <<
"'): " << strerror(nerr) <<
" (error "
439 if (statfs(fullpath, &sfs) < 0)
443 <<
"Cannot statfs('" << fullpath <<
"' alias '"
444 << path <<
"'): " << strerror(nerr) <<
" (error "
450 std::vector<std::string> prev_paths;
454 return m ? m->
local :
false;
481 std::ostringstream warningst;
482 warningst <<
"Cannot use lazy-download because:\n";
484 for (
size_t i = 0,
e = paths.size();
i <
e; ++
i)
487 const char *inpath = paths[
i].c_str();
488 const char *
path = inpath;
492 char *
p = getenv(path+1);
495 else if (! strcmp(path,
"$TMPDIR"))
499 if (! (fullpath = realpath(path, 0)))
500 fullpath = strdup(path);
504 <<
"Checking if '" << fullpath <<
"', from '"
505 << inpath <<
"' is valid cache path with "
506 << minFreeSpace <<
" free space" << std::endl;
509 if (lstat(fullpath, &s) < 0)
512 if (nerr != ENOENT && nerr != EACCES)
514 <<
"Cannot lstat('" << fullpath <<
"', from '"
515 << inpath <<
"'): " << strerror(nerr) <<
" (error "
521 if (statfs(fullpath, &sfs) < 0)
525 <<
"Cannot statfs('" << fullpath <<
"', from '"
526 << inpath <<
"'): " << strerror(nerr) <<
" (error "
532 std::vector<std::string> prev_paths;
536 <<
"Candidate '" << fullpath <<
"': "
537 <<
"found=" << (m ? 1 : 0)
538 <<
" local=" << (m && m->
local)
539 <<
" free=" << (m ? m->
freespc : 0)
540 <<
" access=" << access(fullpath, W_OK)
547 && access(fullpath, W_OK) == 0)
557 warningst <<
"- The mount " << fullpath <<
" is not local.\n";
559 else if (m->
freespc < minFreeSpace)
561 warningst <<
" - The mount at " << fullpath <<
" has only " << m->
freespc <<
" GB free; a minumum of " << minFreeSpace <<
" GB is required.\n";
563 else if (access(fullpath, W_OK))
565 warningst <<
" - The process has no permission to write into " << fullpath <<
"\n";
573 if (warning_str.size())
575 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)
Information about file systems on this node.
std::string unusable_dir_warnings_
std::string findCachePath(const std::vector< std::string > &paths, double minFreeSpace)
bool isLocalPath(const std::string &path)
tuple path
else: Piece not in the list, fine.
std::vector< std::string > fstypes_
FSInfo * initFSInfo(void *p)
std::vector< FSInfo * > fs_
FSInfo * findMount(const char *path, struct statfs *sfs, struct stat *s, std::vector< std::string > &)