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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/llite_lib.c
38 * Lustre Light common routines
45 #include <sys/types.h>
47 #include <sys/queue.h>
49 #include <liblustre.h>
50 #include <lnet/lnetctl.h> /* needed for parse_dump */
51 #include <lustre_log.h>
54 #include "llite_lib.h"
56 int slp_global_init(void);
58 static int lllib_init(void)
60 if (liblustre_init_current("liblustre") ||
72 return _sysio_fssw_register("lustre", &llu_fssw_ops);
75 int liblustre_process_log(struct config_llog_instance *cfg,
76 char *mgsnid, char *profile,
79 struct lustre_cfg_bufs bufs;
80 struct lustre_cfg *lcfg;
81 char *peer = "MGS_UUID";
82 struct obd_device *obd;
83 struct obd_export *exp;
84 char *name = "mgc_dev";
86 struct obd_uuid mgc_uuid;
87 struct llog_ctxt *ctxt;
91 struct obd_connect_data *ocd = NULL;
94 ll_generate_random_uuid(uuid);
95 class_uuid_unparse(uuid, &mgc_uuid);
97 nid = libcfs_str2nid(mgsnid);
98 if (nid == LNET_NID_ANY) {
99 CERROR("Can't parse NID %s\n", mgsnid);
103 lustre_cfg_bufs_reset(&bufs, NULL);
104 lustre_cfg_bufs_set_string(&bufs, 1, peer);
105 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
106 lcfg->lcfg_nid = nid;
107 rc = class_process_config(lcfg);
108 lustre_cfg_free(lcfg);
112 lustre_cfg_bufs_reset(&bufs, name);
113 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
114 lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
115 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
116 rc = class_process_config(lcfg);
117 lustre_cfg_free(lcfg);
119 GOTO(out_del_uuid, rc);
121 lustre_cfg_bufs_reset(&bufs, name);
122 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
123 lustre_cfg_bufs_set_string(&bufs, 2, peer);
124 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
125 rc = class_process_config(lcfg);
126 lustre_cfg_free(lcfg);
128 GOTO(out_detach, rc);
130 while ((mdsnid = strsep(&mgsnid, ","))) {
131 nid = libcfs_str2nid(mdsnid);
132 lustre_cfg_bufs_reset(&bufs, NULL);
133 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
134 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
135 lcfg->lcfg_nid = nid;
136 rc = class_process_config(lcfg);
137 lustre_cfg_free(lcfg);
139 CERROR("Add uuid for %s failed %d\n",
140 libcfs_nid2str(nid), rc);
144 lustre_cfg_bufs_reset(&bufs, name);
145 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
146 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
147 lcfg->lcfg_nid = nid;
148 rc = class_process_config(lcfg);
149 lustre_cfg_free(lcfg);
151 CERROR("Add conn for %s failed %d\n",
152 libcfs_nid2str(nid), rc);
157 obd = class_name2obd(name);
159 GOTO(out_cleanup, rc = -EINVAL);
161 OBD_ALLOC(ocd, sizeof(*ocd));
163 GOTO(out_cleanup, rc = -ENOMEM);
165 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
167 ocd->ocd_version = LUSTRE_VERSION_CODE;
169 rc = obd_connect(NULL, &exp, obd, &mgc_uuid, ocd, NULL);
171 CERROR("cannot connect to %s at %s: rc = %d\n",
172 LUSTRE_MGS_OBDNAME, mgsnid, rc);
173 GOTO(out_cleanup, rc);
176 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
177 cfg->cfg_flags |= CFG_F_COMPAT146;
178 rc = class_config_parse_llog(NULL, ctxt, profile, cfg);
181 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
184 /* We don't so much care about errors in cleaning up the config llog
185 * connection, as we have already read the config by this point. */
186 err = obd_disconnect(exp);
188 CERROR("obd_disconnect failed: rc = %d\n", err);
192 OBD_FREE(ocd, sizeof(*ocd));
194 lustre_cfg_bufs_reset(&bufs, name);
195 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
196 err = class_process_config(lcfg);
197 lustre_cfg_free(lcfg);
199 CERROR("md_cleanup failed: rc = %d\n", err);
202 lustre_cfg_bufs_reset(&bufs, name);
203 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
204 err = class_process_config(lcfg);
205 lustre_cfg_free(lcfg);
207 CERROR("md_detach failed: rc = %d\n", err);
210 lustre_cfg_bufs_reset(&bufs, name);
211 lustre_cfg_bufs_set_string(&bufs, 1, peer);
212 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
213 err = class_process_config(lcfg);
215 CERROR("del MDC UUID failed: rc = %d\n", err);
216 lustre_cfg_free(lcfg);
222 /* parse host:/fsname string */
223 int ll_parse_mount_target(const char *target, char **mgsnid,
226 static char buf[256];
230 strncpy(buf, target, 255);
232 if ((s = strchr(buf, ':'))) {
238 sprintf(s + strlen(s), "-client");
248 * early liblustre init
249 * called from C startup in catamount apps, before main()
251 * The following is a skeleton sysio startup sequence,
252 * as implemented in C startup (skipping error handling).
253 * In this framework none of these calls need be made here
254 * or in the apps themselves. The NAMESPACE_STRING specifying
255 * the initial set of fs ops (creates, mounts, etc.) is passed
256 * as an environment variable.
259 * _sysio_incore_init();
260 * _sysio_native_init();
261 * _sysio_lustre_init();
262 * _sysio_boot(NAMESPACE_STRING);
264 * the name _sysio_lustre_init() follows the naming convention
265 * established in other fs drivers from libsysio:
266 * _sysio_incore_init(), _sysio_native_init()
268 * _sysio_lustre_init() must be called before _sysio_boot()
269 * to enable libsysio's processing of namespace init strings containing
270 * lustre filesystem operations
272 int _sysio_lustre_init(void)
277 extern void __liblustre_cleanup_(void);
280 liblustre_init_random();
284 perror("init llite driver");
288 envstr = getenv("LIBLUSTRE_TIMEOUT");
289 if (envstr != NULL) {
290 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
291 printf("LibLustre: obd timeout=%u seconds\n",
295 /* debug peer on timeout? */
296 envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
297 if (envstr != NULL) {
298 obd_debug_peer_on_timeout =
299 (unsigned int)strtol(envstr, NULL, 0);
300 printf("LibLustre: debug peer on timeout=%d\n",
301 obd_debug_peer_on_timeout ? 0 : 1);
305 (void)atexit(__liblustre_cleanup_);
310 extern int _sysio_native_init();
312 static int mnt_retry = 0;
314 char *lustre_path = NULL;
316 void __liblustre_setup_(void)
319 char *lustre_driver = "lustre";
320 unsigned mntflgs = 0;
323 lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
325 lustre_path = "/mnt/lustre";
328 target = getenv("LIBLUSTRE_MOUNT_RETRY");
330 mnt_retry = atoi(target);
336 target = getenv("LIBLUSTRE_MOUNT_TARGET");
338 printf("LibLustre: no mount target specified\n");
342 CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
343 lustre_path, target);
346 /* initialize libsysio & mount rootfs */
348 perror("init sysio");
351 _sysio_native_init();
353 err = _sysio_mount_root("/", "native", mntflgs, NULL);
355 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
359 if (_sysio_lustre_init())
361 #endif /* INIT_SYSIO */
365 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
366 if (err && mnt_retry && (-- count)) {
367 fprintf(stderr, "Lustre mount failed: %s. "
368 "Will retry %d more times\n",
369 strerror(errno), mnt_retry - count );
372 } while (err && count > 0);
374 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
379 void __liblustre_cleanup_(void)
382 /* guard against being called multiple times */
383 static int cleaned = 0;
390 /* user app might chdir to a lustre directory, and leave busy pnode
391 * during finaly libsysio cleanup. here we chdir back to "/".
392 * but it can't fix the situation that liblustre is mounted
399 /* we can't call umount here, because libsysio will not cleanup
400 * opening files for us. _sysio_shutdown() will cleanup fds at
401 * first but which will also close the sockets we need for umount
402 * liblutre. this dilema lead to another hack in
403 * libsysio/src/file_hack.c FIXME
407 cleanup_lib_portals();