Whamcloud - gitweb
LU-657 test: limit the write size in run_dd
[fs/lustre-release.git] / lustre / liblustre / llite_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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 <liblustre.h>
50 #include <lnet/lnetctl.h>     /* needed for parse_dump */
51 #include <lustre_log.h>
52
53 #include "lutil.h"
54 #include "llite_lib.h"
55
56 int slp_global_init(void);
57
58 static int lllib_init(void)
59 {
60         if (liblustre_init_current("liblustre") ||
61             init_lib_portals() ||
62             init_obdclass() ||
63             ptlrpc_init() ||
64             mgc_init() ||
65             lmv_init() ||
66             mdc_init() ||
67             lov_init() ||
68             osc_init() ||
69             slp_global_init())
70                 return -1;
71
72         return _sysio_fssw_register("lustre", &llu_fssw_ops);
73 }
74
75 int liblustre_process_log(struct config_llog_instance *cfg,
76                           char *mgsnid, char *profile,
77                           int allow_recov)
78 {
79         struct lustre_cfg_bufs bufs;
80         struct lustre_cfg *lcfg;
81         char  *peer = "MGS_UUID";
82         struct obd_device *obd;
83         struct obd_export *exp;
84         char  *name = "mgc_dev";
85         class_uuid_t uuid;
86         struct obd_uuid mgc_uuid;
87         struct llog_ctxt *ctxt;
88         lnet_nid_t nid = 0;
89         char *mdsnid;
90         int err, rc = 0;
91         struct obd_connect_data *ocd = NULL;
92         ENTRY;
93
94         ll_generate_random_uuid(uuid);
95         class_uuid_unparse(uuid, &mgc_uuid);
96
97         nid = libcfs_str2nid(mgsnid);
98         if (nid == LNET_NID_ANY) {
99                 CERROR("Can't parse NID %s\n", mgsnid);
100                 RETURN(-EINVAL);
101         }
102
103         lustre_cfg_bufs_reset(&bufs, NULL);
104         lustre_cfg_bufs_set_string(&bufs, 1, peer);
105         lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
106         lcfg->lcfg_nid = nid;
107         rc = class_process_config(lcfg);
108         lustre_cfg_free(lcfg);
109         if (rc < 0)
110                 GOTO(out, rc);
111
112         lustre_cfg_bufs_reset(&bufs, name);
113         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGC_NAME);
114         lustre_cfg_bufs_set_string(&bufs, 2, mgc_uuid.uuid);
115         lcfg = lustre_cfg_new(LCFG_ATTACH, &bufs);
116         rc = class_process_config(lcfg);
117         lustre_cfg_free(lcfg);
118         if (rc < 0)
119                 GOTO(out_del_uuid, rc);
120
121         lustre_cfg_bufs_reset(&bufs, name);
122         lustre_cfg_bufs_set_string(&bufs, 1, LUSTRE_MGS_OBDNAME);
123         lustre_cfg_bufs_set_string(&bufs, 2, peer);
124         lcfg = lustre_cfg_new(LCFG_SETUP, &bufs);
125         rc = class_process_config(lcfg);
126         lustre_cfg_free(lcfg);
127         if (rc < 0)
128                 GOTO(out_detach, rc);
129
130         while ((mdsnid = strsep(&mgsnid, ","))) {
131                 nid = libcfs_str2nid(mdsnid);
132                 lustre_cfg_bufs_reset(&bufs, NULL);
133                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
134                 lcfg = lustre_cfg_new(LCFG_ADD_UUID, &bufs);
135                 lcfg->lcfg_nid = nid;
136                 rc = class_process_config(lcfg);
137                 lustre_cfg_free(lcfg);
138                 if (rc) {
139                         CERROR("Add uuid for %s failed %d\n",
140                                libcfs_nid2str(nid), rc);
141                         continue;
142                 }
143
144                 lustre_cfg_bufs_reset(&bufs, name);
145                 lustre_cfg_bufs_set_string(&bufs, 1, libcfs_nid2str(nid));
146                 lcfg = lustre_cfg_new(LCFG_ADD_CONN, &bufs);
147                 lcfg->lcfg_nid = nid;
148                 rc = class_process_config(lcfg);
149                 lustre_cfg_free(lcfg);
150                 if (rc) {
151                         CERROR("Add conn for %s failed %d\n",
152                                libcfs_nid2str(nid), rc);
153                         continue;
154                 }
155         }
156
157         obd = class_name2obd(name);
158         if (obd == NULL)
159                 GOTO(out_cleanup, rc = -EINVAL);
160
161         OBD_ALLOC(ocd, sizeof(*ocd));
162         if (ocd == NULL)
163                 GOTO(out_cleanup, rc = -ENOMEM);
164
165         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_AT |
166                                  OBD_CONNECT_FULL20;
167         ocd->ocd_version = LUSTRE_VERSION_CODE;
168
169         rc = obd_connect(NULL, &exp, obd, &mgc_uuid, ocd, NULL);
170         if (rc) {
171                 CERROR("cannot connect to %s at %s: rc = %d\n",
172                        LUSTRE_MGS_OBDNAME, mgsnid, rc);
173                 GOTO(out_cleanup, rc);
174         }
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(NULL, 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 static int mnt_retry = 0;
313
314 char *lustre_path = NULL;
315
316 void __liblustre_setup_(void)
317 {
318         char *target = NULL;
319         char *lustre_driver = "lustre";
320         unsigned mntflgs = 0;
321         int err, count;
322
323         lustre_path = getenv("LIBLUSTRE_MOUNT_POINT");
324         if (!lustre_path) {
325                 lustre_path = "/mnt/lustre";
326         }
327
328         target = getenv("LIBLUSTRE_MOUNT_RETRY");
329         if (target) {
330                 mnt_retry = atoi(target);
331                 if (mnt_retry < 0)
332                         mnt_retry = 0;
333         }
334
335         /* mount target */
336         target = getenv("LIBLUSTRE_MOUNT_TARGET");
337         if (!target) {
338                 printf("LibLustre: no mount target specified\n");
339                 exit(1);
340         }
341
342         CDEBUG(D_CONFIG, "LibLustre: mount point %s, target %s\n",
343                lustre_path, target);
344
345 #ifdef INIT_SYSIO
346         /* initialize libsysio & mount rootfs */
347         if (_sysio_init()) {
348                 perror("init sysio");
349                 exit(1);
350         }
351         _sysio_native_init();
352
353         err = _sysio_mount_root("/", "native", mntflgs, NULL);
354         if (err) {
355                 fprintf(stderr, "sysio mount failed: %s\n", strerror(errno));
356                 exit(1);
357         }
358
359         if (_sysio_lustre_init())
360                 exit(1);
361 #endif /* INIT_SYSIO */
362
363         count = mnt_retry;
364         do {
365                 err = mount(target, lustre_path, lustre_driver, mntflgs, NULL);
366                 if (err && mnt_retry && (-- count)) {
367                         fprintf(stderr, "Lustre mount failed: %s. "
368                                  "Will retry %d more times\n",
369                                 strerror(errno), mnt_retry - count );
370                         sleep(2);
371                 }
372         } while (err && count > 0);
373         if (err) {
374                 fprintf(stderr, "Lustre mount failed: %s\n", strerror(errno));
375                 exit(1);
376         }
377 }
378
379 void __liblustre_cleanup_(void)
380 {
381 #ifndef INIT_SYSIO
382         /* guard against being called multiple times */
383         static int cleaned = 0;
384
385         if (cleaned)
386                 return;
387         cleaned++;
388 #endif
389
390         /* user app might chdir to a lustre directory, and leave busy pnode
391          * during finaly libsysio cleanup. here we chdir back to "/".
392          * but it can't fix the situation that liblustre is mounted
393          * at "/".
394          */
395         if (!chdir("/")) {}
396 #if 0
397         umount(lustre_path);
398 #endif
399         /* we can't call umount here, because libsysio will not cleanup
400          * opening files for us. _sysio_shutdown() will cleanup fds at
401          * first but which will also close the sockets we need for umount
402          * liblutre. this dilema lead to another hack in
403          * libsysio/src/file_hack.c FIXME
404          */
405 #ifdef INIT_SYSIO
406         _sysio_shutdown();
407         cleanup_lib_portals();
408         LNetFini();
409 #endif
410 }