Whamcloud - gitweb
bda17667482759b13b52ea3e4ce3c13ed2ceae4f
[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/stat.h>
30 #include <sys/queue.h>
31
32 #ifdef HAVE_XTIO_H
33 #include <xtio.h>
34 #endif
35 #include <sysio.h>
36 #include <fs.h>
37 #include <mount.h>
38 #include <inode.h>
39 #ifdef HAVE_FILE_H
40 #include <file.h>
41 #endif
42
43 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
44  * of 'LIST_HEAD'. undef it to suppress warnings
45  */
46 #undef LIST_HEAD
47 #include <lnet/lnetctl.h>     /* needed for parse_dump */
48
49 #include "lutil.h"
50 #include "llite_lib.h"
51
52 static int lllib_init(void)
53 {
54         if (liblustre_init_current("liblustre") ||
55             init_lib_portals() ||
56             init_obdclass() ||
57             ptlrpc_init() ||
58             mgc_init() ||
59             mdc_init() ||
60             lov_init() ||
61             osc_init())
62                 return -1;
63
64         return _sysio_fssw_register("lustre", &llu_fssw_ops);
65 }
66
67 int liblustre_process_log(struct config_llog_instance *cfg,
68                           char *mgsnid, char *profile,
69                           int allow_recov)
70 {
71         struct lustre_cfg_bufs bufs;
72         struct lustre_cfg *lcfg;
73         char  *peer = "MGS_UUID";
74         struct obd_device *obd;
75         struct lustre_handle mgc_conn = {0, };
76         struct obd_export *exp;
77         char  *name = "mgc_dev";
78         class_uuid_t uuid;
79         struct obd_uuid mgc_uuid;
80         struct llog_ctxt *ctxt;
81         lnet_nid_t nid = 0;
82         char *mdsnid;
83         int err, rc = 0;
84         struct obd_connect_data *ocd = NULL;
85         ENTRY;
86
87         generate_random_uuid(uuid);
88         class_uuid_unparse(uuid, &mgc_uuid);
89
90         nid = libcfs_str2nid(mgsnid);
91         if (nid == LNET_NID_ANY) {
92                 CERROR("Can't parse NID %s\n", mgsnid);
93                 RETURN(-EINVAL);
94         }
95
96         lustre_cfg_bufs_reset(&bufs, NULL);
97         lustre_cfg_bufs_set_string(&bufs, 1, peer);
98         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
99         lcfg->lcfg_nid = nid;
100         rc = class_process_config(lcfg);
101         lustre_cfg_free(lcfg);
102         if (rc < 0)
103                 GOTO(out, rc);
104
105         lustre_cfg_bufs_reset(&bufs, name);
106         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
107         lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
108         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
109         rc = class_process_config(lcfg);
110         lustre_cfg_free(lcfg);
111         if (rc < 0)
112                 GOTO(out_del_uuid, rc);
113
114         lustre_cfg_bufs_reset(&bufs, name);
115         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
116         lustre_cfg_bufs_set_string(&bufs, 2, peer);
117         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
118         rc = class_process_config(lcfg);
119         lustre_cfg_free(lcfg);
120         if (rc < 0)
121                 GOTO(out_detach, rc);
122
123         while ((mdsnid = strsep(&mgsnid, ","))) {
124                 nid = libcfs_str2nid(mdsnid);
125                 lustre_cfg_bufs_reset(&bufs, NULL);
126                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
127                 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
128                 lcfg->lcfg_nid = nid;
129                 rc = class_process_config(lcfg);
130                 lustre_cfg_free(lcfg);
131                 if (rc) {
132                         CERROR("Add uuid for %s failed %d\n",
133                                libcfs_nid2str(nid), rc);
134                         continue;
135                 }
136
137                 lustre_cfg_bufs_reset(&bufs, name);
138                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
139                 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
140                 lcfg->lcfg_nid = nid;
141                 rc = class_process_config(lcfg);
142                 lustre_cfg_free(lcfg);
143                 if (rc) {
144                         CERROR("Add conn for %s failed %d\n",
145                                libcfs_nid2str(nid), rc);
146                         continue;
147                 }
148         }
149
150         obd = class_name2obd(name);
151         if (obd == NULL)
152                 GOTO(out_cleanup, rc = -EINVAL);
153
154         OBD_ALLOC(ocd, sizeof(*ocd));
155         if (ocd == NULL)
156                 GOTO(out_cleanup, rc = -ENOMEM);
157
158         ocd->ocd_connect_flags = OBD_CONNECT_VERSION;
159         ocd->ocd_version = LUSTRE_VERSION_CODE;
160
161         rc = obd_connect(&mgc_conn, obd, &mgc_uuid, ocd);
162         if (rc) {
163                 CERROR("cannot connect to %s at %s: rc = %d\n",
164                        LUSTRE_MGS_OBDNAME, mgsnid, rc);
165                 GOTO(out_cleanup, rc);
166         }
167
168         exp = class_conn2export(&mgc_conn);
169
170         ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
171         cfg->cfg_flags |= CFG_F_COMPAT146;
172         rc = class_config_parse_llog(ctxt, profile, cfg);
173         if (rc) {
174                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
175         }
176
177         /* We don't so much care about errors in cleaning up the config llog
178          * connection, as we have already read the config by this point. */
179         err = obd_disconnect(exp);
180         if (err)
181                 CERROR("obd_disconnect failed: rc = %d\n", err);
182
183 out_cleanup:
184         if (ocd)
185                 OBD_FREE(ocd, sizeof(*ocd));
186
187         lustre_cfg_bufs_reset(&bufs, name);
188         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
189         err = class_process_config(lcfg);
190         lustre_cfg_free(lcfg);
191         if (err)
192                 CERROR("mdc_cleanup failed: rc = %d\n", err);
193
194 out_detach:
195         lustre_cfg_bufs_reset(&bufs, name);
196         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
197         err = class_process_config(lcfg);
198         lustre_cfg_free(lcfg);
199         if (err)
200                 CERROR("mdc_detach failed: rc = %d\n", err);
201
202 out_del_uuid:
203         lustre_cfg_bufs_reset(&bufs, name);
204         lustre_cfg_bufs_set_string(&bufs, 1, peer);
205         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
206         err = class_process_config(lcfg);
207         if (err)
208                 CERROR("del MDC UUID failed: rc = %d\n", err);
209         lustre_cfg_free(lcfg);
210 out:
211
212         RETURN(rc);
213 }
214
215 /* parse host:/fsname string */
216 int ll_parse_mount_target(const char *target, char **mgsnid,
217                           char **fsname)
218 {
219         static char buf[256];
220         char *s;
221
222         buf[255] = 0;
223         strncpy(buf, target, 255);
224
225         if ((s = strchr(buf, ':'))) {
226                 *mgsnid = buf;
227                 *s = '\0';
228
229                 while (*++s == '/')
230                         ;
231                 sprintf(s + strlen(s), "-client");
232                 *fsname = s;
233
234                 return 0;
235         }
236
237         return -1;
238 }
239
240 /*
241  * early liblustre init
242  * called from C startup in catamount apps, before main()
243  *
244  * The following is a skeleton sysio startup sequence,
245  * as implemented in C startup (skipping error handling).
246  * In this framework none of these calls need be made here
247  * or in the apps themselves.  The NAMESPACE_STRING specifying
248  * the initial set of fs ops (creates, mounts, etc.) is passed
249  * as an environment variable.
250  *
251  *      _sysio_init();
252  *      _sysio_incore_init();
253  *      _sysio_native_init();
254  *      _sysio_lustre_init();
255  *      _sysio_boot(NAMESPACE_STRING);
256  *
257  * the name _sysio_lustre_init() follows the naming convention
258  * established in other fs drivers from libsysio:
259  *  _sysio_incore_init(), _sysio_native_init()
260  *
261  * _sysio_lustre_init() must be called before _sysio_boot()
262  * to enable libsysio's processing of namespace init strings containing
263  * lustre filesystem operations
264  */
265 int _sysio_lustre_init(void)
266 {
267         int err;
268         char *timeout = NULL;
269 #ifndef INIT_SYSIO
270         extern void __liblustre_cleanup_(void);
271 #endif
272
273         liblustre_init_random();
274
275         err = lllib_init();
276         if (err) {
277                 perror("init llite driver");
278                 return err;
279         }
280         timeout = getenv("LIBLUSTRE_TIMEOUT");
281         if (timeout) {
282                 obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
283                 printf("LibLustre: set obd timeout as %u seconds\n",
284                         obd_timeout);
285         }
286
287 #ifndef INIT_SYSIO
288         (void)atexit(__liblustre_cleanup_);
289 #endif
290         return err;
291 }
292
293 extern int _sysio_native_init();
294 extern unsigned int obd_timeout;
295
296 char *lustre_path = NULL;
297
298 void __liblustre_setup_(void)
299 {
300         char *target = NULL;
301         char *root_driver = "native";
302         char *lustre_driver = "lustre";
303         char *root_path = "/";
304         unsigned mntflgs = 0;
305         int err;
306
307         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
308         if (!lustre_path) {
309                 lustre_path = "/mnt/lustre";
310         }
311
312         /* mount target */
313         target = getenv("LIBLUSTRE_MOUNT_TARGET");
314         if (!target) {
315                 printf("LibLustre: no mount target specified\n");
316                 exit(1);
317         }
318
319         CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
320                lustre_path, target);
321
322 #ifdef INIT_SYSIO
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                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
333                 exit(1);
334         }
335
336         if (_sysio_lustre_init())
337                 exit(1);
338 #endif /* INIT_SYSIO */
339
340         err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
341         if (err) {
342                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
343                 exit(1);
344         }
345 }
346
347 void __liblustre_cleanup_(void)
348 {
349 #ifndef INIT_SYSIO
350         /* guard against being called multiple times */
351         static int cleaned = 0;
352
353         if (cleaned)
354                 return;
355         cleaned++;
356 #endif
357
358         /* user app might chdir to a lustre directory, and leave busy pnode
359          * during finaly libsysio cleanup. here we chdir back to "/".
360          * but it can't fix the situation that liblustre is mounted
361          * at "/".
362          */
363         chdir("/");
364 #if 0
365         umount(lustre_path);
366 #endif
367         /* we can't call umount here, because libsysio will not cleanup
368          * opening files for us. _sysio_shutdown() will cleanup fds at
369          * first but which will also close the sockets we need for umount
370          * liblutre. this dilema lead to another hack in
371          * libsysio/src/file_hack.c FIXME
372          */
373 #ifdef INIT_SYSIO
374         _sysio_shutdown();
375         cleanup_lib_portals();
376         LNetFini();
377 #endif
378 }