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