+int llog_cat_declare_add_rec(const struct lu_env *env,
+ struct llog_handle *cathandle,
+ struct llog_rec_hdr *rec, struct thandle *th)
+{
+ struct llog_thread_info *lgi = llog_info(env);
+ struct llog_logid_rec *lirec = &lgi->lgi_logid;
+ struct llog_handle *loghandle, *next;
+ int rc = 0;
+
+ ENTRY;
+
+ if (cathandle->u.chd.chd_current_log == NULL) {
+ /* declare new plain llog */
+ down_write(&cathandle->lgh_lock);
+ if (cathandle->u.chd.chd_current_log == NULL) {
+ rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
+ NULL, NULL, LLOG_OPEN_NEW);
+ if (rc == 0) {
+ cathandle->u.chd.chd_current_log = loghandle;
+ list_add_tail(&loghandle->u.phd.phd_entry,
+ &cathandle->u.chd.chd_head);
+ }
+ }
+ up_write(&cathandle->lgh_lock);
+ } else if (cathandle->u.chd.chd_next_log == NULL ||
+ IS_ERR(cathandle->u.chd.chd_next_log)) {
+ /* declare next plain llog */
+ down_write(&cathandle->lgh_lock);
+ if (cathandle->u.chd.chd_next_log == NULL ||
+ IS_ERR(cathandle->u.chd.chd_next_log)) {
+ rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
+ NULL, NULL, LLOG_OPEN_NEW);
+ if (rc == 0) {
+ cathandle->u.chd.chd_next_log = loghandle;
+ list_add_tail(&loghandle->u.phd.phd_entry,
+ &cathandle->u.chd.chd_head);
+ }
+ }
+ up_write(&cathandle->lgh_lock);
+ }
+ if (rc)
+ GOTO(out, rc);
+
+ lirec->lid_hdr.lrh_len = sizeof(*lirec);
+
+ if (!llog_exist(cathandle->u.chd.chd_current_log)) {
+ if (dt_object_remote(cathandle->lgh_obj)) {
+ /* 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. */
+create_again:
+ down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
+ loghandle = cathandle->u.chd.chd_current_log;
+ down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
+ if (cathandle->lgh_stale) {
+ up_write(&loghandle->lgh_lock);
+ up_read(&cathandle->lgh_lock);
+ GOTO(out, rc = -EIO);
+ }
+ if (!llog_exist(loghandle)) {
+ rc = llog_cat_new_log(env, cathandle, loghandle,
+ NULL);
+ if (rc == -ESTALE)
+ cathandle->lgh_stale = 1;
+ }
+ up_write(&loghandle->lgh_lock);
+ up_read(&cathandle->lgh_lock);
+ if (rc == -ESTALE) {
+ rc = llog_cat_update_header(env, cathandle);
+ if (rc != 0)
+ GOTO(out, rc);
+ goto create_again;
+ } else if (rc < 0) {
+ GOTO(out, rc);
+ }
+ } else {
+ rc = llog_declare_create(env,
+ cathandle->u.chd.chd_current_log, th);
+ if (rc)
+ GOTO(out, rc);
+ llog_declare_write_rec(env, cathandle,
+ &lirec->lid_hdr, -1, th);
+ }
+ }
+
+write_again:
+ /* declare records in the llogs */
+ rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
+ rec, -1, th);
+ if (rc == -ESTALE) {
+ down_write(&cathandle->lgh_lock);
+ if (cathandle->lgh_stale) {
+ up_write(&cathandle->lgh_lock);
+ GOTO(out, rc = -EIO);
+ }
+
+ cathandle->lgh_stale = 1;
+ up_write(&cathandle->lgh_lock);
+ rc = llog_cat_update_header(env, cathandle);
+ if (rc != 0)
+ GOTO(out, rc);
+ goto write_again;
+ } else if (rc < 0) {
+ GOTO(out, rc);
+ }
+
+ next = cathandle->u.chd.chd_next_log;
+ if (!IS_ERR_OR_NULL(next)) {
+ if (!llog_exist(next)) {
+ if (dt_object_remote(cathandle->lgh_obj)) {
+ /* 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. */
+ down_write_nested(&cathandle->lgh_lock,
+ LLOGH_CAT);
+ next = cathandle->u.chd.chd_next_log;
+ if (IS_ERR_OR_NULL(next)) {
+ /* Sigh, another thread just tried,
+ * let's fail as well */
+ up_write(&cathandle->lgh_lock);
+ if (next == NULL)
+ rc = -EIO;
+ else
+ rc = PTR_ERR(next);
+ GOTO(out, rc);
+ }
+
+ down_write_nested(&next->lgh_lock, LLOGH_LOG);
+ if (!llog_exist(next)) {
+ rc = llog_cat_new_log(env, cathandle,
+ next, NULL);
+ if (rc < 0)
+ cathandle->u.chd.chd_next_log =
+ ERR_PTR(rc);
+ }
+ up_write(&next->lgh_lock);
+ up_write(&cathandle->lgh_lock);
+ if (rc < 0)
+ GOTO(out, rc);
+ } else {
+ rc = llog_declare_create(env, next, th);
+ llog_declare_write_rec(env, cathandle,
+ &lirec->lid_hdr, -1, th);
+ }
+ }
+ /* XXX: we hope for declarations made for existing llog
+ * this might be not correct with some backends
+ * where declarations are expected against specific
+ * object like ZFS with full debugging enabled */
+ /*llog_declare_write_rec(env, next, rec, -1, th);*/
+ }
+out:
+ RETURN(rc);
+}
+EXPORT_SYMBOL(llog_cat_declare_add_rec);