1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
26 #include <sys/types.h>
32 #include <sys/utsname.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
37 #include <sys/socket.h>
38 #include <catamount/data.h>
44 void portals_debug_dumplog(void){};
47 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
48 S_GMNAL | S_OPENIBNAL);
49 unsigned int portal_debug = 0;
51 struct task_struct *current;
52 ptl_handle_ni_t tcpnal_ni;
53 ptl_nid_t tcpnal_mynid;
55 void *inter_module_get(char *arg)
57 if (!strcmp(arg, "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;
69 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
71 if (nid == PTL_NID_ANY) {
72 snprintf(str, PTL_NALFMT_SIZE - 1, "%s",
83 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
84 (__u32)(nid >> 32), HIPQUAD(nid));
88 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
89 (__u32)(nid >> 32), (__u32)nid);
93 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
100 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
108 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
109 (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
113 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
114 (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
118 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
119 nal, (long long)id.nid, (long)id.pid );
127 * random number generator stuff
129 static int _rand_dev_fd = -1;
131 static int get_ipv4_addr()
133 struct utsname myname;
134 struct hostent *hptr;
137 if (uname(&myname) < 0)
140 hptr = gethostbyname(myname.nodename);
142 hptr->h_addrtype != AF_INET ||
143 *hptr->h_addr_list == NULL) {
144 printf("LibLustre: Warning: fail to get local IPv4 address\n");
148 ip = ntohl(*((int *) *hptr->h_addr_list));
153 void liblustre_init_random()
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)) ==
165 syscall(SYS_close, _rand_dev_fd);
169 gettimeofday(&tv, NULL);
170 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(get_ipv4_addr()));
173 void get_random_bytes(void *buf, int size)
178 if (_rand_dev_fd >= 0) {
179 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
181 syscall(SYS_close, _rand_dev_fd);
189 static void init_capability(int *res)
192 cap_flag_value_t capval;
197 syscap = cap_get_proc();
199 printf("Liblustre: Warning: failed to get system capability, "
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) {
213 void liblustre_set_nal_nid()
219 /* need to setup mynid before tcpnal initialization */
220 /* a meaningful nid could help debugging */
221 ip = get_ipv4_addr();
223 get_random_bytes(&ip, sizeof(ip));
224 pid = getpid() & 0xffffffff;
225 tcpnal_mynid = ((uint64_t)ip << 32) | pid;
227 in.s_addr = htonl(ip);
228 printf("LibLustre: TCPNAL NID: %016llx (%s:%u)\n",
229 tcpnal_mynid, inet_ntoa(in), pid);
234 void liblustre_init_random()
239 gettimeofday(&tv, NULL);
241 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(nodeid));
244 void get_random_bytes(void *buf, int size)
253 static void init_capability(int *res)
258 void liblustre_set_nal_nid()
266 pid = getpid() & 0xFF;
267 tcpnal_mynid = ip | pid;
268 printf("LibLustre: NAL NID: %08x (%u)\n",
272 #endif /* REDSOTRM */
274 int in_group_p(gid_t gid)
278 if (gid == current->fsgid)
281 for (i = 0; i < current->ngroups; i++) {
282 if (gid == current->groups[i])
289 int liblustre_init_current(char *comm)
291 current = malloc(sizeof(*current));
293 CERROR("Not enough memory\n");
296 current->fs = ¤t->__fs;
297 current->fs->umask = umask(0777);
298 umask(current->fs->umask);
300 strncpy(current->comm, comm, sizeof(current->comm));
301 current->pid = getpid();
302 current->fsuid = geteuid();
303 current->fsgid = getegid();
304 memset(¤t->pending, 0, sizeof(current->pending));
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");
312 current->ngroups = getgroups(current->max_groups, current->groups);
313 if (current->ngroups < 0) {
314 perror("Error getgroups");
318 init_capability(¤t->cap_effective);
323 void generate_random_uuid(unsigned char uuid_out[16])
325 get_random_bytes(uuid_out, sizeof(uuid_out));
328 int init_lib_portals()
334 rc = PtlInit(&max_interfaces);
336 CERROR("PtlInit failed: %d\n", rc);
342 extern void ptlrpc_exit_portals(void);
343 void cleanup_lib_portals()
345 ptlrpc_exit_portals();
349 libcfs_nal_cmd(struct portals_cfg *pcfg)
351 /* handle portals command if we want */