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.
28 #include <sys/types.h>
29 #include <sys/queue.h>
30 #include <sys/capability.h>
32 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
42 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
43 * of 'LIST_HEAD'. undef it to suppress warnings
47 #include <portals/ptlctl.h> /* needed for parse_dump */
48 #include <procbridge.h>
50 #include "llite_lib.h"
52 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
55 ptl_handle_ni_t tcpnal_ni;
56 struct task_struct *current;
58 /* portals interfaces */
60 struct ldlm_namespace;
64 void *inter_module_get(char *arg)
66 if (!strcmp(arg, "tcpnal_ni"))
68 else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
69 return ldlm_cli_cancel_unused;
70 else if (!strcmp(arg, "ldlm_namespace_cleanup"))
71 return ldlm_namespace_cleanup;
72 else if (!strcmp(arg, "ldlm_replay_locks"))
73 return ldlm_replay_locks;
78 /* XXX move to proper place */
79 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
85 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
86 (__u32)(nid >> 32), HIPQUAD(nid));
91 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
92 (__u32)(nid >> 32), (__u32)nid);
95 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
102 int in_group_p(gid_t gid)
106 if (gid == current->fsgid)
109 for (i = 0; i < current->ngroups; i++) {
110 if (gid == current->groups[i])
117 static void init_capability(int *res)
119 cap_value_t cap_types[] = {
128 /* following are linux specific, we could simply
129 * remove them I think */
132 CAP_NET_BIND_SERVICE,
153 cap_flag_value_t capval;
158 syscap = cap_get_proc();
160 printf("Liblustre: Warning: failed to get system capability, "
165 for (i = 0; i < sizeof(cap_types)/sizeof(cap_t); i++) {
166 LASSERT(cap_types[i] < 32);
167 if (!cap_get_flag(syscap, cap_types[i],
168 CAP_EFFECTIVE, &capval)) {
169 if (capval == CAP_SET) {
170 *res |= 1 << cap_types[i];
176 static int init_current(char *comm)
178 current = malloc(sizeof(*current));
180 CERROR("Not enough memory\n");
183 current->fs = ¤t->__fs;
184 current->fs->umask = umask(0777);
185 umask(current->fs->umask);
187 strncpy(current->comm, comm, sizeof(current->comm));
188 current->pid = getpid();
189 current->fsuid = geteuid();
190 current->fsgid = getegid();
191 memset(¤t->pending, 0, sizeof(current->pending));
193 current->max_groups = sysconf(_SC_NGROUPS_MAX);
194 current->groups = malloc(sizeof(gid_t) * current->max_groups);
195 if (!current->groups) {
196 CERROR("Not enough memory\n");
199 current->ngroups = getgroups(current->max_groups, current->groqps);
200 if (current->ngroups < 0) {
201 perror("Error getgroups");
205 init_capability(¤t->cap_effective);
211 void generate_random_uuid(unsigned char uuid_out[16])
213 int *arr = (int*)uuid_out;
216 for (i = 0; i < sizeof(uuid_out)/sizeof(int); i++)
220 ptl_nid_t tcpnal_mynid;
222 int init_lib_portals()
228 rc = PtlInit(&max_interfaces);
230 CERROR("PtlInit failed: %d\n", rc);
237 libcfs_nal_cmd(struct portals_cfg *pcfg)
239 /* handle portals command if we want */
243 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
245 int lib_ioctl_nalcmd(int dev_id, unsigned int opc, void * ptr)
247 struct portal_ioctl_data *ptldata;
249 if (opc == IOC_PORTAL_NAL_CMD) {
250 ptldata = (struct portal_ioctl_data *) ptr;
252 if (ptldata->ioc_nal_cmd == NAL_CMD_REGISTER_MYNID) {
253 tcpnal_mynid = ptldata->ioc_nid;
254 printf("mynid: %u.%u.%u.%u\n",
255 (unsigned)(tcpnal_mynid>>24) & 0xFF,
256 (unsigned)(tcpnal_mynid>>16) & 0xFF,
257 (unsigned)(tcpnal_mynid>>8) & 0xFF,
258 (unsigned)(tcpnal_mynid) & 0xFF);
265 int lib_ioctl(int dev_id, unsigned int opc, void * ptr)
269 if (dev_id == OBD_DEV_ID) {
270 struct obd_ioctl_data *ioc = ptr;
273 ioc->ioc_plen1 = ioc->ioc_inllen1;
274 ioc->ioc_pbuf1 = ioc->ioc_bulk;
277 rc = class_handle_ioctl(opc, (unsigned long)ptr);
279 printf ("proccssing ioctl cmd: %x, rc %d\n", opc, rc);
287 int lllib_init(char *dumpfile)
290 /* this parse only get my nid from config file
291 * before initialize portals
293 if (parse_dump(dumpfile, lib_ioctl_nalcmd))
296 /* XXX need setup mynid before tcpnal initialize */
297 tcpnal_mynid = ((uint64_t)getpid() << 32) | time(0);
298 printf("LibLustre: TCPNAL NID: %016llx\n", tcpnal_mynid);
301 if (init_current("dummy") ||
303 init_lib_portals() ||
310 if (!g_zconf && parse_dump(dumpfile, lib_ioctl))
313 return _sysio_fssw_register("llite", &llu_fssw_ops);
317 static void llu_check_request()
319 liblustre_wait_event(0);
323 int liblustre_process_log(struct config_llog_instance *cfg, int allow_recov)
325 struct lustre_cfg lcfg;
326 char *peer = "MDS_PEER_UUID";
327 struct obd_device *obd;
328 struct lustre_handle mdc_conn = {0, };
329 struct obd_export *exp;
330 char *name = "mdc_dev";
332 struct obd_uuid mdc_uuid;
333 struct llog_ctxt *ctxt;
335 int nal, err, rc = 0;
338 generate_random_uuid(uuid);
339 class_uuid_unparse(uuid, &mdc_uuid);
341 if (ptl_parse_nid(&nid, g_zconf_mdsnid)) {
342 CERROR("Can't parse NID %s\n", g_zconf_mdsnid);
346 nal = ptl_name2nal("tcp");
348 CERROR("Can't parse NAL tcp\n");
351 LCFG_INIT(lcfg, LCFG_ADD_UUID, NULL);
353 lcfg.lcfg_inllen1 = strlen(peer) + 1;
354 lcfg.lcfg_inlbuf1 = peer;
356 err = class_process_config(&lcfg);
360 LCFG_INIT(lcfg, LCFG_ATTACH, name);
361 lcfg.lcfg_inlbuf1 = "mdc";
362 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
363 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
364 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
365 err = class_process_config(&lcfg);
367 GOTO(out_del_uuid, err);
369 LCFG_INIT(lcfg, LCFG_SETUP, name);
370 lcfg.lcfg_inlbuf1 = g_zconf_mdsname;
371 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
372 lcfg.lcfg_inlbuf2 = peer;
373 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
374 err = class_process_config(&lcfg);
376 GOTO(out_detach, err);
378 obd = class_name2obd(name);
380 GOTO(out_cleanup, err = -EINVAL);
382 /* Disable initial recovery on this import */
383 err = obd_set_info(obd->obd_self_export,
384 strlen("initial_recov"), "initial_recov",
385 sizeof(allow_recov), &allow_recov);
387 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
389 CERROR("cannot connect to %s: rc = %d\n",
390 g_zconf_mdsname, err);
391 GOTO(out_cleanup, err);
394 exp = class_conn2export(&mdc_conn);
396 ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
397 rc = class_config_parse_llog(ctxt, g_zconf_profile, cfg);
399 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
402 err = obd_disconnect(exp, 0);
405 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
406 err = class_process_config(&lcfg);
411 LCFG_INIT(lcfg, LCFG_DETACH, name);
412 err = class_process_config(&lcfg);
417 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
418 lcfg.lcfg_inllen1 = strlen(peer) + 1;
419 lcfg.lcfg_inlbuf1 = peer;
420 err = class_process_config(&lcfg);
429 /* parse host:/mdsname/profile string */
430 int ll_parse_mount_target(const char *target, char **mdsnid,
431 char **mdsname, char **profile)
433 static char buf[256];
437 strncpy(buf, target, 255);
439 if ((s = strchr(buf, ':'))) {
446 if ((s = strchr(*mdsname, '/'))) {
457 #define ENV_LUSTRE_MNTPNT "LIBLUSTRE_MOUNT_POINT"
458 #define ENV_LUSTRE_MNTTGT "LIBLUSTRE_MOUNT_TARGET"
459 #define ENV_LUSTRE_TIMEOUT "LIBLUSTRE_TIMEOUT"
460 #define ENV_LUSTRE_DUMPFILE "LIBLUSTRE_DUMPFILE"
462 extern int _sysio_native_init();
464 extern unsigned int obd_timeout;
466 /* global variables */
467 int g_zconf = 0; /* zeroconf or dumpfile */
468 char *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
469 char *g_zconf_mdsnid = NULL; /* mdsnid, for zeroconf */
470 char *g_zconf_profile = NULL; /* profile, for zeroconf */
473 void __liblustre_setup_(void)
475 char *lustre_path = NULL;
477 char *timeout = NULL;
478 char *dumpfile = NULL;
479 char *root_driver = "native";
480 char *lustre_driver = "llite";
481 char *root_path = "/";
482 unsigned mntflgs = 0;
486 /* consider tha case of starting multiple liblustre instances
487 * at a same time on single node.
489 srand(time(NULL) + getpid());
491 lustre_path = getenv(ENV_LUSTRE_MNTPNT);
493 lustre_path = "/mnt/lustre";
496 target = getenv(ENV_LUSTRE_MNTTGT);
498 dumpfile = getenv(ENV_LUSTRE_DUMPFILE);
500 CERROR("Neither mount target, nor dumpfile\n");
504 printf("LibLustre: mount point %s, dumpfile %s\n",
505 lustre_path, dumpfile);
507 if (ll_parse_mount_target(target,
511 CERROR("mal-formed target %s \n", target);
515 printf("LibLustre: mount point %s, target %s\n",
516 lustre_path, target);
519 timeout = getenv(ENV_LUSTRE_TIMEOUT);
521 obd_timeout = (unsigned int) atoi(timeout);
522 printf("LibLustre: set obd timeout as %u seconds\n",
526 if (_sysio_init() != 0) {
527 perror("init sysio");
531 /* cygwin don't need native driver */
533 _sysio_native_init();
536 err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
544 portal_subsystem_debug = 0;
546 err = lllib_init(dumpfile);
548 perror("init llite driver");
552 err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
555 perror(lustre_driver);
560 __sysio_hook_sys_enter = llu_check_request;
561 __sysio_hook_sys_leave = NULL;
565 void __liblustre_cleanup_(void)