CMS 3D CMS Logo

LocalFileSystem.cc
Go to the documentation of this file.
1 #define _GNU_SOURCE 1
2 #define _FILE_OFFSET_BITS 64
5 #include <cerrno>
6 #include <cstdio>
7 #include <cstdlib>
8 #include <cstring>
9 #include <cassert>
10 #include <sys/param.h>
11 #if BSD
12 #include <sys/statvfs.h>
13 #include <sys/ucred.h>
14 #include <sys/mount.h>
15 #else
16 #include <mntent.h>
17 #include <sys/vfs.h>
18 #endif
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <iostream>
22 #include <atomic>
23 #include <memory>
24 
27  char *fsname; //< file system name
28  char *type; //< file system type
29  char *dir; //< mount point directory
30  char *origin = nullptr; //< mount origin
31  dev_t dev; //< device id
32  long fstype; //< file system id
33  double freespc; //< free space in megabytes
34  unsigned local : 1; //< flag for local device
35  unsigned bind : 1; //< flag for bind mounts
36  std::atomic<bool> checked; //< flag for valid dev, fstype
37 };
38 
59  int ret = 0;
60 
61 #if __linux__
62  constexpr char procfs[] = "/proc/filesystems";
63  auto close_ = [](FILE *iFile) { fclose(iFile); };
64  std::unique_ptr<FILE, decltype(close_)> fs(fopen(procfs, "r"), close_);
65  if (!fs) {
66  int nerr = errno;
67  edm::LogWarning("LocalFileSystem::readFSTypes()")
68  << "Cannot read '" << procfs << "': " << strerror(nerr) << " (error " << nerr << ")";
69  return -1;
70  }
71 
72  ssize_t nread;
73  int line = 0;
74  auto free_ = [](char **iPtr) { free(*iPtr); };
75  while (!feof(fs.get())) {
76  char *type = nullptr;
77  std::unique_ptr<char *, decltype(free_)> freeType(&type, free_);
78 
79  size_t len = 0;
80  ++line;
81 
82  if ((nread = getdelim(&type, &len, '\t', fs.get())) == -1 && !feof(fs.get())) {
83  edm::LogError("LocalFileSystem::readFSTypes()")
84  .format("{}:{}: {} ({}; 1)\n", procfs, line, strerror(errno), nread);
85  ret = -1;
86  break;
87  }
88 
89  char *fstype = nullptr;
90  std::unique_ptr<char *, decltype(free_)> freeFSType(&fstype, free_);
91  if ((nread = getdelim(&fstype, &len, '\n', fs.get())) == -1 && !feof(fs.get())) {
92  edm::LogError("LocalFileSystem::readFSTypes()")
93  .format("{}:{}: {} ({}; 2)\n", procfs, line, strerror(errno), nread);
94  ret = -1;
95  break;
96  }
97 
98  if (feof(fs.get())) {
99  break;
100  }
101 
102  if (!strcmp(type, "nodev\t") || !strcmp(fstype, "lustre\n") || !strncmp(fstype, "fuse", 4)) {
103  continue;
104  }
105 
106  assert(nread >= 1);
107  fstype[nread - 1] = 0;
108  fstypes_.push_back(fstype);
109  }
110 #endif // __linux__
111 
112  return ret;
113 }
114 
124 #if BSD
125  struct statfs *m = static_cast<struct statfs *>(arg);
126  size_t infolen = sizeof(struct FSInfo);
127  size_t fslen = strlen(m->f_mntfromname) + 1;
128  size_t dirlen = strlen(m->f_mntonname) + 1;
129  size_t typelen = strlen(m->f_fstypename) + 1;
130  size_t totlen = infolen + fslen + dirlen + typelen;
131  FSInfo *i = (FSInfo *)malloc(totlen);
132  char *p = (char *)i;
133  i->fsname = strncpy(p += infolen, m->f_mntfromname, fslen);
134  i->type = strncpy(p += fslen, m->f_fstypename, typelen);
135  i->dir = strncpy(p += typelen, m->f_mntonname, dirlen);
136  i->dev = m->f_fsid.val[0];
137  i->fstype = m->f_type;
138  i->freespc = 0;
139  i->bind = 0;
140  i->origin = nullptr;
141  if (m->f_bsize > 0) {
142  i->freespc = m->f_bavail;
143  i->freespc *= m->f_bsize;
144  i->freespc /= 1024. * 1024. * 1024.;
145  }
146  /* FIXME: This incorrectly says that mounted disk images are local,
147  even if it was mounted from a network server. The alternative is
148  to walk up the device tree using either a) process IORegistry to
149  get the device tree, which lists devices for disk images, and from
150  there translate volume uuid to a mount point; b) parse output from
151  'hdiutil info -plist' to determine image-path / dev-entry map. */
152  i->local = ((m->f_flags & MNT_LOCAL) ? 1 : 0);
153  i->checked = 1;
154  return i;
155 
156 #else // ! BSD
157  mntent *m = static_cast<mntent *>(arg);
158  size_t infolen = sizeof(struct FSInfo);
159  size_t fslen = strlen(m->mnt_fsname) + 1;
160  size_t dirlen = strlen(m->mnt_dir) + 1;
161  size_t typelen = strlen(m->mnt_type) + 1;
162  size_t originlen = strlen(m->mnt_fsname) + 1;
163  size_t totlen = infolen + fslen + dirlen + typelen + originlen;
164  FSInfo *i = (FSInfo *)malloc(totlen);
165  char *p = (char *)i;
166  i->fsname = static_cast<char *>(memcpy(p += infolen, m->mnt_fsname, fslen));
167  i->type = static_cast<char *>(memcpy(p += fslen, m->mnt_type, typelen));
168  i->dir = static_cast<char *>(memcpy(p += typelen, m->mnt_dir, dirlen));
169  i->origin = static_cast<char *>(memcpy(p += dirlen, m->mnt_fsname, originlen));
170  i->dev = -1;
171  i->fstype = -1;
172  i->freespc = 0;
173  i->local = 0;
174  i->checked = false;
175  i->bind = strstr(m->mnt_opts, "bind") != nullptr;
176 
177  for (size_t j = 0; j < fstypes_.size() && !i->local; ++j)
178  if (fstypes_[j] == i->type)
179  i->local = 1;
180 #endif // BSD
181 
182  return i;
183 }
184 
192 #if BSD
193  int rc;
194  struct statfs *mtab = 0;
195  if ((rc = getmntinfo(&mtab, MNT_NOWAIT)) < 0) {
196  int nerr = errno;
197  edm::LogWarning("LocalFileSystem::initFSList()")
198  << "getmntinfo() failed: " << strerror(nerr) << " (error " << nerr << ")";
199  return -1;
200  }
201 
202  fs_.reserve(rc);
203  for (int ix = 0; ix < rc; ++ix)
204  fs_.push_back(initFSInfo(&mtab[ix]));
205 
206  free(mtab);
207 #else
208  const char *const _PATH_MOUNTED_LINUX = "/proc/self/mounts";
209  struct mntent *m;
210  FILE *mtab = setmntent(_PATH_MOUNTED_LINUX, "r");
211  if (!mtab) {
212  int nerr = errno;
213  edm::LogWarning("LocalFileSystem::initFSList()")
214  << "Cannot read '" << _PATH_MOUNTED_LINUX << "': " << strerror(nerr) << " (error " << nerr << ")";
215  return -1;
216  }
217 
218  fs_.reserve(20);
219  while ((m = getmntent(mtab)))
220  fs_.push_back(initFSInfo(m));
221 
222  endmntent(mtab);
223 #endif
224 
225  return 0;
226 }
227 
237  int ret = 0;
238  struct stat s;
239  struct statfs sfs;
240 
241  if (!i->checked) {
242  if (lstat(i->dir, &s) < 0) {
243  i->checked = true;
244 
245  int nerr = errno;
246  if (nerr != ENOENT && nerr != EACCES)
247  edm::LogWarning("LocalFileSystem::statFSInfo()")
248  << "Cannot lstat('" << i->dir << "'): " << strerror(nerr) << " (error " << nerr << ")";
249  return -1;
250  }
251 
252  if (statfs(i->dir, &sfs) < 0) {
253  i->checked = true;
254  int nerr = errno;
255  edm::LogWarning("LocalFileSystem::statFSInfo()")
256  << "Cannot statfs('" << i->dir << "'): " << strerror(nerr) << " (error " << nerr << ")";
257  return -1;
258  }
259 
260  i->dev = s.st_dev;
261  i->fstype = sfs.f_type;
262  if (sfs.f_bsize > 0) {
263  i->freespc = sfs.f_bavail;
264  i->freespc *= sfs.f_bsize;
265  i->freespc /= 1024. * 1024. * 1024.;
266  }
267  i->checked = true;
268  } else if (i->fstype == -1) {
269  errno = ENOENT;
270  ret = -1;
271  }
272 
273  return ret;
274 }
275 
290  struct statfs *sfs,
291  struct stat *s,
292  std::vector<std::string> &prev_paths) const {
293  for (const auto &old_path : prev_paths) {
294  if (!strcmp(old_path.c_str(), path)) {
295  edm::LogWarning("LocalFileSystem::findMount()") << "Found a loop in bind mounts; stopping evaluation.";
296  return nullptr;
297  }
298  }
299 
300  FSInfo *best = nullptr;
301  size_t bestlen = 0;
302  size_t len = strlen(path);
303  for (size_t i = 0; i < fs_.size(); ++i) {
304  // First match simply against the file system path. We don't
305  // touch the file system until the path prefix matches.
306  // When we have a path prefix match, check the file system if
307  // we don't have a best match candidate yet, OR
308  // this match is longer (more specific) than the previous best OR
309  // this match is the same length and the previous best isn't local
310  // The final condition handles cases such as '/' that can appear twice
311  // in the file system list, once as 'rootfs' and once as local fs.
312  size_t fslen = strlen(fs_[i]->dir);
313  if (!strncmp(fs_[i]->dir, path, fslen) &&
314  ((fslen == 1 && fs_[i]->dir[0] == '/') || len == fslen || path[fslen] == '/') &&
315  (!best || fslen > bestlen || (fslen == bestlen && !best->local))) {
316  // Get the file system device and file system ids.
317  if (statFSInfo(fs_[i]) < 0)
318  return nullptr;
319 
320  // Check the path is on the same device / file system. If this
321  // fails, we found a better prefix match on path, but it's the
322  // wrong device, so reset our idea of the best match: it can't
323  // be the outer mount any more. Not sure this is the right
324  // thing to do with e.g. loop-back or union mounts.
325  if (fs_[i]->dev != s->st_dev || fs_[i]->fstype != sfs->f_type) {
326  best = nullptr;
327  continue;
328  }
329 
330  // OK this is better than anything else we found so far.
331  best = fs_[i];
332  bestlen = fslen;
333  }
334  }
335  // In the case of a bind mount, try looking again at the source directory.
336  if (best && best->bind && best->origin) {
337  struct stat s2;
338  struct statfs sfs2;
339  char *fullpath = realpath(best->origin, nullptr);
340 
341  if (!fullpath)
342  fullpath = strdup(best->origin);
343 
344  if (lstat(fullpath, &s2) < 0) {
345  int nerr = errno;
346  edm::LogWarning("LocalFileSystem::findMount()") << "Cannot lstat('" << fullpath << "' alias '" << path
347  << "'): " << strerror(nerr) << " (error " << nerr << ")";
348  free(fullpath);
349  return best;
350  }
351 
352  if (statfs(fullpath, &sfs2) < 0) {
353  int nerr = errno;
354  edm::LogWarning("LocalFileSystem::findMount()") << "Cannot statfs('" << fullpath << "' alias '" << path
355  << "'): " << strerror(nerr) << " (error " << nerr << ")";
356  free(fullpath);
357  return best;
358  }
359 
360  prev_paths.push_back(path);
361  LocalFileSystem::FSInfo *new_best = findMount(fullpath, &sfs2, &s2, prev_paths);
362  return new_best ? new_best : best;
363  }
364 
365  return best;
366 }
367 
378  struct stat s;
379  struct statfs sfs;
380  char *fullpath = realpath(path.c_str(), nullptr);
381 
382  if (!fullpath)
383  fullpath = strdup(path.c_str());
384 
385  if (lstat(fullpath, &s) < 0) {
386  int nerr = errno;
387  edm::LogWarning("LocalFileSystem::isLocalPath()")
388  << "Cannot lstat('" << fullpath << "' alias '" << path << "'): " << strerror(nerr) << " (error " << nerr << ")";
389  free(fullpath);
390  return false;
391  }
392 
393  if (statfs(fullpath, &sfs) < 0) {
394  int nerr = errno;
395  edm::LogWarning("LocalFileSystem::isLocalPath()") << "Cannot statfs('" << fullpath << "' alias '" << path
396  << "'): " << strerror(nerr) << " (error " << nerr << ")";
397  free(fullpath);
398  return false;
399  }
400 
401  std::vector<std::string> prev_paths;
402  FSInfo *m = findMount(fullpath, &sfs, &s, prev_paths);
403  free(fullpath);
404 
405  return m ? m->local : false;
406 }
407 
426 std::pair<std::string, std::string> LocalFileSystem::findCachePath(const std::vector<std::string> &paths,
427  double minFreeSpace) const {
428  struct stat s;
429  struct statfs sfs;
430  std::ostringstream warningst;
431  warningst << "Cannot use lazy-download because:\n";
432 
433  for (size_t i = 0, e = paths.size(); i < e; ++i) {
434  char *fullpath;
435  const char *inpath = paths[i].c_str();
436  const char *path = inpath;
437 
438  if (*path == '$') {
439  char *p = std::getenv(path + 1);
440  if (p && *p)
441  path = p;
442  else if (!strcmp(path, "$TMPDIR"))
443  path = "/tmp";
444  }
445 
446  if (!(fullpath = realpath(path, nullptr)))
447  fullpath = strdup(path);
448 
449 #if 0
450  std::cerr /* edm::LogInfo("LocalFileSystem") */
451  << "Checking if '" << fullpath << "', from '"
452  << inpath << "' is valid cache path with "
453  << minFreeSpace << " free space" << std::endl;
454 #endif
455 
456  if (lstat(fullpath, &s) < 0) {
457  int nerr = errno;
458  if (nerr != ENOENT && nerr != EACCES)
459  edm::LogWarning("LocalFileSystem::findCachePath()") << "Cannot lstat('" << fullpath << "', from '" << inpath
460  << "'): " << strerror(nerr) << " (error " << nerr << ")";
461  free(fullpath);
462  continue;
463  }
464 
465  if (statfs(fullpath, &sfs) < 0) {
466  int nerr = errno;
467  edm::LogWarning("LocalFileSystem::findCachePath()") << "Cannot statfs('" << fullpath << "', from '" << inpath
468  << "'): " << strerror(nerr) << " (error " << nerr << ")";
469  free(fullpath);
470  continue;
471  }
472 
473  std::vector<std::string> prev_paths;
474  FSInfo *m = findMount(fullpath, &sfs, &s, prev_paths);
475 #if 0
476  std::cerr /* edm::LogInfo("LocalFileSystem") */
477  << "Candidate '" << fullpath << "': "
478  << "found=" << (m ? 1 : 0)
479  << " local=" << (m && m->local)
480  << " free=" << (m ? m->freespc : 0)
481  << " access=" << access(fullpath, W_OK)
482  << std::endl;
483 #endif
484 
485  if (m && m->local && m->freespc >= minFreeSpace && access(fullpath, W_OK) == 0) {
487  free(fullpath);
488  return std::make_pair(result, std::string());
489  } else if (m) {
490  if (!m->local) {
491  warningst << "- The mount " << fullpath << " is not local.\n";
492  } else if (m->freespc < minFreeSpace) {
493  warningst << " - The mount at " << fullpath << " has only " << m->freespc << " GB free; a minumum of "
494  << minFreeSpace << " GB is required.\n";
495  } else if (access(fullpath, W_OK)) {
496  warningst << " - The process has no permission to write into " << fullpath << "\n";
497  }
498  }
499 
500  free(fullpath);
501  }
502 
503  std::string warning_str = warningst.str();
504  if (!warning_str.empty()) {
505  warning_str = warning_str.substr(0, warning_str.size() - 2);
506  }
507 
508  return std::make_pair(std::string(), std::move(warning_str));
509 }
510 
513  if (readFSTypes() < 0)
514  return;
515 
516  if (initFSList() < 0)
517  return;
518 }
519 
522  for (size_t i = 0, e = fs_.size(); i < e; ++i)
523  free(fs_[i]);
524 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
LocalFileSystem::FSInfo::fstype
long fstype
Definition: LocalFileSystem.cc:32
LocalFileSystem::FSInfo::fsname
char * fsname
Definition: LocalFileSystem.cc:27
LocalFileSystem::initFSList
int initFSList(void)
Definition: LocalFileSystem.cc:191
LocalFileSystem::findMount
FSInfo * findMount(const char *path, struct statfs *sfs, struct stat *s, std::vector< std::string > &) const
Definition: LocalFileSystem.cc:289
LocalFileSystem::FSInfo::origin
char * origin
Definition: LocalFileSystem.cc:30
mps_fire.i
i
Definition: mps_fire.py:428
MessageLogger.h
reco_skim_cfg_mod.fullpath
fullpath
Definition: reco_skim_cfg_mod.py:202
LocalFileSystem::fstypes_
std::vector< std::string > fstypes_
Definition: LocalFileSystem.h:29
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
cms::cuda::assert
assert(be >=bs)
LocalFileSystem::fs_
std::vector< FSInfo * > fs_
Definition: LocalFileSystem.h:28
indexGen.s2
s2
Definition: indexGen.py:107
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
alignCSCRings.s
s
Definition: alignCSCRings.py:92
LocalFileSystem::findCachePath
std::pair< std::string, std::string > findCachePath(const std::vector< std::string > &paths, double minFreeSpace) const
Definition: LocalFileSystem.cc:426
visualization-live-secondInstance_cfg.m
m
Definition: visualization-live-secondInstance_cfg.py:72
LocalFileSystem::statFSInfo
int statFSInfo(FSInfo *i) const
Definition: LocalFileSystem.cc:236
LocalFileSystem::FSInfo::dev
dev_t dev
Definition: LocalFileSystem.cc:31
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
LocalFileSystem.h
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
LocalFileSystem::readFSTypes
int readFSTypes(void)
Definition: LocalFileSystem.cc:58
LocalFileSystem::initFSInfo
FSInfo * initFSInfo(void *p)
Definition: LocalFileSystem.cc:123
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
LocalFileSystem::~LocalFileSystem
~LocalFileSystem(void)
Definition: LocalFileSystem.cc:521
LocalFileSystem::LocalFileSystem
LocalFileSystem(void)
Definition: LocalFileSystem.cc:512
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
LocalFileSystem::FSInfo::bind
unsigned bind
Definition: LocalFileSystem.cc:35
LocalFileSystem::FSInfo
Information about file systems on this node.
Definition: LocalFileSystem.cc:26
eostools.move
def move(src, dest)
Definition: eostools.py:511
Skims_PA_cff.paths
paths
Definition: Skims_PA_cff.py:18
funct::arg
A arg
Definition: Factorize.h:31
mps_fire.result
result
Definition: mps_fire.py:311
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
DTRecHitClients_cfi.local
local
Definition: DTRecHitClients_cfi.py:10
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm_modernize_messagelogger.stat
stat
Definition: edm_modernize_messagelogger.py:27
LocalFileSystem::isLocalPath
bool isLocalPath(const std::string &path) const
Definition: LocalFileSystem.cc:377
LocalFileSystem::FSInfo::type
char * type
Definition: LocalFileSystem.cc:28
mps_splice.line
line
Definition: mps_splice.py:76
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
LocalFileSystem::FSInfo::checked
std::atomic< bool > checked
Definition: LocalFileSystem.cc:36
LocalFileSystem::FSInfo::freespc
double freespc
Definition: LocalFileSystem.cc:33
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
LocalFileSystem::FSInfo::dir
char * dir
Definition: LocalFileSystem.cc:29