Whamcloud - gitweb
8a100049fff4ed3d0e898b1d32cf8ed2b6c0f50e
[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
50 #include "lutil.h"
51 #include "llite_lib.h"
52
53 int slp_global_init(void);
54
55 static int lllib_init(void)
56 {
57         if (liblustre_init_current("liblustre") ||
58             init_lib_portals() ||
59             init_obdclass() ||
60             ptlrpc_init() ||
61             mgc_init() ||
62             lmv_init() ||
63             mdc_init() ||
64             lov_init() ||
65             osc_init() ||
66             slp_global_init())
67                 return -1;
68
69         return _sysio_fssw_register("lustre", &llu_fssw_ops);
70 }
71
72 int liblustre_process_log(struct config_llog_instance *cfg,
73                           char *mgsnid, char *profile,
74                           int allow_recov)
75 {
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";
82         class_uuid_t uuid;
83         struct obd_uuid mgc_uuid;
84         struct llog_ctxt *ctxt;
85         lnet_nid_t nid = 0;
86         char *mdsnid;
87         int err, rc = 0;
88         struct obd_connect_data *ocd = NULL;
89         ENTRY;
90
91         ll_generate_random_uuid(uuid);
92         class_uuid_unparse(uuid, &mgc_uuid);
93
94         nid = libcfs_str2nid(mgsnid);
95         if (nid == LNET_NID_ANY) {
96                 CERROR("Can't parse NID %s\n", mgsnid);
97                 RETURN(-EINVAL);
98         }
99
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);
106         if (rc < 0)
107                 GOTO(out, rc);
108
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);
115         if (rc < 0)
116                 GOTO(out_del_uuid, rc);
117
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);
124         if (rc < 0)
125                 GOTO(out_detach, rc);
126
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);
135                 if (rc) {
136                         CERROR("Add uuid for %s failed %d\n",
137                                libcfs_nid2str(nid), rc);
138                         continue;
139                 }
140
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);
147                 if (rc) {
148                         CERROR("Add conn for %s failed %d\n",
149                                libcfs_nid2str(nid), rc);
150                         continue;
151                 }
152         }
153
154         obd = class_name2obd(name);
155         if (obd == NULL)
156                 GOTO(out_cleanup, rc = -EINVAL);
157
158         OBD_ALLOC(ocd, sizeof(*ocd));
159         if (ocd == NULL)
160                 GOTO(out_cleanup, rc = -ENOMEM);
161
162         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_FID |
163                                  OBD_CONNECT_AT | OBD_CONNECT_VBR |
164                                  OBD_CONNECT_FULL20;
165 #ifdef LIBLUSTRE_POSIX_ACL
166         ocd->ocd_connect_flags |= OBD_CONNECT_ACL;
167 #endif
168         ocd->ocd_version = LUSTRE_VERSION_CODE;
169
170         rc = obd_connect(NULL, &exp, obd, &mgc_uuid, ocd, NULL);
171         if (rc) {
172                 CERROR("cannot connect to %s at %s: rc = %d\n",
173                        LUSTRE_MGS_OBDNAME, mgsnid, rc);
174                 GOTO(out_cleanup, rc);
175         }
176
177         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
178         cfg->cfg_flags |= CFG_F_COMPAT146;
179         rc = class_config_parse_llog(ctxt, profile, cfg);
180         llog_ctxt_put(ctxt);
181         if (rc) {
182                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
183         }
184
185         /* We don't so much care about errors in cleaning up the config llog
186          * connection, as we have already read the config by this point. */
187         err = obd_disconnect(exp);
188         if (err)
189                 CERROR("obd_disconnect failed: rc = %d\n", err);
190
191 out_cleanup:
192         if (ocd)
193                 OBD_FREE(ocd, sizeof(*ocd));
194
195         lustre_cfg_bufs_reset(&bufs, name);
196         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
197         err = class_process_config(lcfg);
198         lustre_cfg_free(lcfg);
199         if (err)
200                 CERROR("md_cleanup failed: rc = %d\n", err);
201
202 out_detach:
203         lustre_cfg_bufs_reset(&bufs, name);
204         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
205         err = class_process_config(lcfg);
206         lustre_cfg_free(lcfg);
207         if (err)
208                 CERROR("md_detach failed: rc = %d\n", err);
209
210 out_del_uuid:
211         lustre_cfg_bufs_reset(&bufs, name);
212         lustre_cfg_bufs_set_string(&bufs, 1, peer);
213         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
214         err = class_process_config(lcfg);
215         if (err)
216                 CERROR("del MDC UUID failed: rc = %d\n", err);
217         lustre_cfg_free(lcfg);
218 out:
219
220         RETURN(rc);
221 }
222
223 /* parse host:/fsname string */
224 int ll_parse_mount_target(const char *target, char **mgsnid,
225                           char **fsname)
226 {
227         static char buf[256];
228         char *s;
229
230         buf[255] = 0;
231         strncpy(buf, target, 255);
232
233         if ((s = strchr(buf, ':'))) {
234                 *mgsnid = buf;
235                 *s = '\0';
236
237                 while (*++s == '/')
238                         ;
239                 sprintf(s + strlen(s), "-client");
240                 *fsname = s;
241
242                 return 0;
243         }
244
245         return -1;
246 }
247
248 /*
249  * early liblustre init
250  * called from C startup in catamount apps, before main()
251  *
252  * The following is a skeleton sysio startup sequence,
253  * as implemented in C startup (skipping error handling).
254  * In this framework none of these calls need be made here
255  * or in the apps themselves.  The NAMESPACE_STRING specifying
256  * the initial set of fs ops (creates, mounts, etc.) is passed
257  * as an environment variable.
258  *
259  *      _sysio_init();
260  *      _sysio_incore_init();
261  *      _sysio_native_init();
262  *      _sysio_lustre_init();
263  *      _sysio_boot(NAMESPACE_STRING);
264  *
265  * the name _sysio_lustre_init() follows the naming convention
266  * established in other fs drivers from libsysio:
267  *  _sysio_incore_init(), _sysio_native_init()
268  *
269  * _sysio_lustre_init() must be called before _sysio_boot()
270  * to enable libsysio's processing of namespace init strings containing
271  * lustre filesystem operations
272  */
273 int _sysio_lustre_init(void)
274 {
275         int err;
276         char *envstr;
277 #ifndef INIT_SYSIO
278         extern void __liblustre_cleanup_(void);
279 #endif
280
281         liblustre_init_random();
282
283         err = lllib_init();
284         if (err) {
285                 perror("init llite driver");
286                 return err;
287         }
288
289         envstr = getenv("LIBLUSTRE_TIMEOUT");
290         if (envstr != NULL) {
291                 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
292                 printf("LibLustre: obd timeout=%u seconds\n",
293                         obd_timeout);
294         }
295
296         /* debug peer on timeout? */
297         envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
298         if (envstr != NULL) {
299                 obd_debug_peer_on_timeout = 
300                         (unsigned int)strtol(envstr, NULL, 0);
301                 printf("LibLustre: debug peer on timeout=%d\n",
302                         obd_debug_peer_on_timeout ? 0 : 1);
303         }
304
305 #ifndef INIT_SYSIO
306         (void)atexit(__liblustre_cleanup_);
307 #endif
308         return err;
309 }
310
311 extern int _sysio_native_init();
312
313 static int mnt_retry = 0;
314
315 char *lustre_path = NULL;
316
317 void __liblustre_setup_(void)
318 {
319         char *target = NULL;
320         char *lustre_driver = "lustre";
321         unsigned mntflgs = 0;
322         int err, count;
323
324         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
325         if (!lustre_path) {
326                 lustre_path = "/mnt/lustre";
327         }
328
329         target = getenv("LIBLUSTRE_MOUNT_RETRY");
330         if (target) {
331                 mnt_retry = atoi(target);
332                 if (mnt_retry < 0)
333                         mnt_retry = 0;
334         }
335
336         /* mount target */
337         target = getenv("LIBLUSTRE_MOUNT_TARGET");
338         if (!target) {
339                 printf("LibLustre: no mount target specified\n");
340                 exit(1);
341         }
342
343         CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
344                lustre_path, target);
345
346 #ifdef INIT_SYSIO
347         /* initialize libsysio & mount rootfs */
348         if (_sysio_init()) {
349                 perror("init sysio");
350                 exit(1);
351         }
352         _sysio_native_init();
353
354         err = _sysio_mount_root("/", "native", mntflgs, NULL);
355         if (err) {
356                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
357                 exit(1);
358         }
359
360         if (_sysio_lustre_init())
361                 exit(1);
362 #endif /* INIT_SYSIO */
363
364         count = mnt_retry;
365         do {
366                 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
367                 if (err && mnt_retry && (-- count)) {
368                         fprintf(stderr, "Lustre mount failed: %s. "
369                                  "Will retry %d more times\n",
370                                 strerror(errno), mnt_retry - count );
371                         sleep(2);
372                 }
373         } while (err && count > 0);
374         if (err) {
375                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
376                 exit(1);
377         }
378 }
379
380 void __liblustre_cleanup_(void)
381 {
382 #ifndef INIT_SYSIO
383         /* guard against being called multiple times */
384         static int cleaned = 0;
385
386         if (cleaned)
387                 return;
388         cleaned++;
389 #endif
390
391         /* user app might chdir to a lustre directory, and leave busy pnode
392          * during finaly libsysio cleanup. here we chdir back to "/".
393          * but it can't fix the situation that liblustre is mounted
394          * at "/".
395          */
396         if (!chdir("/")) {}
397 #if 0
398         umount(lustre_path);
399 #endif
400         /* we can't call umount here, because libsysio will not cleanup
401          * opening files for us. _sysio_shutdown() will cleanup fds at
402          * first but which will also close the sockets we need for umount
403          * liblutre. this dilema lead to another hack in
404          * libsysio/src/file_hack.c FIXME
405          */
406 #ifdef INIT_SYSIO
407         _sysio_shutdown();
408         cleanup_lib_portals();
409         LNetFini();
410 #endif
411 }