Whamcloud - gitweb
LU-15481 llog: Add LLOG_SKIP_PLAIN to skip llog plain
[fs/lustre-release.git] / lustre / obdclass / llog_cat.c
index 1369f53..4f5b0b1 100644 (file)
@@ -27,7 +27,6 @@
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/obdclass/llog_cat.c
  *
@@ -97,13 +96,12 @@ static int llog_cat_new_log(const struct lu_env *env,
                if (cathandle->lgh_name == NULL) {
                        CWARN("%s: there are no more free slots in catalog "
                              DFID":%x\n",
-                             loghandle->lgh_ctxt->loc_obd->obd_name,
+                             loghandle2name(loghandle),
                              PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
                              cathandle->lgh_id.lgl_ogen);
                } else {
                        CWARN("%s: there are no more free slots in "
-                             "catalog %s\n",
-                             loghandle->lgh_ctxt->loc_obd->obd_name,
+                             "catalog %s\n", loghandle2name(loghandle),
                              cathandle->lgh_name);
                }
                RETURN(-ENOSPC);
@@ -162,7 +160,7 @@ static int llog_cat_new_log(const struct lu_env *env,
                GOTO(out, rc = 0);
        } else if (rc != 0) {
                CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
-                      loghandle->lgh_ctxt->loc_obd->obd_name, rc);
+                      loghandle2name(loghandle), rc);
                GOTO(out, rc);
        }
 
@@ -248,14 +246,16 @@ static int llog_cat_refresh(const struct lu_env *env,
                if (!llog_exist(loghandle))
                        continue;
 
+               down_write(&loghandle->lgh_lock);
                rc = llog_read_header(env, loghandle, NULL);
+               up_write(&loghandle->lgh_lock);
                if (rc)
                        goto unlock;
        }
 
        rc = llog_read_header(env, cathandle, NULL);
 unlock:
-       up_write(&loghandle->lgh_lock);
+       up_write(&cathandle->lgh_lock);
 
        return rc;
 }
@@ -391,7 +391,7 @@ int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
                    ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
                        if (cgl->lgl_ogen != logid->lgl_ogen) {
                                CWARN("%s: log "DFID" generation %x != %x\n",
-                                     loghandle->lgh_ctxt->loc_obd->obd_name,
+                                     loghandle2name(loghandle),
                                      PFID(&logid->lgl_oi.oi_fid),
                                      cgl->lgl_ogen, logid->lgl_ogen);
                                continue;
@@ -399,7 +399,7 @@ int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
                        *res = llog_handle_get(loghandle);
                        if (!*res) {
                                CERROR("%s: log "DFID" refcount is zero!\n",
-                                      loghandle->lgh_ctxt->loc_obd->obd_name,
+                                      loghandle2name(loghandle),
                                       PFID(&logid->lgl_oi.oi_fid));
                                continue;
                        }
@@ -414,12 +414,13 @@ int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
                       LLOG_OPEN_EXISTS);
        if (rc < 0) {
                CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name,
-                      PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
+                      loghandle2name(cathandle), PFID(&logid->lgl_oi.oi_fid),
+                      logid->lgl_ogen, rc);
                RETURN(rc);
        }
 
-       rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
+       rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN |
+                             LLOG_F_ZAP_WHEN_EMPTY | fmt, NULL);
        if (rc < 0) {
                llog_close(env, loghandle);
                *res = NULL;
@@ -461,8 +462,7 @@ int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
                        if (rc)
                                CERROR("%s: failure destroying log during "
                                       "cleanup: rc = %d\n",
-                                      loghandle->lgh_ctxt->loc_obd->obd_name,
-                                      rc);
+                                      loghandle2name(loghandle), rc);
 
                        index = loghandle->u.phd.phd_cookie.lgc_index;
                        llog_cat_cleanup(env, cathandle, NULL, index);
@@ -540,7 +540,7 @@ next:
         * meet this situation. */
        if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
                CERROR("%s: next log does not exist!\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name);
+                      loghandle2name(cathandle));
                loghandle = ERR_PTR(-EIO);
                if (cathandle->u.chd.chd_next_log == NULL) {
                        /* Store the error in chd_next_log, so
@@ -617,7 +617,7 @@ retry:
                if (retried++ == 0)
                        GOTO(retry, rc);
                CERROR("%s: error on 2nd llog: rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name, rc);
+                      loghandle2name(cathandle), rc);
        }
 
        RETURN(rc);
@@ -713,8 +713,7 @@ int llog_cat_cancel_arr_rec(const struct lu_env *env,
        rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
        if (rc) {
                CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
-                      ": rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name,
+                      ": rc = %d\n", loghandle2name(cathandle),
                       PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
                RETURN(rc);
        }
@@ -728,8 +727,7 @@ int llog_cat_cancel_arr_rec(const struct lu_env *env,
                 */
                rc = -ENOENT;
                CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
-                      ": rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name,
+                      ": rc = %d\n", loghandle2name(cathandle),
                       PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
 
                llog_handle_put(env, loghandle);
@@ -744,7 +742,7 @@ int llog_cat_cancel_arr_rec(const struct lu_env *env,
                rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
                if (rc)
                        CERROR("%s: fail to cancel catalog record: rc = %d\n",
-                              cathandle->lgh_ctxt->loc_obd->obd_name, rc);
+                              loghandle2name(cathandle), rc);
                rc = 0;
 
        }
@@ -752,8 +750,7 @@ int llog_cat_cancel_arr_rec(const struct lu_env *env,
 
        if (rc)
                CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name, count,
-                      rc);
+                      loghandle2name(cathandle), count, rc);
 
        RETURN(rc);
 }
@@ -789,8 +786,7 @@ int llog_cat_cancel_records(const struct lu_env *env,
        }
        if (failed)
                CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
-                      cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
-                      rc);
+                      loghandle2name(cathandle), failed, count, rc);
        RETURN(rc);
 }
 EXPORT_SYMBOL(llog_cat_cancel_records);
@@ -808,7 +804,7 @@ static int llog_cat_process_common(const struct lu_env *env,
        if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
                rc = -EINVAL;
                CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
-                     cat_llh->lgh_ctxt->loc_obd->obd_name,
+                     loghandle2name(cat_llh),
                      PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
                      cat_llh->lgh_id.lgl_ogen, rc);
                RETURN(rc);
@@ -828,7 +824,7 @@ static int llog_cat_process_common(const struct lu_env *env,
                        rc = LLOG_DEL_RECORD;
                else if (rc)
                        CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
-                             cat_llh->lgh_ctxt->loc_obd->obd_name,
+                             loghandle2name(cat_llh),
                              PFID(&lir->lid_id.lgl_oi.oi_fid),
                              lir->lid_id.lgl_ogen, rc);
 
@@ -840,11 +836,12 @@ static int llog_cat_process_common(const struct lu_env *env,
        hdr = (*llhp)->lgh_hdr;
        if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
            hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
-           *llhp != cat_llh->u.chd.chd_current_log) {
+           *llhp != cat_llh->u.chd.chd_current_log &&
+           *llhp != cat_llh->u.chd.chd_next_log) {
                rc = llog_destroy(env, *llhp);
                if (rc)
                        CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
-                             (*llhp)->lgh_ctxt->loc_obd->obd_name,
+                             loghandle2name((*llhp)),
                              PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
                rc = LLOG_DEL_PLAIN;
        }
@@ -871,6 +868,7 @@ static int llog_cat_process_cb(const struct lu_env *env,
        } else if (d->lpd_startidx > 0) {
                 struct llog_process_cat_data cd;
 
+                cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
                 cd.lpcd_first_idx = d->lpd_startidx;
                 cd.lpcd_last_idx = 0;
                rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
@@ -881,16 +879,25 @@ static int llog_cat_process_cb(const struct lu_env *env,
                rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
                                          NULL, false);
        }
+       if (rc == -ENOENT && (cat_llh->lgh_hdr->llh_flags & LLOG_F_RM_ON_ERR)) {
+               /*
+                * plain llog is reported corrupted, so better to just remove
+                * it if the caller is fine with that.
+                */
+               CERROR("%s: remove corrupted/missing llog "DFID"\n",
+                      loghandle2name(cat_llh),
+                      PFID(&llh->lgh_id.lgl_oi.oi_fid));
+               rc = LLOG_DEL_PLAIN;
+       }
 
 out:
        /* The empty plain log was destroyed while processing */
-       if (rc == LLOG_DEL_PLAIN) {
-               rc = llog_cat_cleanup(env, cat_llh, llh,
-                                     llh->u.phd.phd_cookie.lgc_index);
-       } else if (rc == LLOG_DEL_RECORD) {
+       if (rc == LLOG_DEL_PLAIN || rc == LLOG_DEL_RECORD)
                /* clear wrong catalog entry */
-               rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
-       }
+               rc = llog_cat_cleanup(env, cat_llh, llh, rec->lrh_index);
+       else if (rc == LLOG_SKIP_PLAIN)
+               /* processing callback ask to skip the llog -> continue */
+               rc = 0;
 
        if (llh)
                llog_handle_put(env, llh);
@@ -917,10 +924,12 @@ int llog_cat_process_or_fork(const struct lu_env *env,
 
        if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
            llh->llh_count > 1) {
-               struct llog_process_cat_data cd;
+               struct llog_process_cat_data cd = {
+                       .lpcd_read_mode = LLOG_READ_MODE_NORMAL
+               };
 
                CWARN("%s: catlog "DFID" crosses index zero\n",
-                     cat_llh->lgh_ctxt->loc_obd->obd_name,
+                     loghandle2name(cat_llh),
                      PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
                /*startcat = 0 is default value for general processing */
                if ((startcat != LLOG_CAT_FIRST &&
@@ -1054,6 +1063,9 @@ static int llog_cat_reverse_process_cb(const struct lu_env *env,
        } else if (rc == LLOG_DEL_RECORD) {
                /* clear wrong catalog entry */
                rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
+       } else if (rc == LLOG_SKIP_PLAIN) {
+               /* processing callback ask to skip the llog -> continue */
+               rc = 0;
        }
        if (rc)
                RETURN(rc);
@@ -1080,13 +1092,14 @@ int llog_cat_reverse_process(const struct lu_env *env,
         ENTRY;
 
         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
+       cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
         d.lpd_data = data;
         d.lpd_cb = cb;
 
        if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
            llh->llh_count > 1) {
                CWARN("%s: catalog "DFID" crosses index zero\n",
-                     cat_llh->lgh_ctxt->loc_obd->obd_name,
+                     loghandle2name(cat_llh),
                      PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
 
                cd.lpcd_first_idx = 0;
@@ -1130,7 +1143,7 @@ static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
 
                while (idx != cathandle->lgh_last_idx) {
                        idx = (idx + 1) % bitmap_size;
-                       if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
+                       if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
                                /* update llh_cat_idx for each unset bit,
                                 * expecting the next one is set */
                                llh->llh_cat_idx = idx;
@@ -1144,7 +1157,7 @@ static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
                        }
                }
 
-               CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
+               CDEBUG(D_HA, "catlog "DFID" first idx %u, last_idx %u\n",
                       PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
                       llh->llh_cat_idx, cathandle->lgh_last_idx);
        }
@@ -1157,17 +1170,20 @@ int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
                     struct llog_handle *loghandle, int index)
 {
        int rc;
+       struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
 
        LASSERT(index);
        if (loghandle != NULL) {
                /* remove destroyed llog from catalog list and
                 * chd_current_log variable */
+               fid = loghandle->lgh_id.lgl_oi.oi_fid;
                down_write(&cathandle->lgh_lock);
                if (cathandle->u.chd.chd_current_log == loghandle)
                        cathandle->u.chd.chd_current_log = NULL;
                list_del_init(&loghandle->u.phd.phd_entry);
                up_write(&cathandle->lgh_lock);
-               LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
+               LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index ||
+                       loghandle->u.phd.phd_cookie.lgc_index == 0);
                /* llog was opened and keep in a list, close it now */
                llog_close(env, loghandle);
        }
@@ -1180,7 +1196,9 @@ int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
        llog_cat_set_first_idx(cathandle, index);
        rc = llog_cancel_rec(env, cathandle, index);
        if (rc == 0)
-               CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
-                      index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
+               CDEBUG(D_HA,
+                      "cancel plain log "DFID" at index %u of catalog "DFID"\n",
+                      PFID(&fid), index,
+                      PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
        return rc;
 }