fs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. long magic;
  76. };
  77. enum {
  78. FS__SYSFS = 0,
  79. FS__PROCFS = 1,
  80. FS__DEBUGFS = 2,
  81. FS__TRACEFS = 3,
  82. FS__HUGETLBFS = 4,
  83. FS__BPF_FS = 5,
  84. };
  85. #ifndef TRACEFS_MAGIC
  86. #define TRACEFS_MAGIC 0x74726163
  87. #endif
  88. static struct fs fs__entries[] = {
  89. [FS__SYSFS] = {
  90. .name = "sysfs",
  91. .mounts = sysfs__fs_known_mountpoints,
  92. .magic = SYSFS_MAGIC,
  93. },
  94. [FS__PROCFS] = {
  95. .name = "proc",
  96. .mounts = procfs__known_mountpoints,
  97. .magic = PROC_SUPER_MAGIC,
  98. },
  99. [FS__DEBUGFS] = {
  100. .name = "debugfs",
  101. .mounts = debugfs__known_mountpoints,
  102. .magic = DEBUGFS_MAGIC,
  103. },
  104. [FS__TRACEFS] = {
  105. .name = "tracefs",
  106. .mounts = tracefs__known_mountpoints,
  107. .magic = TRACEFS_MAGIC,
  108. },
  109. [FS__HUGETLBFS] = {
  110. .name = "hugetlbfs",
  111. .mounts = hugetlbfs__known_mountpoints,
  112. .magic = HUGETLBFS_MAGIC,
  113. },
  114. [FS__BPF_FS] = {
  115. .name = "bpf",
  116. .mounts = bpf_fs__known_mountpoints,
  117. .magic = BPF_FS_MAGIC,
  118. },
  119. };
  120. static bool fs__read_mounts(struct fs *fs)
  121. {
  122. bool found = false;
  123. char type[100];
  124. FILE *fp;
  125. fp = fopen("/proc/mounts", "r");
  126. if (fp == NULL)
  127. return NULL;
  128. while (!found &&
  129. fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
  130. fs->path, type) == 2) {
  131. if (strcmp(type, fs->name) == 0)
  132. found = true;
  133. }
  134. fclose(fp);
  135. return fs->found = found;
  136. }
  137. static int fs__valid_mount(const char *fs, long magic)
  138. {
  139. struct statfs st_fs;
  140. if (statfs(fs, &st_fs) < 0)
  141. return -ENOENT;
  142. else if ((long)st_fs.f_type != magic)
  143. return -ENOENT;
  144. return 0;
  145. }
  146. static bool fs__check_mounts(struct fs *fs)
  147. {
  148. const char * const *ptr;
  149. ptr = fs->mounts;
  150. while (*ptr) {
  151. if (fs__valid_mount(*ptr, fs->magic) == 0) {
  152. fs->found = true;
  153. strcpy(fs->path, *ptr);
  154. return true;
  155. }
  156. ptr++;
  157. }
  158. return false;
  159. }
  160. static void mem_toupper(char *f, size_t len)
  161. {
  162. while (len) {
  163. *f = toupper(*f);
  164. f++;
  165. len--;
  166. }
  167. }
  168. /*
  169. * Check for "NAME_PATH" environment variable to override fs location (for
  170. * testing). This matches the recommendation in Documentation/admin-guide/sysfs-rules.rst
  171. * for SYSFS_PATH.
  172. */
  173. static bool fs__env_override(struct fs *fs)
  174. {
  175. char *override_path;
  176. size_t name_len = strlen(fs->name);
  177. /* name + "_PATH" + '\0' */
  178. char upper_name[name_len + 5 + 1];
  179. memcpy(upper_name, fs->name, name_len);
  180. mem_toupper(upper_name, name_len);
  181. strcpy(&upper_name[name_len], "_PATH");
  182. override_path = getenv(upper_name);
  183. if (!override_path)
  184. return false;
  185. fs->found = true;
  186. strncpy(fs->path, override_path, sizeof(fs->path));
  187. return true;
  188. }
  189. static const char *fs__get_mountpoint(struct fs *fs)
  190. {
  191. if (fs__env_override(fs))
  192. return fs->path;
  193. if (fs__check_mounts(fs))
  194. return fs->path;
  195. if (fs__read_mounts(fs))
  196. return fs->path;
  197. return NULL;
  198. }
  199. static const char *fs__mountpoint(int idx)
  200. {
  201. struct fs *fs = &fs__entries[idx];
  202. if (fs->found)
  203. return (const char *)fs->path;
  204. return fs__get_mountpoint(fs);
  205. }
  206. static const char *mount_overload(struct fs *fs)
  207. {
  208. size_t name_len = strlen(fs->name);
  209. /* "PERF_" + name + "_ENVIRONMENT" + '\0' */
  210. char upper_name[5 + name_len + 12 + 1];
  211. snprintf(upper_name, name_len, "PERF_%s_ENVIRONMENT", fs->name);
  212. mem_toupper(upper_name, name_len);
  213. return getenv(upper_name) ?: *fs->mounts;
  214. }
  215. static const char *fs__mount(int idx)
  216. {
  217. struct fs *fs = &fs__entries[idx];
  218. const char *mountpoint;
  219. if (fs__mountpoint(idx))
  220. return (const char *)fs->path;
  221. mountpoint = mount_overload(fs);
  222. if (mount(NULL, mountpoint, fs->name, 0, NULL) < 0)
  223. return NULL;
  224. return fs__check_mounts(fs) ? fs->path : NULL;
  225. }
  226. #define FS(name, idx) \
  227. const char *name##__mountpoint(void) \
  228. { \
  229. return fs__mountpoint(idx); \
  230. } \
  231. \
  232. const char *name##__mount(void) \
  233. { \
  234. return fs__mount(idx); \
  235. } \
  236. \
  237. bool name##__configured(void) \
  238. { \
  239. return name##__mountpoint() != NULL; \
  240. }
  241. FS(sysfs, FS__SYSFS);
  242. FS(procfs, FS__PROCFS);
  243. FS(debugfs, FS__DEBUGFS);
  244. FS(tracefs, FS__TRACEFS);
  245. FS(hugetlbfs, FS__HUGETLBFS);
  246. FS(bpf_fs, FS__BPF_FS);
  247. int filename__read_int(const char *filename, int *value)
  248. {
  249. char line[64];
  250. int fd = open(filename, O_RDONLY), err = -1;
  251. if (fd < 0)
  252. return -1;
  253. if (read(fd, line, sizeof(line)) > 0) {
  254. *value = atoi(line);
  255. err = 0;
  256. }
  257. close(fd);
  258. return err;
  259. }
  260. static int filename__read_ull_base(const char *filename,
  261. unsigned long long *value, int base)
  262. {
  263. char line[64];
  264. int fd = open(filename, O_RDONLY), err = -1;
  265. if (fd < 0)
  266. return -1;
  267. if (read(fd, line, sizeof(line)) > 0) {
  268. *value = strtoull(line, NULL, base);
  269. if (*value != ULLONG_MAX)
  270. err = 0;
  271. }
  272. close(fd);
  273. return err;
  274. }
  275. /*
  276. * Parses @value out of @filename with strtoull.
  277. * By using 16 for base to treat the number as hex.
  278. */
  279. int filename__read_xll(const char *filename, unsigned long long *value)
  280. {
  281. return filename__read_ull_base(filename, value, 16);
  282. }
  283. /*
  284. * Parses @value out of @filename with strtoull.
  285. * By using 0 for base, the strtoull detects the
  286. * base automatically (see man strtoull).
  287. */
  288. int filename__read_ull(const char *filename, unsigned long long *value)
  289. {
  290. return filename__read_ull_base(filename, value, 0);
  291. }
  292. #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
  293. int filename__read_str(const char *filename, char **buf, size_t *sizep)
  294. {
  295. size_t size = 0, alloc_size = 0;
  296. void *bf = NULL, *nbf;
  297. int fd, n, err = 0;
  298. char sbuf[STRERR_BUFSIZE];
  299. fd = open(filename, O_RDONLY);
  300. if (fd < 0)
  301. return -errno;
  302. do {
  303. if (size == alloc_size) {
  304. alloc_size += BUFSIZ;
  305. nbf = realloc(bf, alloc_size);
  306. if (!nbf) {
  307. err = -ENOMEM;
  308. break;
  309. }
  310. bf = nbf;
  311. }
  312. n = read(fd, bf + size, alloc_size - size);
  313. if (n < 0) {
  314. if (size) {
  315. pr_warning("read failed %d: %s\n", errno,
  316. strerror_r(errno, sbuf, sizeof(sbuf)));
  317. err = 0;
  318. } else
  319. err = -errno;
  320. break;
  321. }
  322. size += n;
  323. } while (n > 0);
  324. if (!err) {
  325. *sizep = size;
  326. *buf = bf;
  327. } else
  328. free(bf);
  329. close(fd);
  330. return err;
  331. }
  332. int filename__write_int(const char *filename, int value)
  333. {
  334. int fd = open(filename, O_WRONLY), err = -1;
  335. char buf[64];
  336. if (fd < 0)
  337. return err;
  338. sprintf(buf, "%d", value);
  339. if (write(fd, buf, sizeof(buf)) == sizeof(buf))
  340. err = 0;
  341. close(fd);
  342. return err;
  343. }
  344. int procfs__read_str(const char *entry, char **buf, size_t *sizep)
  345. {
  346. char path[PATH_MAX];
  347. const char *procfs = procfs__mountpoint();
  348. if (!procfs)
  349. return -1;
  350. snprintf(path, sizeof(path), "%s/%s", procfs, entry);
  351. return filename__read_str(path, buf, sizep);
  352. }
  353. static int sysfs__read_ull_base(const char *entry,
  354. unsigned long long *value, int base)
  355. {
  356. char path[PATH_MAX];
  357. const char *sysfs = sysfs__mountpoint();
  358. if (!sysfs)
  359. return -1;
  360. snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
  361. return filename__read_ull_base(path, value, base);
  362. }
  363. int sysfs__read_xll(const char *entry, unsigned long long *value)
  364. {
  365. return sysfs__read_ull_base(entry, value, 16);
  366. }
  367. int sysfs__read_ull(const char *entry, unsigned long long *value)
  368. {
  369. return sysfs__read_ull_base(entry, value, 0);
  370. }
  371. int sysfs__read_int(const char *entry, int *value)
  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_int(path, value);
  379. }
  380. int sysfs__read_str(const char *entry, char **buf, size_t *sizep)
  381. {
  382. char path[PATH_MAX];
  383. const char *sysfs = sysfs__mountpoint();
  384. if (!sysfs)
  385. return -1;
  386. snprintf(path, sizeof(path), "%s/%s", sysfs, entry);
  387. return filename__read_str(path, buf, sizep);
  388. }
  389. int sysfs__read_bool(const char *entry, bool *value)
  390. {
  391. char *buf;
  392. size_t size;
  393. int ret;
  394. ret = sysfs__read_str(entry, &buf, &size);
  395. if (ret < 0)
  396. return ret;
  397. switch (buf[0]) {
  398. case '1':
  399. case 'y':
  400. case 'Y':
  401. *value = true;
  402. break;
  403. case '0':
  404. case 'n':
  405. case 'N':
  406. *value = false;
  407. break;
  408. default:
  409. ret = -1;
  410. }
  411. free(buf);
  412. return ret;
  413. }
  414. int sysctl__read_int(const char *sysctl, int *value)
  415. {
  416. char path[PATH_MAX];
  417. const char *procfs = procfs__mountpoint();
  418. if (!procfs)
  419. return -1;
  420. snprintf(path, sizeof(path), "%s/sys/%s", procfs, sysctl);
  421. return filename__read_int(path, value);
  422. }
  423. int sysfs__write_int(const char *entry, int value)
  424. {
  425. char path[PATH_MAX];
  426. const char *sysfs = sysfs__mountpoint();
  427. if (!sysfs)
  428. return -1;
  429. if (snprintf(path, sizeof(path), "%s/%s", sysfs, entry) >= PATH_MAX)
  430. return -1;
  431. return filename__write_int(path, value);
  432. }