Whamcloud - gitweb
248cc333a8c230bae25d175c6b862ed38b7a11f2
[fs/lustre-release.git] / lustre / liblustre / lutil.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre 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
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <signal.h>
26 #include <sys/types.h>
27
28 #ifndef REDSTORM
29 #include <fcntl.h>
30 #include <netdb.h>
31 #include <syscall.h>
32 #include <sys/utsname.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #else
37 #include <sys/socket.h>
38 #include <catamount/data.h>
39 #endif
40
41 #include "lutil.h"
42
43 #ifdef CRAY_PORTALS
44 void portals_debug_dumplog(void){};
45 #endif
46
47 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
48                                             S_GMNAL | S_OPENIBNAL);
49 unsigned int portal_debug = 0;
50
51 struct task_struct     *current;
52 ptl_handle_ni_t         tcpnal_ni;
53 ptl_nid_t               tcpnal_mynid;
54
55 void *inter_module_get(char *arg)
56 {
57         if (!strcmp(arg, "tcpnal_ni"))
58                 return &tcpnal_ni;
59         else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
60                 return ldlm_cli_cancel_unused;
61         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
62                 return ldlm_namespace_cleanup;
63         else if (!strcmp(arg, "ldlm_replay_locks"))
64                 return ldlm_replay_locks;
65         else
66                 return NULL;
67 }
68
69 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
70 {
71         if (nid == PTL_NID_ANY) {
72                 snprintf(str, PTL_NALFMT_SIZE - 1, "%s",
73                          "PTL_NID_ANY");
74                 return str;
75         }
76
77         switch(nal){
78 #ifndef CRAY_PORTALS
79         case TCPNAL:
80                 /* userspace NAL */
81         case OPENIBNAL:
82         case SOCKNAL:
83                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
84                          (__u32)(nid >> 32), HIPQUAD(nid));
85                 break;
86         case QSWNAL:
87         case GMNAL:
88                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
89                          (__u32)(nid >> 32), (__u32)nid);
90                 break;
91 #endif
92         default:
93                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
94                          nal, (long long)nid);
95                 break;
96         }
97         return str;
98 }
99
100 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
101 {
102         switch(nal){
103 #ifndef CRAY_PORTALS
104         case TCPNAL:
105                 /* userspace NAL */
106         case OPENIBNAL:
107         case SOCKNAL:
108                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
109                          (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
110                 break;
111         case QSWNAL:
112         case GMNAL:
113                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
114                          (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
115                 break;
116 #endif
117         default:
118                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
119                          nal, (long long)id.nid, (long)id.pid );
120                 break;
121         }
122         return str;
123 }
124
125 #ifndef REDSTORM
126 /*
127  * random number generator stuff
128  */
129 static int _rand_dev_fd = -1;
130
131 static int get_ipv4_addr()
132 {
133         struct utsname myname;
134         struct hostent *hptr;
135         int ip;
136
137         if (uname(&myname) < 0)
138                 return 0;
139
140         hptr = gethostbyname(myname.nodename);
141         if (hptr == NULL ||
142             hptr->h_addrtype != AF_INET ||
143             *hptr->h_addr_list == NULL) {
144                 printf("LibLustre: Warning: fail to get local IPv4 address\n");
145                 return 0;
146         }
147
148         ip = ntohl(*((int *) *hptr->h_addr_list));
149
150         return ip;
151 }
152
153 void liblustre_init_random()
154 {
155         int seed;
156         struct timeval tv;
157
158         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
159         if (_rand_dev_fd >= 0) {
160                 if (syscall(SYS_read, _rand_dev_fd, &seed, sizeof(int)) ==
161                     sizeof(int)) {
162                         srand(seed);
163                         return;
164                 }
165                 syscall(SYS_close, _rand_dev_fd);
166                 _rand_dev_fd = -1;
167         }
168
169         gettimeofday(&tv, NULL);
170         srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(get_ipv4_addr()));
171 }
172
173 void get_random_bytes(void *buf, int size)
174 {
175         char *p = buf;
176         LASSERT(size >= 0);
177
178         if (_rand_dev_fd >= 0) {
179                 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
180                         return;
181                 syscall(SYS_close, _rand_dev_fd);
182                 _rand_dev_fd = -1;
183         }
184
185         while (size--) 
186                 *p++ = rand();
187 }
188
189 static void init_capability(int *res)
190 {
191         cap_t syscap;
192         cap_flag_value_t capval;
193         int i;
194
195         *res = 0;
196
197         syscap = cap_get_proc();
198         if (!syscap) {
199                 printf("Liblustre: Warning: failed to get system capability, "
200                        "set to minimal\n");
201                 return;
202         }
203
204         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
205                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
206                         if (capval == CAP_SET) {
207                                 *res |= 1 << i;
208                         }
209                 }
210         }
211 }
212
213 void liblustre_set_nal_nid()
214 {
215         pid_t pid;
216         uint32_t ip;
217         struct in_addr in;
218
219         /* need to setup mynid before tcpnal initialization */
220         /* a meaningful nid could help debugging */
221         ip = get_ipv4_addr();
222         if (ip == 0)
223                 get_random_bytes(&ip, sizeof(ip));
224         pid = getpid() & 0xffffffff;
225         tcpnal_mynid = ((uint64_t)ip << 32) | pid;
226
227         in.s_addr = htonl(ip);
228         printf("LibLustre: TCPNAL NID: %016llx (%s:%u)\n", 
229                tcpnal_mynid, inet_ntoa(in), pid);
230 }
231
232 #else /* REDSTORM */
233
234 void liblustre_init_random()
235 {
236         struct timeval tv;
237         UINT32 nodeid;
238
239         gettimeofday(&tv, NULL);
240         nodeid = _my_pnid;
241         srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(nodeid));
242 }
243
244 void get_random_bytes(void *buf, int size)
245 {
246         char *p = buf;
247         LASSERT(size >= 0);
248
249         while (size--) 
250                 *p++ = rand();
251 }
252
253 static void init_capability(int *res)
254 {
255         *res = 0;
256 }
257
258 void liblustre_set_nal_nid()
259 {
260         pid_t pid;
261         uint32_t ip;
262
263         ip = _my_pnid;
264         if (ip & 0xFF)
265                 ip <<= 8;
266         pid = getpid() & 0xFF;
267         tcpnal_mynid = ip | pid;
268         printf("LibLustre: NAL NID: %08x (%u)\n", 
269                tcpnal_mynid, pid);
270 }
271
272 #endif /* REDSOTRM */
273
274 int in_group_p(gid_t gid)
275 {
276         int i;
277
278         if (gid == current->fsgid)
279                 return 1;
280
281         for (i = 0; i < current->ngroups; i++) {
282                 if (gid == current->groups[i])
283                         return 1;
284         }
285
286         return 0;
287 }
288
289 int liblustre_init_current(char *comm)
290 {
291         current = malloc(sizeof(*current));
292         if (!current) {
293                 CERROR("Not enough memory\n");
294                 return -ENOMEM;
295         }
296         current->fs = &current->__fs;
297         current->fs->umask = umask(0777);
298         umask(current->fs->umask);
299
300         strncpy(current->comm, comm, sizeof(current->comm));
301         current->pid = getpid();
302         current->fsuid = geteuid();
303         current->fsgid = getegid();
304         memset(&current->pending, 0, sizeof(current->pending));
305
306         current->max_groups = sysconf(_SC_NGROUPS_MAX);
307         current->groups = malloc(sizeof(gid_t) * current->max_groups);
308         if (!current->groups) {
309                 CERROR("Not enough memory\n");
310                 return -ENOMEM;
311         }
312         current->ngroups = getgroups(current->max_groups, current->groups);
313         if (current->ngroups < 0) {
314                 perror("Error getgroups");
315                 return -EINVAL;
316         }
317
318         init_capability(&current->cap_effective);
319
320         return 0;
321 }
322
323 void generate_random_uuid(unsigned char uuid_out[16])
324 {
325         get_random_bytes(uuid_out, sizeof(uuid_out));
326 }
327
328 int init_lib_portals()
329 {
330         int max_interfaces;
331         int rc;
332         ENTRY;
333
334         rc = PtlInit(&max_interfaces);
335         if (rc != PTL_OK) {
336                 CERROR("PtlInit failed: %d\n", rc);
337                 RETURN (-ENXIO);
338         }
339         RETURN(0);
340 }
341
342 extern void ptlrpc_exit_portals(void);
343 void cleanup_lib_portals()
344 {
345         ptlrpc_exit_portals();
346 }
347
348 int
349 libcfs_nal_cmd(struct portals_cfg *pcfg)
350 {
351         /* handle portals command if we want */
352         return 0;
353 }