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 <lnet/lnetctl.h> /* needed for parse_dump */
50 #include "llite_lib.h"
52 static int lllib_init(void)
54 if (liblustre_init_current("liblustre") ||
64 return _sysio_fssw_register("lustre", &llu_fssw_ops);
67 int liblustre_process_log(struct config_llog_instance *cfg,
68 char *mgsnid, char *profile,
71 struct lustre_cfg_bufs bufs;
72 struct lustre_cfg *lcfg;
73 char *peer = "MGS_UUID";
74 struct obd_device *obd;
75 struct lustre_handle mgc_conn = {0, };
76 struct obd_export *exp;
77 char *name = "mgc_dev";
79 struct obd_uuid mgc_uuid;
80 struct llog_ctxt *ctxt;
84 struct obd_connect_data *ocd = NULL;
87 generate_random_uuid(uuid);
88 class_uuid_unparse(uuid, &mgc_uuid);
90 nid = libcfs_str2nid(mgsnid);
91 if (nid == LNET_NID_ANY) {
92 CERROR("Can't parse NID %s\n", mgsnid);
96 lustre_cfg_bufs_reset(&bufs, NULL);
97 lustre_cfg_bufs_set_string(&bufs, 1, peer);
98 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
100 rc = class_process_config(lcfg);
101 lustre_cfg_free(lcfg);
105 lustre_cfg_bufs_reset(&bufs, name);
106 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
107 lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
108 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
109 rc = class_process_config(lcfg);
110 lustre_cfg_free(lcfg);
112 GOTO(out_del_uuid, rc);
114 lustre_cfg_bufs_reset(&bufs, name);
115 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
116 lustre_cfg_bufs_set_string(&bufs, 2, peer);
117 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
118 rc = class_process_config(lcfg);
119 lustre_cfg_free(lcfg);
121 GOTO(out_detach, rc);
123 while ((mdsnid = strsep(&mgsnid, ","))) {
124 nid = libcfs_str2nid(mdsnid);
125 lustre_cfg_bufs_reset(&bufs, NULL);
126 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
127 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
128 lcfg->lcfg_nid = nid;
129 rc = class_process_config(lcfg);
130 lustre_cfg_free(lcfg);
132 CERROR("Add uuid for %s failed %d\n",
133 libcfs_nid2str(nid), rc);
137 lustre_cfg_bufs_reset(&bufs, name);
138 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
139 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
140 lcfg->lcfg_nid = nid;
141 rc = class_process_config(lcfg);
142 lustre_cfg_free(lcfg);
144 CERROR("Add conn for %s failed %d\n",
145 libcfs_nid2str(nid), rc);
150 obd = class_name2obd(name);
152 GOTO(out_cleanup, rc = -EINVAL);
154 OBD_ALLOC(ocd, sizeof(*ocd));
156 GOTO(out_cleanup, rc = -ENOMEM);
158 ocd->ocd_connect_flags = OBD_CONNECT_VERSION;
159 ocd->ocd_version = LUSTRE_VERSION_CODE;
161 rc = obd_connect(&mgc_conn, obd, &mgc_uuid, ocd);
163 CERROR("cannot connect to %s at %s: rc = %d\n",
164 LUSTRE_MGS_OBDNAME, mgsnid, rc);
165 GOTO(out_cleanup, rc);
168 exp = class_conn2export(&mgc_conn);
170 ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
171 cfg->cfg_flags |= CFG_F_COMPAT146;
172 rc = class_config_parse_llog(ctxt, profile, cfg);
174 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
177 /* We don't so much care about errors in cleaning up the config llog
178 * connection, as we have already read the config by this point. */
179 err = obd_disconnect(exp);
181 CERROR("obd_disconnect failed: rc = %d\n", err);
185 OBD_FREE(ocd, sizeof(*ocd));
187 lustre_cfg_bufs_reset(&bufs, name);
188 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
189 err = class_process_config(lcfg);
190 lustre_cfg_free(lcfg);
192 CERROR("mdc_cleanup failed: rc = %d\n", err);
195 lustre_cfg_bufs_reset(&bufs, name);
196 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
197 err = class_process_config(lcfg);
198 lustre_cfg_free(lcfg);
200 CERROR("mdc_detach failed: rc = %d\n", err);
203 lustre_cfg_bufs_reset(&bufs, name);
204 lustre_cfg_bufs_set_string(&bufs, 1, peer);
205 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
206 err = class_process_config(lcfg);
208 CERROR("del MDC UUID failed: rc = %d\n", err);
209 lustre_cfg_free(lcfg);
215 /* parse host:/fsname string */
216 int ll_parse_mount_target(const char *target, char **mgsnid,
219 static char buf[256];
223 strncpy(buf, target, 255);
225 if ((s = strchr(buf, ':'))) {
231 sprintf(s + strlen(s), "-client");
241 * early liblustre init
242 * called from C startup in catamount apps, before main()
244 * The following is a skeleton sysio startup sequence,
245 * as implemented in C startup (skipping error handling).
246 * In this framework none of these calls need be made here
247 * or in the apps themselves. The NAMESPACE_STRING specifying
248 * the initial set of fs ops (creates, mounts, etc.) is passed
249 * as an environment variable.
252 * _sysio_incore_init();
253 * _sysio_native_init();
254 * _sysio_lustre_init();
255 * _sysio_boot(NAMESPACE_STRING);
257 * the name _sysio_lustre_init() follows the naming convention
258 * established in other fs drivers from libsysio:
259 * _sysio_incore_init(), _sysio_native_init()
261 * _sysio_lustre_init() must be called before _sysio_boot()
262 * to enable libsysio's processing of namespace init strings containing
263 * lustre filesystem operations
265 int _sysio_lustre_init(void)
270 extern void __liblustre_cleanup_(void);
273 liblustre_init_random();
277 perror("init llite driver");
281 envstr = getenv("LIBLUSTRE_TIMEOUT");
282 if (envstr != NULL) {
283 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
284 printf("LibLustre: obd timeout=%u seconds\n",
288 /* debug peer on timeout? */
289 envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
290 if (envstr != NULL) {
291 obd_debug_peer_on_timeout =
292 (unsigned int)strtol(envstr, NULL, 0);
293 printf("LibLustre: debug peer on timeout=%d\n",
294 obd_debug_peer_on_timeout ? 0 : 1);
298 (void)atexit(__liblustre_cleanup_);
303 extern int _sysio_native_init();
305 char *lustre_path = NULL;
307 void __liblustre_setup_(void)
310 char *root_driver = "native";
311 char *lustre_driver = "lustre";
312 char *root_path = "/";
313 unsigned mntflgs = 0;
316 lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
318 lustre_path = "/mnt/lustre";
322 target = getenv("LIBLUSTRE_MOUNT_TARGET");
324 printf("LibLustre: no mount target specified\n");
328 CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
329 lustre_path, target);
332 /* initialize libsysio & mount rootfs */
334 perror("init sysio");
337 _sysio_native_init();
339 err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
341 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
345 if (_sysio_lustre_init())
347 #endif /* INIT_SYSIO */
349 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
351 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
356 void __liblustre_cleanup_(void)
359 /* guard against being called multiple times */
360 static int cleaned = 0;
367 /* user app might chdir to a lustre directory, and leave busy pnode
368 * during finaly libsysio cleanup. here we chdir back to "/".
369 * but it can't fix the situation that liblustre is mounted
376 /* we can't call umount here, because libsysio will not cleanup
377 * opening files for us. _sysio_shutdown() will cleanup fds at
378 * first but which will also close the sockets we need for umount
379 * liblutre. this dilema lead to another hack in
380 * libsysio/src/file_hack.c FIXME
384 cleanup_lib_portals();