Whamcloud - gitweb
a024f29fc917504574ba4b93bbb0d5db3594aa10
[fs/lustre-release.git] / lustre / liblustre / llite_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/liblustre/llite_lib.c
35  *
36  * Lustre Light common routines
37  */
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <assert.h>
42 #include <signal.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/queue.h>
46
47 #include <liblustre.h>
48 #include <lnet/lnetctl.h>     /* needed for parse_dump */
49 #include <lustre_log.h>
50
51 #include "lutil.h"
52 #include "llite_lib.h"
53
54 int slp_global_init(void);
55
56 static int lllib_init(void)
57 {
58         if (liblustre_init_current("liblustre") ||
59             init_lib_portals() ||
60             init_obdclass() ||
61             ptlrpc_init() ||
62             mgc_init() ||
63             lmv_init() ||
64             mdc_init() ||
65             lov_init() ||
66             osc_init() ||
67             slp_global_init())
68                 return -1;
69
70         return _sysio_fssw_register("lustre", &llu_fssw_ops);
71 }
72
73 int liblustre_process_log(struct config_llog_instance *cfg,
74                           char *mgsnid, char *profile,
75                           int allow_recov)
76 {
77         struct lustre_cfg_bufs bufs;
78         struct lustre_cfg *lcfg;
79         char  *peer = "MGS_UUID";
80         struct obd_device *obd;
81         struct obd_export *exp;
82         char  *name = "mgc_dev";
83         class_uuid_t uuid;
84         struct obd_uuid mgc_uuid;
85         struct llog_ctxt *ctxt;
86         lnet_nid_t nid = 0;
87         char *mdsnid;
88         int err, rc = 0;
89         struct obd_connect_data *ocd = NULL;
90         ENTRY;
91
92         ll_generate_random_uuid(uuid);
93         class_uuid_unparse(uuid, &mgc_uuid);
94
95         nid = libcfs_str2nid(mgsnid);
96         if (nid == LNET_NID_ANY) {
97                 CERROR("Can't parse NID %s\n", mgsnid);
98                 RETURN(-EINVAL);
99         }
100
101         lustre_cfg_bufs_reset(&bufs, NULL);
102         lustre_cfg_bufs_set_string(&bufs, 1, peer);
103         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
104         lcfg->lcfg_nid = nid;
105         rc = class_process_config(lcfg);
106         lustre_cfg_free(lcfg);
107         if (rc < 0)
108                 GOTO(out, rc);
109
110         lustre_cfg_bufs_reset(&bufs, name);
111         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
112         lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
113         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
114         rc = class_process_config(lcfg);
115         lustre_cfg_free(lcfg);
116         if (rc < 0)
117                 GOTO(out_del_uuid, rc);
118
119         lustre_cfg_bufs_reset(&bufs, name);
120         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
121         lustre_cfg_bufs_set_string(&bufs, 2, peer);
122         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
123         rc = class_process_config(lcfg);
124         lustre_cfg_free(lcfg);
125         if (rc < 0)
126                 GOTO(out_detach, rc);
127
128         while ((mdsnid = strsep(&mgsnid, ","))) {
129                 nid = libcfs_str2nid(mdsnid);
130                 lustre_cfg_bufs_reset(&bufs, NULL);
131                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
132                 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
133                 lcfg->lcfg_nid = nid;
134                 rc = class_process_config(lcfg);
135                 lustre_cfg_free(lcfg);
136                 if (rc) {
137                         CERROR("Add uuid for %s failed %d\n",
138                                libcfs_nid2str(nid), rc);
139                         continue;
140                 }
141
142                 lustre_cfg_bufs_reset(&bufs, name);
143                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
144                 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
145                 lcfg->lcfg_nid = nid;
146                 rc = class_process_config(lcfg);
147                 lustre_cfg_free(lcfg);
148                 if (rc) {
149                         CERROR("Add conn for %s failed %d\n",
150                                libcfs_nid2str(nid), rc);
151                         continue;
152                 }
153         }
154
155         obd = class_name2obd(name);
156         if (obd == NULL)
157                 GOTO(out_cleanup, rc = -EINVAL);
158
159         OBD_ALLOC(ocd, sizeof(*ocd));
160         if (ocd == NULL)
161                 GOTO(out_cleanup, rc = -ENOMEM);
162
163         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
164                                  OBD_CONNECT_FULL20;
165         ocd->ocd_version = LUSTRE_VERSION_CODE;
166
167         rc = obd_connect(NULL, &exp, obd, &mgc_uuid, ocd, NULL);
168         if (rc) {
169                 CERROR("cannot connect to %s at %s: rc = %d\n",
170                        LUSTRE_MGS_OBDNAME, mgsnid, rc);
171                 GOTO(out_cleanup, rc);
172         }
173
174         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
175         cfg->cfg_flags |= CFG_F_COMPAT146;
176         rc = class_config_parse_llog(NULL, ctxt, profile, cfg);
177         llog_ctxt_put(ctxt);
178         if (rc) {
179                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
180         }
181
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);
185         if (err)
186                 CERROR("obd_disconnect failed: rc = %d\n", err);
187
188 out_cleanup:
189         if (ocd)
190                 OBD_FREE(ocd, sizeof(*ocd));
191
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);
196         if (err)
197                 CERROR("md_cleanup failed: rc = %d\n", err);
198
199 out_detach:
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);
204         if (err)
205                 CERROR("md_detach failed: rc = %d\n", err);
206
207 out_del_uuid:
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);
212         if (err)
213                 CERROR("del MDC UUID failed: rc = %d\n", err);
214         lustre_cfg_free(lcfg);
215 out:
216
217         RETURN(rc);
218 }
219
220 /* parse host:/fsname string */
221 int ll_parse_mount_target(const char *target, char **mgsnid,
222                           char **fsname)
223 {
224         static char buf[256];
225         char *s;
226
227         buf[255] = 0;
228         strncpy(buf, target, 255);
229
230         if ((s = strchr(buf, ':'))) {
231                 *mgsnid = buf;
232                 *s = '\0';
233
234                 while (*++s == '/')
235                         ;
236                 sprintf(s + strlen(s), "-client");
237                 *fsname = s;
238
239                 return 0;
240         }
241
242         return -1;
243 }
244
245 /*
246  * early liblustre init
247  * called from C startup in catamount apps, before main()
248  *
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.
255  *
256  *      _sysio_init();
257  *      _sysio_incore_init();
258  *      _sysio_native_init();
259  *      _sysio_lustre_init();
260  *      _sysio_boot(NAMESPACE_STRING);
261  *
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()
265  *
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
269  */
270 int _sysio_lustre_init(void)
271 {
272         int err;
273         char *envstr;
274 #ifndef INIT_SYSIO
275         extern void __liblustre_cleanup_(void);
276 #endif
277
278         liblustre_init_random();
279
280         err = lllib_init();
281         if (err) {
282                 perror("init llite driver");
283                 return err;
284         }
285
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",
290                         obd_timeout);
291         }
292
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);
300         }
301
302 #ifndef INIT_SYSIO
303         (void)atexit(__liblustre_cleanup_);
304 #endif
305         return err;
306 }
307
308 extern int _sysio_native_init();
309
310 static int mnt_retry = 0;
311
312 char *lustre_path = NULL;
313
314 void __liblustre_setup_(void)
315 {
316         char *target = NULL;
317         char *lustre_driver = "lustre";
318         unsigned mntflgs = 0;
319         int err, count;
320
321         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
322         if (!lustre_path) {
323                 lustre_path = "/mnt/lustre";
324         }
325
326         target = getenv("LIBLUSTRE_MOUNT_RETRY");
327         if (target) {
328                 mnt_retry = atoi(target);
329                 if (mnt_retry < 0)
330                         mnt_retry = 0;
331         }
332
333         /* mount target */
334         target = getenv("LIBLUSTRE_MOUNT_TARGET");
335         if (!target) {
336                 printf("LibLustre: no mount target specified\n");
337                 exit(1);
338         }
339
340         CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
341                lustre_path, target);
342
343 #ifdef INIT_SYSIO
344         /* initialize libsysio & mount rootfs */
345         if (_sysio_init()) {
346                 perror("init sysio");
347                 exit(1);
348         }
349         _sysio_native_init();
350
351         err = _sysio_mount_root("/", "native", mntflgs, NULL);
352         if (err) {
353                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
354                 exit(1);
355         }
356
357         if (_sysio_lustre_init())
358                 exit(1);
359 #endif /* INIT_SYSIO */
360
361         count = mnt_retry;
362         do {
363                 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
364                 if (err && mnt_retry && (-- count)) {
365                         fprintf(stderr, "Lustre mount failed: %s. "
366                                  "Will retry %d more times\n",
367                                 strerror(errno), mnt_retry - count );
368                         sleep(2);
369                 }
370         } while (err && count > 0);
371         if (err) {
372                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
373                 exit(1);
374         }
375 }
376
377 void __liblustre_cleanup_(void)
378 {
379 #ifndef INIT_SYSIO
380         /* guard against being called multiple times */
381         static int cleaned = 0;
382
383         if (cleaned)
384                 return;
385         cleaned++;
386 #endif
387
388         /* user app might chdir to a lustre directory, and leave busy pnode
389          * during finaly libsysio cleanup. here we chdir back to "/".
390          * but it can't fix the situation that liblustre is mounted
391          * at "/".
392          */
393         if (!chdir("/")) {}
394 #if 0
395         umount(lustre_path);
396 #endif
397         /* we can't call umount here, because libsysio will not cleanup
398          * opening files for us. _sysio_shutdown() will cleanup fds at
399          * first but which will also close the sockets we need for umount
400          * liblutre. this dilema lead to another hack in
401          * libsysio/src/file_hack.c FIXME
402          */
403 #ifdef INIT_SYSIO
404         _sysio_shutdown();
405         cleanup_lib_portals();
406         LNetFini();
407 #endif
408 }