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