4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/liblustre/llite_lib.c
36 * Lustre Light common routines
43 #include <sys/types.h>
45 #include <sys/queue.h>
47 #include <liblustre.h>
48 #include <lnet/lnetctl.h> /* needed for parse_dump */
51 #include "llite_lib.h"
53 int slp_global_init(void);
55 static int lllib_init(void)
57 if (liblustre_init_current("liblustre") ||
69 return _sysio_fssw_register("lustre", &llu_fssw_ops);
72 int liblustre_process_log(struct config_llog_instance *cfg,
73 char *mgsnid, char *profile,
76 struct lustre_cfg_bufs bufs;
77 struct lustre_cfg *lcfg;
78 char *peer = "MGS_UUID";
79 struct obd_device *obd;
80 struct obd_export *exp;
81 char *name = "mgc_dev";
83 struct obd_uuid mgc_uuid;
84 struct llog_ctxt *ctxt;
88 struct obd_connect_data *ocd = NULL;
91 ll_generate_random_uuid(uuid);
92 class_uuid_unparse(uuid, &mgc_uuid);
94 nid = libcfs_str2nid(mgsnid);
95 if (nid == LNET_NID_ANY) {
96 CERROR("Can't parse NID %s\n", mgsnid);
100 lustre_cfg_bufs_reset(&bufs, NULL);
101 lustre_cfg_bufs_set_string(&bufs, 1, peer);
102 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
103 lcfg->lcfg_nid = nid;
104 rc = class_process_config(lcfg);
105 lustre_cfg_free(lcfg);
109 lustre_cfg_bufs_reset(&bufs, name);
110 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
111 lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
112 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
113 rc = class_process_config(lcfg);
114 lustre_cfg_free(lcfg);
116 GOTO(out_del_uuid, rc);
118 lustre_cfg_bufs_reset(&bufs, name);
119 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
120 lustre_cfg_bufs_set_string(&bufs, 2, peer);
121 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
122 rc = class_process_config(lcfg);
123 lustre_cfg_free(lcfg);
125 GOTO(out_detach, rc);
127 while ((mdsnid = strsep(&mgsnid, ","))) {
128 nid = libcfs_str2nid(mdsnid);
129 lustre_cfg_bufs_reset(&bufs, NULL);
130 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
131 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
132 lcfg->lcfg_nid = nid;
133 rc = class_process_config(lcfg);
134 lustre_cfg_free(lcfg);
136 CERROR("Add uuid for %s failed %d\n",
137 libcfs_nid2str(nid), rc);
141 lustre_cfg_bufs_reset(&bufs, name);
142 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
143 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
144 lcfg->lcfg_nid = nid;
145 rc = class_process_config(lcfg);
146 lustre_cfg_free(lcfg);
148 CERROR("Add conn for %s failed %d\n",
149 libcfs_nid2str(nid), rc);
154 obd = class_name2obd(name);
156 GOTO(out_cleanup, rc = -EINVAL);
158 OBD_ALLOC(ocd, sizeof(*ocd));
160 GOTO(out_cleanup, rc = -ENOMEM);
162 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
164 ocd->ocd_version = LUSTRE_VERSION_CODE;
166 rc = obd_connect(NULL, &exp, obd, &mgc_uuid, ocd, NULL);
168 CERROR("cannot connect to %s at %s: rc = %d\n",
169 LUSTRE_MGS_OBDNAME, mgsnid, rc);
170 GOTO(out_cleanup, rc);
173 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
174 cfg->cfg_flags |= CFG_F_COMPAT146;
175 rc = class_config_parse_llog(ctxt, profile, cfg);
178 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
181 /* We don't so much care about errors in cleaning up the config llog
182 * connection, as we have already read the config by this point. */
183 err = obd_disconnect(exp);
185 CERROR("obd_disconnect failed: rc = %d\n", err);
189 OBD_FREE(ocd, sizeof(*ocd));
191 lustre_cfg_bufs_reset(&bufs, name);
192 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
193 err = class_process_config(lcfg);
194 lustre_cfg_free(lcfg);
196 CERROR("md_cleanup failed: rc = %d\n", err);
199 lustre_cfg_bufs_reset(&bufs, name);
200 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
201 err = class_process_config(lcfg);
202 lustre_cfg_free(lcfg);
204 CERROR("md_detach failed: rc = %d\n", err);
207 lustre_cfg_bufs_reset(&bufs, name);
208 lustre_cfg_bufs_set_string(&bufs, 1, peer);
209 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
210 err = class_process_config(lcfg);
212 CERROR("del MDC UUID failed: rc = %d\n", err);
213 lustre_cfg_free(lcfg);
219 /* parse host:/fsname string */
220 int ll_parse_mount_target(const char *target, char **mgsnid,
223 static char buf[256];
227 strncpy(buf, target, 255);
229 if ((s = strchr(buf, ':'))) {
235 sprintf(s + strlen(s), "-client");
245 * early liblustre init
246 * called from C startup in catamount apps, before main()
248 * The following is a skeleton sysio startup sequence,
249 * as implemented in C startup (skipping error handling).
250 * In this framework none of these calls need be made here
251 * or in the apps themselves. The NAMESPACE_STRING specifying
252 * the initial set of fs ops (creates, mounts, etc.) is passed
253 * as an environment variable.
256 * _sysio_incore_init();
257 * _sysio_native_init();
258 * _sysio_lustre_init();
259 * _sysio_boot(NAMESPACE_STRING);
261 * the name _sysio_lustre_init() follows the naming convention
262 * established in other fs drivers from libsysio:
263 * _sysio_incore_init(), _sysio_native_init()
265 * _sysio_lustre_init() must be called before _sysio_boot()
266 * to enable libsysio's processing of namespace init strings containing
267 * lustre filesystem operations
269 int _sysio_lustre_init(void)
274 extern void __liblustre_cleanup_(void);
277 liblustre_init_random();
281 perror("init llite driver");
285 envstr = getenv("LIBLUSTRE_TIMEOUT");
286 if (envstr != NULL) {
287 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
288 printf("LibLustre: obd timeout=%u seconds\n",
292 /* debug peer on timeout? */
293 envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
294 if (envstr != NULL) {
295 obd_debug_peer_on_timeout =
296 (unsigned int)strtol(envstr, NULL, 0);
297 printf("LibLustre: debug peer on timeout=%d\n",
298 obd_debug_peer_on_timeout ? 0 : 1);
302 (void)atexit(__liblustre_cleanup_);
307 extern int _sysio_native_init();
309 static int mnt_retry = 0;
311 char *lustre_path = NULL;
313 void __liblustre_setup_(void)
316 char *lustre_driver = "lustre";
317 unsigned mntflgs = 0;
320 lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
322 lustre_path = "/mnt/lustre";
325 target = getenv("LIBLUSTRE_MOUNT_RETRY");
327 mnt_retry = atoi(target);
333 target = getenv("LIBLUSTRE_MOUNT_TARGET");
335 printf("LibLustre: no mount target specified\n");
339 CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
340 lustre_path, target);
343 /* initialize libsysio & mount rootfs */
345 perror("init sysio");
348 _sysio_native_init();
350 err = _sysio_mount_root("/", "native", mntflgs, NULL);
352 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
356 if (_sysio_lustre_init())
358 #endif /* INIT_SYSIO */
362 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
363 if (err && mnt_retry && (-- count)) {
364 fprintf(stderr, "Lustre mount failed: %s. "
365 "Will retry %d more times\n",
366 strerror(errno), mnt_retry - count );
369 } while (err && count > 0);
371 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
376 void __liblustre_cleanup_(void)
379 /* guard against being called multiple times */
380 static int cleaned = 0;
387 /* user app might chdir to a lustre directory, and leave busy pnode
388 * during finaly libsysio cleanup. here we chdir back to "/".
389 * but it can't fix the situation that liblustre is mounted
396 /* we can't call umount here, because libsysio will not cleanup
397 * opening files for us. _sysio_shutdown() will cleanup fds at
398 * first but which will also close the sockets we need for umount
399 * liblutre. this dilema lead to another hack in
400 * libsysio/src/file_hack.c FIXME
404 cleanup_lib_portals();