1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| | /*
* Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
* License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
*/
#include "cmogstored.h"
/*
* statvfs() on GNU/Linux may call stat() internally, and stat() is bad
* for network mounts which may be stalled/slow, so favor the non-portable
* statfs() on GNU/Linux
*/
#ifdef __linux__
#include <sys/vfs.h>
#define MY_STATFS statfs
#else /* statvfs() is POSIX */
#define MY_STATFS statvfs
#endif
static bool resolve_symlink(char **orig)
{
char *p = canonicalize_filename_mode(*orig, CAN_EXISTING);
if (p) {
free(*orig);
*orig = p;
return true;
}
return false;
}
static bool stat_harder(struct mount_entry *me)
{
struct stat sb;
/* the device number may not have been populated, do it */
if (me->me_dev == (dev_t)-1) {
if (stat(me->me_mountdir, &sb) != 0)
return false;
me->me_dev = sb.st_dev;
}
/*
* resolve symlinks for things that look like paths
* and skip dead symlinks
*/
if (me->me_devname[0] == '/') {
if (lstat(me->me_devname, &sb) == 0
&& S_ISLNK(sb.st_mode)
&& ! resolve_symlink(&me->me_devname))
return false;
}
return true;
}
/*
* prevents us from using filesystems of unknown size, since those could
* be stalled/dead network mounts
*/
bool mog_mnt_usable(struct mount_entry *me)
{
struct MY_STATFS buf;
const char *path = me->me_mountdir;
if (me->me_dummy)
return false;
retry:
errno = 0;
if (MY_STATFS(path, &buf) == 0)
return (buf.f_blocks > 0) ? stat_harder(me) : false;
/* unknown */
assert(errno != EFAULT && "BUG: EFAULT from statfs/statvfs");
switch (errno) {
case EINTR: goto retry;
case EIO: /* this is important enough to log: */
case EOVERFLOW: /* this is important enough to log: */
syslog(LOG_ERR, MOG_STR(MY_STATFS) "(%s) failed: %m", path);
case ENOTDIR: /* race between read_file_system_list and statvfs */
case ELOOP: /* race between read_file_system_list and statvfs? */
case ENOSYS: /* if statvfs() doesn't work, fstatvfs() won't, either */
case EACCES: /* this is common */
return false;
}
/*
* assume other errors are recoverable (or fail elsewhere)
* ENAMETOOLONG - would fail anyways if we need to stat(2)
* ENOMEM - maybe the kernel will get more memory back, soon
*/
return true;
}
|