1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light common routines
6 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <sys/utsname.h>
32 #include <sys/types.h>
33 #include <sys/queue.h>
35 #include <netinet/in.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
45 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
46 * of 'LIST_HEAD'. undef it to suppress warnings
50 #include <portals/ptlctl.h> /* needed for parse_dump */
51 #include <procbridge.h>
53 #include "llite_lib.h"
55 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
58 ptl_handle_ni_t tcpnal_ni;
59 struct task_struct *current;
61 /* portals interfaces */
63 struct ldlm_namespace;
67 void *inter_module_get(char *arg)
69 if (!strcmp(arg, "tcpnal_ni"))
71 else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
72 return ldlm_cli_cancel_unused;
73 else if (!strcmp(arg, "ldlm_namespace_cleanup"))
74 return ldlm_namespace_cleanup;
75 else if (!strcmp(arg, "ldlm_replay_locks"))
76 return ldlm_replay_locks;
81 /* XXX move to proper place */
82 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
88 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
89 (__u32)(nid >> 32), HIPQUAD(nid));
94 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
95 (__u32)(nid >> 32), (__u32)nid);
98 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
106 * random number generator stuff
108 static int _rand_dev_fd = -1;
110 static int get_ipv4_addr()
112 struct utsname myname;
113 struct hostent *hptr;
116 if (uname(&myname) < 0)
119 hptr = gethostbyname(myname.nodename);
121 hptr->h_addrtype != AF_INET ||
122 *hptr->h_addr_list == NULL) {
123 printf("LibLustre: Warning: fail to get local IPv4 address\n");
127 ip = ntohl(*((int *) *hptr->h_addr_list));
132 static void init_random()
137 _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
138 if (_rand_dev_fd >= 0) {
139 if (syscall(SYS_read, _rand_dev_fd, &seed, sizeof(int)) ==
144 syscall(SYS_close, _rand_dev_fd);
148 gettimeofday(&tv, NULL);
149 srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(get_ipv4_addr()));
152 void get_random_bytes(void *buf, int size)
159 if (_rand_dev_fd >= 0) {
160 if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
162 syscall(SYS_close, _rand_dev_fd);
170 int in_group_p(gid_t gid)
174 if (gid == current->fsgid)
177 for (i = 0; i < current->ngroups; i++) {
178 if (gid == current->groups[i])
185 static void init_capability(int *res)
188 cap_flag_value_t capval;
193 syscap = cap_get_proc();
195 printf("Liblustre: Warning: failed to get system capability, "
200 for (i = 0; i < sizeof(cap_value_t) * 8; i++) {
201 if (!cap_get_flag(syscap, i, CAP_EFFECTIVE, &capval)) {
202 if (capval == CAP_SET) {
209 static int init_current(char *comm)
211 current = malloc(sizeof(*current));
213 CERROR("Not enough memory\n");
216 current->fs = ¤t->__fs;
217 current->fs->umask = umask(0777);
218 umask(current->fs->umask);
220 strncpy(current->comm, comm, sizeof(current->comm));
221 current->pid = getpid();
222 current->fsuid = geteuid();
223 current->fsgid = getegid();
224 memset(¤t->pending, 0, sizeof(current->pending));
226 current->max_groups = sysconf(_SC_NGROUPS_MAX);
227 current->groups = malloc(sizeof(gid_t) * current->max_groups);
228 if (!current->groups) {
229 CERROR("Not enough memory\n");
232 current->ngroups = getgroups(current->max_groups, current->groups);
233 if (current->ngroups < 0) {
234 perror("Error getgroups");
238 init_capability(¤t->cap_effective);
243 void generate_random_uuid(unsigned char uuid_out[16])
245 get_random_bytes(uuid_out, sizeof(uuid_out));
248 ptl_nid_t tcpnal_mynid;
250 int init_lib_portals()
256 rc = PtlInit(&max_interfaces);
258 CERROR("PtlInit failed: %d\n", rc);
265 libcfs_nal_cmd(struct portals_cfg *pcfg)
267 /* handle portals command if we want */
271 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
273 int lib_ioctl_nalcmd(int dev_id, unsigned int opc, void * ptr)
275 struct portal_ioctl_data *ptldata;
277 if (opc == IOC_PORTAL_NAL_CMD) {
278 ptldata = (struct portal_ioctl_data *) ptr;
280 if (ptldata->ioc_nal_cmd == NAL_CMD_REGISTER_MYNID) {
281 tcpnal_mynid = ptldata->ioc_nid;
282 printf("mynid: %u.%u.%u.%u\n",
283 (unsigned)(tcpnal_mynid>>24) & 0xFF,
284 (unsigned)(tcpnal_mynid>>16) & 0xFF,
285 (unsigned)(tcpnal_mynid>>8) & 0xFF,
286 (unsigned)(tcpnal_mynid) & 0xFF);
293 int lib_ioctl(int dev_id, unsigned int opc, void * ptr)
297 if (dev_id == OBD_DEV_ID) {
298 struct obd_ioctl_data *ioc = ptr;
301 ioc->ioc_plen1 = ioc->ioc_inllen1;
302 ioc->ioc_pbuf1 = ioc->ioc_bulk;
305 rc = class_handle_ioctl(opc, (unsigned long)ptr);
307 printf ("proccssing ioctl cmd: %x, rc %d\n", opc, rc);
315 int lllib_init(char *dumpfile)
322 /* this parse only get my nid from config file
323 * before initialize portals
325 if (parse_dump(dumpfile, lib_ioctl_nalcmd))
328 /* need to setup mynid before tcpnal initialization */
329 /* a meaningful nid could help debugging */
330 ip = get_ipv4_addr();
332 get_random_bytes(&ip, sizeof(ip));
333 pid = getpid() & 0xffffffff;
334 tcpnal_mynid = ((uint64_t)ip << 32) | pid;
336 in.s_addr = htonl(ip);
337 printf("LibLustre: TCPNAL NID: %016llx (%s:%u)\n",
338 tcpnal_mynid, inet_ntoa(in), pid);
341 if (init_current("dummy") ||
343 init_lib_portals() ||
350 if (!g_zconf && parse_dump(dumpfile, lib_ioctl))
353 return _sysio_fssw_register("llite", &llu_fssw_ops);
357 static void llu_check_request()
359 liblustre_wait_event(0);
363 int liblustre_process_log(struct config_llog_instance *cfg, int allow_recov)
365 struct lustre_cfg lcfg;
366 char *peer = "MDS_PEER_UUID";
367 struct obd_device *obd;
368 struct lustre_handle mdc_conn = {0, };
369 struct obd_export *exp;
370 char *name = "mdc_dev";
372 struct obd_uuid mdc_uuid;
373 struct llog_ctxt *ctxt;
375 int nal, err, rc = 0;
378 generate_random_uuid(uuid);
379 class_uuid_unparse(uuid, &mdc_uuid);
381 if (ptl_parse_nid(&nid, g_zconf_mdsnid)) {
382 CERROR("Can't parse NID %s\n", g_zconf_mdsnid);
386 nal = ptl_name2nal("tcp");
388 CERROR("Can't parse NAL tcp\n");
391 LCFG_INIT(lcfg, LCFG_ADD_UUID, NULL);
393 lcfg.lcfg_inllen1 = strlen(peer) + 1;
394 lcfg.lcfg_inlbuf1 = peer;
396 err = class_process_config(&lcfg);
400 LCFG_INIT(lcfg, LCFG_ATTACH, name);
401 lcfg.lcfg_inlbuf1 = "mdc";
402 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
403 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
404 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
405 err = class_process_config(&lcfg);
407 GOTO(out_del_uuid, err);
409 LCFG_INIT(lcfg, LCFG_SETUP, name);
410 lcfg.lcfg_inlbuf1 = g_zconf_mdsname;
411 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
412 lcfg.lcfg_inlbuf2 = peer;
413 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
414 err = class_process_config(&lcfg);
416 GOTO(out_detach, err);
418 obd = class_name2obd(name);
420 GOTO(out_cleanup, err = -EINVAL);
422 /* Disable initial recovery on this import */
423 err = obd_set_info(obd->obd_self_export,
424 strlen("initial_recov"), "initial_recov",
425 sizeof(allow_recov), &allow_recov);
427 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
429 CERROR("cannot connect to %s: rc = %d\n",
430 g_zconf_mdsname, err);
431 GOTO(out_cleanup, err);
434 exp = class_conn2export(&mdc_conn);
436 ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
437 rc = class_config_process_llog(ctxt, g_zconf_profile, &lcfg);
439 CERROR("class_config_process_llog failed: rc = %d\n", rc);
441 err = obd_disconnect(exp, 0);
444 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
445 err = class_process_config(&lcfg);
450 LCFG_INIT(lcfg, LCFG_DETACH, name);
451 err = class_process_config(&lcfg);
456 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
457 lcfg.lcfg_inllen1 = strlen(peer) + 1;
458 lcfg.lcfg_inlbuf1 = peer;
459 err = class_process_config(&lcfg);
468 /* parse host:/mdsname/profile string */
469 int ll_parse_mount_target(const char *target, char **mdsnid,
470 char **mdsname, char **profile)
472 static char buf[256];
476 strncpy(buf, target, 255);
478 if ((s = strchr(buf, ':'))) {
485 if ((s = strchr(*mdsname, '/'))) {
496 #define ENV_LUSTRE_MNTPNT "LIBLUSTRE_MOUNT_POINT"
497 #define ENV_LUSTRE_MNTTGT "LIBLUSTRE_MOUNT_TARGET"
498 #define ENV_LUSTRE_TIMEOUT "LIBLUSTRE_TIMEOUT"
499 #define ENV_LUSTRE_DUMPFILE "LIBLUSTRE_DUMPFILE"
501 extern int _sysio_native_init();
503 extern unsigned int obd_timeout;
505 /* global variables */
506 int g_zconf = 0; /* zeroconf or dumpfile */
507 char *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
508 char *g_zconf_mdsnid = NULL; /* mdsnid, for zeroconf */
509 char *g_zconf_profile = NULL; /* profile, for zeroconf */
512 void __liblustre_setup_(void)
514 char *lustre_path = NULL;
516 char *timeout = NULL;
517 char *dumpfile = NULL;
518 char *root_driver = "native";
519 char *lustre_driver = "llite";
520 char *root_path = "/";
521 unsigned mntflgs = 0;
524 lustre_path = getenv(ENV_LUSTRE_MNTPNT);
526 lustre_path = "/mnt/lustre";
529 target = getenv(ENV_LUSTRE_MNTTGT);
531 dumpfile = getenv(ENV_LUSTRE_DUMPFILE);
533 CERROR("Neither mount target, nor dumpfile\n");
537 printf("LibLustre: mount point %s, dumpfile %s\n",
538 lustre_path, dumpfile);
540 if (ll_parse_mount_target(target,
544 CERROR("mal-formed target %s \n", target);
548 printf("LibLustre: mount point %s, target %s\n",
549 lustre_path, target);
552 timeout = getenv(ENV_LUSTRE_TIMEOUT);
554 obd_timeout = (unsigned int) atoi(timeout);
555 printf("LibLustre: set obd timeout as %u seconds\n",
559 if (_sysio_init() != 0) {
560 perror("init sysio");
564 /* cygwin don't need native driver */
566 _sysio_native_init();
569 err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
577 portal_subsystem_debug = 0;
581 err = lllib_init(dumpfile);
583 perror("init llite driver");
587 err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
590 perror(lustre_driver);
595 __sysio_hook_sys_enter = llu_check_request;
596 __sysio_hook_sys_leave = NULL;
600 void __liblustre_cleanup_(void)