Whamcloud - gitweb
LU-7988 hsm: run HSM coordinator once per second at most
[fs/lustre-release.git] / lustre / mdt / mdt_hsm_cdt_actions.c
index 137cb4a..abb3e6a 100644 (file)
@@ -23,7 +23,7 @@
  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
  *     alternatives
  *
- * Copyright (c) 2013, 2014, Intel Corporation.
+ * Copyright (c) 2013, 2016, Intel Corporation.
  */
 /*
  * lustre/mdt/mdt_hsm_cdt_actions.c
 
 #define DEBUG_SUBSYSTEM S_MDS
 
+#include <libcfs/libcfs.h>
+#include <libcfs/libcfs_hash.h>
 #include <obd_support.h>
-#include <lustre_net.h>
 #include <lustre_export.h>
 #include <obd.h>
 #include <lprocfs_status.h>
 #include <lustre_log.h>
 #include "mdt_internal.h"
 
+struct cdt_agent_record_loc {
+       struct hlist_node carl_hnode;
+       atomic_t carl_refcount;
+       u64 carl_cookie;
+       u32 carl_cat_idx;
+       u32 carl_rec_idx;
+};
+
+static inline void cdt_agent_record_loc_get(struct cdt_agent_record_loc *carl)
+{
+       LASSERT(atomic_read(&carl->carl_refcount) > 0);
+       atomic_inc(&carl->carl_refcount);
+}
+
+static inline void cdt_agent_record_loc_put(struct cdt_agent_record_loc *carl)
+{
+       LASSERT(atomic_read(&carl->carl_refcount) > 0);
+       if (atomic_dec_and_test(&carl->carl_refcount))
+               OBD_FREE_PTR(carl);
+}
+
+static unsigned int
+cdt_agent_record_hash(struct cfs_hash *hs, const void *key, unsigned int mask)
+{
+       return cfs_hash_djb2_hash(key, sizeof(u64), mask);
+}
+
+static void *cdt_agent_record_object(struct hlist_node *hnode)
+{
+       return hlist_entry(hnode, struct cdt_agent_record_loc, carl_hnode);
+}
+
+static void *cdt_agent_record_key(struct hlist_node *hnode)
+{
+       struct cdt_agent_record_loc *carl = cdt_agent_record_object(hnode);
+
+       return &carl->carl_cookie;
+}
+
+static int cdt_agent_record_keycmp(const void *key, struct hlist_node *hnode)
+{
+       const u64 *cookie2 = cdt_agent_record_key(hnode);
+
+       return *(const u64 *)key == *cookie2;
+}
+
+static void cdt_agent_record_get(struct cfs_hash *hs, struct hlist_node *hnode)
+{
+       struct cdt_agent_record_loc *carl = cdt_agent_record_object(hnode);
+
+       cdt_agent_record_loc_get(carl);
+}
+
+static void cdt_agent_record_put(struct cfs_hash *hs, struct hlist_node *hnode)
+{
+       struct cdt_agent_record_loc *carl = cdt_agent_record_object(hnode);
+
+       cdt_agent_record_loc_put(carl);
+}
+
+struct cfs_hash_ops cdt_agent_record_hash_ops = {
+       .hs_hash        = cdt_agent_record_hash,
+       .hs_key         = cdt_agent_record_key,
+       .hs_keycmp      = cdt_agent_record_keycmp,
+       .hs_object      = cdt_agent_record_object,
+       .hs_get         = cdt_agent_record_get,
+       .hs_put_locked  = cdt_agent_record_put,
+};
+
+void cdt_agent_record_hash_add(struct coordinator *cdt, u64 cookie, u32 cat_idx,
+                              u32 rec_idx)
+{
+       struct cdt_agent_record_loc *carl0;
+       struct cdt_agent_record_loc *carl1;
+
+       OBD_ALLOC_PTR(carl1);
+       if (carl1 == NULL)
+               return;
+
+       INIT_HLIST_NODE(&carl1->carl_hnode);
+       atomic_set(&carl1->carl_refcount, 1);
+       carl1->carl_cookie = cookie;
+       carl1->carl_cat_idx = cat_idx;
+       carl1->carl_rec_idx = rec_idx;
+
+       carl0 = cfs_hash_findadd_unique(cdt->cdt_agent_record_hash,
+                                       &carl1->carl_cookie,
+                                       &carl1->carl_hnode);
+
+       LASSERT(carl0->carl_cookie == carl1->carl_cookie);
+       LASSERT(carl0->carl_cat_idx == carl1->carl_cat_idx);
+       LASSERT(carl0->carl_rec_idx == carl1->carl_rec_idx);
+
+       if (carl0 != carl1)
+               cdt_agent_record_loc_put(carl0);
+
+       cdt_agent_record_loc_put(carl1);
+}
+
+void cdt_agent_record_hash_lookup(struct coordinator *cdt, u64 cookie,
+                                 u32 *cat_idx, u32 *rec_idx)
+{
+       struct cdt_agent_record_loc *carl;
+
+       carl = cfs_hash_lookup(cdt->cdt_agent_record_hash, &cookie);
+       if (carl != NULL) {
+               LASSERT(carl->carl_cookie == cookie);
+               *cat_idx = carl->carl_cat_idx;
+               *rec_idx = carl->carl_rec_idx;
+               cdt_agent_record_loc_put(carl);
+       } else {
+               *cat_idx = 0;
+               *rec_idx = 0;
+       }
+}
+
+void cdt_agent_record_hash_del(struct coordinator *cdt, u64 cookie)
+{
+       cfs_hash_del_key(cdt->cdt_agent_record_hash, &cookie);
+}
+
 void dump_llog_agent_req_rec(const char *prefix,
                             const struct llog_agent_req_rec *larr)
 {
@@ -53,10 +175,10 @@ void dump_llog_agent_req_rec(const char *prefix,
        sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
        CDEBUG(D_HSM, "%slrh=[type=%X len=%d idx=%d] fid="DFID
               " dfid="DFID
-              " compound/cookie="LPX64"/"LPX64
-              " status=%s action=%s archive#=%d flags="LPX64
-              " create="LPU64" change="LPU64
-              " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
+              " compound/cookie=%#llx/%#llx"
+              " status=%s action=%s archive#=%d flags=%#llx"
+              " create=%llu change=%llu"
+              " extent=%#llx-%#llx gid=%#llx datalen=%d"
               " data=[%s]\n",
               prefix,
               larr->arr_hdr.lrh_type,
@@ -81,11 +203,15 @@ void dump_llog_agent_req_rec(const char *prefix,
  * \param mdt [IN] MDT device
  * \param cb [IN] llog callback funtion
  * \param data [IN] llog callback  data
+ * \param rw [IN] cdt_llog_lock mode (READ or WRITE)
+ * \param start_cat_idx first catalog index to examine
+ * \param start_rec_idx first record index to examine
  * \retval 0 success
  * \retval -ve failure
  */
 int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
-                    llog_cb_t cb, void *data)
+                    llog_cb_t cb, void *data, u32 start_cat_idx,
+                    u32 start_rec_idx, int rw)
 {
        struct obd_device       *obd = mdt2obd_dev(mdt);
        struct llog_ctxt        *lctxt = NULL;
@@ -97,9 +223,13 @@ int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
        if (lctxt == NULL || lctxt->loc_handle == NULL)
                RETURN(-ENOENT);
 
-       mutex_lock(&cdt->cdt_llog_lock);
+       if (rw == READ)
+               down_read(&cdt->cdt_llog_lock);
+       else
+               down_write(&cdt->cdt_llog_lock);
 
-       rc = llog_cat_process(env, lctxt->loc_handle, cb, data, 0, 0);
+       rc = llog_cat_process(env, lctxt->loc_handle, cb, data, start_cat_idx,
+                             start_rec_idx);
        if (rc < 0)
                CERROR("%s: failed to process HSM_ACTIONS llog (rc=%d)\n",
                        mdt_obd_name(mdt), rc);
@@ -107,7 +237,12 @@ int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
                rc = 0;
 
        llog_ctxt_put(lctxt);
-       mutex_unlock(&cdt->cdt_llog_lock);
+
+       if (rw == READ)
+               up_read(&cdt->cdt_llog_lock);
+       else
+               up_write(&cdt->cdt_llog_lock);
+
        RETURN(rc);
 }
 
@@ -152,21 +287,23 @@ int mdt_agent_record_add(const struct lu_env *env,
        if (lctxt == NULL || lctxt->loc_handle == NULL)
                GOTO(free, rc = -ENOENT);
 
-       mutex_lock(&cdt->cdt_llog_lock);
+       down_write(&cdt->cdt_llog_lock);
 
        /* in case of cancel request, the cookie is already set to the
         * value of the request cookie to be cancelled
         * so we do not change it */
-       if (hai->hai_action != HSMA_CANCEL) {
+       if (hai->hai_action == HSMA_CANCEL) {
+               larr->arr_hai.hai_cookie = hai->hai_cookie;
+       } else {
                cdt->cdt_last_cookie++;
-               hai->hai_cookie = cdt->cdt_last_cookie;
+               larr->arr_hai.hai_cookie = cdt->cdt_last_cookie;
        }
-       larr->arr_hai.hai_cookie = hai->hai_cookie;
+
        rc = llog_cat_add(env, lctxt->loc_handle, &larr->arr_hdr, NULL);
        if (rc > 0)
                rc = 0;
 
-       mutex_unlock(&cdt->cdt_llog_lock);
+       up_write(&cdt->cdt_llog_lock);
        llog_ctxt_put(lctxt);
 
        EXIT;
@@ -228,7 +365,7 @@ static int mdt_agent_record_update_cb(const struct lu_env *env,
 
        rc = 0;
        for (i = 0 ; i < ducb->cookies_count ; i++) {
-               CDEBUG(D_HSM, "%s: search "LPX64", found "LPX64"\n",
+               CDEBUG(D_HSM, "%s: search %#llx, found %#llx\n",
                       mdt_obd_name(ducb->mdt), ducb->cookies[i],
                       larr->arr_hai.hai_cookie);
                if (larr->arr_hai.hai_cookie == ducb->cookies[i]) {
@@ -263,9 +400,37 @@ int mdt_agent_record_update(const struct lu_env *env, struct mdt_device *mdt,
                            enum agent_req_status status)
 {
        struct data_update_cb    ducb;
-       int                      rc;
+       u32 start_cat_idx = -1;
+       u32 start_rec_idx = -1;
+       u32 cat_idx;
+       u32 rec_idx;
+       int i;
+       int rc;
        ENTRY;
 
+       /* Find the first location (start_cat_idx, start_rec_idx)
+        * among the records corresponding to cookies. */
+       for (i = 0; i < cookies_count; i++) {
+               /* If we cannot find a cached location for a cookie
+                * (perhaps because the MDT was restart then we must
+                * start from the beginning. In this case
+                * mdt_agent_record_hash_get() sets both of cat_idx and
+                * rec_idx to 0. */
+               cdt_agent_record_hash_lookup(&mdt->mdt_coordinator, cookies[i],
+                                            &cat_idx, &rec_idx);
+               if (cat_idx < start_cat_idx) {
+                       start_cat_idx = cat_idx;
+                       start_rec_idx = rec_idx;
+               } else if (cat_idx == start_cat_idx &&
+                          rec_idx < start_rec_idx) {
+                       start_rec_idx = rec_idx;
+               }
+       }
+
+       /* Fixup starting record index for llog_cat_process(). */
+       if (start_rec_idx != 0)
+               start_rec_idx -= 1;
+
        ducb.mdt = mdt;
        ducb.cookies = cookies;
        ducb.cookies_count = cookies_count;
@@ -273,7 +438,8 @@ int mdt_agent_record_update(const struct lu_env *env, struct mdt_device *mdt,
        ducb.status = status;
        ducb.change_time = cfs_time_current_sec();
 
-       rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb);
+       rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb,
+                             start_cat_idx, start_rec_idx, WRITE);
        if (rc < 0)
                CERROR("%s: cdt_llog_process() failed, rc=%d, cannot update "
                       "status to %s for %d cookies, done %d\n",
@@ -329,7 +495,7 @@ static void *mdt_hsm_actions_proc_start(struct seq_file *s, loff_t *pos)
                RETURN(ERR_PTR(-ENOENT));
        }
 
-       CDEBUG(D_HSM, "llog successfully initialized, start from "LPD64"\n",
+       CDEBUG(D_HSM, "llog successfully initialized, start from %lld\n",
               *pos);
        /* first call = rewind */
        if (*pos == 0) {
@@ -363,7 +529,6 @@ static int hsm_actions_show_cb(const struct lu_env *env,
        struct seq_file              *s = data;
        struct agent_action_iterator *aai;
        int                           sz;
-       size_t                        count;
        char                          buf[12];
        ENTRY;
 
@@ -379,13 +544,12 @@ static int hsm_actions_show_cb(const struct lu_env *env,
                     hdr->lrh_index <= aai->aai_index))
                RETURN(0);
 
-       count = s->count;
        sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
        seq_printf(s, "lrh=[type=%X len=%d idx=%d/%d] fid="DFID
-                  " dfid="DFID" compound/cookie="LPX64"/"LPX64
-                  " action=%s archive#=%d flags="LPX64
-                  " extent="LPX64"-"LPX64
-                  " gid="LPX64" datalen=%d status=%s data=[%s]\n",
+                  " dfid="DFID" compound/cookie=%#llx/%#llx"
+                  " action=%s archive#=%d flags=%#llx"
+                  " extent=%#llx-%#llx"
+                  " gid=%#llx datalen=%d status=%s data=[%s]\n",
                   hdr->lrh_type, hdr->lrh_len,
                   llh->lgh_hdr->llh_cat_idx, hdr->lrh_index,
                   PFID(&larr->arr_hai.hai_fid),
@@ -426,11 +590,11 @@ static int mdt_hsm_actions_proc_show(struct seq_file *s, void *v)
        if (aai->aai_eof)
                RETURN(0);
 
-       mutex_lock(&cdt->cdt_llog_lock);
+       down_read(&cdt->cdt_llog_lock);
        rc = llog_cat_process(&aai->aai_env, aai->aai_ctxt->loc_handle,
                              hsm_actions_show_cb, s,
                              aai->aai_cat_index, aai->aai_index);
-       mutex_unlock(&cdt->cdt_llog_lock);
+       up_read(&cdt->cdt_llog_lock);
        if (rc == 0) /* all llog parsed */
                aai->aai_eof = true;
        if (rc == LLOG_PROC_BREAK) /* buffer full */