4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the GNU Lesser General Public License
8 * (LGPL) version 2.1 or (at your discretion) any later version.
9 * (LGPL) version 2.1 accompanies this distribution, and is available at
10 * http://www.gnu.org/licenses/lgpl-2.1.html
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
20 * libcfs/libcfs/utils/param.c
22 * This code handles user interaction with the configuration interface
23 * to the Lustre file system to fine tune it.
35 #include <sys/mount.h>
37 #include <sys/types.h>
39 #include <linux/limits.h>
40 #include <libcfs/util/string.h>
43 * Get parameter path matching the pattern
45 * \param[out] paths glob_t structure used to hold the final result
46 * \param[in] pattern the pattern containing sprintf format specifiers
47 * which will be used to create the path to match
49 * The \param pattern is appended to the default path glob to complete the
50 * absolute path to the file the caller is requesting. If the results point
51 * to one or more files that exist those results are stored in the \param
52 * paths glob_t structure that is passed by the caller.
54 * Lustre tunables traditionally were in /proc/{sys,fs}/{lnet,lustre}
55 * but in upstream kernels starting with Linux 4.2 these parameters
56 * have been moved to /sys/fs/lustre and /sys/kernel/debug/{lnet,lustre}
57 * so the user tools need to check both locations.
59 * \retval 0 for success, with results stored in \param paths.
60 * \retval -1 for failure with errno set to report the reason.
63 cfs_get_param_paths(glob_t *paths, const char *pattern, ...)
65 char path[PATH_MAX] = "{/sys/{fs,kernel/debug}/{lnet,lustre}/,"
66 "/proc/{fs,sys}/{lnet,lustre}/}";
67 static bool test_mounted = false;
68 size_t len = strlen(path);
77 if (mount("none", "/sys/kernel/debug", "debugfs", 0, "") == -1) {
78 /* Already mounted or don't have permission to mount is okay */
79 if (errno != EPERM && errno != EBUSY)
80 fprintf(stderr, "Warning: failed to mount debug: %s\n",
85 /* This is all for RHEL6 which is old school. Can be removed
86 * later when RHEL6 client support is dropped. */
87 rc = lstat(_PATH_MOUNTED, &mtab);
88 if (!rc && !S_ISLNK(mtab.st_mode)) {
89 FILE *fp = setmntent(_PATH_MOUNTED, "r+");
92 const struct mntent fs = {
93 .mnt_fsname = "debugfs",
94 .mnt_dir = "/sys/kernel/debug",
95 .mnt_type = "debugfs",
96 .mnt_opts = "rw,relatime",
99 rc = addmntent(fp, &fs);
102 "failed to add debugfs to %s: %s\n",
103 _PATH_MOUNTED, strerror(errno));
107 fprintf(stderr, "could not open %s: %s\n",
108 _PATH_MOUNTED, strerror(errno));
113 va_start(args, pattern);
114 rc = vsnprintf(buf, sizeof(buf), pattern, args);
118 } else if (rc >= sizeof(buf)) {
124 if (strlcat(path, buf, sizeof(path)) != len) {
129 rc = glob(path, GLOB_BRACE, NULL, paths);