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