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