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