Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/liblustre/llite_lib.c
37  *
38  * Lustre Light common routines
39  */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <assert.h>
44 #include <signal.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/queue.h>
48
49 #include <sysio.h>
50 #ifdef HAVE_XTIO_H
51 #include <xtio.h>
52 #endif
53 #include <fs.h>
54 #include <mount.h>
55 #include <inode.h>
56 #ifdef HAVE_FILE_H
57 #include <file.h>
58 #endif
59
60 /* both sys/queue.h (libsysio require it) and portals/lists.h have definition
61  * of 'LIST_HEAD'. undef it to suppress warnings
62  */
63 #undef LIST_HEAD
64 #include <liblustre.h>
65 #include <lnet/lnetctl.h>     /* needed for parse_dump */
66
67 #include "lutil.h"
68 #include "llite_lib.h"
69
70 static int lllib_init(void)
71 {
72         if (liblustre_init_current("liblustre") ||
73             init_lib_portals() ||
74             init_obdclass() ||
75             ptlrpc_init() ||
76             mgc_init() ||
77             mdc_init() ||
78             lov_init() ||
79             osc_init())
80                 return -1;
81
82         return _sysio_fssw_register("lustre", &llu_fssw_ops);
83 }
84
85 int liblustre_process_log(struct config_llog_instance *cfg,
86                           char *mgsnid, char *profile,
87                           int allow_recov)
88 {
89         struct lustre_cfg_bufs bufs;
90         struct lustre_cfg *lcfg;
91         char  *peer = "MGS_UUID";
92         struct obd_device *obd;
93         struct lustre_handle mgc_conn = {0, };
94         struct obd_export *exp;
95         char  *name = "mgc_dev";
96         class_uuid_t uuid;
97         struct obd_uuid mgc_uuid;
98         struct llog_ctxt *ctxt;
99         lnet_nid_t nid = 0;
100         char *mdsnid;
101         int err, rc = 0;
102         struct obd_connect_data *ocd = NULL;
103         ENTRY;
104
105         ll_generate_random_uuid(uuid);
106         class_uuid_unparse(uuid, &mgc_uuid);
107
108         nid = libcfs_str2nid(mgsnid);
109         if (nid == LNET_NID_ANY) {
110                 CERROR("Can't parse NID %s\n", mgsnid);
111                 RETURN(-EINVAL);
112         }
113
114         lustre_cfg_bufs_reset(&bufs, NULL);
115         lustre_cfg_bufs_set_string(&bufs, 1, peer);
116         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
117         lcfg->lcfg_nid = nid;
118         rc = class_process_config(lcfg);
119         lustre_cfg_free(lcfg);
120         if (rc < 0)
121                 GOTO(out, rc);
122
123         lustre_cfg_bufs_reset(&bufs, name);
124         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
125         lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
126         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
127         rc = class_process_config(lcfg);
128         lustre_cfg_free(lcfg);
129         if (rc < 0)
130                 GOTO(out_del_uuid, rc);
131
132         lustre_cfg_bufs_reset(&bufs, name);
133         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
134         lustre_cfg_bufs_set_string(&bufs, 2, peer);
135         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
136         rc = class_process_config(lcfg);
137         lustre_cfg_free(lcfg);
138         if (rc < 0)
139                 GOTO(out_detach, rc);
140
141         while ((mdsnid = strsep(&mgsnid, ","))) {
142                 nid = libcfs_str2nid(mdsnid);
143                 lustre_cfg_bufs_reset(&bufs, NULL);
144                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
145                 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
146                 lcfg->lcfg_nid = nid;
147                 rc = class_process_config(lcfg);
148                 lustre_cfg_free(lcfg);
149                 if (rc) {
150                         CERROR("Add uuid for %s failed %d\n",
151                                libcfs_nid2str(nid), rc);
152                         continue;
153                 }
154
155                 lustre_cfg_bufs_reset(&bufs, name);
156                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
157                 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
158                 lcfg->lcfg_nid = nid;
159                 rc = class_process_config(lcfg);
160                 lustre_cfg_free(lcfg);
161                 if (rc) {
162                         CERROR("Add conn for %s failed %d\n",
163                                libcfs_nid2str(nid), rc);
164                         continue;
165                 }
166         }
167
168         obd = class_name2obd(name);
169         if (obd == NULL)
170                 GOTO(out_cleanup, rc = -EINVAL);
171
172         OBD_ALLOC(ocd, sizeof(*ocd));
173         if (ocd == NULL)
174                 GOTO(out_cleanup, rc = -ENOMEM);
175
176         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
177                                  OBD_CONNECT_VBR;
178 #ifdef LIBLUSTRE_POSIX_ACL
179         ocd->ocd_connect_flags |= OBD_CONNECT_ACL;
180 #endif
181         ocd->ocd_version = LUSTRE_VERSION_CODE;
182
183         rc = obd_connect(&mgc_conn, obd, &mgc_uuid, ocd, NULL);
184         if (rc) {
185                 CERROR("cannot connect to %s at %s: rc = %d\n",
186                        LUSTRE_MGS_OBDNAME, mgsnid, rc);
187                 GOTO(out_cleanup, rc);
188         }
189
190         exp = class_conn2export(&mgc_conn);
191
192         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
193         cfg->cfg_flags |= CFG_F_COMPAT146;
194         rc = class_config_parse_llog(ctxt, profile, cfg);
195         llog_ctxt_put(ctxt);
196         if (rc) {
197                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
198         }
199
200         /* We don't so much care about errors in cleaning up the config llog
201          * connection, as we have already read the config by this point. */
202         err = obd_disconnect(exp);
203         if (err)
204                 CERROR("obd_disconnect failed: rc = %d\n", err);
205
206 out_cleanup:
207         if (ocd)
208                 OBD_FREE(ocd, sizeof(*ocd));
209
210         lustre_cfg_bufs_reset(&bufs, name);
211         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
212         err = class_process_config(lcfg);
213         lustre_cfg_free(lcfg);
214         if (err)
215                 CERROR("mdc_cleanup failed: rc = %d\n", err);
216
217 out_detach:
218         lustre_cfg_bufs_reset(&bufs, name);
219         lcfg = lustre_cfg_new(LCFG_DETACH, &bufs);
220         err = class_process_config(lcfg);
221         lustre_cfg_free(lcfg);
222         if (err)
223                 CERROR("mdc_detach failed: rc = %d\n", err);
224
225 out_del_uuid:
226         lustre_cfg_bufs_reset(&bufs, name);
227         lustre_cfg_bufs_set_string(&bufs, 1, peer);
228         lcfg = lustre_cfg_new(LCFG_DEL_UUID, &bufs);
229         err = class_process_config(lcfg);
230         if (err)
231                 CERROR("del MDC UUID failed: rc = %d\n", err);
232         lustre_cfg_free(lcfg);
233 out:
234
235         RETURN(rc);
236 }
237
238 /* parse host:/fsname string */
239 int ll_parse_mount_target(const char *target, char **mgsnid,
240                           char **fsname)
241 {
242         static char buf[256];
243         char *s;
244
245         buf[255] = 0;
246         strncpy(buf, target, 255);
247
248         if ((s = strchr(buf, ':'))) {
249                 *mgsnid = buf;
250                 *s = '\0';
251
252                 while (*++s == '/')
253                         ;
254                 sprintf(s + strlen(s), "-client");
255                 *fsname = s;
256
257                 return 0;
258         }
259
260         return -1;
261 }
262
263 /*
264  * early liblustre init
265  * called from C startup in catamount apps, before main()
266  *
267  * The following is a skeleton sysio startup sequence,
268  * as implemented in C startup (skipping error handling).
269  * In this framework none of these calls need be made here
270  * or in the apps themselves.  The NAMESPACE_STRING specifying
271  * the initial set of fs ops (creates, mounts, etc.) is passed
272  * as an environment variable.
273  *
274  *      _sysio_init();
275  *      _sysio_incore_init();
276  *      _sysio_native_init();
277  *      _sysio_lustre_init();
278  *      _sysio_boot(NAMESPACE_STRING);
279  *
280  * the name _sysio_lustre_init() follows the naming convention
281  * established in other fs drivers from libsysio:
282  *  _sysio_incore_init(), _sysio_native_init()
283  *
284  * _sysio_lustre_init() must be called before _sysio_boot()
285  * to enable libsysio's processing of namespace init strings containing
286  * lustre filesystem operations
287  */
288 int _sysio_lustre_init(void)
289 {
290         int err;
291         char *envstr;
292 #ifndef INIT_SYSIO
293         extern void __liblustre_cleanup_(void);
294 #endif
295
296         liblustre_init_random();
297
298         err = lllib_init();
299         if (err) {
300                 perror("init llite driver");
301                 return err;
302         }
303
304         envstr = getenv("LIBLUSTRE_TIMEOUT");
305         if (envstr != NULL) {
306                 obd_timeout = (unsigned int)strtol(envstr, NULL, 0);
307                 printf("LibLustre: obd timeout=%u seconds\n",
308                         obd_timeout);
309         }
310
311         /* debug peer on timeout? */
312         envstr = getenv("LIBLUSTRE_DEBUG_PEER_ON_TIMEOUT");
313         if (envstr != NULL) {
314                 obd_debug_peer_on_timeout = 
315                         (unsigned int)strtol(envstr, NULL, 0);
316                 printf("LibLustre: debug peer on timeout=%d\n",
317                         obd_debug_peer_on_timeout ? 0 : 1);
318         }
319
320 #ifndef INIT_SYSIO
321         (void)atexit(__liblustre_cleanup_);
322 #endif
323         return err;
324 }
325
326 extern int _sysio_native_init();
327
328 static int mnt_retry = 0;
329
330 char *lustre_path = NULL;
331
332 void __liblustre_setup_(void)
333 {
334         char *target = NULL;
335         char *lustre_driver = "lustre";
336         unsigned mntflgs = 0;
337         int err, count;
338
339         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
340         if (!lustre_path) {
341                 lustre_path = "/mnt/lustre";
342         }
343
344         target = getenv("LIBLUSTRE_MOUNT_RETRY");
345         if (target) {
346                 mnt_retry = atoi(target);
347                 if (mnt_retry < 0)
348                         mnt_retry = 0;
349         }
350
351         /* mount target */
352         target = getenv("LIBLUSTRE_MOUNT_TARGET");
353         if (!target) {
354                 printf("LibLustre: no mount target specified\n");
355                 exit(1);
356         }
357
358         CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
359                lustre_path, target);
360
361 #ifdef INIT_SYSIO
362         /* initialize libsysio & mount rootfs */
363         if (_sysio_init()) {
364                 perror("init sysio");
365                 exit(1);
366         }
367         _sysio_native_init();
368
369         err = _sysio_mount_root("/", "native", mntflgs, NULL);
370         if (err) {
371                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
372                 exit(1);
373         }
374
375         if (_sysio_lustre_init())
376                 exit(1);
377 #endif /* INIT_SYSIO */
378
379         count = mnt_retry;
380         do {
381                 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
382                 if (err && mnt_retry && (-- count)) {
383                         fprintf(stderr, "Lustre mount failed: %s. "
384                                  "Will retry %d more times\n",
385                                 strerror(errno), mnt_retry - count );
386                         sleep(2);
387                 }
388         } while (err && count > 0);
389         if (err) {
390                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
391                 exit(1);
392         }
393 }
394
395 void __liblustre_cleanup_(void)
396 {
397 #ifndef INIT_SYSIO
398         /* guard against being called multiple times */
399         static int cleaned = 0;
400
401         if (cleaned)
402                 return;
403         cleaned++;
404 #endif
405
406         /* user app might chdir to a lustre directory, and leave busy pnode
407          * during finaly libsysio cleanup. here we chdir back to "/".
408          * but it can't fix the situation that liblustre is mounted
409          * at "/".
410          */
411         chdir("/");
412 #if 0
413         umount(lustre_path);
414 #endif
415         /* we can't call umount here, because libsysio will not cleanup
416          * opening files for us. _sysio_shutdown() will cleanup fds at
417          * first but which will also close the sockets we need for umount
418          * liblutre. this dilema lead to another hack in
419          * libsysio/src/file_hack.c FIXME
420          */
421 #ifdef INIT_SYSIO
422         _sysio_shutdown();
423         cleanup_lib_portals();
424         LNetFini();
425 #endif
426 }