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