Whamcloud - gitweb
- put GSS check after linux modules check to make sure that $LINUX is defined.
[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_bufs bufs;
91         struct lustre_cfg *lcfg;
92
93         char  *peer = "MDS_PEER_UUID";
94         struct obd_device *obd;
95         struct lustre_handle mdc_conn = {0, };
96         struct obd_export *exp;
97         char  *name = "mdc_dev";
98         class_uuid_t uuid;
99         struct obd_uuid mdc_uuid;
100         struct llog_ctxt *ctxt;
101         ptl_nid_t nid = 0;
102         int nal, err, rc = 0;
103         char *nal_name;
104         ENTRY;
105
106         generate_random_uuid(uuid);
107         class_uuid_unparse(uuid, &mdc_uuid);
108
109         if (ptl_parse_nid(&nid, g_zconf_mdsnid)) {
110                 CERROR("Can't parse NID %s\n", g_zconf_mdsnid);
111                 RETURN(-EINVAL);
112         }
113
114         nal_name = getenv(ENV_LUSTRE_NAL_NAME);
115         if (!nal_name)
116                 nal_name = "tcp";
117         nal = ptl_name2nal(nal_name);
118         if (nal <= 0) {
119                 CERROR("Can't parse NAL %s\n", nal_name);
120                 RETURN(-EINVAL);
121         }
122         lustre_cfg_bufs_reset(&bufs, NULL);
123         lustre_cfg_bufs_set_string(&bufs, 1, peer);
124         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
125         lcfg->lcfg_nid = nid;
126         lcfg->lcfg_nal = nal;
127         err = class_process_config(lcfg);
128         lustre_cfg_free(lcfg);
129         if (err < 0)
130                 GOTO(out, err);
131
132         lustre_cfg_bufs_reset(&bufs, name);
133         lustre_cfg_bufs_set_string(&bufs, 1, OBD_MDC_DEVICENAME);
134         lustre_cfg_bufs_set_string(&bufs, 2, mdc_uuid.uuid);
135         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
136         err = class_process_config(lcfg);
137         lustre_cfg_free(lcfg);
138         if (err < 0)
139                 GOTO(out_del_uuid, err);
140
141         lustre_cfg_bufs_reset(&bufs, name);
142         lustre_cfg_bufs_set_string(&bufs, 1, g_zconf_mdsname);
143         lustre_cfg_bufs_set_string(&bufs, 2, peer);
144         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
145         err = class_process_config(lcfg);
146         lustre_cfg_free(lcfg);
147         if (err < 0)
148                 GOTO(out_detach, err);
149
150         obd = class_name2obd(name);
151         if (obd == NULL)
152                 GOTO(out_cleanup, err = -EINVAL);
153
154         /* Disable initial recovery on this import */
155         err = obd_set_info(obd->obd_self_export,
156                            strlen("initial_recov"), "initial_recov",
157                            sizeof(allow_recov), &allow_recov);
158
159         err = obd_connect(&mdc_conn, obd, &mdc_uuid, NULL, 0);
160         if (err) {
161                 CERROR("cannot connect to %s: rc = %d\n",
162                         g_zconf_mdsname, err);
163                 GOTO(out_cleanup, err);
164         }
165
166         exp = class_conn2export(&mdc_conn);
167
168         ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
169         rc = class_config_process_llog(ctxt, g_zconf_profile, cfg);
170         if (rc)
171                 CERROR("class_config_process_llog failed: rc = %d\n", rc);
172
173         err = obd_disconnect(exp, 0);
174
175 out_cleanup:
176         lustre_cfg_bufs_reset(&bufs, name);
177         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
178         err = class_process_config(lcfg);
179         lustre_cfg_free(lcfg);
180         if (err < 0)
181                 GOTO(out, err);
182
183 out_detach:
184         lustre_cfg_bufs_reset(&bufs, name);
185         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
186         err = class_process_config(lcfg);
187         lustre_cfg_free(lcfg);
188         if (err < 0)
189                 GOTO(out, err);
190
191 out_del_uuid:
192         lustre_cfg_bufs_reset(&bufs, name);
193         lustre_cfg_bufs_set_string(&bufs, 1, peer);
194         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
195         err = class_process_config(lcfg);
196         lustre_cfg_free(lcfg);
197 out:
198         if (rc == 0)
199                 rc = err;
200
201         RETURN(rc);
202 }
203
204 /* parse host:/mdsname/profile string */
205 int ll_parse_mount_target(const char *target, char **mdsnid,
206                           char **mdsname, char **profile)
207 {
208         static char buf[256];
209         char *s;
210
211         buf[255] = 0;
212         strncpy(buf, target, 255);
213
214         if ((s = strchr(buf, ':'))) {
215                 *mdsnid = buf;
216                 *s = '\0';
217
218                 while (*++s == '/')
219                         ;
220                 *mdsname = s;
221                 if ((s = strchr(*mdsname, '/'))) {
222                         *s = '\0';
223                         *profile = s + 1;
224                         return 0;
225                 }
226         }
227
228         return -1;
229 }
230
231 /*
232  * early liblustre init
233  * called from C startup in catamount apps, before main()
234  *
235  * The following is a skeleton sysio startup sequence,
236  * as implemented in C startup (skipping error handling).
237  * In this framework none of these calls need be made here
238  * or in the apps themselves.  The NAMESPACE_STRING specifying
239  * the initial set of fs ops (creates, mounts, etc.) is passed
240  * as an environment variable.
241  *
242  *      _sysio_init();
243  *      _sysio_incore_init();
244  *      _sysio_native_init();
245  *      _sysio_lustre_init();
246  *      _sysio_boot(NAMESPACE_STRING);
247  *
248  * the name _sysio_lustre_init() follows the naming convention
249  * established in other fs drivers from libsysio:
250  *  _sysio_incore_init(), _sysio_native_init()
251  *
252  * _sysio_lustre_init() must be called before _sysio_boot()
253  * to enable libsysio's processing of namespace init strings containing
254  * lustre filesystem operations
255  */
256 int _sysio_lustre_init(void)
257 {
258         int err;
259
260 #if 0
261         portal_debug = -1;
262         portal_subsystem_debug = -1;
263 #endif
264
265         liblustre_init_random();
266
267         err = lllib_init();
268         if (err) {
269                 perror("init llite driver");
270         }
271         return err;
272 }
273
274 extern int _sysio_native_init();
275 extern unsigned int obd_timeout;
276
277 char *lustre_path = NULL;
278
279 /* global variables */
280 char   *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
281 char   *g_zconf_mdsnid = NULL;  /* mdsnid, for zeroconf */
282 char   *g_zconf_profile = NULL; /* profile, for zeroconf */
283
284
285 void __liblustre_setup_(void)
286 {
287         char *target = NULL;
288         char *timeout = NULL;
289         char *debug_mask = NULL;
290         char *debug_subsys = NULL;
291         char *root_driver = "native";
292         char *lustre_driver = "llite";
293         char *root_path = "/";
294         unsigned mntflgs = 0;
295         int err;
296
297         lustre_path = getenv(ENV_LUSTRE_MNTPNT);
298         if (!lustre_path) {
299                 lustre_path = "/mnt/lustre";
300         }
301
302         /* mount target */
303         target = getenv(ENV_LUSTRE_MNTTGT);
304         if (!target) {
305                 printf("LibLustre: no mount target specified\n");
306                 exit(1);
307         }
308         if (ll_parse_mount_target(target,
309                                   &g_zconf_mdsnid,
310                                   &g_zconf_mdsname,
311                                   &g_zconf_profile)) {
312                 CERROR("mal-formed target %s \n", target);
313                 exit(1);
314         }
315         if (!g_zconf_mdsnid || !g_zconf_mdsname || !g_zconf_profile) {
316                 printf("Liblustre: invalid target %s\n", target);
317                 exit(1);
318         }
319         printf("LibLustre: mount point %s, target %s\n",
320                 lustre_path, target);
321
322         timeout = getenv(ENV_LUSTRE_TIMEOUT);
323         if (timeout) {
324                 obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
325                 printf("LibLustre: set obd timeout as %u seconds\n",
326                         obd_timeout);
327         }
328
329         /* debug masks */
330         debug_mask = getenv(ENV_LUSTRE_DEBUG_MASK);
331         if (debug_mask)
332                 portal_debug = (unsigned int) strtol(debug_mask, NULL, 0);
333
334         debug_subsys = getenv(ENV_LUSTRE_DEBUG_SUBSYS);
335         if (debug_subsys)
336                 portal_subsystem_debug =
337                                 (unsigned int) strtol(debug_subsys, NULL, 0);
338
339
340 #ifdef INIT_SYSIO
341         /* initialize libsysio & mount rootfs */
342         if (_sysio_init()) {
343                 perror("init sysio");
344                 exit(1);
345         }
346         _sysio_native_init();
347
348         err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
349         if (err) {
350                 perror(root_driver);
351                 exit(1);
352         }
353
354         if (_sysio_lustre_init())
355                 exit(1);
356 #endif /* INIT_SYSIO */
357
358         err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
359         if (err) {
360                 errno = -err;
361                 perror(lustre_driver);
362                 exit(1);
363         }
364 }
365
366 void __liblustre_cleanup_(void)
367 {
368         /* user app might chdir to a lustre directory, and leave busy pnode
369          * during finaly libsysio cleanup. here we chdir back to "/".
370          * but it can't fix the situation that liblustre is mounted
371          * at "/".
372          */
373         chdir("/");
374 #if 0
375         umount(lustre_path);
376 #endif
377         /* we can't call umount here, because libsysio will not cleanup
378          * opening files for us. _sysio_shutdown() will cleanup fds at
379          * first but which will also close the sockets we need for umount
380          * liblutre. this delima lead to another hack in
381          * libsysio/src/file_hack.c FIXME
382          */
383 #ifdef INIT_SYSIO
384         _sysio_shutdown();
385         cleanup_lib_portals();
386         PtlFini();
387 #else
388         /*
389          * don't do any libsysio or low level portals cleanups
390          * platform framework does it
391          */
392         cleanup_lib_portals();
393 #endif
394 }