Whamcloud - gitweb
LU-1581 utils: osd_write_ldd() wrapper
[fs/lustre-release.git] / lustre / utils / nidlist.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/utils/nidlist.c
35  *
36  * Author: Jim Garlick <garlick@llnl.gov>
37  */
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <netdb.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46
47 #include "nidlist.h"
48
49 struct nl_struct {
50         char **nids;
51         int len;
52         int count;
53 };
54 #define NL_CHUNK        64
55
56 extern char *prog;
57
58 static void nl_oom(void)
59 {
60         fprintf(stderr, "%s: out of memory\n", prog);
61         exit(1);
62 }
63
64 NIDList nl_create(void)
65 {
66         struct nl_struct *nl;
67
68         if (!(nl = malloc(sizeof(struct nl_struct))))
69                 nl_oom();
70         nl->len = NL_CHUNK;
71         if (!(nl->nids = malloc(nl->len * sizeof(char *))))
72                 nl_oom();
73         nl->count = 0;
74
75         return nl;
76 }
77
78 void nl_destroy(NIDList nl)
79 {
80         int i;
81
82         for (i = 0; i < nl->count; i++)
83                 free(nl->nids[i]);
84         free(nl->nids);
85         free(nl);
86 }
87
88 static void nl_grow(NIDList nl, int n)
89 {
90         nl->len += n;
91         if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
92                 nl_oom();
93 }
94
95 void nl_add(NIDList nl, char *nid)
96 {
97         char *cp;
98
99         if (!(cp = strdup(nid)))
100                 nl_oom();
101         if (nl->count == nl->len)
102                 nl_grow(nl, NL_CHUNK);
103         nl->nids[nl->count++] = cp;
104 }
105
106 int nl_count(NIDList nl)
107 {
108         return nl->count;
109 }
110
111 static char *nl_nid_addr(char *nid)
112 {
113         char *addr, *p;
114
115         if (!(addr = strdup(nid)))
116                 nl_oom();
117         if ((p = strchr(addr, '@')))
118                 *p = '\0';
119
120         return addr;
121 }
122
123 static int nl_nid_parse_addr(char *addr)
124 {
125         int o;
126
127         for (o = strlen(addr); o > 0; o--)
128                 if (!isdigit(addr[o - 1]))
129                         break;
130
131         return o;
132 }
133
134 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
135 {
136         char *p1 = nl_nid_addr(nid1);
137         char *p2 = nl_nid_addr(nid2);
138         int res, o1, o2, cflag = 0;
139
140         o1 = nl_nid_parse_addr(p1);
141         o2 = nl_nid_parse_addr(p2);
142
143         if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 0) {
144                 res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
145                 if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
146                         cflag = 1;
147         } else
148                 res = strcmp(p1, p2);
149         free(p1);
150         free(p2);
151         if (cflagp)
152                 *cflagp = cflag;
153         return res;
154 }
155
156 static int nl_cmp_lnet(char *nid1, char *nid2)
157 {
158         char *s1 = strchr(nid1, '@');
159         char *s2 = strchr(nid2, '@');
160
161         return strcmp(s1 ? s1 + 1 : "", s2 ? s2 + 1 : "");
162 }
163
164 static int nl_cmp(const void *p1, const void *p2)
165 {
166         int res;
167
168         if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
169                 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
170         return res;
171 }
172
173 void nl_sort(NIDList nl)
174 {
175         qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
176 }
177
178 void nl_uniq(NIDList nl)
179 {
180         int i, j;
181
182         for (i = 1; i < nl->count; i++) {
183                 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
184                         free(nl->nids[i]);
185                         for (j = i; j < nl->count - 1; j++)
186                                 nl->nids[j] = nl->nids[j + 1];
187                         nl->count--;
188                         i--;
189                 }
190         }
191 }
192
193 static char *nl_nid_lookup_ipaddr(char *nid)
194 {
195         struct addrinfo *ai, *aip;
196         char name[NI_MAXHOST] = "";
197         char *p, *addr, *lnet = NULL, *res = NULL;
198         int len, x;
199
200         addr = nl_nid_addr(nid);
201         if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
202                 if ((p = strchr(nid, '@')))
203                         lnet = p + 1;
204                 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
205                         for (aip = ai; aip != NULL; aip = aip->ai_next) {
206                                 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
207                                     name, sizeof(name), NULL, 0,
208                                     NI_NAMEREQD | NI_NOFQDN) == 0) {
209                                         if ((p = strchr(name, '.')))
210                                                 *p = '\0';
211                                         len = strlen(name) + strlen(lnet) + 2;
212                                         if (!(res = malloc(len)))
213                                                 nl_oom();
214                                         snprintf(res, len, "%s@%s", name, lnet);
215                                         break;
216                                 }
217                         }
218                         freeaddrinfo(ai);
219                 }
220         }
221         free(addr);
222
223         return res;
224 }
225
226 void nl_lookup_ip(NIDList nl)
227 {
228         int i;
229         char *new;
230
231         for (i = 0; i < nl->count; i++) {
232                 if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
233                         free(nl->nids[i]);
234                         nl->nids[i] = new;
235                 }
236         }
237 }
238
239 char *nl_string(NIDList nl, char *sep)
240 {
241         int seplen = strlen(sep);
242         int i, len = 1;
243         char *s;
244
245         for (i = 0; i < nl->count; i++)
246                 len += strlen(nl->nids[i]) + seplen;
247         if (!(s = malloc(len)))
248                 nl_oom();
249         s[0] = '\0';
250         for (i = 0; i < nl->count; i++) {
251                 if (i > 0)
252                         strcat(s, sep);
253                 strcat(s, nl->nids[i]);
254         }
255         return s;
256 }
257
258 static void nl_strxcat(char *s, char **nids, int len)
259 {
260         int i, o, lastn = 0;
261         char *base, *p, *lnet = NULL, *savedn = NULL;
262
263         if ((p = strchr(nids[0], '@')))
264                 lnet = p + 1;
265         base = nl_nid_addr(nids[0]);
266         o = nl_nid_parse_addr(base);
267         base[o] = '\0';
268         for (i = 0; i < len; i++) {
269                 char *addr = nl_nid_addr(nids[i]);
270                 int n = strtoul(&addr[o], NULL, 10);
271
272                 if (i == 0)
273                         sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
274                 else if (i < len) {
275                         if (n == lastn + 1) {
276                                 if (savedn)
277                                         free(savedn);
278                                 if (!(savedn = strdup(&addr[o])))
279                                         nl_oom();
280                         } else {
281                                 if (savedn) {
282                                         sprintf(s + strlen(s), "-%s", savedn);
283                                         free(savedn);
284                                         savedn = NULL;
285                                 }
286                                 sprintf(s + strlen(s), ",%s", &addr[o]);
287                         }
288                 }
289                 if (i == len - 1) {
290                         if (savedn) {
291                                 sprintf(s + strlen(s), "-%s", savedn);
292                                 free(savedn);
293                         }
294                         strcat(s, "]");
295                         if (lnet)
296                                 sprintf(s + strlen(s), "@%s", lnet);
297                 }
298                 free(addr);
299                 lastn = n;
300         }
301         free(base);
302 }
303
304 char *nl_xstring(NIDList nl, char *sep)
305 {
306         int seplen = strlen(sep);
307         int cflag, i, j, len = 1;
308         char *s;
309
310         for (i = 0; i < nl->count; i++)
311                 len += strlen(nl->nids[i]) + seplen;
312         if (!(s = malloc(len)))
313                 nl_oom();
314         s[0] = '\0';
315         for (i = 0; i < nl->count; i++) {
316                 if (i > 0)
317                         strcat(s, sep);
318                 for (j = i + 1; j < nl->count; j++) {
319                         if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
320                                 break;
321                         (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
322                         if (!cflag)
323                                 break;
324                 }
325                 if (j - i > 1)
326                         nl_strxcat(s, &nl->nids[i], j - i);
327                 else
328                         strcat(s, nl->nids[i]);
329                 i += j - i - 1;
330         }
331         return s;
332 }