Whamcloud - gitweb
land b_hd_sec onto HEAD:
[fs/lustre-release.git] / lustre / liblustre / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light common routines
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/queue.h>
30
31 #ifdef HAVE_XTIO_H
32 #include <xtio.h>
33 #endif
34 #include <sysio.h>
35 #include <fs.h>
36 #include <mount.h>
37 #include <inode.h>
38 #ifdef HAVE_FILE_H
39 #include <file.h>
40 #endif
41
42 /* env variables */
43 #define ENV_LUSTRE_MNTPNT               "LIBLUSTRE_MOUNT_POINT"
44 #define ENV_LUSTRE_MNTTGT               "LIBLUSTRE_MOUNT_TARGET"
45 #define ENV_LUSTRE_TIMEOUT              "LIBLUSTRE_TIMEOUT"
46 #define ENV_LUSTRE_DUMPFILE             "LIBLUSTRE_DUMPFILE"
47 #define ENV_LUSTRE_DEBUG_MASK           "LIBLUSTRE_DEBUG_MASK"
48 #define ENV_LUSTRE_DEBUG_SUBSYS         "LIBLUSTRE_DEBUG_SUBSYS"
49 #define ENV_LUSTRE_NAL_NAME             "LIBLUSTRE_NAL_NAME"
50
51 #ifdef REDSTORM
52 #define CSTART_INIT
53 #endif
54
55 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
56  * of 'LIST_HEAD'. undef it to suppress warnings
57  */
58 #undef LIST_HEAD
59 #include <portals/ptlctl.h>
60
61 #include "lutil.h"
62 #include "llite_lib.h"
63
64 static int lllib_init(void)
65 {
66         liblustre_set_nal_nid();
67
68         if (liblustre_init_current("dummy") ||
69             init_obdclass() ||
70             init_lib_portals() ||
71             ptlrpc_init() ||
72             mdc_init() ||
73             lov_init() ||
74             osc_init())
75                 return -1;
76
77         return _sysio_fssw_register("llite", &llu_fssw_ops);
78 }
79  
80 #ifndef CRAY_PORTALS
81 #define LIBLUSTRE_NAL_NAME "tcp"
82 #elif defined REDSTORM
83 #define LIBLUSTRE_NAL_NAME "cray_qk_ernal"
84 #else
85 #define LIBLUSTRE_NAL_NAME "cray_pb_ernal"
86 #endif
87
88 int liblustre_process_log(struct config_llog_instance *cfg, int allow_recov)
89 {
90         struct lustre_cfg lcfg;
91         char  *peer = "MDS_PEER_UUID";
92         struct obd_device *obd;
93         struct lustre_handle mdc_conn = {0, };
94         struct obd_export *exp;
95         char  *name = "mdc_dev";
96         class_uuid_t uuid;
97         struct obd_uuid mdc_uuid;
98         struct llog_ctxt *ctxt;
99         ptl_nid_t nid = 0;
100         int nal, err, rc = 0;
101         char *nal_name;
102         ENTRY;
103
104         generate_random_uuid(uuid);
105         class_uuid_unparse(uuid, &mdc_uuid);
106
107         if (ptl_parse_nid(&nid, g_zconf_mdsnid)) {
108                 CERROR("Can't parse NID %s\n", g_zconf_mdsnid);
109                 RETURN(-EINVAL);
110         }
111
112         nal_name = getenv(ENV_LUSTRE_NAL_NAME);
113         if (!nal_name)
114                 nal_name = "tcp";
115         nal = ptl_name2nal(nal_name);
116         if (nal <= 0) {
117                 CERROR("Can't parse NAL %s\n", nal_name);
118                 RETURN(-EINVAL);
119         }
120         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
121         lcfg.lcfg_nid = nid;
122         lcfg.lcfg_inllen1 = strlen(peer) + 1;
123         lcfg.lcfg_inlbuf1 = peer;
124         lcfg.lcfg_nal = nal;
125         err = class_process_config(&lcfg);
126         if (err < 0)
127                 GOTO(out, err);
128
129         LCFG_INIT(lcfg, LCFG_ATTACH, name);
130         lcfg.lcfg_inlbuf1 = "mdc";
131         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
132         lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
133         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
134         err = class_process_config(&lcfg);
135         if (err < 0)
136                 GOTO(out_del_uuid, err);
137
138         LCFG_INIT(lcfg, LCFG_SETUP, name);
139         lcfg.lcfg_inlbuf1 = g_zconf_mdsname;
140         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
141         lcfg.lcfg_inlbuf2 = peer;
142         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
143         err = class_process_config(&lcfg);
144         if (err < 0)
145                 GOTO(out_detach, err);
146
147         obd = class_name2obd(name);
148         if (obd == NULL)
149                 GOTO(out_cleanup, err = -EINVAL);
150
151         /* Disable initial recovery on this import */
152         err = obd_set_info(obd->obd_self_export,
153                            strlen("initial_recov"), "initial_recov",
154                            sizeof(allow_recov), &allow_recov);
155
156         err = obd_connect(&mdc_conn, obd, &mdc_uuid, NULL, 0);
157         if (err) {
158                 CERROR("cannot connect to %s: rc = %d\n",
159                         g_zconf_mdsname, err);
160                 GOTO(out_cleanup, err);
161         }
162
163         exp = class_conn2export(&mdc_conn);
164
165         ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
166         rc = class_config_process_llog(ctxt, g_zconf_profile, cfg);
167         if (rc)
168                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
169
170         err = obd_disconnect(exp, 0);
171
172 out_cleanup:
173         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
174         err = class_process_config(&lcfg);
175         if (err < 0)
176                 GOTO(out, err);
177
178 out_detach:
179         LCFG_INIT(lcfg, LCFG_DETACH, name);
180         err = class_process_config(&lcfg);
181         if (err < 0)
182                 GOTO(out, err);
183
184 out_del_uuid:
185         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
186         lcfg.lcfg_inllen1 = strlen(peer) + 1;
187         lcfg.lcfg_inlbuf1 = peer;
188         err = class_process_config(&lcfg);
189
190 out:
191         if (rc == 0)
192                 rc = err;
193
194         RETURN(rc);
195 }
196
197 /* parse host:/mdsname/profile string */
198 int ll_parse_mount_target(const char *target, char **mdsnid,
199                           char **mdsname, char **profile)
200 {
201         static char buf[256];
202         char *s;
203
204         buf[255] = 0;
205         strncpy(buf, target, 255);
206
207         if ((s = strchr(buf, ':'))) {
208                 *mdsnid = buf;
209                 *s = '\0';
210
211                 while (*++s == '/')
212                         ;
213                 *mdsname = s;
214                 if ((s = strchr(*mdsname, '/'))) {
215                         *s = '\0';
216                         *profile = s + 1;
217                         return 0;
218                 }
219         }
220
221         return -1;
222 }
223
224 /*
225  * early liblustre init
226  * called from C startup in catamount apps, before main()
227  *
228  * The following is a skeleton sysio startup sequence,
229  * as implemented in C startup (skipping error handling).
230  * In this framework none of these calls need be made here
231  * or in the apps themselves.  The NAMESPACE_STRING specifying
232  * the initial set of fs ops (creates, mounts, etc.) is passed
233  * as an environment variable.
234  *
235  *      _sysio_init();
236  *      _sysio_incore_init();
237  *      _sysio_native_init();
238  *      _sysio_lustre_init();
239  *      _sysio_boot(NAMESPACE_STRING);
240  *
241  * the name _sysio_lustre_init() follows the naming convention
242  * established in other fs drivers from libsysio:
243  *  _sysio_incore_init(), _sysio_native_init()
244  *
245  * _sysio_lustre_init() must be called before _sysio_boot()
246  * to enable libsysio's processing of namespace init strings containing
247  * lustre filesystem operations
248  */
249 int _sysio_lustre_init(void)
250 {
251         int err;
252
253 #if 0
254         portal_debug = -1;
255         portal_subsystem_debug = -1;
256 #endif
257
258         liblustre_init_random();
259
260         err = lllib_init();
261         if (err) {
262                 perror("init llite driver");
263         }
264         return err;
265 }
266
267 extern int _sysio_native_init();
268 extern unsigned int obd_timeout;
269
270 char *lustre_path = NULL;
271
272 /* global variables */
273 char   *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
274 char   *g_zconf_mdsnid = NULL;  /* mdsnid, for zeroconf */
275 char   *g_zconf_profile = NULL; /* profile, for zeroconf */
276
277
278 void __liblustre_setup_(void)
279 {
280         char *target = NULL;
281         char *timeout = NULL;
282         char *debug_mask = NULL;
283         char *debug_subsys = NULL;
284         char *root_driver = "native";
285         char *lustre_driver = "llite";
286         char *root_path = "/";
287         unsigned mntflgs = 0;
288         int err;
289
290         lustre_path = getenv(ENV_LUSTRE_MNTPNT);
291         if (!lustre_path) {
292                 lustre_path = "/mnt/lustre";
293         }
294
295         /* mount target */
296         target = getenv(ENV_LUSTRE_MNTTGT);
297         if (!target) {
298                 printf("LibLustre: no mount target specified\n");
299                 exit(1);
300         }
301         if (ll_parse_mount_target(target,
302                                   &g_zconf_mdsnid,
303                                   &g_zconf_mdsname,
304                                   &g_zconf_profile)) {
305                 CERROR("mal-formed target %s \n", target);
306                 exit(1);
307         }
308         if (!g_zconf_mdsnid || !g_zconf_mdsname || !g_zconf_profile) {
309                 printf("Liblustre: invalid target %s\n", target);
310                 exit(1);
311         }
312         printf("LibLustre: mount point %s, target %s\n",
313                 lustre_path, target);
314
315         timeout = getenv(ENV_LUSTRE_TIMEOUT);
316         if (timeout) {
317                 obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
318                 printf("LibLustre: set obd timeout as %u seconds\n",
319                         obd_timeout);
320         }
321
322         /* debug masks */
323         debug_mask = getenv(ENV_LUSTRE_DEBUG_MASK);
324         if (debug_mask)
325                 portal_debug = (unsigned int) strtol(debug_mask, NULL, 0);
326
327         debug_subsys = getenv(ENV_LUSTRE_DEBUG_SUBSYS);
328         if (debug_subsys)
329                 portal_subsystem_debug =
330                                 (unsigned int) strtol(debug_subsys, NULL, 0);
331
332
333 #ifdef INIT_SYSIO
334         /* initialize libsysio & mount rootfs */
335         if (_sysio_init()) {
336                 perror("init sysio");
337                 exit(1);
338         }
339         _sysio_native_init();
340
341         err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
342         if (err) {
343                 perror(root_driver);
344                 exit(1);
345         }
346
347         if (_sysio_lustre_init())
348                 exit(1);
349 #endif /* INIT_SYSIO */
350
351         err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
352         if (err) {
353                 errno = -err;
354                 perror(lustre_driver);
355                 exit(1);
356         }
357 }
358
359 void __liblustre_cleanup_(void)
360 {
361         /* user app might chdir to a lustre directory, and leave busy pnode
362          * during finaly libsysio cleanup. here we chdir back to "/".
363          * but it can't fix the situation that liblustre is mounted
364          * at "/".
365          */
366         chdir("/");
367 #if 0
368         umount(lustre_path);
369 #endif
370         /* we can't call umount here, because libsysio will not cleanup
371          * opening files for us. _sysio_shutdown() will cleanup fds at
372          * first but which will also close the sockets we need for umount
373          * liblutre. this delima lead to another hack in
374          * libsysio/src/file_hack.c FIXME
375          */
376 #ifdef INIT_SYSIO
377         _sysio_shutdown();
378         cleanup_lib_portals();
379         PtlFini();
380 #else
381         /*
382          * don't do any libsysio or low level portals cleanups
383          * platform framework does it
384          */
385         cleanup_lib_portals();
386 #endif
387 }