fs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <limits.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/vfs.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/mount.h>
  15. #include "fs.h"
  16. #include "debug-internal.h"
  17. #define _STR(x) #x
  18. #define STR(x) _STR(x)
  19. #ifndef SYSFS_MAGIC
  20. #define SYSFS_MAGIC 0x62656572
  21. #endif
  22. #ifndef PROC_SUPER_MAGIC
  23. #define PROC_SUPER_MAGIC 0x9fa0
  24. #endif
  25. #ifndef DEBUGFS_MAGIC
  26. #define DEBUGFS_MAGIC 0x64626720
  27. #endif
  28. #ifndef TRACEFS_MAGIC
  29. #define TRACEFS_MAGIC 0x74726163
  30. #endif
  31. #ifndef HUGETLBFS_MAGIC
  32. #define HUGETLBFS_MAGIC 0x958458f6
  33. #endif
  34. #ifndef BPF_FS_MAGIC
  35. #define BPF_FS_MAGIC 0xcafe4a11
  36. #endif
  37. static const char * const sysfs__fs_known_mountpoints[] = {
  38. "/sys",
  39. 0,
  40. };
  41. static const char * const procfs__known_mountpoints[] = {
  42. "/proc",
  43. 0,
  44. };
  45. #ifndef DEBUGFS_DEFAULT_PATH
  46. #define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"
  47. #endif
  48. static const char * const debugfs__known_mountpoints[] = {
  49. DEBUGFS_DEFAULT_PATH,
  50. "/debug",
  51. 0,
  52. };
  53. #ifndef TRACEFS_DEFAULT_PATH
  54. #define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
  55. #endif
  56. static const char * const tracefs__known_mountpoints[] = {
  57. TRACEFS_DEFAULT_PATH,
  58. "/sys/kernel/debug/tracing",
  59. "/tracing",
  60. "/trace",
  61. 0,
  62. };
  63. static const char * const hugetlbfs__known_mountpoints[] = {
  64. 0,
  65. };
  66. static const char * const bpf_fs__known_mountpoints[] = {
  67. "/sys/fs/bpf",
  68. 0,
  69. };
  70. struct fs {
  71. const char *name;
  72. const char * const *mounts;
  73. char path[PATH_MAX];
  74. bool found;
  75. bool checked;
  76. long magic;
  77. };
  78. enum {
  79. FS__SYSFS = 0,
  80. FS__PROCFS = 1,
  81. FS__DEBUGFS = 2,
  82. FS__TRACEFS = 3,
  83. FS__HUGETLBFS = 4,
  84. FS__BPF_FS = 5,
  85. };
  86. #ifndef TRACEFS_MAGIC
  87. #define TRACEFS_MAGIC 0x74726163
  88. #endif
  89. static struct fs fs__entries[] = {
  90. [FS__SYSFS] = {
  91. .name = "sysfs",
  92. .mounts = sysfs__fs_known_mountpoints,
  93. .magic = SYSFS_MAGIC,
  94. .checked = false,
  95. },
  96. [FS__PROCFS] = {
  97. .name = "proc",
  98. .mounts = procfs__known_mountpoints,
  99. .magic = PROC_SUPER_MAGIC,
  100. .checked = false,
  101. },
  102. [FS__DEBUGFS] = {
  103. .name = "debugfs",
  104. .mounts = debugfs__known_mountpoints,
  105. .magic = DEBUGFS_MAGIC,
  106. .checked = false,
  107. },
  108. [FS__TRACEFS] = {
  109. .name = "tracefs",
  110. .mounts = tracefs__known_mountpoints,
  111. .magic = TRACEFS_MAGIC,
  112. .checked = false,
  113. },
  114. [FS__HUGETLBFS] = {
  115. .name = "hugetlbfs",
  116. .mounts = hugetlbfs__known_mountpoints,
  117. .magic = HUGETLBFS_MAGIC,
  118. .checked = false,
  119. },
  120. [FS__BPF_FS] = {
  121. .name = "bpf",
  122. .mounts = bpf_fs__known_mountpoints,
  123. .magic = BPF_FS_MAGIC,
  124. .checked = false,
  125. },
  126. };
  127. static bool fs__read_mounts(struct fs *fs)
  128. {
  129. bool found = false;
  130. char type[100];
  131. FILE *fp;
  132. fp = fopen("/proc/mounts", "r");
  133. if (fp == NULL)
  134. return NULL;
  135. while (!found &&
  136. fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  137. fs->path, type) == 2) {
  138. if (strcmp(type, fs->name) == 0)
  139. found = true;
  140. }
  141. fclose(fp);
  142. fs->checked = true;
  143. return fs->found = found;
  144. }
  145. static int fs__valid_mount(const char *fs, long magic)
  146. {
  147. struct statfs st_fs;
  148. if (statfs(fs, &st_fs) < 0)
  149. return -ENOENT;
  150. else if ((long)st_fs.f_type != magic)
  151. return -ENOENT;
  152. return 0;
  153. }
  154. static bool fs__check_mounts(struct fs *fs)
  155. {
  156. const char * const *ptr;
  157. ptr = fs->mounts;
  158. while (*ptr) {
  159. if (fs__valid_mount(*ptr, fs->magic) == 0) {
  160. fs->found = true;
  161. strcpy(fs->path, *ptr);
  162. return true;
  163. }
  164. ptr++;
  165. }
  166. return false;
  167. }
  168. static void mem_toupper(char *f, size_t len)
  169. {
  170. while (len) {
  171. *f = toupper(*f);
  172. f++;
  173. len--;
  174. }
  175. }
  176. /*
  177. * Check for "NAME_PATH" environment variable to override fs location (for
  178. * testing). This matches the recommendation in Documentation/admin-guide/sysfs-rules.rst
  179. * for SYSFS_PATH.
  180. */
  181. static bool fs__env_override(struct fs *fs)
  182. {
  183. char *override_path;
  184. size_t name_len = strlen(fs->name);
  185. /* name + "_PATH" + '\0' */
  186. char upper_name[name_len + 5 + 1];
  187. memcpy(upper_name, fs->name, name_len);
  188. mem_toupper(upper_name, name_len);
  189. strcpy(&upper_name[name_len], "_PATH");
  190. override_path = getenv(upper_name);
  191. if (!override_path)
  192. return false;
  193. fs->found = true;
  194. fs->checked = true;
  195. strncpy(fs->path, override_path, sizeof(fs->path) - 1);
  196. fs->path[sizeof(fs->path) - 1] = '\0';
  197. return true;
  198. }
  199. static const char *fs__get_mountpoint(struct fs *fs)
  200. {
  201. if (fs__env_override(fs))
  202. return fs->path;
  203. if (fs__check_mounts(fs))
  204. return fs->path;
  205. if (fs__read_mounts(fs))
  206. return fs->path;
  207. return NULL;
  208. }
  209. static const char *fs__mountpoint(int idx)
  210. {
  211. struct fs *fs = &fs__entries[idx];
  212. if (fs->found)
  213. return (const char *)fs->path;
  214. /* the mount point was already checked for the mount point
  215. * but and did not exist, so return NULL to avoid scanning again.
  216. * This makes the found and not found paths cost equivalent
  217. * in case of multiple calls.
  218. */
  219. if (fs->checked)
  220. return NULL;
  221. return fs__get_mountpoint(fs);
  222. }
  223. static const char *mount_overload(struct fs *fs)
  224. {
  225. size_t name_len = strlen(fs->name);
  226. /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
  227. char upper_name[5 + name_len + 12 + 1];
  228. snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
  229. mem_toupper(upper_name, name_len);
  230. return getenv(upper_name) ?: *fs->mounts;
  231. }
  232. static const char *fs__mount(int idx)
  233. {
  234. struct fs *fs = &fs__entries[idx];
  235. const char *mountpoint;
  236. if (fs__mountpoint(idx))
  237. return (const char *)fs->path;
  238. mountpoint = mount_overload(fs);
  239. if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
  240. return NULL;
  241. return fs__check_mounts(fs) ? fs->path : NULL;
  242. }
  243. #define FS(name, idx) \
  244. const char *name##__mountpoint(void) \
  245. { \
  246. return fs__mountpoint(idx); \
  247. } \
  248. \
  249. const char *name##__mount(void) \
  250. { \
  251. return fs__mount(idx); \
  252. } \
  253. \
  254. bool name##__configured(void) \
  255. { \
  256. return name##__mountpoint() != NULL; \
  257. }
  258. FS(sysfs, FS__SYSFS);
  259. FS(procfs, FS__PROCFS);
  260. FS(debugfs, FS__DEBUGFS);
  261. FS(tracefs, FS__TRACEFS);
  262. FS(hugetlbfs, FS__HUGETLBFS);
  263. FS(bpf_fs, FS__BPF_FS);
  264. int filename__read_int(const char *filename, int *value)
  265. {
  266. char line[64];
  267. int fd = open(filename, O_RDONLY), err = -1;
  268. if (fd < 0)
  269. return -1;
  270. if (read(fd, line, sizeof(line)) > 0) {
  271. *value = atoi(line);
  272. err = 0;
  273. }
  274. close(fd);
  275. return err;
  276. }
  277. static int filename__read_ull_base(const char *filename,
  278. unsigned long long *value, int base)
  279. {
  280. char line[64];
  281. int fd = open(filename, O_RDONLY), err = -1;
  282. if (fd < 0)
  283. return -1;
  284. if (read(fd, line, sizeof(line)) > 0) {
  285. *value = strtoull(line, NULL, base);
  286. if (*value != ULLONG_MAX)
  287. err = 0;
  288. }
  289. close(fd);
  290. return err;
  291. }
  292. /*
  293. * Parses @value out of @filename with strtoull.
  294. * By using 16 for base to treat the number as hex.
  295. */
  296. int filename__read_xll(const char *filename, unsigned long long *value)
  297. {
  298. return filename__read_ull_base(filename, value, 16);
  299. }
  300. /*
  301. * Parses @value out of @filename with strtoull.
  302. * By using 0 for base, the strtoull detects the
  303. * base automatically (see man strtoull).
  304. */
  305. int filename__read_ull(const char *filename, unsigned long long *value)
  306. {
  307. return filename__read_ull_base(filename, value, 0);
  308. }
  309. #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
  310. int filename__read_str(const char *filename, char **buf, size_t *sizep)
  311. {
  312. size_t size = 0, alloc_size = 0;
  313. void *bf = NULL, *nbf;
  314. int fd, n, err = 0;
  315. char sbuf[STRERR_BUFSIZE];
  316. fd = open(filename, O_RDONLY);
  317. if (fd < 0)
  318. return -errno;
  319. do {
  320. if (size == alloc_size) {
  321. alloc_size += BUFSIZ;
  322. nbf = realloc(bf, alloc_size);
  323. if (!nbf) {
  324. err = -ENOMEM;
  325. break;
  326. }
  327. bf = nbf;
  328. }
  329. n = read(fd, bf + size, alloc_size - size);
  330. if (n < 0) {
  331. if (size) {
  332. pr_warn("read failed %d: %s\n", errno,
  333. strerror_r(errno, sbuf, sizeof(sbuf)));
  334. err = 0;
  335. } else
  336. err = -errno;
  337. break;
  338. }
  339. size += n;
  340. } while (n > 0);
  341. if (!err) {
  342. *sizep = size;
  343. *buf = bf;
  344. } else
  345. free(bf);
  346. close(fd);
  347. return err;
  348. }
  349. int filename__write_int(const char *filename, int value)
  350. {
  351. int fd = open(filename, O_WRONLY), err = -1;
  352. char buf[64];
  353. if (fd < 0)
  354. return err;
  355. sprintf(buf, "%d", value);
  356. if (write(fd, buf, sizeof(buf)) == sizeof(buf))
  357. err = 0;
  358. close(fd);
  359. return err;
  360. }
  361. int procfs__read_str(const char *entry, char **buf, size_t *sizep)
  362. {
  363. char path[PATH_MAX];
  364. const char *procfs = procfs__mountpoint();
  365. if (!procfs)
  366. return -1;
  367. snprintf(path, sizeof(path), "%s/%s", procfs, entry);
  368. return filename__read_str(path, buf, sizep);
  369. }
  370. static int sysfs__read_ull_base(const char *entry,
  371. unsigned long long *value, int base)
  372. {
  373. char path[PATH_MAX];
  374. const char *sysfs = sysfs__mountpoint();
  375. if (!sysfs)
  376. return -1;
  377. snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
  378. return filename__read_ull_base(path, value, base);
  379. }
  380. int sysfs__read_xll(const char *entry, unsigned long long *value)
  381. {
  382. return sysfs__read_ull_base(entry, value, 16);
  383. }
  384. int sysfs__read_ull(const char *entry, unsigned long long *value)
  385. {
  386. return sysfs__read_ull_base(entry, value, 0);
  387. }
  388. int sysfs__read_int(const char *entry, int *value)
  389. {
  390. char path[PATH_MAX];
  391. const char *sysfs = sysfs__mountpoint();
  392. if (!sysfs)
  393. return -1;
  394. snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
  395. return filename__read_int(path, value);
  396. }
  397. int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
  398. {
  399. char path[PATH_MAX];
  400. const char *sysfs = sysfs__mountpoint();
  401. if (!sysfs)
  402. return -1;
  403. snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
  404. return filename__read_str(path, buf, sizep);
  405. }
  406. int sysfs__read_bool(const char *entry, bool *value)
  407. {
  408. char *buf;
  409. size_t size;
  410. int ret;
  411. ret = sysfs__read_str(entry, &buf, &size);
  412. if (ret < 0)
  413. return ret;
  414. switch (buf[0]) {
  415. case '1':
  416. case 'y':
  417. case 'Y':
  418. *value = true;
  419. break;
  420. case '0':
  421. case 'n':
  422. case 'N':
  423. *value = false;
  424. break;
  425. default:
  426. ret = -1;
  427. }
  428. free(buf);
  429. return ret;
  430. }
  431. int sysctl__read_int(const char *sysctl, int *value)
  432. {
  433. char path[PATH_MAX];
  434. const char *procfs = procfs__mountpoint();
  435. if (!procfs)
  436. return -1;
  437. snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
  438. return filename__read_int(path, value);
  439. }
  440. int sysfs__write_int(const char *entry, int value)
  441. {
  442. char path[PATH_MAX];
  443. const char *sysfs = sysfs__mountpoint();
  444. if (!sysfs)
  445. return -1;
  446. if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
  447. return -1;
  448. return filename__write_int(path, value);
  449. }