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