Whamcloud - gitweb
- landing of b_fid after merge with b_hd_cleanup_merge.
[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, name);
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_process_llog failed: rc = %d\n", rc);
151
152         err = obd_disconnect(exp, 0);
153
154 out_cleanup:
155         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
156         err = class_process_config(&lcfg);
157         if (err < 0)
158                 GOTO(out, err);
159
160 out_detach:
161         LCFG_INIT(lcfg, LCFG_DETACH, name);
162         err = class_process_config(&lcfg);
163         if (err < 0)
164                 GOTO(out, err);
165
166 out_del_uuid:
167         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
168         lcfg.lcfg_inllen1 = strlen(peer) + 1;
169         lcfg.lcfg_inlbuf1 = peer;
170         err = class_process_config(&lcfg);
171
172 out:
173         if (rc == 0)
174                 rc = err;
175         
176         RETURN(rc);
177 }
178
179 /* parse host:/mdsname/profile string */
180 int ll_parse_mount_target(const char *target, char **mdsnid,
181                           char **mdsname, char **profile)
182 {
183         static char buf[256];
184         char *s;
185
186         buf[255] = 0;
187         strncpy(buf, target, 255);
188
189         if ((s = strchr(buf, ':'))) {
190                 *mdsnid = buf;
191                 *s = '\0';
192                                                                                                                         
193                 while (*++s == '/')
194                         ;
195                 *mdsname = s;
196                 if ((s = strchr(*mdsname, '/'))) {
197                         *s = '\0';
198                         *profile = s + 1;
199                         return 0;
200                 }
201         }
202
203         return -1;
204 }
205
206 /*
207  * early liblustre init
208  * called from C startup in catamount apps, before main()
209  *
210  * The following is a skeleton sysio startup sequence,
211  * as implemented in C startup (skipping error handling).
212  * In this framework none of these calls need be made here
213  * or in the apps themselves.  The NAMESPACE_STRING specifying
214  * the initial set of fs ops (creates, mounts, etc.) is passed
215  * as an environment variable.
216  * 
217  *      _sysio_init();
218  *      _sysio_incore_init();
219  *      _sysio_native_init();
220  *      _sysio_lustre_init();
221  *      _sysio_boot(NAMESPACE_STRING);
222  *
223  * the name _sysio_lustre_init() follows the naming convention
224  * established in other fs drivers from libsysio:
225  *  _sysio_incore_init(), _sysio_native_init()
226  *
227  * _sysio_lustre_init() must be called before _sysio_boot()
228  * to enable libsysio's processing of namespace init strings containing
229  * lustre filesystem operations
230  */
231 int _sysio_lustre_init(void)
232 {
233         int err;
234
235 #if 0
236         portal_debug = -1;
237         portal_subsystem_debug = -1;
238 #endif
239
240         liblustre_init_random();
241
242         err = lllib_init();
243         if (err) {
244                 perror("init llite driver");
245         }       
246         return err;
247 }
248
249 /* env variables */
250 #define ENV_LUSTRE_MNTPNT               "LIBLUSTRE_MOUNT_POINT"
251 #define ENV_LUSTRE_MNTTGT               "LIBLUSTRE_MOUNT_TARGET"
252 #define ENV_LUSTRE_TIMEOUT              "LIBLUSTRE_TIMEOUT"
253 #define ENV_LUSTRE_DUMPFILE             "LIBLUSTRE_DUMPFILE"
254 #define ENV_LUSTRE_DEBUG_MASK           "LIBLUSTRE_DEBUG_MASK"
255 #define ENV_LUSTRE_DEBUG_SUBSYS         "LIBLUSTRE_DEBUG_SUBSYS"
256
257 extern int _sysio_native_init();
258 extern unsigned int obd_timeout;
259
260 static char *lustre_path = NULL;
261
262 /* global variables */
263 char   *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
264 char   *g_zconf_mdsnid = NULL;  /* mdsnid, for zeroconf */
265 char   *g_zconf_profile = NULL; /* profile, for zeroconf */
266
267
268 void __liblustre_setup_(void)
269 {
270         char *target = NULL;
271         char *timeout = NULL;
272         char *debug_mask = NULL;
273         char *debug_subsys = NULL;
274         char *root_driver = "native";
275         char *lustre_driver = "llite";
276         char *root_path = "/";
277         unsigned mntflgs = 0;
278         int err;
279
280         lustre_path = getenv(ENV_LUSTRE_MNTPNT);
281         if (!lustre_path) {
282                 lustre_path = "/mnt/lustre";
283         }
284
285         /* mount target */
286         target = getenv(ENV_LUSTRE_MNTTGT);
287         if (!target) {
288                 printf("LibLustre: no mount target specified\n");
289                 exit(1);
290         }
291         if (ll_parse_mount_target(target,
292                                   &g_zconf_mdsnid,
293                                   &g_zconf_mdsname,
294                                   &g_zconf_profile)) {
295                 CERROR("mal-formed target %s \n", target);
296                 exit(1);
297         }
298         if (!g_zconf_mdsnid || !g_zconf_mdsname || !g_zconf_profile) {
299                 printf("Liblustre: invalid target %s\n", target);
300                 exit(1);
301         }
302         printf("LibLustre: mount point %s, target %s\n",
303                 lustre_path, target);
304
305         timeout = getenv(ENV_LUSTRE_TIMEOUT);
306         if (timeout) {
307                 obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
308                 printf("LibLustre: set obd timeout as %u seconds\n",
309                         obd_timeout);
310         }
311
312         /* debug masks */
313         debug_mask = getenv(ENV_LUSTRE_DEBUG_MASK);
314         if (debug_mask)
315                 portal_debug = (unsigned int) strtol(debug_mask, NULL, 0);
316
317         debug_subsys = getenv(ENV_LUSTRE_DEBUG_SUBSYS);
318         if (debug_subsys)
319                 portal_subsystem_debug =
320                                 (unsigned int) strtol(debug_subsys, NULL, 0);
321
322 #ifndef CSTART_INIT
323         /* initialize libsysio & mount rootfs */
324         if (_sysio_init()) {
325                 perror("init sysio");
326                 exit(1);
327         }
328         _sysio_native_init();
329
330         err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
331         if (err) {
332                 perror(root_driver);
333                 exit(1);
334         }
335
336         if (_sysio_lustre_init())
337                 exit(1);
338 #endif
339
340         err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
341         if (err) {
342                 errno = -err;
343                 perror(lustre_driver);
344                 exit(1);
345         }
346 }
347
348 void __liblustre_cleanup_(void)
349 {
350         /* user app might chdir to a lustre directory, and leave busy pnode
351          * during finaly libsysio cleanup. here we chdir back to "/".
352          * but it can't fix the situation that liblustre is mounted
353          * at "/".
354          */
355         chdir("/");
356 #if 0
357         umount(lustre_path);
358 #endif
359         /* we can't call umount here, because libsysio will not cleanup
360          * opening files for us. _sysio_shutdown() will cleanup fds at
361          * first but which will also close the sockets we need for umount
362          * liblutre. this delima lead to another hack in
363          * libsysio/src/file_hack.c FIXME
364          */
365         _sysio_shutdown();
366         cleanup_lib_portals();
367         PtlFini();
368 }