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-2004 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>
30 #include <sys/queue.h>
43 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
44 * of 'LIST_HEAD'. undef it to suppress warnings
47 #include <liblustre.h>
48 #include <lnet/lnetctl.h> /* needed for parse_dump */
51 #include "llite_lib.h"
53 static int lllib_init(void)
55 if (liblustre_init_current("liblustre") ||
66 return _sysio_fssw_register("lustre", &llu_fssw_ops);
69 int liblustre_process_log(struct config_llog_instance *cfg,
70 char *mgsnid, char *profile,
73 struct lustre_cfg_bufs bufs;
74 struct lustre_cfg *lcfg;
75 char *peer = "MGS_UUID";
76 struct obd_device *obd;
77 struct lustre_handle mgc_conn = {0, };
78 struct obd_export *exp;
79 char *name = "mgc_dev";
81 struct obd_uuid mgc_uuid;
82 struct llog_ctxt *ctxt;
86 struct obd_connect_data *ocd = NULL;
89 ll_generate_random_uuid(uuid);
90 class_uuid_unparse(uuid, &mgc_uuid);
92 nid = libcfs_str2nid(mgsnid);
93 if (nid == LNET_NID_ANY) {
94 CERROR("Can't parse NID %s\n", mgsnid);
98 lustre_cfg_bufs_reset(&bufs, NULL);
99 lustre_cfg_bufs_set_string(&bufs, 1, peer);
100 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
101 lcfg->lcfg_nid = nid;
102 rc = class_process_config(lcfg);
103 lustre_cfg_free(lcfg);
107 lustre_cfg_bufs_reset(&bufs, name);
108 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
109 lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
110 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
111 rc = class_process_config(lcfg);
112 lustre_cfg_free(lcfg);
114 GOTO(out_del_uuid, rc);
116 lustre_cfg_bufs_reset(&bufs, name);
117 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
118 lustre_cfg_bufs_set_string(&bufs, 2, peer);
119 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
120 rc = class_process_config(lcfg);
121 lustre_cfg_free(lcfg);
123 GOTO(out_detach, rc);
125 while ((mdsnid = strsep(&mgsnid, ","))) {
126 nid = libcfs_str2nid(mdsnid);
127 lustre_cfg_bufs_reset(&bufs, NULL);
128 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
129 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
130 lcfg->lcfg_nid = nid;
131 rc = class_process_config(lcfg);
132 lustre_cfg_free(lcfg);
134 CERROR("Add uuid for %s failed %d\n",
135 libcfs_nid2str(nid), rc);
139 lustre_cfg_bufs_reset(&bufs, name);
140 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
141 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
142 lcfg->lcfg_nid = nid;
143 rc = class_process_config(lcfg);
144 lustre_cfg_free(lcfg);
146 CERROR("Add conn for %s failed %d\n",
147 libcfs_nid2str(nid), rc);
152 obd = class_name2obd(name);
154 GOTO(out_cleanup, rc = -EINVAL);
156 OBD_ALLOC(ocd, sizeof(*ocd));
158 GOTO(out_cleanup, rc = -ENOMEM);
160 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_FID;
161 #ifdef LIBLUSTRE_POSIX_ACL
162 ocd->ocd_connect_flags |= OBD_CONNECT_ACL;
164 ocd->ocd_version = LUSTRE_VERSION_CODE;
166 rc = obd_connect(NULL, &mgc_conn, obd, &mgc_uuid, ocd);
168 CERROR("cannot connect to %s at %s: rc = %d\n",
169 LUSTRE_MGS_OBDNAME, mgsnid, rc);
170 GOTO(out_cleanup, rc);
173 exp = class_conn2export(&mgc_conn);
175 ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
176 cfg->cfg_flags |= CFG_F_COMPAT146;
177 rc = class_config_parse_llog(ctxt, profile, cfg);
179 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
182 /* We don't so much care about errors in cleaning up the config llog
183 * connection, as we have already read the config by this point. */
184 err = obd_disconnect(exp);
186 CERROR("obd_disconnect failed: rc = %d\n", err);
190 OBD_FREE(ocd, sizeof(*ocd));
192 lustre_cfg_bufs_reset(&bufs, name);
193 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
194 err = class_process_config(lcfg);
195 lustre_cfg_free(lcfg);
197 CERROR("md_cleanup failed: rc = %d\n", err);
200 lustre_cfg_bufs_reset(&bufs, name);
201 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
202 err = class_process_config(lcfg);
203 lustre_cfg_free(lcfg);
205 CERROR("md_detach failed: rc = %d\n", err);
208 lustre_cfg_bufs_reset(&bufs, name);
209 lustre_cfg_bufs_set_string(&bufs, 1, peer);
210 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
211 err = class_process_config(lcfg);
213 CERROR("del MDC UUID failed: rc = %d\n", err);
214 lustre_cfg_free(lcfg);
220 /* parse host:/fsname string */
221 int ll_parse_mount_target(const char *target, char **mgsnid,
224 static char buf[256];
228 strncpy(buf, target, 255);
230 if ((s = strchr(buf, ':'))) {
236 sprintf(s + strlen(s), "-client");
246 * early liblustre init
247 * called from C startup in catamount apps, before main()
249 * The following is a skeleton sysio startup sequence,
250 * as implemented in C startup (skipping error handling).
251 * In this framework none of these calls need be made here
252 * or in the apps themselves. The NAMESPACE_STRING specifying
253 * the initial set of fs ops (creates, mounts, etc.) is passed
254 * as an environment variable.
257 * _sysio_incore_init();
258 * _sysio_native_init();
259 * _sysio_lustre_init();
260 * _sysio_boot(NAMESPACE_STRING);
262 * the name _sysio_lustre_init() follows the naming convention
263 * established in other fs drivers from libsysio:
264 * _sysio_incore_init(), _sysio_native_init()
266 * _sysio_lustre_init() must be called before _sysio_boot()
267 * to enable libsysio's processing of namespace init strings containing
268 * lustre filesystem operations
270 int _sysio_lustre_init(void)
275 extern void __liblustre_cleanup_(void);
278 liblustre_init_random();
282 perror("init llite driver");
286 envstr = getenv("LIBLUSTRE_TIMEOUT");
287 if (envstr != NULL) {
288 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
289 printf("LibLustre: obd timeout=%u seconds\n",
293 /* debug peer on timeout? */
294 envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
295 if (envstr != NULL) {
296 obd_debug_peer_on_timeout =
297 (unsigned int)strtol(envstr, NULL, 0);
298 printf("LibLustre: debug peer on timeout=%d\n",
299 obd_debug_peer_on_timeout ? 0 : 1);
303 (void)atexit(__liblustre_cleanup_);
308 extern int _sysio_native_init();
310 char *lustre_path = NULL;
312 void __liblustre_setup_(void)
315 char *lustre_driver = "lustre";
316 unsigned mntflgs = 0;
319 lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
321 lustre_path = "/mnt/lustre";
325 target = getenv("LIBLUSTRE_MOUNT_TARGET");
327 printf("LibLustre: no mount target specified\n");
331 CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
332 lustre_path, target);
335 /* initialize libsysio & mount rootfs */
337 perror("init sysio");
340 _sysio_native_init();
342 err = _sysio_mount_root("/", "native", mntflgs, NULL);
344 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
348 if (_sysio_lustre_init())
350 #endif /* INIT_SYSIO */
352 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
354 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
359 void __liblustre_cleanup_(void)
362 /* guard against being called multiple times */
363 static int cleaned = 0;
370 /* user app might chdir to a lustre directory, and leave busy pnode
371 * during finaly libsysio cleanup. here we chdir back to "/".
372 * but it can't fix the situation that liblustre is mounted
379 /* we can't call umount here, because libsysio will not cleanup
380 * opening files for us. _sysio_shutdown() will cleanup fds at
381 * first but which will also close the sockets we need for umount
382 * liblutre. this dilema lead to another hack in
383 * libsysio/src/file_hack.c FIXME
387 cleanup_lib_portals();