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