Whamcloud - gitweb
Branch b1_4_mountconf
[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 #include <linux/lustre_ver.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             mdc_init() ||
60             lov_init() ||
61             osc_init())
62                 return -1;
63
64         return _sysio_fssw_register("llite", &llu_fssw_ops);
65 }
66
67 int liblustre_process_log(struct config_llog_instance *cfg,
68                           char *mdsnid, char *mdsname, char *profile,
69                           int allow_recov)
70 {
71         struct lustre_cfg_bufs bufs;
72         struct lustre_cfg *lcfg;
73         char  *peer = "MDS_PEER_UUID";
74         struct obd_device *obd;
75         struct lustre_handle mdc_conn = {0, };
76         struct obd_export *exp;
77         char  *name = "mdc_dev";
78         class_uuid_t uuid;
79         struct obd_uuid mdc_uuid;
80         struct llog_ctxt *ctxt;
81         lnet_nid_t nid = 0;
82         int err, rc = 0;
83         struct obd_connect_data *ocd = NULL;
84         ENTRY;
85
86         generate_random_uuid(uuid);
87         class_uuid_unparse(uuid, &mdc_uuid);
88
89         nid = libcfs_str2nid(mdsnid);
90         if (nid == LNET_NID_ANY) {
91                 CERROR("Can't parse NID %s\n", mdsnid);
92                 RETURN(-EINVAL);
93         }
94
95         lustre_cfg_bufs_reset(&bufs, NULL);
96         lustre_cfg_bufs_set_string(&bufs, 1, peer);
97         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
98         lcfg->lcfg_nid = nid;
99         rc = class_process_config(lcfg);
100         lustre_cfg_free(lcfg);
101         if (rc < 0)
102                 GOTO(out, rc);
103
104         lustre_cfg_bufs_reset(&bufs, name);
105         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MDC_NAME);//FIXME connect to mgc
106         lustre_cfg_bufs_set_string(&bufs, 2, mdc_uuid.uuid);
107         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
108         rc = class_process_config(lcfg);
109         lustre_cfg_free(lcfg);
110         if (rc < 0)
111                 GOTO(out_del_uuid, rc);
112
113         lustre_cfg_bufs_reset(&bufs, name);
114         lustre_cfg_bufs_set_string(&bufs, 1, mdsname);
115         lustre_cfg_bufs_set_string(&bufs, 2, peer);
116         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
117         rc = class_process_config(lcfg);
118         lustre_cfg_free(lcfg);
119         if (rc < 0)
120                 GOTO(out_detach, rc);
121
122         obd = class_name2obd(name);
123         if (obd == NULL)
124                 GOTO(out_cleanup, rc = -EINVAL);
125
126         OBD_ALLOC(ocd, sizeof(*ocd));
127         if (ocd == NULL)
128                 GOTO(out_cleanup, rc = -ENOMEM);
129
130         ocd->ocd_version = LUSTRE_VERSION_CODE;
131
132         /* Disable initial recovery on this import */
133         rc = obd_set_info(obd->obd_self_export,
134                           strlen(KEY_INIT_RECOV), KEY_INIT_RECOV,
135                           sizeof(allow_recov), &allow_recov);
136
137         rc = obd_connect(&mdc_conn, obd, &mdc_uuid, ocd);
138         if (rc) {
139                 CERROR("cannot connect to %s: rc = %d\n", mdsname, rc);
140                 GOTO(out_cleanup, rc);
141         }
142
143         exp = class_conn2export(&mdc_conn);
144
145         ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
146         rc = class_config_parse_llog(ctxt, profile, cfg);
147         if (rc) {
148                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
149         }
150
151         /* We don't so much care about errors in cleaning up the config llog
152          * connection, as we have already read the config by this point. */
153         err = obd_disconnect(exp);
154         if (err)
155                 CERROR("obd_disconnect failed: rc = %d\n", err);
156
157 out_cleanup:
158         if (ocd)
159                 OBD_FREE(ocd, sizeof(*ocd));
160
161         lustre_cfg_bufs_reset(&bufs, name);
162         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
163         err = class_process_config(lcfg);
164         lustre_cfg_free(lcfg);
165         if (err)
166                 CERROR("mdc_cleanup failed: rc = %d\n", err);
167
168 out_detach:
169         lustre_cfg_bufs_reset(&bufs, name);
170         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
171         err = class_process_config(lcfg);
172         lustre_cfg_free(lcfg);
173         if (err)
174                 CERROR("mdc_detach failed: rc = %d\n", err);
175
176 out_del_uuid:
177         lustre_cfg_bufs_reset(&bufs, name);
178         lustre_cfg_bufs_set_string(&bufs, 1, peer);
179         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
180         err = class_process_config(lcfg);
181         if (err)
182                 CERROR("del MDC UUID failed: rc = %d\n", err);
183         lustre_cfg_free(lcfg);
184 out:
185
186         RETURN(rc);
187 }
188
189 /* parse host:/mdsname/profile string */
190 int ll_parse_mount_target(const char *target, char **mdsnid,
191                           char **mdsname, char **profile)
192 {
193         static char buf[256];
194         char *s;
195
196         buf[255] = 0;
197         strncpy(buf, target, 255);
198
199         if ((s = strchr(buf, ':'))) {
200                 *mdsnid = buf;
201                 *s = '\0';
202
203                 while (*++s == '/')
204                         ;
205                 *mdsname = s;
206                 if ((s = strchr(*mdsname, '/'))) {
207                         *s = '\0';
208                         *profile = s + 1;
209                         return 0;
210                 }
211         }
212
213         return -1;
214 }
215
216 /*
217  * early liblustre init
218  * called from C startup in catamount apps, before main()
219  *
220  * The following is a skeleton sysio startup sequence,
221  * as implemented in C startup (skipping error handling).
222  * In this framework none of these calls need be made here
223  * or in the apps themselves.  The NAMESPACE_STRING specifying
224  * the initial set of fs ops (creates, mounts, etc.) is passed
225  * as an environment variable.
226  *
227  *      _sysio_init();
228  *      _sysio_incore_init();
229  *      _sysio_native_init();
230  *      _sysio_lustre_init();
231  *      _sysio_boot(NAMESPACE_STRING);
232  *
233  * the name _sysio_lustre_init() follows the naming convention
234  * established in other fs drivers from libsysio:
235  *  _sysio_incore_init(), _sysio_native_init()
236  *
237  * _sysio_lustre_init() must be called before _sysio_boot()
238  * to enable libsysio's processing of namespace init strings containing
239  * lustre filesystem operations
240  */
241 int _sysio_lustre_init(void)
242 {
243         int err;
244         char *timeout = NULL;
245         char *debug_mask = NULL;
246         char *debug_subsys = NULL;
247 #ifndef INIT_SYSIO
248         extern void __liblustre_cleanup_(void);
249 #endif
250
251 #if 0
252         libcfs_debug = -1;
253         libcfs_subsystem_debug = -1;
254 #endif
255
256         liblustre_init_random();
257
258         err = lllib_init();
259         if (err) {
260                 perror("init llite driver");
261                 return err;
262         }
263         timeout = getenv("LIBLUSTRE_TIMEOUT");
264         if (timeout) {
265                 obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
266                 printf("LibLustre: set obd timeout as %u seconds\n",
267                         obd_timeout);
268         }
269
270         /* debug masks */
271         debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
272         if (debug_mask)
273                 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
274
275         debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
276         if (debug_subsys)
277                 libcfs_subsystem_debug =
278                                 (unsigned int) strtol(debug_subsys, NULL, 0);
279
280 #ifndef INIT_SYSIO
281         (void)atexit(__liblustre_cleanup_);
282 #endif
283         return err;
284 }
285
286 extern int _sysio_native_init();
287 extern unsigned int obd_timeout;
288
289 char *lustre_path = NULL;
290
291 void __liblustre_setup_(void)
292 {
293         char *target = NULL;
294         char *root_driver = "native";
295         char *lustre_driver = "llite";
296         char *root_path = "/";
297         unsigned mntflgs = 0;
298         int err;
299
300         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
301         if (!lustre_path) {
302                 lustre_path = "/mnt/lustre";
303         }
304
305         /* mount target */
306         target = getenv("LIBLUSTRE_MOUNT_TARGET");
307         if (!target) {
308                 printf("LibLustre: no mount target specified\n");
309                 exit(1);
310         }
311         printf("LibLustre: mount point %s, target %s\n",
312                 lustre_path, target);
313
314
315 #ifdef INIT_SYSIO
316         /* initialize libsysio & mount rootfs */
317         if (_sysio_init()) {
318                 perror("init sysio");
319                 exit(1);
320         }
321         _sysio_native_init();
322
323         err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
324         if (err) {
325                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
326                 exit(1);
327         }
328
329         if (_sysio_lustre_init())
330                 exit(1);
331 #endif /* INIT_SYSIO */
332
333         err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
334         if (err) {
335                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
336                 exit(1);
337         }
338 }
339
340 void __liblustre_cleanup_(void)
341 {
342 #ifndef INIT_SYSIO
343         /* guard against being called multiple times */
344         static int cleaned = 0;
345
346         if (cleaned)
347                 return;
348         cleaned++;
349 #endif
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 dilema lead to another hack in
364          * libsysio/src/file_hack.c FIXME
365          */
366 #ifdef INIT_SYSIO
367         _sysio_shutdown();
368         cleanup_lib_portals();
369         LNetFini();
370 #endif
371 }