+static int llog_cat_refresh(const struct lu_env *env,
+ struct llog_handle *cathandle)
+{
+ struct llog_handle *loghandle;
+ int rc;
+
+ down_write(&cathandle->lgh_lock);
+ list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
+ u.phd.phd_entry) {
+ 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(&cathandle->lgh_lock);
+
+ return rc;
+}
+
+/*
+ * prepare current/next log for catalog.
+ *
+ * if \a *ploghandle is NULL, open it, and declare create, NB, if \a
+ * *ploghandle is remote, create it synchronously here, see comments
+ * below.
+ *
+ * \a cathandle->lgh_lock is down_read-ed, it gets down_write-ed if \a
+ * *ploghandle has to be opened.
+ */
+static int llog_cat_prep_log(const struct lu_env *env,
+ struct llog_handle *cathandle,
+ struct llog_handle **ploghandle,
+ struct thandle *th)
+{
+ int rc;
+ int sem_upgraded;
+
+start:
+ rc = 0;
+ sem_upgraded = 0;
+ if (IS_ERR_OR_NULL(*ploghandle)) {
+ up_read(&cathandle->lgh_lock);
+ down_write(&cathandle->lgh_lock);
+ sem_upgraded = 1;
+ if (IS_ERR_OR_NULL(*ploghandle)) {
+ struct llog_handle *loghandle;
+
+ rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
+ NULL, NULL, LLOG_OPEN_NEW);
+ if (!rc) {
+ *ploghandle = loghandle;
+ list_add_tail(&loghandle->u.phd.phd_entry,
+ &cathandle->u.chd.chd_head);
+ }
+ }
+ if (rc)
+ GOTO(out, rc);
+ }
+
+ rc = llog_exist(*ploghandle);
+ if (rc < 0)
+ GOTO(out, rc);
+ if (rc)
+ GOTO(out, rc = 0);
+
+ if (dt_object_remote(cathandle->lgh_obj)) {
+ down_write_nested(&(*ploghandle)->lgh_lock, LLOGH_LOG);
+ if (!llog_exist(*ploghandle)) {
+ /* For remote operation, if we put the llog object
+ * creation in the current transaction, then the
+ * llog object will not be created on the remote
+ * target until the transaction stop, if other
+ * operations start before the transaction stop,
+ * and use the same llog object, will be dependent
+ * on the success of this transaction. So let's
+ * create the llog object synchronously here to
+ * remove the dependency. */
+ rc = llog_cat_new_log(env, cathandle, *ploghandle,
+ NULL);
+ if (rc == -ESTALE) {
+ up_write(&(*ploghandle)->lgh_lock);
+ if (sem_upgraded)
+ up_write(&cathandle->lgh_lock);
+ else
+ up_read(&cathandle->lgh_lock);
+
+ rc = llog_cat_refresh(env, cathandle);
+ down_read_nested(&cathandle->lgh_lock,
+ LLOGH_CAT);
+ if (rc)
+ return rc;
+ /* *ploghandle might become NULL, restart */
+ goto start;
+ }
+ }
+ up_write(&(*ploghandle)->lgh_lock);
+ } else {
+ struct llog_thread_info *lgi = llog_info(env);
+ struct llog_logid_rec *lirec = &lgi->lgi_logid;
+
+ rc = llog_declare_create(env, *ploghandle, th);
+ if (rc)
+ GOTO(out, rc);
+
+ lirec->lid_hdr.lrh_len = sizeof(*lirec);
+ rc = llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1,
+ th);
+ }
+
+out:
+ if (sem_upgraded) {
+ up_write(&cathandle->lgh_lock);
+ down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
+ if (rc == 0)
+ goto start;
+ }
+ return rc;
+}
+