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_NAL;
48 unsigned int portal_debug = 0;
50 struct task_struct *current;
51 ptl_handle_ni_t tcpnal_ni;
52 ptl_nid_t tcpnal_mynid;
54 void *inter_module_get(char *arg)
56 if (!strcmp(arg, "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;
68 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
70 if (nid == PTL_NID_ANY) {
71 snprintf(str, PTL_NALFMT_SIZE - 1, "%s",
82 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
83 (__u32)(nid >> 32), HIPQUAD(nid));
87 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
88 (__u32)(nid >> 32), (__u32)nid);
92 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
99 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
107 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
108 (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
112 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
113 (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
117 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
118 nal, (long long)id.nid, (long)id.pid );
126 * random number generator stuff
128 static int _rand_dev_fd = -1;
130 static int get_ipv4_addr()
132 struct utsname myname;
133 struct hostent *hptr;
136 if (uname(&myname) < 0)
139 hptr = gethostbyname(myname.nodename);
141 hptr->h_addrtype != AF_INET ||
142 *hptr->h_addr_list == NULL) {
143 printf("LibLustre: Warning: fail to get local IPv4 address\n");
147 ip = ntohl(*((int *) *hptr->h_addr_list));
152 void liblustre_init_random()
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)) ==
164 syscall(SYS_close, _rand_dev_fd);
168 gettimeofday(&tv, NULL);
169 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(get_ipv4_addr()));
172 void get_random_bytes(void *buf, int size)
177 if (_rand_dev_fd >= 0) {
178 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
180 syscall(SYS_close, _rand_dev_fd);
188 static void init_capability(int *res)
191 cap_flag_value_t capval;
196 syscap = cap_get_proc();
198 printf("Liblustre: Warning: failed to get system capability, "
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) {
212 void liblustre_set_nal_nid()
218 /* need to setup mynid before tcpnal initialization */
219 /* a meaningful nid could help debugging */
220 ip = get_ipv4_addr();
222 get_random_bytes(&ip, sizeof(ip));
223 pid = getpid() & 0xffffffff;
224 tcpnal_mynid = ((uint64_t)ip << 32) | pid;
226 in.s_addr = htonl(ip);
227 printf("LibLustre: TCPNAL NID: %016llx (%s:%u)\n",
228 tcpnal_mynid, inet_ntoa(in), pid);
233 void liblustre_init_random()
238 gettimeofday(&tv, NULL);
240 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(nodeid));
243 void get_random_bytes(void *buf, int size)
252 static void init_capability(int *res)
257 void liblustre_set_nal_nid()
265 pid = getpid() & 0xFF;
266 tcpnal_mynid = ip | pid;
267 printf("LibLustre: NAL NID: %08x (%u)\n",
271 #endif /* REDSOTRM */
273 int in_group_p(gid_t gid)
277 if (gid == current->fsgid)
280 for (i = 0; i < current->ngroups; i++) {
281 if (gid == current->groups[i])
288 int liblustre_init_current(char *comm)
290 current = malloc(sizeof(*current));
292 CERROR("Not enough memory\n");
295 current->fs = ¤t->__fs;
296 current->fs->umask = umask(0777);
297 umask(current->fs->umask);
299 strncpy(current->comm, comm, sizeof(current->comm));
300 current->pid = getpid();
301 current->fsuid = geteuid();
302 current->fsgid = getegid();
303 memset(¤t->pending, 0, sizeof(current->pending));
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");
311 current->ngroups = getgroups(current->max_groups, current->groups);
312 if (current->ngroups < 0) {
313 perror("Error getgroups");
317 init_capability(¤t->cap_effective);
322 void generate_random_uuid(unsigned char uuid_out[16])
324 get_random_bytes(uuid_out, sizeof(uuid_out));
327 int init_lib_portals()
333 rc = PtlInit(&max_interfaces);
335 CERROR("PtlInit failed: %d\n", rc);
341 extern void ptlrpc_exit_portals(void);
342 void cleanup_lib_portals()
344 ptlrpc_exit_portals();
348 libcfs_nal_cmd(struct portals_cfg *pcfg)
350 /* handle portals command if we want */