Blame SOURCES/0067-Ticket-49551-v3-correct-handling-of-numsubordinates-.patch

b045b9
From 233b64f26df76aa50f4b37aaf6b3804d208fdc1b Mon Sep 17 00:00:00 2001
b045b9
From: Ludwig Krispenz <lkrispen@redhat.com>
b045b9
Date: Mon, 12 Feb 2018 09:24:25 +0100
b045b9
Subject: [PATCH] Ticket 49551 - v3 - correct handling of numsubordinates for
b045b9
 cenotaphs and tombstone delete
b045b9
b045b9
        Bug: The ticket exposed several problems with tombstone handling.
b045b9
                - tombstone entries of conflicts were not purged in tombstone purging
b045b9
                - cenotaphs are tombstone, but the subordinate count was not managed properly
b045b9
                - direct delete of tombstones failed with err=1
b045b9
                - delete of entry with only conflict children failed correctly, but gave no hint why
b045b9
b045b9
        Fix: update the correct numsobordinates attribut for cenotaphs
b045b9
             set proper flag in directly deleting a tombstone
b045b9
             change search filter for tombstone purging to include ldapsubentries
b045b9
             check for conflict children if a delete is rejected and add a message to the response
b045b9
b045b9
        Reviewed by; Thierry, William - thanks
b045b9
---
b045b9
 ldap/servers/plugins/replication/repl5_replica.c | 14 +++++++++--
b045b9
 ldap/servers/plugins/replication/urp.c           |  8 +++---
b045b9
 ldap/servers/slapd/back-ldbm/ldbm_add.c          |  8 +++---
b045b9
 ldap/servers/slapd/back-ldbm/ldbm_delete.c       | 14 ++++++++---
b045b9
 ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c     |  3 ++-
b045b9
 ldap/servers/slapd/back-ldbm/parents.c           | 12 ++++++---
b045b9
 ldap/servers/slapd/entry.c                       | 31 ++++++++++++++++++++++++
b045b9
 ldap/servers/slapd/slapi-plugin.h                |  2 ++
b045b9
 ldap/servers/slapd/slapi-private.h               |  1 +
b045b9
 ldap/servers/slapd/task.c                        |  4 +--
b045b9
 10 files changed, 78 insertions(+), 19 deletions(-)
b045b9
b045b9
diff --git a/ldap/servers/plugins/replication/repl5_replica.c b/ldap/servers/plugins/replication/repl5_replica.c
b045b9
index bdb8a5167..628fb9ceb 100644
b045b9
--- a/ldap/servers/plugins/replication/repl5_replica.c
b045b9
+++ b/ldap/servers/plugins/replication/repl5_replica.c
b045b9
@@ -3017,6 +3017,16 @@ process_reap_entry(Slapi_Entry *entry, void *cb_data)
b045b9
        search in the future, see _replica_reap_tombstones below and add more to the
b045b9
        attrs array */
b045b9
     deletion_csn = entry_get_deletion_csn(entry);
b045b9
+    if (deletion_csn == NULL) {
b045b9
+        /* this might be a tombstone which was directly added, eg a cenotaph
b045b9
+         * check if a tombstonecsn exist and use it
b045b9
+         */
b045b9
+        char *tombstonecsn = slapi_entry_attr_get_charptr(entry, SLAPI_ATTR_TOMBSTONE_CSN);
b045b9
+        if (tombstonecsn) {
b045b9
+            deletion_csn = csn_new_by_string(tombstonecsn);
b045b9
+            slapi_ch_free_string(&tombstonecsn);
b045b9
+        }
b045b9
+    }
b045b9
 
b045b9
     if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
b045b9
         (!is_ruv_tombstone_entry(entry))) {
b045b9
@@ -3116,11 +3126,11 @@ _replica_reap_tombstones(void *arg)
b045b9
              */
b045b9
             csn_as_string(purge_csn, PR_FALSE, deletion_csn_str);
b045b9
             PR_snprintf(tombstone_filter, 128,
b045b9
-                        "(&(%s<=%s)(objectclass=nsTombstone))", SLAPI_ATTR_TOMBSTONE_CSN,
b045b9
+                        "(&(%s<=%s)(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))", SLAPI_ATTR_TOMBSTONE_CSN,
b045b9
                         csn_as_string(purge_csn, PR_FALSE, deletion_csn_str));
b045b9
         } else {
b045b9
             /* Use the old inefficient filter */
b045b9
-            PR_snprintf(tombstone_filter, 128, "(objectclass=nsTombstone)");
b045b9
+            PR_snprintf(tombstone_filter, 128, "(&(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))");
b045b9
         }
b045b9
 
b045b9
         /* we just need the objectclass - for the deletion csn
b045b9
diff --git a/ldap/servers/plugins/replication/urp.c b/ldap/servers/plugins/replication/urp.c
b045b9
index d4556d7fd..11c5da7cf 100644
b045b9
--- a/ldap/servers/plugins/replication/urp.c
b045b9
+++ b/ldap/servers/plugins/replication/urp.c
b045b9
@@ -911,7 +911,7 @@ urp_fixup_add_cenotaph (Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
b045b9
                                     cenotaph,
b045b9
                                     NULL,
b045b9
                                     repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
b045b9
-                                    OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP);
b045b9
+                                    OP_FLAG_REPL_FIXUP|OP_FLAG_NOOP|OP_FLAG_CENOTAPH_ENTRY);
b045b9
     slapi_add_internal_pb(add_pb);
b045b9
     slapi_pblock_get(add_pb, SLAPI_PLUGIN_INTOP_RESULT, &ret;;
b045b9
 
b045b9
@@ -1922,7 +1922,7 @@ done:
b045b9
     newpb = NULL;
b045b9
 
b045b9
     slapi_log_err(SLAPI_LOG_REPL, sessionid,
b045b9
-                  "urp_get_min_naming_conflict_entry - Found %d entries\n", i);
b045b9
+                  "urp_get_min_naming_conflict_entry - Found %d entries\n", min_csn?1:0);
b045b9
 
b045b9
     return min_naming_conflict_entry;
b045b9
 }
b045b9
@@ -2172,8 +2172,8 @@ mod_objectclass_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi
b045b9
     char csnstr[CSN_STRSIZE+1] = {0};
b045b9
 
b045b9
     slapi_mods_init(&smods, 3);
b045b9
-    slapi_mods_add(&smods, LDAP_MOD_ADD, "objectclass", strlen("ldapsubentry"),"ldapsubentry");
b045b9
-    slapi_mods_add(&smods, LDAP_MOD_REPLACE, "conflictcsn", CSN_STRSIZE, csn_as_string(opcsn, PR_FALSE, csnstr));
b045b9
+    slapi_mods_add_string(&smods, LDAP_MOD_ADD, "objectclass", "ldapsubentry");
b045b9
+    slapi_mods_add_string(&smods, LDAP_MOD_REPLACE, "conflictcsn", csn_as_string(opcsn, PR_FALSE, csnstr));
b045b9
     op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, 0);
b045b9
     slapi_mods_done(&smods);
b045b9
     if (op_result == LDAP_TYPE_OR_VALUE_EXISTS) {
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_add.c b/ldap/servers/slapd/back-ldbm/ldbm_add.c
b045b9
index c93d44a65..f0a3262ec 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/ldbm_add.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/ldbm_add.c
b045b9
@@ -81,6 +81,7 @@ ldbm_back_add(Slapi_PBlock *pb)
b045b9
     Slapi_Operation *operation;
b045b9
     int is_replicated_operation = 0;
b045b9
     int is_resurect_operation = 0;
b045b9
+    int is_cenotaph_operation = 0;
b045b9
     int is_tombstone_operation = 0;
b045b9
     int is_fixup_operation = 0;
b045b9
     int is_remove_from_cache = 0;
b045b9
@@ -116,6 +117,7 @@ ldbm_back_add(Slapi_PBlock *pb)
b045b9
     }
b045b9
 
b045b9
     is_resurect_operation = operation_is_flag_set(operation, OP_FLAG_RESURECT_ENTRY);
b045b9
+    is_cenotaph_operation = operation_is_flag_set(operation, OP_FLAG_CENOTAPH_ENTRY);
b045b9
     is_tombstone_operation = operation_is_flag_set(operation, OP_FLAG_TOMBSTONE_ENTRY);
b045b9
     is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
b045b9
     is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
b045b9
@@ -846,9 +848,9 @@ ldbm_back_add(Slapi_PBlock *pb)
b045b9
                the in-memory state of the parent to reflect the new child (update
b045b9
                subordinate count specifically */
b045b9
             if (parententry) {
b045b9
-                retval = parent_update_on_childchange(&parent_modify_c,
b045b9
-                                                      is_resurect_operation ? PARENTUPDATE_RESURECT : PARENTUPDATE_ADD,
b045b9
-                                                      NULL);
b045b9
+                int op = is_resurect_operation ? PARENTUPDATE_RESURECT : PARENTUPDATE_ADD;
b045b9
+                if (is_cenotaph_operation ) op |= PARENTUPDATE_CREATE_TOMBSTONE;
b045b9
+                retval = parent_update_on_childchange(&parent_modify_c, op, NULL);
b045b9
                 slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_add",
b045b9
                               "conn=%lu op=%d parent_update_on_childchange: old_entry=0x%p, new_entry=0x%p, rc=%d\n",
b045b9
                               conn_id, op_id, parent_modify_c.old_entry, parent_modify_c.new_entry, retval);
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_delete.c b/ldap/servers/slapd/back-ldbm/ldbm_delete.c
b045b9
index be0db1bd0..bc0a3654e 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/ldbm_delete.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/ldbm_delete.c
b045b9
@@ -291,9 +291,16 @@ replace_entry:
b045b9
             retval = slapi_entry_has_children(e->ep_entry);
b045b9
             if (retval && !is_replicated_operation) {
b045b9
                 ldap_result_code= LDAP_NOT_ALLOWED_ON_NONLEAF;
b045b9
-                slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_delete", 
b045b9
-                              "conn=%lu op=%d Deleting entry %s has %d children.\n", 
b045b9
-                               conn_id, op_id, slapi_entry_get_dn(e->ep_entry), retval);
b045b9
+                if (slapi_entry_has_conflict_children(e->ep_entry, (void *)li->li_identity) > 0) {
b045b9
+                    ldap_result_message = "Entry has replication conflicts as children";
b045b9
+                    slapi_log_err(SLAPI_LOG_ERR, "ldbm_back_delete",
b045b9
+                                  "conn=%lu op=%d Deleting entry %s has replication conflicts as children.\n",
b045b9
+                                   conn_id, op_id, slapi_entry_get_dn(e->ep_entry));
b045b9
+                } else {
b045b9
+                    slapi_log_err(SLAPI_LOG_BACKLDBM, "ldbm_back_delete",
b045b9
+                                  "conn=%lu op=%d Deleting entry %s has %d children.\n",
b045b9
+                                   conn_id, op_id, slapi_entry_get_dn(e->ep_entry), retval);
b045b9
+                }
b045b9
                 retval = -1;
b045b9
                 goto error_return;
b045b9
             }
b045b9
@@ -431,6 +438,7 @@ replace_entry:
b045b9
                     slapi_log_err(SLAPI_LOG_WARNING, "ldbm_back_delete",
b045b9
                             "Attempt to Tombstone again a tombstone entry %s\n", dn);
b045b9
                     delete_tombstone_entry = 1;
b045b9
+                    operation_set_flag(operation, OP_FLAG_TOMBSTONE_ENTRY);
b045b9
                 }
b045b9
             }
b045b9
 
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c b/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
b045b9
index b41a2d241..5797dd779 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/ldbm_entryrdn.c
b045b9
@@ -2824,7 +2824,8 @@ _entryrdn_delete_key(backend *be,
b045b9
                     break;
b045b9
                 }
b045b9
                 childelem = (rdn_elem *)dataret.data;
b045b9
-                if (!slapi_is_special_rdn(childelem->rdn_elem_nrdn_rdn, RDN_IS_TOMBSTONE)) {
b045b9
+                if (!slapi_is_special_rdn(childelem->rdn_elem_nrdn_rdn, RDN_IS_TOMBSTONE) &&
b045b9
+                    !strcasestr(childelem->rdn_elem_nrdn_rdn, "cenotaphid")) {
b045b9
                     /* there's at least one live child */
b045b9
                     slapi_log_err(SLAPI_LOG_ERR, "_entryrdn_delete_key",
b045b9
                                   "Failed to remove %s; has a child %s\n", nrdn,
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/parents.c b/ldap/servers/slapd/back-ldbm/parents.c
b045b9
index 79e66451e..1afc795c0 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/parents.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/parents.c
b045b9
@@ -89,7 +89,11 @@ parent_update_on_childchange(modify_context *mc, int op, size_t *new_sub_count)
b045b9
         }
b045b9
     }
b045b9
 
b045b9
-    if (PARENTUPDATE_DELETE_TOMBSTONE != repl_op) {
b045b9
+    if ((PARENTUPDATE_ADD == op) && (PARENTUPDATE_CREATE_TOMBSTONE == repl_op)) {
b045b9
+        /* we are directly adding a tombstone entry, only need to
b045b9
+         * update the tombstone subordinates
b045b9
+         */
b045b9
+    } else if (PARENTUPDATE_DELETE_TOMBSTONE != repl_op) {
b045b9
         /* are we adding ? */
b045b9
         if (((PARENTUPDATE_ADD == op) || (PARENTUPDATE_RESURECT == op)) && !already_present) {
b045b9
             /* If so, and the parent entry does not already have a subcount
b045b9
@@ -136,10 +140,10 @@ parent_update_on_childchange(modify_context *mc, int op, size_t *new_sub_count)
b045b9
         }
b045b9
     }
b045b9
 
b045b9
-    /* tombstoneNumSubordinates is needed only when this is repl op
b045b9
-     * and a child is being deleted */
b045b9
+    /* tombstoneNumSubordinates has to be updated if a tombstone child has been
b045b9
+     * deleted or a tombstone has been directly added (cenotaph) */
b045b9
     current_sub_count = LDAP_MAXINT;
b045b9
-    if ((repl_op && (PARENTUPDATE_DEL == op)) || (PARENTUPDATE_RESURECT == op)) {
b045b9
+    if (repl_op) {
b045b9
         ret = slapi_entry_attr_find(mc->old_entry->ep_entry,
b045b9
                                     tombstone_numsubordinates, &read_attr);
b045b9
         if (0 == ret) {
b045b9
diff --git a/ldap/servers/slapd/entry.c b/ldap/servers/slapd/entry.c
b045b9
index 32828b4e2..b85e9f5b0 100644
b045b9
--- a/ldap/servers/slapd/entry.c
b045b9
+++ b/ldap/servers/slapd/entry.c
b045b9
@@ -3238,6 +3238,37 @@ slapi_entry_has_children(const Slapi_Entry *entry)
b045b9
     return slapi_entry_has_children_ext(entry, 0);
b045b9
 }
b045b9
 
b045b9
+int
b045b9
+slapi_entry_has_conflict_children(const Slapi_Entry *entry, void *plg_id)
b045b9
+{
b045b9
+    Slapi_PBlock *search_pb = NULL;
b045b9
+    Slapi_Entry **entries;
b045b9
+    int rc = 0;
b045b9
+
b045b9
+    search_pb = slapi_pblock_new();
b045b9
+    slapi_search_internal_set_pb(search_pb, slapi_entry_get_dn_const(entry),
b045b9
+                                 LDAP_SCOPE_ONELEVEL,
b045b9
+                                 "(&(objectclass=ldapsubentry)(nsds5ReplConflict=namingConflict*))",
b045b9
+                                 NULL, 0, NULL, NULL, plg_id, 0);
b045b9
+    slapi_search_internal_pb(search_pb);
b045b9
+    slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
b045b9
+    if (rc) {
b045b9
+        rc = -1;
b045b9
+    } else {
b045b9
+        slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
b045b9
+        if (entries && entries[0]) {
b045b9
+            /* we found at least one conflict entry */
b045b9
+            rc = 1;
b045b9
+        } else {
b045b9
+            rc = 0;
b045b9
+        }
b045b9
+        slapi_free_search_results_internal(search_pb);
b045b9
+    }
b045b9
+    slapi_pblock_destroy(search_pb);
b045b9
+
b045b9
+    return rc;
b045b9
+}
b045b9
+
b045b9
 /*
b045b9
  * Renames an entry to simulate a MODRDN operation
b045b9
  */
b045b9
diff --git a/ldap/servers/slapd/slapi-plugin.h b/ldap/servers/slapd/slapi-plugin.h
b045b9
index 95cdcc0da..6978e258f 100644
b045b9
--- a/ldap/servers/slapd/slapi-plugin.h
b045b9
+++ b/ldap/servers/slapd/slapi-plugin.h
b045b9
@@ -2000,6 +2000,8 @@ int slapi_entry_has_children(const Slapi_Entry *e);
b045b9
  */
b045b9
 int slapi_entry_has_children_ext(const Slapi_Entry *e, int include_tombstone);
b045b9
 
b045b9
+int slapi_entry_has_conflict_children(const Slapi_Entry *e, void *plg_id);
b045b9
+
b045b9
 /**
b045b9
  * This function determines if an entry is the root DSE.
b045b9
  *
b045b9
diff --git a/ldap/servers/slapd/slapi-private.h b/ldap/servers/slapd/slapi-private.h
b045b9
index 548d5cabb..b08c0d7ce 100644
b045b9
--- a/ldap/servers/slapd/slapi-private.h
b045b9
+++ b/ldap/servers/slapd/slapi-private.h
b045b9
@@ -403,6 +403,7 @@ char *slapi_filter_to_string_internal(const struct slapi_filter *f, char *buf, s
b045b9
 #define OP_FLAG_NEVER_CHAIN SLAPI_OP_FLAG_NEVER_CHAIN         /* 0x000800 */
b045b9
 #define OP_FLAG_TOMBSTONE_ENTRY SLAPI_OP_FLAG_TOMBSTONE_ENTRY /* 0x001000 */
b045b9
 #define OP_FLAG_RESURECT_ENTRY 0x002000
b045b9
+#define OP_FLAG_CENOTAPH_ENTRY 0x004000
b045b9
 #define OP_FLAG_ACTION_NOLOG 0x008000            /* Do not log the entry in \
b045b9
                                                   * audit log or change log  \
b045b9
                                                   */
b045b9
diff --git a/ldap/servers/slapd/task.c b/ldap/servers/slapd/task.c
b045b9
index 4bd8895ff..3f9d5d995 100644
b045b9
--- a/ldap/servers/slapd/task.c
b045b9
+++ b/ldap/servers/slapd/task.c
b045b9
@@ -2352,10 +2352,10 @@ task_fixup_tombstone_thread(void *arg)
b045b9
 
b045b9
     if (task_data->stripcsn) {
b045b9
         /* find tombstones with nsTombstoneCSN */
b045b9
-        filter = "(&(nstombstonecsn=*)(objectclass=nsTombstone))";
b045b9
+        filter = "(&(nstombstonecsn=*)(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))";
b045b9
     } else {
b045b9
         /* find tombstones missing nsTombstoneCSN */
b045b9
-        filter = "(&(!(nstombstonecsn=*))(objectclass=nsTombstone))";
b045b9
+        filter = "(&(!(nstombstonecsn=*))(objectclass=nsTombstone)(|(objectclass=*)(objectclass=ldapsubentry)))";
b045b9
     }
b045b9
 
b045b9
     /* Okay check the specified backends only */
b045b9
-- 
b045b9
2.13.6
b045b9