Whamcloud - gitweb
Branch:HEAD
[fs/lustre-release.git] / lustre / liblustre / llite_lib.c
index b11de88..bf006fa 100644 (file)
@@ -1,9 +1,9 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Lustre Light Super operations
+ * Lustre Light common routines
  *
- *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
+ *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
  *
  *   This file is part of Lustre, http://www.lustre.org.
  *
  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#define DEBUG_SUBSYSTEM S_LLITE
-
 #include <stdlib.h>
 #include <string.h>
-#include <error.h>
 #include <assert.h>
+#include <signal.h>
 #include <sys/types.h>
 #include <sys/queue.h>
 
 #include <inode.h>
 #include <file.h>
 
-#include <netinet/in.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
+#ifdef REDSTORM
+#define CSTART_INIT
+#endif
 
-#include <portals/api-support.h> /* needed for ptpctl.h */
-#include <portals/ptlctl.h>    /* needed for parse_dump */
+/* both sys/queue.h (libsysio require it) and portals/lists.h have definition
+ * of 'LIST_HEAD'. undef it to suppress warnings
+ */
+#undef LIST_HEAD
+#include <portals/ptlctl.h>
 
+#include "lutil.h"
 #include "llite_lib.h"
 
-
-ptl_handle_ni_t         tcpnal_ni;
-struct task_struct *current;
-struct obd_class_user_state ocus;
-
-/* portals interfaces */
-ptl_handle_ni_t *
-kportal_get_ni (int nal)
+static int lllib_init(void)
 {
-        return &tcpnal_ni;
-}
-
-inline void
-kportal_put_ni (int nal)
-{
-        return;
-}
+        liblustre_set_nal_nid();
 
-struct ldlm_namespace;
-struct ldlm_res_id;
-struct obd_import;
-
-extern int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, struct ldlm_res_id *res_id, int flags);
-extern int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int local_only);
-extern int ldlm_replay_locks(struct obd_import *imp);
+        if (liblustre_init_current("dummy") ||
+            init_obdclass() ||
+            init_lib_portals() ||
+            ptlrpc_init() ||
+            mdc_init() ||
+            lov_init() ||
+            osc_init())
+                return -1;
 
-void *inter_module_get(char *arg)
-{
-        if (!strcmp(arg, "tcpnal_ni"))
-                return &tcpnal_ni;
-        else if (!strcmp(arg, "ldlm_cli_cancel_unused"))
-                return ldlm_cli_cancel_unused;
-        else if (!strcmp(arg, "ldlm_namespace_cleanup"))
-                return ldlm_namespace_cleanup;
-        else if (!strcmp(arg, "ldlm_replay_locks"))
-                return ldlm_replay_locks;
-        else
-                return NULL;
+        return _sysio_fssw_register("llite", &llu_fssw_ops);
 }
+#ifndef CRAY_PORTALS
+#define LIBLUSTRE_NAL_NAME "tcp"
+#elif defined REDSTORM
+#define LIBLUSTRE_NAL_NAME "cray_qk_ernal"
+#else
+#define LIBLUSTRE_NAL_NAME "cray_pb_ernal"
+#endif
+
+int liblustre_process_log(struct config_llog_instance *cfg, int allow_recov)
+{
+        struct lustre_cfg lcfg;
+        char  *peer = "MDS_PEER_UUID";
+        struct obd_device *obd;
+        struct lustre_handle mdc_conn = {0, };
+        struct obd_export *exp;
+        char  *name = "mdc_dev";
+        class_uuid_t uuid;
+        struct obd_uuid mdc_uuid;
+        struct llog_ctxt *ctxt;
+        ptl_nid_t nid = 0;
+        int nal, err, rc = 0;
+        ENTRY;
+
+        generate_random_uuid(uuid);
+        class_uuid_unparse(uuid, &mdc_uuid);
+
+        if (ptl_parse_nid(&nid, g_zconf_mdsnid)) {
+                CERROR("Can't parse NID %s\n", g_zconf_mdsnid);
+                RETURN(-EINVAL);
+        }
 
-void init_current(char *comm)
-{ 
-        current = malloc(sizeof(*current));
-        current->fs = malloc(sizeof(*current->fs));
-        current->fs->umask = umask(0777);
-        umask(current->fs->umask);
-        strncpy(current->comm, comm, sizeof(current->comm));
-        current->pid = getpid();
-        current->fsuid = 0;
-        current->fsgid = 0;
-        current->cap_effective = 0;
-        memset(&current->pending, 0, sizeof(current->pending));
+        nal = ptl_name2nal(LIBLUSTRE_NAL_NAME);
+        if (nal <= 0) {
+                CERROR("Can't parse NAL %s\n", LIBLUSTRE_NAL_NAME);
+                RETURN(-EINVAL);
+        }
+        LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
+        lcfg.lcfg_nid = nid;
+        lcfg.lcfg_inllen1 = strlen(peer) + 1;
+        lcfg.lcfg_inlbuf1 = peer;
+        lcfg.lcfg_nal = nal;
+        err = class_process_config(&lcfg);
+        if (err < 0)
+                GOTO(out, err);
+
+        LCFG_INIT(lcfg, LCFG_ATTACH, name);
+        lcfg.lcfg_inlbuf1 = "mdc";
+        lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
+        lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
+        lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
+        err = class_process_config(&lcfg);
+        if (err < 0)
+                GOTO(out_del_uuid, err);
+
+        LCFG_INIT(lcfg, LCFG_SETUP, name);
+        lcfg.lcfg_inlbuf1 = g_zconf_mdsname;
+        lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
+        lcfg.lcfg_inlbuf2 = peer;
+        lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
+        err = class_process_config(&lcfg);
+        if (err < 0)
+                GOTO(out_detach, err);
+        
+        obd = class_name2obd(name);
+        if (obd == NULL)
+                GOTO(out_cleanup, err = -EINVAL);
+
+        /* Disable initial recovery on this import */
+        err = obd_set_info(obd->obd_self_export,
+                           strlen("initial_recov"), "initial_recov",
+                           sizeof(allow_recov), &allow_recov);
+
+        err = obd_connect(&mdc_conn, obd, &mdc_uuid, 0);
+        if (err) {
+                CERROR("cannot connect to %s: rc = %d\n",
+                        g_zconf_mdsname, err);
+                GOTO(out_cleanup, err);
+        }
+        
+        exp = class_conn2export(&mdc_conn);
+        
+        ctxt = exp->exp_obd->obd_llog_ctxt[LLOG_CONFIG_REPL_CTXT];
+        rc = class_config_process_llog(ctxt, g_zconf_profile, cfg);
+        if (rc)
+                CERROR("class_config_process_llog failed: rc = %d\n", rc);
+
+        err = obd_disconnect(exp, 0);
+
+out_cleanup:
+        LCFG_INIT(lcfg, LCFG_CLEANUP, name);
+        err = class_process_config(&lcfg);
+        if (err < 0)
+                GOTO(out, err);
+
+out_detach:
+        LCFG_INIT(lcfg, LCFG_DETACH, name);
+        err = class_process_config(&lcfg);
+        if (err < 0)
+                GOTO(out, err);
+
+out_del_uuid:
+        LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
+        lcfg.lcfg_inllen1 = strlen(peer) + 1;
+        lcfg.lcfg_inlbuf1 = peer;
+        err = class_process_config(&lcfg);
+
+out:
+        if (rc == 0)
+                rc = err;
+        
+        RETURN(rc);
 }
 
-ptl_nid_t tcpnal_mynid;
-
-int init_lib_portals()
+/* parse host:/mdsname/profile string */
+int ll_parse_mount_target(const char *target, char **mdsnid,
+                          char **mdsname, char **profile)
 {
-        int rc;
-
-        PtlInit();
-        rc = PtlNIInit(procbridge_interface, 0, 0, 0, &tcpnal_ni);
-        if (rc != 0) {
-                CERROR("ksocknal: PtlNIInit failed: error %d\n", rc);
-                PtlFini();
-                RETURN (rc);
+        static char buf[256];
+        char *s;
+
+        buf[255] = 0;
+        strncpy(buf, target, 255);
+
+        if ((s = strchr(buf, ':'))) {
+                *mdsnid = buf;
+                *s = '\0';
+                                                                                                                        
+                while (*++s == '/')
+                        ;
+                *mdsname = s;
+                if ((s = strchr(*mdsname, '/'))) {
+                        *s = '\0';
+                        *profile = s + 1;
+                        return 0;
+                }
         }
-        PtlNIDebug(tcpnal_ni, ~0);
-        return rc;
-}
-
-extern int class_handle_ioctl(struct obd_class_user_state *ocus, unsigned int cmd, unsigned long arg);
 
-struct mount_option_s mount_option = {NULL, NULL};
+        return -1;
+}
 
-/* FIXME simple arg parser FIXME */
-void parse_mount_options(void *arg)
+/*
+ * early liblustre init
+ * called from C startup in catamount apps, before main()
+ *
+ * The following is a skeleton sysio startup sequence,
+ * as implemented in C startup (skipping error handling).
+ * In this framework none of these calls need be made here
+ * or in the apps themselves.  The NAMESPACE_STRING specifying
+ * the initial set of fs ops (creates, mounts, etc.) is passed
+ * as an environment variable.
+ * 
+ *      _sysio_init();
+ *      _sysio_incore_init();
+ *      _sysio_native_init();
+ *      _sysio_lustre_init();
+ *      _sysio_boot(NAMESPACE_STRING);
+ *
+ * the name _sysio_lustre_init() follows the naming convention
+ * established in other fs drivers from libsysio:
+ *  _sysio_incore_init(), _sysio_native_init()
+ *
+ * _sysio_lustre_init() must be called before _sysio_boot()
+ * to enable libsysio's processing of namespace init strings containing
+ * lustre filesystem operations
+ */
+int _sysio_lustre_init(void)
 {
-        char *buf = NULL;
-        struct obd_ioctl_data *data;
-        char *ptr, *comma, *eq, **tgt, *v;
-        int len;
-
-        if (obd_ioctl_getdata(&buf, &len, arg)) {
-                CERROR("OBD ioctl: data error\n");
-                return;
-        }
-        data = (struct obd_ioctl_data *)buf;
-        ptr = data->ioc_inlbuf1;
-        printf("mount option: %s\n", ptr);
-
-        while (ptr) {
-                eq = strchr(ptr, '=');
-                if (!eq)
-                        return;
-
-                *eq = 0;
-                if (!strcmp("osc", ptr))
-                        tgt = &mount_option.osc_uuid;
-                else if (!strcmp("mdc", ptr))
-                        tgt = &mount_option.mdc_uuid;
-                else {
-                        printf("Unknown mount option %s\n", ptr);
-                        return;
-                }
+        int err;
 
-                v = eq + 1;
-                comma = strchr(v, ',');
-                if (comma) {
-                        *comma = 0;
-                        ptr = comma + 1;
-                } else
-                        ptr = NULL;
+#if 0
+        portal_debug = -1;
+        portal_subsystem_debug = -1;
+#endif
 
-                *tgt = malloc(strlen(v)+1);
-                strcpy(*tgt, v);
-        }
+        liblustre_init_random();
 
-        if (buf)
-                obd_ioctl_freedata(buf, len);
+        err = lllib_init();
+        if (err) {
+                perror("init llite driver");
+        }       
+        return err;
 }
 
-int lib_ioctl(int dev_id, int opc, void * ptr)
-{
-        int rc;
+/* env variables */
+#define ENV_LUSTRE_MNTPNT               "LIBLUSTRE_MOUNT_POINT"
+#define ENV_LUSTRE_MNTTGT               "LIBLUSTRE_MOUNT_TARGET"
+#define ENV_LUSTRE_TIMEOUT              "LIBLUSTRE_TIMEOUT"
+#define ENV_LUSTRE_DUMPFILE             "LIBLUSTRE_DUMPFILE"
+#define ENV_LUSTRE_DEBUG_MASK           "LIBLUSTRE_DEBUG_MASK"
+#define ENV_LUSTRE_DEBUG_SUBSYS         "LIBLUSTRE_DEBUG_SUBSYS"
 
-       if (dev_id == OBD_DEV_ID) {
-                struct obd_ioctl_data *ioc = ptr;
+extern int _sysio_native_init();
+extern unsigned int obd_timeout;
 
-                if (opc == OBD_IOC_MOUNTOPT) {
-                        parse_mount_options(ptr);
-                        return 0;
-                }
+static char *lustre_path = NULL;
 
-               rc = class_handle_ioctl(&ocus, opc, (unsigned long)ptr);
+/* global variables */
+char   *g_zconf_mdsname = NULL; /* mdsname, for zeroconf */
+char   *g_zconf_mdsnid = NULL;  /* mdsnid, for zeroconf */
+char   *g_zconf_profile = NULL; /* profile, for zeroconf */
 
-               /* you _may_ need to call obd_ioctl_unpack or some
-                  other verification function if you want to use ioc
-                  directly here */
-               printf ("processing ioctl cmd: %x buf len: %d, rc %d\n", 
-                       opc,  ioc->ioc_len, rc);
 
-                if (rc)
-                        return rc;
+void __liblustre_setup_(void)
+{
+        char *target = NULL;
+        char *timeout = NULL;
+        char *debug_mask = NULL;
+        char *debug_subsys = NULL;
+        char *root_driver = "native";
+        char *lustre_driver = "llite";
+        char *root_path = "/";
+        unsigned mntflgs = 0;
+       int err;
+
+       lustre_path = getenv(ENV_LUSTRE_MNTPNT);
+       if (!lustre_path) {
+                lustre_path = "/mnt/lustre";
        }
-       return (0);
-}
 
-int lllib_init(char *arg)
-{
-       tcpnal_mynid = ntohl(inet_addr(arg));
-        INIT_LIST_HEAD(&ocus.ocus_conns);
+        /* mount target */
+        target = getenv(ENV_LUSTRE_MNTTGT);
+        if (!target) {
+                printf("LibLustre: no mount target specified\n");
+                exit(1);
+        }
+        if (ll_parse_mount_target(target,
+                                  &g_zconf_mdsnid,
+                                  &g_zconf_mdsname,
+                                  &g_zconf_profile)) {
+                CERROR("mal-formed target %s \n", target);
+                exit(1);
+        }
+        if (!g_zconf_mdsnid || !g_zconf_mdsname || !g_zconf_profile) {
+                printf("Liblustre: invalid target %s\n", target);
+                exit(1);
+        }
+        printf("LibLustre: mount point %s, target %s\n",
+                lustre_path, target);
+
+        timeout = getenv(ENV_LUSTRE_TIMEOUT);
+        if (timeout) {
+                obd_timeout = (unsigned int) strtol(timeout, NULL, 0);
+                printf("LibLustre: set obd timeout as %u seconds\n",
+                        obd_timeout);
+        }
 
-        init_current("dummy");
-        if (init_obdclass() ||
-            init_lib_portals() ||
-            ptlrpc_init() ||
-            ldlm_init() ||
-            mdc_init() ||
-            lov_init() ||
-            osc_init())
-                return -1;
+        /* debug masks */
+        debug_mask = getenv(ENV_LUSTRE_DEBUG_MASK);
+        if (debug_mask)
+                portal_debug = (unsigned int) strtol(debug_mask, NULL, 0);
+
+        debug_subsys = getenv(ENV_LUSTRE_DEBUG_SUBSYS);
+        if (debug_subsys)
+                portal_subsystem_debug =
+                                (unsigned int) strtol(debug_subsys, NULL, 0);
+
+#ifndef CSTART_INIT
+        /* initialize libsysio & mount rootfs */
+       if (_sysio_init()) {
+               perror("init sysio");
+               exit(1);
+       }
+        _sysio_native_init();
 
-       if (parse_dump("/tmp/DUMP_FILE", lib_ioctl))
-                return -1;
+       err = _sysio_mount_root(root_path, root_driver, mntflgs, NULL);
+       if (err) {
+               perror(root_driver);
+               exit(1);
+       }
 
-        return _sysio_fssw_register("llite", &llu_fssw_ops);
+        if (_sysio_lustre_init())
+               exit(1);
+#endif
+
+        err = mount("/", lustre_path, lustre_driver, mntflgs, NULL);
+       if (err) {
+               errno = -err;
+               perror(lustre_driver);
+               exit(1);
+       }
 }
 
-/* FIXME */
-void generate_random_uuid(unsigned char uuid_out[16])
+void __liblustre_cleanup_(void)
 {
-        int *arr = (int*)uuid_out;
-        int i;
-
-        for (i = 0; i < sizeof(uuid_out)/sizeof(int); i++)
-                arr[i] = rand();
+        /* user app might chdir to a lustre directory, and leave busy pnode
+         * during finaly libsysio cleanup. here we chdir back to "/".
+         * but it can't fix the situation that liblustre is mounted
+         * at "/".
+         */
+        chdir("/");
+#if 0
+        umount(lustre_path);
+#endif
+        /* we can't call umount here, because libsysio will not cleanup
+         * opening files for us. _sysio_shutdown() will cleanup fds at
+         * first but which will also close the sockets we need for umount
+         * liblutre. this delima lead to another hack in
+         * libsysio/src/file_hack.c FIXME
+         */
+        _sysio_shutdown();
+        cleanup_lib_portals();
+        PtlFini();
 }
-