Whamcloud - gitweb
LU-8058 utils: Remove old commented out code
[fs/lustre-release.git] / lustre / utils / liblustreapi_param.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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
11  *
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.
16  *
17  * LGPL HEADER END
18  */
19 /*
20  * lustre/utils/liblustreapi_param.c
21  *
22  * This code handles user interaction with the configuration interface
23  * to the Lustre file system to fine tune it.
24  */
25 #include <errno.h>
26 #include <stdint.h>
27
28 #include <libcfs/util/param.h>
29 #include <lustre/lustre_user.h>
30 #include <lustre/lustreapi.h>
31 #include "lustreapi_internal.h"
32
33 /**
34  * return the parameter's path for a specific device type or mountpoint
35  *
36  * \param param         the results returned to the caller
37  * \param obd_type      Lustre OBD device type
38  *
39  * \param filter        filter combined with the type agrument allow the
40  * \param type          caller to limit the scope of the search for the
41  *                      parameter's path. Typical options are search by
42  *                      Lustre filesystem name or by the path to a file
43  *                      or directory in the filesystem.
44  *
45  * \param param_name    parameter name to fetch
46  *
47  * Using filter and the type argument we can limit the scope of the
48  * search to either the parameter belonging to a specific lustre filesystem
49  * (if it exists) or using a given file or directory path located on a
50  * mounted Lustre filesystem. The last case it can do is a special search
51  * based on exactly what the user passed instead of scanning file paths
52  * or specific file systems.
53  *
54  * If "obd_type" matches a Lustre device then the first matching device
55  * (as with "lctl dl", constrained by \param filter and \param type)
56  * will be used to provide the return value, otherwise the first such
57  * device found will be used.
58  *
59  * Return 0 for success, with the results stored in \param param.
60  * Return -ve value for error.
61  */
62 int
63 get_lustre_param_path(const char *obd_type, const char *filter,
64                       enum param_filter type, const char *param_name,
65                       glob_t *param)
66 {
67         char pattern[PATH_MAX];
68         int rc = 0;
69
70         if (filter == NULL && type != FILTER_BY_NONE)
71                 return -EINVAL;
72
73         switch (type) {
74         case FILTER_BY_PATH:
75                 rc = llapi_search_fsname(filter, pattern);
76                 if (rc) {
77                         llapi_error(LLAPI_MSG_ERROR, rc,
78                                     "'%s' is not on a Lustre filesystem",
79                                     filter);
80                         return rc;
81                 }
82                 if (strlen(pattern) + 3 > sizeof(pattern))
83                         return -E2BIG;
84                 strncat(pattern, "-*", sizeof(pattern));
85                 break;
86         case FILTER_BY_FS_NAME:
87                 rc = snprintf(pattern, sizeof(pattern) - 1, "%s-*", filter);
88                 if (rc < 0)
89                         return rc;
90                 else if (rc >= sizeof(pattern))
91                         return -EINVAL;
92                 rc = 0;
93                 break;
94         case FILTER_BY_EXACT:
95                 if (strlen(filter) + 1 > sizeof(pattern))
96                         return -E2BIG;
97                 strncpy(pattern, filter, sizeof(pattern));
98                 break;
99         case FILTER_BY_NONE:
100         default:
101                 break;
102         }
103
104         if (type == FILTER_BY_NONE) {
105                 if (cfs_get_param_paths(param, "%s", param_name) != 0)
106                         rc = -errno;
107         } else if (param_name != NULL) {
108                 if (cfs_get_param_paths(param, "%s/%s/%s",
109                                        obd_type, pattern, param_name) != 0)
110                         rc = -errno;
111         } else {
112                 if (cfs_get_param_paths(param, "%s/%s",
113                                        obd_type, pattern) != 0)
114                         rc = -errno;
115         }
116
117         return rc;
118 }
119
120 /**
121  * return a parameter of a single line value for a specific device type
122  * or mountpoint
123  *
124  * \param obd_type      Lustre OBD device type
125  *
126  * \param filter        filter combined with the type agruments allow the
127  * \param type          caller to limit the scope of the search for the
128  *                      parameter's path. Typical options are search by
129  *                      Lustre filesystem name or by the path to a file
130  *                      or directory in the filesystem.
131  *
132  * \param param_name    parameter name to fetch
133  * \param value         return buffer for parameter value string
134  * \param val_len       size of buffer for return value
135  *
136  * Using filter and the type argument we can limit the scope of the
137  * search to either the parameter belonging to a specific lustre filesystem
138  * (if it exists) or using a given file or directory path located on a
139  * mounted Lustre filesystem. The last case it can do is a special search
140  * based on exactly what the user passed instead of scanning file paths
141  * or specific file systems.
142  *
143  * If "obd_type" matches a Lustre device then the first matching device
144  * (as with "lctl dl", constrained by \param filter and \param type)
145  * will be used to provide the return value, otherwise the first such
146  * device found will be used.
147  *
148  * Return 0 for success, with a NUL-terminated string in \param value.
149  * Return negative errno value for error.
150  */
151 int
152 get_lustre_param_value(const char *obd_type, const char *filter,
153                        enum param_filter type, const char *param_name,
154                        char *value, size_t val_len)
155 {
156         glob_t param;
157         FILE *fp;
158         int rc;
159
160         rc = get_lustre_param_path(obd_type, filter, type, param_name, &param);
161         if (rc != 0)
162                 return -ENOENT;
163
164         fp = fopen(param.gl_pathv[0], "r");
165         if (fp == NULL) {
166                 rc = -errno;
167                 llapi_error(LLAPI_MSG_ERROR, rc, "error: opening '%s'",
168                             param.gl_pathv[0]);
169                 goto err;
170         }
171
172         if (fgets(value, val_len, fp) == NULL) {
173                 if (!feof(fp))
174                         rc = -ferror(fp);
175         }
176         fclose(fp);
177 err:
178         cfs_free_param_data(&param);
179
180         return rc;
181 }