Whamcloud - gitweb
Revert "LU-462 Don't alloc/free client data for self export"
[fs/lustre-release.git] / lustre / mgs / mgs_handler.c
index 266c835..cf3a0ba 100644 (file)
@@ -129,11 +129,12 @@ static int mgs_disconnect(struct obd_export *exp)
 
         LASSERT(exp);
 
+        mgs_fsc_cleanup(exp);
+
         class_export_get(exp);
         mgs_counter_incr(exp, LPROC_MGS_DISCONNECT);
 
         rc = server_disconnect_export(exp);
-
         class_export_put(exp);
         RETURN(rc);
 }
@@ -196,6 +197,9 @@ static int mgs_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
                 GOTO(err_ops, rc = -EROFS);
         }
 
+        obd->u.obt.obt_magic = OBT_MAGIC;
+        obd->u.obt.obt_instance = 0;
+
         /* namespace for mgs llog */
         obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
                                                 LDLM_NAMESPACE_SERVER,
@@ -225,6 +229,7 @@ static int mgs_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
         /* Internal mgs setup */
         mgs_init_fsdb_list(obd);
         cfs_sema_init(&mgs->mgs_sem, 1);
+        mgs->mgs_start_time = cfs_time_current_sec();
 
         /* Setup proc */
         lprocfs_mgs_init_vars(&lvars);
@@ -324,52 +329,89 @@ static int mgs_cleanup(struct obd_device *obd)
         RETURN(0);
 }
 
-/* similar to filter_prepare_destroy */
-static int mgs_get_cfg_lock(struct obd_device *obd, char *fsname,
-                            struct lustre_handle *lockh)
+static int mgs_completion_ast_config(struct ldlm_lock *lock, int flags,
+                                     void *cbdata)
 {
-        struct ldlm_res_id res_id;
-        int rc, flags = 0;
         ENTRY;
 
-        rc = mgc_fsname2resid(fsname, &res_id);
-        if (!rc)
-                rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
-                                            LDLM_PLAIN, NULL, LCK_EX,
-                                            &flags, ldlm_blocking_ast,
-                                            ldlm_completion_ast, NULL,
-                                            fsname, 0, NULL, lockh);
-        if (rc)
-                CERROR("can't take cfg lock for %s (%d)\n", fsname, rc);
+        if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
+                       LDLM_FL_BLOCK_CONV))) {
+                struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
+                struct lustre_handle lockh;
 
-        RETURN(rc);
+                /* clear the bit before lock put */
+                cfs_clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
+
+                ldlm_lock2handle(lock, &lockh);
+                ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
+        }
+
+        RETURN(ldlm_completion_ast(lock, flags, cbdata));
 }
 
-static int mgs_put_cfg_lock(struct lustre_handle *lockh)
+static int mgs_completion_ast_ir(struct ldlm_lock *lock, int flags,
+                                 void *cbdata)
 {
         ENTRY;
-        ldlm_lock_decref(lockh, LCK_EX);
-        RETURN(0);
+
+        if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
+                       LDLM_FL_BLOCK_CONV))) {
+                struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
+                struct lustre_handle lockh;
+
+                mgs_ir_notify_complete(fsdb);
+
+                ldlm_lock2handle(lock, &lockh);
+                ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
+        }
+
+        RETURN(ldlm_completion_ast(lock, flags, cbdata));
 }
 
-void mgs_revoke_lock(struct obd_device *obd, struct fs_db *fsdb)
+void mgs_revoke_lock(struct obd_device *obd, struct fs_db *fsdb, int type)
 {
-        struct lustre_handle lockh;
-        int                  lockrc;
+        ldlm_completion_callback cp = NULL;
+        struct lustre_handle     lockh = { 0 };
+        struct ldlm_res_id       res_id;
+        int flags = LDLM_FL_ATOMIC_CB;
+        int rc;
+        ENTRY;
 
         LASSERT(fsdb->fsdb_name[0] != '\0');
+        rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
+        LASSERT(rc == 0);
+
+        switch (type) {
+        case CONFIG_T_CONFIG:
+                cp = mgs_completion_ast_config;
+                if (cfs_test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
+                        rc = -EALREADY;
+                break;
+        case CONFIG_T_RECOVER:
+                cp = mgs_completion_ast_ir;
+        default:
+                break;
+        }
 
-        if (cfs_test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags) == 0) {
-                lockrc = mgs_get_cfg_lock(obd, fsdb->fsdb_name, &lockh);
-                /* clear the bit before lock put */
-                cfs_clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
-
-                if (lockrc != ELDLM_OK)
-                        CERROR("lock error %d for fs %s\n",
-                               lockrc, fsdb->fsdb_name);
-                else
-                        mgs_put_cfg_lock(&lockh);
+        if (!rc) {
+                LASSERT(cp != NULL);
+                rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
+                                            LDLM_PLAIN, NULL, LCK_EX, &flags,
+                                            ldlm_blocking_ast, cp, NULL,
+                                            fsdb, 0, NULL, &lockh);
+                if (rc != ELDLM_OK) {
+                        CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
+                               le64_to_cpu(res_id.name[0]),
+                               le64_to_cpu(res_id.name[1]), rc);
+
+                        if (type == CONFIG_T_CONFIG)
+                                cfs_clear_bit(FSDB_REVOKING_LOCK,
+                                              &fsdb->fsdb_flags);
+                }
+                /* lock has been cancelled in completion_ast. */
         }
+
+        RETURN_EXIT;
 }
 
 /* rc=0 means ok
@@ -433,6 +475,7 @@ static int mgs_handle_target_reg(struct ptlrpc_request *req)
         struct obd_device *obd = req->rq_export->exp_obd;
         struct mgs_target_info *mti, *rep_mti;
         struct fs_db *fsdb;
+        int opc;
         int rc = 0;
         ENTRY;
 
@@ -440,6 +483,26 @@ static int mgs_handle_target_reg(struct ptlrpc_request *req)
 
         mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
 
+        opc = mti->mti_flags & LDD_F_OPC_MASK;
+        if (opc == LDD_F_OPC_READY) {
+                CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
+                       mti->mti_fsname, mti->mti_stripe_index);
+                rc = mgs_ir_update(obd, mti);
+                if (rc) {
+                        LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
+                        CERROR("Update IR return with %d(ignore and IR "
+                               "disabled)\n", rc);
+                }
+                GOTO(out_nolock, rc);
+        }
+
+        /* Do not support unregistering right now. */
+        if (opc != LDD_F_OPC_REG)
+                GOTO(out_nolock, rc = -EINVAL);
+
+        CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
+               mti->mti_fsname, mti->mti_stripe_index);
+
         if (mti->mti_flags & LDD_F_NEED_INDEX)
                 mti->mti_flags |= LDD_F_WRITECONF;
 
@@ -454,7 +517,8 @@ static int mgs_handle_target_reg(struct ptlrpc_request *req)
                         /* Nothing wrong, or fatal error */
                         GOTO(out_nolock, rc);
         } else {
-                if ((rc = mgs_check_failover_reg(mti)))
+                if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)
+                    && (rc = mgs_check_failover_reg(mti)))
                         GOTO(out_nolock, rc);
         }
 
@@ -527,12 +591,17 @@ static int mgs_handle_target_reg(struct ptlrpc_request *req)
         }
 
 out:
-        mgs_revoke_lock(obd, fsdb);
+        mgs_revoke_lock(obd, fsdb, CONFIG_T_CONFIG);
 
 out_nolock:
         CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
                mti->mti_stripe_index, rc);
         req->rq_status = rc;
+        if (rc)
+                /* we need an error flag to tell the target what's going on,
+                 * instead of just doing it by error code only. */
+                mti->mti_flags |= LDD_F_ERROR;
+
         rc = req_capsule_server_pack(&req->rq_pill);
         if (rc)
                 RETURN(rc);
@@ -580,6 +649,33 @@ static int mgs_set_info_rpc(struct ptlrpc_request *req)
         RETURN(rc);
 }
 
+static int mgs_config_read(struct ptlrpc_request *req)
+{
+        struct mgs_config_body *body;
+        int rc;
+        ENTRY;
+
+        body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
+        if (body == NULL)
+                RETURN(-EINVAL);
+
+        switch (body->mcb_type) {
+        case CONFIG_T_RECOVER:
+                rc = mgs_get_ir_logs(req);
+                break;
+
+        case CONFIG_T_CONFIG:
+                rc = -ENOTSUPP;
+                break;
+
+        default:
+                rc = -EINVAL;
+                break;
+        }
+
+        RETURN(rc);
+}
+
 /*
  * similar as in ost_connect_check_sptlrpc()
  */
@@ -651,6 +747,37 @@ static int mgs_handle_exception(struct ptlrpc_request *req)
         RETURN(0);
 }
 
+static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
+{
+        char *logname;
+        char fsname[16];
+        char *ptr;
+        int rc;
+
+        /* XXX: We suppose that llog at mgs is only used for
+         * fetching file system log */
+        logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
+        if (logname == NULL) {
+                CERROR("No logname, is llog on MGS used for something else?\n");
+                return -EINVAL;
+        }
+
+        ptr = strchr(logname, '-');
+        rc = (int)(ptr - logname);
+        if (ptr == NULL || rc >= sizeof(fsname)) {
+                CERROR("Invalid logname received: %s\n", logname);
+                return -EINVAL;
+        }
+
+        strncpy(fsname, logname, rc);
+        fsname[rc] = 0;
+        rc = mgs_fsc_attach(req->rq_export, fsname);
+        if (rc < 0 && rc != -EEXIST)
+                CERROR("add fs client %s returns %d\n", fsname, rc);
+
+        return rc;
+}
+
 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
 int mgs_handle(struct ptlrpc_request *req)
 {
@@ -720,7 +847,11 @@ int mgs_handle(struct ptlrpc_request *req)
                 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
                 rc = mgs_set_info_rpc(req);
                 break;
-
+        case MGS_CONFIG_READ:
+                DEBUG_REQ(D_MGS, req, "read config");
+                req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
+                rc = mgs_config_read(req);
+                break;
         case LDLM_ENQUEUE:
                 DEBUG_REQ(D_MGS, req, "enqueue");
                 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
@@ -748,6 +879,8 @@ int mgs_handle(struct ptlrpc_request *req)
                 DEBUG_REQ(D_MGS, req, "llog_init");
                 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
                 rc = llog_origin_handle_create(req);
+                if (rc == 0)
+                        (void)mgs_handle_fslog_hack(req);
                 break;
         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
                 DEBUG_REQ(D_MGS, req, "llog next block");
@@ -788,6 +921,12 @@ out:
 
 static inline int mgs_init_export(struct obd_export *exp)
 {
+        struct mgs_export_data *data = &exp->u.eu_mgs_data;
+
+        /* init mgs_export_data for fsc */
+        cfs_spin_lock_init(&data->med_lock);
+        CFS_INIT_LIST_HEAD(&data->med_clients);
+
         cfs_spin_lock(&exp->exp_lock);
         exp->exp_connecting = 1;
         cfs_spin_unlock(&exp->exp_lock);