Whamcloud - gitweb
Fixes from b_port_step needed to get HEAD to build.
[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_NAL;
48 unsigned int portal_debug = 0;
49
50 struct task_struct     *current;
51 ptl_handle_ni_t         tcpnal_ni;
52 ptl_nid_t               tcpnal_mynid;
53
54 void *inter_module_get(char *arg)
55 {
56         if (!strcmp(arg, "tcpnal_ni"))
57                 return &tcpnal_ni;
58         else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
59                 return ldlm_cli_cancel_unused;
60         else if (!strcmp(arg, "ldlm_namespace_cleanup"))
61                 return ldlm_namespace_cleanup;
62         else if (!strcmp(arg, "ldlm_replay_locks"))
63                 return ldlm_replay_locks;
64         else
65                 return NULL;
66 }
67
68 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
69 {
70         if (nid == PTL_NID_ANY) {
71                 snprintf(str, PTL_NALFMT_SIZE - 1, "%s",
72                          "PTL_NID_ANY");
73                 return str;
74         }
75
76         switch(nal){
77 #ifndef CRAY_PORTALS
78         case TCPNAL:
79                 /* userspace NAL */
80         case OPENIBNAL:
81         case SOCKNAL:
82                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
83                          (__u32)(nid >> 32), HIPQUAD(nid));
84                 break;
85         case QSWNAL:
86         case GMNAL:
87                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
88                          (__u32)(nid >> 32), (__u32)nid);
89                 break;
90 #endif
91         default:
92                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
93                          nal, (long long)nid);
94                 break;
95         }
96         return str;
97 }
98
99 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
100 {
101         switch(nal){
102 #ifndef CRAY_PORTALS
103         case TCPNAL:
104                 /* userspace NAL */
105         case OPENIBNAL:
106         case SOCKNAL:
107                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
108                          (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
109                 break;
110         case QSWNAL:
111         case GMNAL:
112                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
113                          (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
114                 break;
115 #endif
116         default:
117                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
118                          nal, (long long)id.nid, (long)id.pid );
119                 break;
120         }
121         return str;
122 }
123
124 #ifndef REDSTORM
125 /*
126  * random number generator stuff
127  */
128 static int _rand_dev_fd = -1;
129
130 static int get_ipv4_addr()
131 {
132         struct utsname myname;
133         struct hostent *hptr;
134         int ip;
135
136         if (uname(&myname) < 0)
137                 return 0;
138
139         hptr = gethostbyname(myname.nodename);
140         if (hptr == NULL ||
141             hptr->h_addrtype != AF_INET ||
142             *hptr->h_addr_list == NULL) {
143                 printf("LibLustre: Warning: fail to get local IPv4 address\n");
144                 return 0;
145         }
146
147         ip = ntohl(*((int *) *hptr->h_addr_list));
148
149         return ip;
150 }
151
152 void liblustre_init_random()
153 {
154         int seed;
155         struct timeval tv;
156
157         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
158         if (_rand_dev_fd >= 0) {
159                 if (syscall(SYS_read, _rand_dev_fd, &seed, sizeof(int)) ==
160                     sizeof(int)) {
161                         srand(seed);
162                         return;
163                 }
164                 syscall(SYS_close, _rand_dev_fd);
165                 _rand_dev_fd = -1;
166         }
167
168         gettimeofday(&tv, NULL);
169         srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(get_ipv4_addr()));
170 }
171
172 void get_random_bytes(void *buf, int size)
173 {
174         char *p = buf;
175         LASSERT(size >= 0);
176
177         if (_rand_dev_fd >= 0) {
178                 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
179                         return;
180                 syscall(SYS_close, _rand_dev_fd);
181                 _rand_dev_fd = -1;
182         }
183
184         while (size--) 
185                 *p++ = rand();
186 }
187
188 static void init_capability(int *res)
189 {
190         cap_t syscap;
191         cap_flag_value_t capval;
192         int i;
193
194         *res = 0;
195
196         syscap = cap_get_proc();
197         if (!syscap) {
198                 printf("Liblustre: Warning: failed to get system capability, "
199                        "set to minimal\n");
200                 return;
201         }
202
203         for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
204                 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
205                         if (capval == CAP_SET) {
206                                 *res |= 1 << i;
207                         }
208                 }
209         }
210 }
211
212 void liblustre_set_nal_nid()
213 {
214         pid_t pid;
215         uint32_t ip;
216         struct in_addr in;
217
218         /* need to setup mynid before tcpnal initialization */
219         /* a meaningful nid could help debugging */
220         ip = get_ipv4_addr();
221         if (ip == 0)
222                 get_random_bytes(&ip, sizeof(ip));
223         pid = getpid() & 0xffffffff;
224         tcpnal_mynid = ((uint64_t)ip << 32) | pid;
225
226         in.s_addr = htonl(ip);
227         printf("LibLustre: TCPNAL NID: %016llx (%s:%u)\n", 
228                tcpnal_mynid, inet_ntoa(in), pid);
229 }
230
231 #else /* REDSTORM */
232
233 void liblustre_init_random()
234 {
235         struct timeval tv;
236         UINT32 nodeid;
237
238         gettimeofday(&tv, NULL);
239         nodeid = _my_pnid;
240         srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(nodeid));
241 }
242
243 void get_random_bytes(void *buf, int size)
244 {
245         char *p = buf;
246         LASSERT(size >= 0);
247
248         while (size--) 
249                 *p++ = rand();
250 }
251
252 static void init_capability(int *res)
253 {
254         *res = 0;
255 }
256
257 void liblustre_set_nal_nid()
258 {
259         pid_t pid;
260         uint32_t ip;
261
262         ip = _my_pnid;
263         if (ip & 0xFF)
264                 ip <<= 8;
265         pid = getpid() & 0xFF;
266         tcpnal_mynid = ip | pid;
267         printf("LibLustre: NAL NID: %08x (%u)\n", 
268                tcpnal_mynid, pid);
269 }
270
271 #endif /* REDSOTRM */
272
273 int in_group_p(gid_t gid)
274 {
275         int i;
276
277         if (gid == current->fsgid)
278                 return 1;
279
280         for (i = 0; i < current->ngroups; i++) {
281                 if (gid == current->groups[i])
282                         return 1;
283         }
284
285         return 0;
286 }
287
288 int liblustre_init_current(char *comm)
289 {
290         current = malloc(sizeof(*current));
291         if (!current) {
292                 CERROR("Not enough memory\n");
293                 return -ENOMEM;
294         }
295         current->fs = &current->__fs;
296         current->fs->umask = umask(0777);
297         umask(current->fs->umask);
298
299         strncpy(current->comm, comm, sizeof(current->comm));
300         current->pid = getpid();
301         current->fsuid = geteuid();
302         current->fsgid = getegid();
303         memset(&current->pending, 0, sizeof(current->pending));
304
305         current->max_groups = sysconf(_SC_NGROUPS_MAX);
306         current->groups = malloc(sizeof(gid_t) * current->max_groups);
307         if (!current->groups) {
308                 CERROR("Not enough memory\n");
309                 return -ENOMEM;
310         }
311         current->ngroups = getgroups(current->max_groups, current->groups);
312         if (current->ngroups < 0) {
313                 perror("Error getgroups");
314                 return -EINVAL;
315         }
316
317         init_capability(&current->cap_effective);
318
319         return 0;
320 }
321
322 void generate_random_uuid(unsigned char uuid_out[16])
323 {
324         get_random_bytes(uuid_out, sizeof(uuid_out));
325 }
326
327 int init_lib_portals()
328 {
329         int max_interfaces;
330         int rc;
331         ENTRY;
332
333         rc = PtlInit(&max_interfaces);
334         if (rc != PTL_OK) {
335                 CERROR("PtlInit failed: %d\n", rc);
336                 RETURN (-ENXIO);
337         }
338         RETURN(0);
339 }
340
341 extern void ptlrpc_exit_portals(void);
342 void cleanup_lib_portals()
343 {
344         ptlrpc_exit_portals();
345 }
346
347 int
348 libcfs_nal_cmd(struct portals_cfg *pcfg)
349 {
350         /* handle portals command if we want */
351         return 0;
352 }