1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
60 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
61 * of 'LIST_HEAD'. undef it to suppress warnings
64 #include <liblustre.h>
65 #include <lnet/lnetctl.h> /* needed for parse_dump */
68 #include "llite_lib.h"
70 static int lllib_init(void)
72 if (liblustre_init_current("liblustre") ||
83 return _sysio_fssw_register("lustre", &llu_fssw_ops);
86 int liblustre_process_log(struct config_llog_instance *cfg,
87 char *mgsnid, char *profile,
90 struct lustre_cfg_bufs bufs;
91 struct lustre_cfg *lcfg;
92 char *peer = "MGS_UUID";
93 struct obd_device *obd;
94 struct lustre_handle mgc_conn = {0, };
95 struct obd_export *exp;
96 char *name = "mgc_dev";
98 struct obd_uuid mgc_uuid;
99 struct llog_ctxt *ctxt;
103 struct obd_connect_data *ocd = NULL;
106 ll_generate_random_uuid(uuid);
107 class_uuid_unparse(uuid, &mgc_uuid);
109 nid = libcfs_str2nid(mgsnid);
110 if (nid == LNET_NID_ANY) {
111 CERROR("Can't parse NID %s\n", mgsnid);
115 lustre_cfg_bufs_reset(&bufs, NULL);
116 lustre_cfg_bufs_set_string(&bufs, 1, peer);
117 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
118 lcfg->lcfg_nid = nid;
119 rc = class_process_config(lcfg);
120 lustre_cfg_free(lcfg);
124 lustre_cfg_bufs_reset(&bufs, name);
125 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
126 lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
127 lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
128 rc = class_process_config(lcfg);
129 lustre_cfg_free(lcfg);
131 GOTO(out_del_uuid, rc);
133 lustre_cfg_bufs_reset(&bufs, name);
134 lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
135 lustre_cfg_bufs_set_string(&bufs, 2, peer);
136 lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
137 rc = class_process_config(lcfg);
138 lustre_cfg_free(lcfg);
140 GOTO(out_detach, rc);
142 while ((mdsnid = strsep(&mgsnid, ","))) {
143 nid = libcfs_str2nid(mdsnid);
144 lustre_cfg_bufs_reset(&bufs, NULL);
145 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
146 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
147 lcfg->lcfg_nid = nid;
148 rc = class_process_config(lcfg);
149 lustre_cfg_free(lcfg);
151 CERROR("Add uuid for %s failed %d\n",
152 libcfs_nid2str(nid), rc);
156 lustre_cfg_bufs_reset(&bufs, name);
157 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
158 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
159 lcfg->lcfg_nid = nid;
160 rc = class_process_config(lcfg);
161 lustre_cfg_free(lcfg);
163 CERROR("Add conn for %s failed %d\n",
164 libcfs_nid2str(nid), rc);
169 obd = class_name2obd(name);
171 GOTO(out_cleanup, rc = -EINVAL);
173 OBD_ALLOC(ocd, sizeof(*ocd));
175 GOTO(out_cleanup, rc = -ENOMEM);
177 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_FID |
179 #ifdef LIBLUSTRE_POSIX_ACL
180 ocd->ocd_connect_flags |= OBD_CONNECT_ACL;
182 ocd->ocd_version = LUSTRE_VERSION_CODE;
184 rc = obd_connect(NULL, &mgc_conn, obd, &mgc_uuid, ocd, NULL);
186 CERROR("cannot connect to %s at %s: rc = %d\n",
187 LUSTRE_MGS_OBDNAME, mgsnid, rc);
188 GOTO(out_cleanup, rc);
191 exp = class_conn2export(&mgc_conn);
193 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
194 cfg->cfg_flags |= CFG_F_COMPAT146;
195 rc = class_config_parse_llog(ctxt, profile, cfg);
198 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
201 /* We don't so much care about errors in cleaning up the config llog
202 * connection, as we have already read the config by this point. */
203 err = obd_disconnect(exp);
205 CERROR("obd_disconnect failed: rc = %d\n", err);
209 OBD_FREE(ocd, sizeof(*ocd));
211 lustre_cfg_bufs_reset(&bufs, name);
212 lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
213 err = class_process_config(lcfg);
214 lustre_cfg_free(lcfg);
216 CERROR("md_cleanup failed: rc = %d\n", err);
219 lustre_cfg_bufs_reset(&bufs, name);
220 lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
221 err = class_process_config(lcfg);
222 lustre_cfg_free(lcfg);
224 CERROR("md_detach failed: rc = %d\n", err);
227 lustre_cfg_bufs_reset(&bufs, name);
228 lustre_cfg_bufs_set_string(&bufs, 1, peer);
229 lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
230 err = class_process_config(lcfg);
232 CERROR("del MDC UUID failed: rc = %d\n", err);
233 lustre_cfg_free(lcfg);
239 /* parse host:/fsname string */
240 int ll_parse_mount_target(const char *target, char **mgsnid,
243 static char buf[256];
247 strncpy(buf, target, 255);
249 if ((s = strchr(buf, ':'))) {
255 sprintf(s + strlen(s), "-client");
265 * early liblustre init
266 * called from C startup in catamount apps, before main()
268 * The following is a skeleton sysio startup sequence,
269 * as implemented in C startup (skipping error handling).
270 * In this framework none of these calls need be made here
271 * or in the apps themselves. The NAMESPACE_STRING specifying
272 * the initial set of fs ops (creates, mounts, etc.) is passed
273 * as an environment variable.
276 * _sysio_incore_init();
277 * _sysio_native_init();
278 * _sysio_lustre_init();
279 * _sysio_boot(NAMESPACE_STRING);
281 * the name _sysio_lustre_init() follows the naming convention
282 * established in other fs drivers from libsysio:
283 * _sysio_incore_init(), _sysio_native_init()
285 * _sysio_lustre_init() must be called before _sysio_boot()
286 * to enable libsysio's processing of namespace init strings containing
287 * lustre filesystem operations
289 int _sysio_lustre_init(void)
294 extern void __liblustre_cleanup_(void);
297 liblustre_init_random();
301 perror("init llite driver");
305 envstr = getenv("LIBLUSTRE_TIMEOUT");
306 if (envstr != NULL) {
307 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
308 printf("LibLustre: obd timeout=%u seconds\n",
312 /* debug peer on timeout? */
313 envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
314 if (envstr != NULL) {
315 obd_debug_peer_on_timeout =
316 (unsigned int)strtol(envstr, NULL, 0);
317 printf("LibLustre: debug peer on timeout=%d\n",
318 obd_debug_peer_on_timeout ? 0 : 1);
322 (void)atexit(__liblustre_cleanup_);
327 extern int _sysio_native_init();
329 char *lustre_path = NULL;
331 void __liblustre_setup_(void)
334 char *lustre_driver = "lustre";
335 unsigned mntflgs = 0;
338 lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
340 lustre_path = "/mnt/lustre";
344 target = getenv("LIBLUSTRE_MOUNT_TARGET");
346 printf("LibLustre: no mount target specified\n");
350 CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
351 lustre_path, target);
354 /* initialize libsysio & mount rootfs */
356 perror("init sysio");
359 _sysio_native_init();
361 err = _sysio_mount_root("/", "native", mntflgs, NULL);
363 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
367 if (_sysio_lustre_init())
369 #endif /* INIT_SYSIO */
371 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
373 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
378 void __liblustre_cleanup_(void)
381 /* guard against being called multiple times */
382 static int cleaned = 0;
389 /* user app might chdir to a lustre directory, and leave busy pnode
390 * during finaly libsysio cleanup. here we chdir back to "/".
391 * but it can't fix the situation that liblustre is mounted
398 /* we can't call umount here, because libsysio will not cleanup
399 * opening files for us. _sysio_shutdown() will cleanup fds at
400 * first but which will also close the sockets we need for umount
401 * liblutre. this dilema lead to another hack in
402 * libsysio/src/file_hack.c FIXME
406 cleanup_lib_portals();