Whamcloud - gitweb
LU-11116 llog: error handling cleanup
[fs/lustre-release.git] / lustre / obdclass / llog_cat.c
index 084edea..59a0950 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2016, Intel Corporation.
+ * Copyright (c) 2012, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -213,8 +213,10 @@ out_destroy:
        loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
        /* this is to mimic full log, so another llog_cat_current_log()
         * can skip it and ask for another onet */
-       loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
+       loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(loghandle->lgh_hdr) + 1;
        llog_trans_destroy(env, loghandle, th);
+       if (handle != NULL)
+               dt_trans_stop(env, dt, handle);
        RETURN(rc);
 }
 
@@ -386,8 +388,7 @@ static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
 
                down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
                llh = loghandle->lgh_hdr;
-               LASSERT(llh);
-               if (!llog_is_full(loghandle))
+               if (llh == NULL || !llog_is_full(loghandle))
                        GOTO(out_unlock, loghandle);
                else
                        up_write(&loghandle->lgh_lock);
@@ -487,7 +488,12 @@ retry:
                        up_write(&loghandle->lgh_lock);
                        /* nobody should be trying to use this llog */
                        down_write(&cathandle->lgh_lock);
-                       if (cathandle->u.chd.chd_current_log == loghandle)
+                       /* only reset current log if still room in catalog, to
+                        * avoid unnecessarily and racy creation of new and
+                        * partially initialized llog_handle
+                        */
+                       if ((cathandle->u.chd.chd_current_log == loghandle) &&
+                           rc != -ENOSPC)
                                cathandle->u.chd.chd_current_log = NULL;
                        up_write(&cathandle->lgh_lock);
                        RETURN(rc);
@@ -997,6 +1003,28 @@ __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
 }
 EXPORT_SYMBOL(llog_cat_size);
 
+/* currently returns the number of "free" entries in catalog,
+ * ie the available entries for a new plain LLOG file creation,
+ * even if catalog has wrapped
+ */
+__u32 llog_cat_free_space(struct llog_handle *cat_llh)
+{
+       /* simulate almost full Catalog */
+       if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
+               return cfs_fail_val;
+
+       if (cat_llh->lgh_hdr->llh_count == 1)
+               return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
+
+       if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
+               return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
+                      cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
+
+       /* catalog is presently wrapped */
+       return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
+}
+EXPORT_SYMBOL(llog_cat_free_space);
+
 static int llog_cat_reverse_process_cb(const struct lu_env *env,
                                       struct llog_handle *cat_llh,
                                       struct llog_rec_hdr *rec, void *data)