ldbm_delete.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  37. * All rights reserved.
  38. * END COPYRIGHT BLOCK **/
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. /* delete.c - ldbm backend delete routine */
  43. #include "back-ldbm.h"
  44. #define DEL_SET_ERROR(rc, error, count) \
  45. { \
  46. (rc) = (error); \
  47. (count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
  48. }
  49. int
  50. ldbm_back_delete( Slapi_PBlock *pb )
  51. {
  52. backend *be;
  53. ldbm_instance *inst = NULL;
  54. struct ldbminfo *li = NULL;
  55. struct backentry *e = NULL;
  56. struct backentry *tombstone = NULL;
  57. struct backentry *original_tombstone = NULL;
  58. const char *dn = NULL;
  59. back_txn txn;
  60. back_txnid parent_txn;
  61. int retval = -1;
  62. char *msg;
  63. char *errbuf = NULL;
  64. int retry_count = 0;
  65. int disk_full = 0;
  66. int parent_found = 0;
  67. int ruv_c_init = 0;
  68. modify_context parent_modify_c = {0};
  69. modify_context ruv_c = {0};
  70. int rc = 0;
  71. int ldap_result_code= LDAP_SUCCESS;
  72. char *ldap_result_message= NULL;
  73. Slapi_DN *sdnp = NULL;
  74. char *e_uniqueid = NULL;
  75. Slapi_DN nscpEntrySDN;
  76. Slapi_Operation *operation;
  77. CSN *opcsn = NULL;
  78. int is_fixup_operation = 0;
  79. int is_ruv = 0; /* True if the current entry is RUV */
  80. int is_replicated_operation= 0;
  81. int is_tombstone_entry = 0; /* True if the current entry is alreday a tombstone */
  82. int delete_tombstone_entry = 0; /* We must remove the given tombstone entry from the DB */
  83. int create_tombstone_entry = 0; /* We perform a "regular" LDAP delete but since we use */
  84. /* replication, we must create a new tombstone entry */
  85. int tombstone_in_cache = 0;
  86. int e_in_cache = 0;
  87. entry_address *addr;
  88. int addordel_flags = 0; /* passed to index_addordel */
  89. char *entryusn_str = NULL;
  90. Slapi_Entry *orig_entry = NULL;
  91. Slapi_DN parentsdn;
  92. int opreturn = 0;
  93. int free_delete_existing_entry = 0;
  94. int not_an_error = 0;
  95. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  96. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  97. slapi_pblock_get( pb, SLAPI_DELETE_TARGET_SDN, &sdnp );
  98. slapi_pblock_get( pb, SLAPI_TARGET_ADDRESS, &addr);
  99. slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
  100. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  101. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation );
  102. slapi_sdn_init(&nscpEntrySDN);
  103. /* dblayer_txn_init needs to be called before "goto error_return" */
  104. dblayer_txn_init(li,&txn);
  105. /* the calls to perform searches require the parent txn if any
  106. so set txn to the parent_txn until we begin the child transaction */
  107. if (parent_txn) {
  108. txn.back_txn_txn = parent_txn;
  109. } else {
  110. parent_txn = txn.back_txn_txn;
  111. slapi_pblock_set( pb, SLAPI_TXN, parent_txn );
  112. }
  113. if (pb->pb_conn)
  114. {
  115. slapi_log_error (SLAPI_LOG_TRACE, "ldbm_back_delete", "enter conn=%" NSPRIu64 " op=%d\n", pb->pb_conn->c_connid, operation->o_opid);
  116. }
  117. if ((NULL == addr) || (NULL == sdnp))
  118. {
  119. /* retval is -1 */
  120. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
  121. "Either of DELETE_TARGET_SDN or TARGET_ADDRESS is NULL.\n");
  122. goto error_return;
  123. }
  124. dn = slapi_sdn_get_dn(sdnp);
  125. ldap_result_code = slapi_dn_syntax_check(pb, dn, 1);
  126. if (ldap_result_code)
  127. {
  128. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  129. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  130. /* retval is -1 */
  131. goto error_return;
  132. }
  133. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  134. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  135. delete_tombstone_entry = operation_is_flag_set(operation, OP_FLAG_TOMBSTONE_ENTRY);
  136. inst = (ldbm_instance *) be->be_instance_info;
  137. if (inst && inst->inst_ref_count) {
  138. slapi_counter_increment(inst->inst_ref_count);
  139. } else {
  140. LDAPDebug1Arg(LDAP_DEBUG_ANY,
  141. "ldbm_delete: instance \"%s\" does not exist.\n",
  142. inst ? inst->inst_name : "null instance");
  143. goto error_return;
  144. }
  145. /* The dblock serializes writes to the database,
  146. * which reduces deadlocking in the db code,
  147. * which means that we run faster.
  148. *
  149. * But, this lock is re-enterant for the fixup
  150. * operations that the URP code in the Replication
  151. * plugin generates.
  152. *
  153. * SERIALLOCK is moved to dblayer_txn_begin along with exposing be
  154. * transaction to plugins (see slapi_back_transaction_* APIs).
  155. *
  156. if(SERIALLOCK(li) && !operation_is_flag_set(operation,OP_FLAG_REPL_FIXUP))
  157. {
  158. dblayer_lock_backend(be);
  159. dblock_acquired= 1;
  160. }
  161. */
  162. /*
  163. * We are about to pass the last abandon test, so from now on we are
  164. * committed to finish this operation. Set status to "will complete"
  165. * before we make our last abandon check to avoid race conditions in
  166. * the code that processes abandon operations.
  167. */
  168. if (operation) {
  169. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  170. }
  171. if ( slapi_op_abandoned( pb ) ) {
  172. /* retval is -1 */
  173. goto error_return;
  174. }
  175. /*
  176. * So, we believe that no code up till here actually added anything
  177. * to the persistent store. From now on, we're transacted
  178. */
  179. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  180. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  181. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  182. Slapi_Entry *ent = NULL;
  183. /* Don't release SERIAL LOCK */
  184. dblayer_txn_abort_ext(li, &txn, PR_FALSE);
  185. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  186. /* reset original entry */
  187. slapi_pblock_get( pb, SLAPI_DELETE_EXISTING_ENTRY, &ent );
  188. if (ent && free_delete_existing_entry) {
  189. slapi_entry_free(ent);
  190. slapi_pblock_set( pb, SLAPI_DELETE_EXISTING_ENTRY, NULL );
  191. }
  192. slapi_pblock_set( pb, SLAPI_DELETE_EXISTING_ENTRY, slapi_entry_dup( e->ep_entry ) );
  193. free_delete_existing_entry = 1; /* must free the dup */
  194. if (create_tombstone_entry) {
  195. slapi_sdn_set_ndn_byval(&nscpEntrySDN, slapi_sdn_get_ndn(slapi_entry_get_sdn(e->ep_entry)));
  196. }
  197. /* reset tombstone entry */
  198. if (original_tombstone) {
  199. if (tombstone_in_cache) {
  200. CACHE_REMOVE(&inst->inst_cache, tombstone);
  201. CACHE_RETURN(&inst->inst_cache, &tombstone);
  202. tombstone_in_cache = 0;
  203. } else {
  204. backentry_free(&tombstone);
  205. }
  206. tombstone = original_tombstone;
  207. if ( (original_tombstone = backentry_dup( tombstone )) == NULL ) {
  208. ldap_result_code= LDAP_OPERATIONS_ERROR;
  209. goto error_return;
  210. }
  211. }
  212. /* reset original entry in cache */
  213. if (!e_in_cache) {
  214. CACHE_ADD(&inst->inst_cache, e, NULL);
  215. e_in_cache = 1;
  216. }
  217. if (ruv_c_init) {
  218. /* reset the ruv txn stuff */
  219. modify_term(&ruv_c, be);
  220. ruv_c_init = 0;
  221. }
  222. /* We're re-trying */
  223. LDAPDebug0Args(LDAP_DEBUG_BACKLDBM,
  224. "Delete Retrying Transaction\n");
  225. #ifndef LDBM_NO_BACKOFF_DELAY
  226. {
  227. PRIntervalTime interval;
  228. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  229. DS_Sleep(interval);
  230. }
  231. #endif
  232. }
  233. if (0 == retry_count) {
  234. /* First time, hold SERIAL LOCK */
  235. retval = dblayer_txn_begin(be, parent_txn, &txn);
  236. } else {
  237. /* Otherwise, no SERIAL LOCK */
  238. retval = dblayer_txn_begin_ext(li, parent_txn, &txn, PR_FALSE);
  239. }
  240. if (0 != retval) {
  241. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  242. ldap_result_code= LDAP_OPERATIONS_ERROR;
  243. goto error_return;
  244. }
  245. /* stash the transaction */
  246. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  247. if (0 == retry_count) { /* just once */
  248. /* find and lock the entry we are about to modify */
  249. if ( (e = find_entry2modify( pb, be, addr, &txn )) == NULL )
  250. {
  251. ldap_result_code= LDAP_NO_SUCH_OBJECT;
  252. retval = -1;
  253. goto error_return; /* error result sent by find_entry2modify() */
  254. }
  255. e_in_cache = 1; /* e is cached */
  256. if ( slapi_entry_has_children( e->ep_entry ) )
  257. {
  258. ldap_result_code= LDAP_NOT_ALLOWED_ON_NONLEAF;
  259. retval = -1;
  260. goto error_return;
  261. }
  262. /* Don't call pre-op for Tombstone entries */
  263. if (!delete_tombstone_entry)
  264. {
  265. int rc = 0;
  266. /*
  267. * Some present state information is passed through the PBlock to the
  268. * backend pre-op plugin. To ensure a consistent snapshot of this state
  269. * we wrap the reading of the entry with the dblock.
  270. */
  271. ldap_result_code= get_copy_of_entry(pb, addr, &txn,
  272. SLAPI_DELETE_EXISTING_ENTRY, !is_replicated_operation);
  273. free_delete_existing_entry = 1;
  274. if(ldap_result_code==LDAP_OPERATIONS_ERROR ||
  275. ldap_result_code==LDAP_INVALID_DN_SYNTAX)
  276. {
  277. /* restore original entry so the front-end delete code can free it */
  278. retval = -1;
  279. goto error_return;
  280. }
  281. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  282. rc = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_DELETE_FN);
  283. if (rc)
  284. {
  285. if (SLAPI_PLUGIN_NOOP == rc) {
  286. not_an_error = 1;
  287. }
  288. /*
  289. * Plugin indicated some kind of failure,
  290. * or that this Operation became a No-Op.
  291. */
  292. if (!ldap_result_code) {
  293. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  294. }
  295. /* restore original entry so the front-end delete code can free it */
  296. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  297. if (!opreturn) {
  298. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &rc );
  299. }
  300. retval = -1;
  301. goto error_return;
  302. }
  303. /* the flag could be set in a preop plugin (e.g., USN) */
  304. delete_tombstone_entry = operation_is_flag_set(operation,
  305. OP_FLAG_TOMBSTONE_ENTRY);
  306. }
  307. /* call the transaction pre delete plugins just after the
  308. * to-be-deleted entry is prepared. */
  309. /* these should not need to modify the entry to be deleted -
  310. if for some reason they ever do, do not use e->ep_entry since
  311. it could be in the cache and referenced by other search threads -
  312. instead, have them modify a copy of the entry */
  313. retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN);
  314. if (retval) {
  315. LDAPDebug1Arg( LDAP_DEBUG_TRACE,
  316. "SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN plugin "
  317. "returned error code %d\n", retval );
  318. if (!ldap_result_code) {
  319. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  320. }
  321. if (!opreturn) {
  322. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  323. }
  324. if (!opreturn) {
  325. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN,
  326. ldap_result_code ?
  327. &ldap_result_code : &retval );
  328. }
  329. goto error_return;
  330. }
  331. /*
  332. * Sanity check to avoid to delete a non-tombstone or to tombstone again
  333. * a tombstone entry. This should not happen (see bug 561003).
  334. */
  335. is_tombstone_entry = slapi_entry_flag_is_set(e->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  336. if (delete_tombstone_entry) {
  337. PR_ASSERT(is_tombstone_entry);
  338. if (!is_tombstone_entry) {
  339. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
  340. "Attempt to delete a non-tombstone entry %s\n", dn);
  341. delete_tombstone_entry = 0;
  342. }
  343. } else {
  344. PR_ASSERT(!is_tombstone_entry);
  345. if (is_tombstone_entry) {
  346. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
  347. "Attempt to Tombstone again a tombstone entry %s\n", dn);
  348. delete_tombstone_entry = 1;
  349. }
  350. }
  351. /*
  352. * If a CSN is set, we need to tombstone the entry,
  353. * rather than deleting it outright.
  354. */
  355. opcsn = operation_get_csn (operation);
  356. if (!delete_tombstone_entry)
  357. {
  358. if ((opcsn == NULL) && !is_fixup_operation && operation->o_csngen_handler)
  359. {
  360. /*
  361. * Current op is a user request. Opcsn will be assigned
  362. * by entry_assign_operation_csn() if the dn is in an
  363. * updatable replica.
  364. */
  365. opcsn = entry_assign_operation_csn ( pb, e->ep_entry, NULL );
  366. }
  367. if (opcsn != NULL)
  368. {
  369. if (!is_fixup_operation)
  370. {
  371. entry_set_maxcsn (e->ep_entry, opcsn);
  372. }
  373. }
  374. /*
  375. * We are dealing with replication and if we haven't been called to
  376. * remove a tombstone, then it's because we want to create a new one.
  377. */
  378. if ( slapi_operation_get_replica_attr (pb, operation, "nsds5ReplicaTombstonePurgeInterval",
  379. &create_tombstone_entry) == 0 )
  380. {
  381. create_tombstone_entry = (create_tombstone_entry < 0) ? 0 : 1;
  382. }
  383. }
  384. if (create_tombstone_entry && is_tombstone_entry) {
  385. slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
  386. "Attempt to convert a tombstone entry %s to tombstone\n", dn);
  387. retval = -1;
  388. ldap_result_code = LDAP_UNWILLING_TO_PERFORM;
  389. goto error_return;
  390. }
  391. #ifdef DEBUG
  392. slapi_log_error(SLAPI_LOG_REPL, "ldbm_back_delete",
  393. "entry: %s - flags: delete %d is_tombstone_entry %d create %d \n",
  394. dn, delete_tombstone_entry, is_tombstone_entry, create_tombstone_entry);
  395. #endif
  396. /* Save away a copy of the entry, before modifications */
  397. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
  398. /* JCMACL - Shouldn't the access check be before the has children check...
  399. * otherwise we're revealing the fact that an entry exists and has children */
  400. ldap_result_code = plugin_call_acl_plugin (pb, e->ep_entry, NULL, NULL, SLAPI_ACL_DELETE,
  401. ACLPLUGIN_ACCESS_DEFAULT, &errbuf );
  402. if ( ldap_result_code != LDAP_SUCCESS )
  403. {
  404. ldap_result_message= errbuf;
  405. retval = -1;
  406. goto error_return;
  407. }
  408. /*
  409. * Get the entry's parent. We do this here because index_read
  410. * seems to deadlock the database when dblayer_txn_begin is
  411. * called.
  412. */
  413. slapi_sdn_init(&parentsdn);
  414. slapi_sdn_get_backend_parent_ext(sdnp, &parentsdn, pb->pb_backend, is_tombstone_entry);
  415. if ( !slapi_sdn_isempty(&parentsdn) )
  416. {
  417. struct backentry *parent = NULL;
  418. entry_address parent_addr;
  419. parent_addr.sdn = &parentsdn;
  420. parent_addr.uniqueid = NULL;
  421. parent = find_entry2modify_only_ext(pb, be, &parent_addr,
  422. TOMBSTONE_INCLUDED, &txn);
  423. if (NULL != parent) {
  424. int isglue;
  425. size_t haschildren = 0;
  426. int op = PARENTUPDATE_DEL;
  427. /* Unfortunately findentry doesn't tell us whether it just
  428. * didn't find the entry, or if there was an error, so we
  429. * have to assume that the parent wasn't found */
  430. parent_found = 1;
  431. /* Modify the parent in memory */
  432. modify_init(&parent_modify_c,parent);
  433. if (create_tombstone_entry) {
  434. op |= PARENTUPDATE_CREATE_TOMBSTONE;
  435. } else if (delete_tombstone_entry) {
  436. op |= PARENTUPDATE_DELETE_TOMBSTONE;
  437. }
  438. retval = parent_update_on_childchange(&parent_modify_c,
  439. op, &haschildren);
  440. /* The modify context now contains info needed later */
  441. if (0 != retval) {
  442. ldap_result_code= LDAP_OPERATIONS_ERROR;
  443. retval = -1;
  444. goto error_return;
  445. }
  446. /*
  447. * Replication urp_post_delete will delete the parent entry
  448. * if it is a glue entry without any more children.
  449. * Those urp condition checkings are done here to
  450. * save unnecessary entry dup.
  451. */
  452. isglue = slapi_entry_attr_hasvalue (parent_modify_c.new_entry->ep_entry,
  453. SLAPI_ATTR_OBJECTCLASS, "glue");
  454. if ( opcsn && parent_modify_c.new_entry && !haschildren && isglue)
  455. {
  456. slapi_pblock_set ( pb, SLAPI_DELETE_GLUE_PARENT_ENTRY,
  457. slapi_entry_dup (parent_modify_c.new_entry->ep_entry) );
  458. }
  459. }
  460. }
  461. slapi_sdn_done(&parentsdn);
  462. if(create_tombstone_entry)
  463. {
  464. /*
  465. * The entry is not removed from the disk when we tombstone an
  466. * entry. We change the DN, add objectclass=tombstone, and record
  467. * the UniqueID of the parent entry.
  468. */
  469. const char *childuniqueid= slapi_entry_get_uniqueid(e->ep_entry);
  470. const char *parentuniqueid= NULL;
  471. char *tombstone_dn = compute_entry_tombstone_dn(slapi_entry_get_dn(e->ep_entry),
  472. childuniqueid);
  473. Slapi_Value *tomb_value;
  474. slapi_sdn_set_ndn_byval(&nscpEntrySDN, slapi_sdn_get_ndn(slapi_entry_get_sdn(e->ep_entry)));
  475. /* Copy the entry unique_id for URP conflict checking */
  476. e_uniqueid = slapi_ch_strdup(childuniqueid);
  477. if(parent_modify_c.old_entry!=NULL)
  478. {
  479. /* The suffix entry has no parent */
  480. parentuniqueid= slapi_entry_get_uniqueid(parent_modify_c.old_entry->ep_entry);
  481. }
  482. tombstone = backentry_dup( e );
  483. slapi_entry_set_dn(tombstone->ep_entry,tombstone_dn); /* Consumes DN */
  484. if (entryrdn_get_switch()) /* subtree-rename: on */
  485. {
  486. Slapi_RDN *srdn = slapi_entry_get_srdn(tombstone->ep_entry);
  487. char *tombstone_rdn =
  488. compute_entry_tombstone_rdn(slapi_entry_get_rdn_const(e->ep_entry),
  489. childuniqueid);
  490. /* e_srdn has "uniaqueid=..., <ORIG RDN>" */
  491. slapi_rdn_replace_rdn(srdn, tombstone_rdn);
  492. slapi_ch_free_string(&tombstone_rdn);
  493. }
  494. /* Set tombstone flag on ep_entry */
  495. slapi_entry_set_flag(tombstone->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  496. if(parentuniqueid!=NULL)
  497. {
  498. /* The suffix entry has no parent */
  499. slapi_entry_add_string(tombstone->ep_entry, SLAPI_ATTR_VALUE_PARENT_UNIQUEID, parentuniqueid);
  500. }
  501. slapi_entry_add_string(tombstone->ep_entry, SLAPI_ATTR_NSCP_ENTRYDN, slapi_sdn_get_ndn(&nscpEntrySDN));
  502. tomb_value = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
  503. value_update_csn(tomb_value, CSN_TYPE_VALUE_UPDATED,
  504. operation_get_csn(operation));
  505. slapi_entry_add_value(tombstone->ep_entry, SLAPI_ATTR_OBJECTCLASS, tomb_value);
  506. slapi_value_free(&tomb_value);
  507. /* XXXggood above used to be: slapi_entry_add_string(tombstone->ep_entry, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE); */
  508. /* JCMREPL - Add a description of what's going on? */
  509. if ( (original_tombstone = backentry_dup( tombstone )) == NULL ) {
  510. ldap_result_code= LDAP_OPERATIONS_ERROR;
  511. retval = -1;
  512. goto error_return;
  513. }
  514. }
  515. } /* if (0 == retry_count) just once */
  516. else {
  517. /* call the transaction pre delete plugins not just once
  518. * but every time transaction is restarted. */
  519. /* these should not need to modify the entry to be deleted -
  520. if for some reason they ever do, do not use e->ep_entry since
  521. it could be in the cache and referenced by other search threads -
  522. instead, have them modify a copy of the entry */
  523. retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN);
  524. if (retval) {
  525. if (SLAPI_PLUGIN_NOOP == retval) {
  526. not_an_error = 1;
  527. }
  528. LDAPDebug1Arg( LDAP_DEBUG_TRACE,
  529. "SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN plugin "
  530. "returned error code %d\n", retval );
  531. if (!ldap_result_code) {
  532. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  533. }
  534. if (!opreturn) {
  535. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  536. }
  537. if (!opreturn) {
  538. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN,
  539. ldap_result_code ?
  540. &ldap_result_code : &retval );
  541. }
  542. goto error_return;
  543. }
  544. }
  545. if(create_tombstone_entry)
  546. {
  547. slapi_pblock_get( pb, SLAPI_DELETE_BEPREOP_ENTRY, &orig_entry );
  548. /* this is ok because no other threads should be accessing
  549. the tombstone entry */
  550. slapi_pblock_set( pb, SLAPI_DELETE_BEPREOP_ENTRY,
  551. tombstone->ep_entry );
  552. rc = plugin_call_plugins(pb,
  553. SLAPI_PLUGIN_BE_TXN_PRE_DELETE_TOMBSTONE_FN);
  554. if (rc == -1) {
  555. /*
  556. * Plugin indicated some kind of failure,
  557. * or that this Operation became a No-Op.
  558. */
  559. if (!ldap_result_code) {
  560. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  561. }
  562. /* restore original entry so the front-end delete code
  563. * can free it */
  564. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  565. if (!opreturn) {
  566. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN,
  567. ldap_result_code ?
  568. &ldap_result_code : &rc );
  569. }
  570. /* retval is -1 */
  571. goto error_return;
  572. }
  573. slapi_pblock_set( pb, SLAPI_DELETE_BEPREOP_ENTRY, orig_entry );
  574. orig_entry = NULL;
  575. /*
  576. * The entry is not removed from the disk when we tombstone an
  577. * entry. We change the DN, add objectclass=tombstone, and record
  578. * the UniqueID of the parent entry.
  579. */
  580. /* Note: cache_add (tombstone) fails since the original entry having
  581. * the same ID is already in the cache. Thus, we have to add it
  582. * tentatively for now, then cache_add again when the original
  583. * entry is removed from the cache.
  584. */
  585. if (cache_add_tentative( &inst->inst_cache, tombstone, NULL) == 0) {
  586. tombstone_in_cache = 1;
  587. } else if (!(tombstone->ep_state & ENTRY_STATE_NOTINCACHE)) {
  588. LDAPDebug1Arg(LDAP_DEBUG_CACHE,
  589. "id2entry_add tombstone (%s) is in cache\n",
  590. slapi_entry_get_dn(tombstone->ep_entry));
  591. tombstone_in_cache = 1;
  592. }
  593. retval = id2entry_add( be, tombstone, &txn );
  594. if (DB_LOCK_DEADLOCK == retval) {
  595. LDAPDebug( LDAP_DEBUG_ARGS, "delete 1 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  596. /* Abort and re-try */
  597. continue;
  598. }
  599. if (0 != retval) {
  600. LDAPDebug( LDAP_DEBUG_ANY, "id2entry_add failed, err=%d %s\n",
  601. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  602. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  603. DEL_SET_ERROR(ldap_result_code,
  604. LDAP_OPERATIONS_ERROR, retry_count);
  605. goto error_return;
  606. }
  607. if (cache_replace( &inst->inst_cache, e, tombstone ) != 0 ) {
  608. LDAPDebug0Args( LDAP_DEBUG_BACKLDBM, "ldbm_back_delete cache_replace failed\n");
  609. DEL_SET_ERROR(ldap_result_code,
  610. LDAP_OPERATIONS_ERROR, retry_count);
  611. retval= -1;
  612. goto error_return;
  613. } else {
  614. e_in_cache = 0; /* e un-cached */
  615. }
  616. /* tombstone was already added to the cache via cache_add_tentative (to reserve its spot in the cache)
  617. and/or id2entry_add - so it already had one refcount - cache_replace adds another refcount -
  618. drop the extra ref added by cache_replace */
  619. CACHE_RETURN( &inst->inst_cache, &tombstone );
  620. }
  621. else
  622. {
  623. /* delete the entry from disk */
  624. retval = id2entry_delete( be, e, &txn );
  625. if (DB_LOCK_DEADLOCK == retval)
  626. {
  627. LDAPDebug( LDAP_DEBUG_ARGS, "delete 2 DEADLOCK\n", 0, 0, 0 );
  628. /* Retry txn */
  629. continue;
  630. }
  631. if (retval != 0 ) {
  632. if (retval == DB_RUNRECOVERY ||
  633. LDBM_OS_ERR_IS_DISKFULL(retval)) {
  634. disk_full = 1;
  635. }
  636. DEL_SET_ERROR(ldap_result_code,
  637. LDAP_OPERATIONS_ERROR, retry_count);
  638. goto error_return;
  639. }
  640. }
  641. /* delete from attribute indexes */
  642. addordel_flags = BE_INDEX_DEL|BE_INDEX_PRESENCE|BE_INDEX_EQUALITY;
  643. if (delete_tombstone_entry)
  644. {
  645. addordel_flags |= BE_INDEX_TOMBSTONE; /* tell index code we are deleting a tombstone */
  646. }
  647. retval = index_addordel_entry( be, e, addordel_flags, &txn );
  648. if (DB_LOCK_DEADLOCK == retval)
  649. {
  650. LDAPDebug( LDAP_DEBUG_ARGS, "delete 1 DEADLOCK\n", 0, 0, 0 );
  651. /* Retry txn */
  652. continue;
  653. }
  654. if (retval != 0) {
  655. LDAPDebug( LDAP_DEBUG_TRACE, "index_del_entry failed\n", 0, 0, 0 );
  656. DEL_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  657. goto error_return;
  658. }
  659. if(create_tombstone_entry)
  660. {
  661. /*
  662. * The tombstone entry is removed from all attribute indexes
  663. * above, but we want it to remain in the nsUniqueID and nscpEntryDN indexes
  664. * and for objectclass=tombstone.
  665. */
  666. retval = index_addordel_string(be, SLAPI_ATTR_OBJECTCLASS,
  667. SLAPI_ATTR_VALUE_TOMBSTONE,
  668. tombstone->ep_id,BE_INDEX_ADD, &txn);
  669. if (DB_LOCK_DEADLOCK == retval) {
  670. LDAPDebug( LDAP_DEBUG_ARGS,
  671. "delete (adding %s) DB_LOCK_DEADLOCK\n",
  672. SLAPI_ATTR_VALUE_TOMBSTONE, 0, 0 );
  673. /* Retry txn */
  674. continue;
  675. }
  676. if (0 != retval) {
  677. LDAPDebug( LDAP_DEBUG_TRACE,
  678. "delete (adding %s) failed, err=%d %s\n",
  679. SLAPI_ATTR_VALUE_TOMBSTONE, retval,
  680. (msg = dblayer_strerror( retval )) ? msg : "" );
  681. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  682. DEL_SET_ERROR(ldap_result_code,
  683. LDAP_OPERATIONS_ERROR, retry_count);
  684. goto error_return;
  685. }
  686. retval = index_addordel_string(be, SLAPI_ATTR_UNIQUEID,
  687. slapi_entry_get_uniqueid(tombstone->ep_entry),
  688. tombstone->ep_id,BE_INDEX_ADD,&txn);
  689. if (DB_LOCK_DEADLOCK == retval) {
  690. LDAPDebug( LDAP_DEBUG_ARGS,
  691. "delete (adding %s) DB_LOCK_DEADLOCK\n",
  692. SLAPI_ATTR_UNIQUEID, 0, 0 );
  693. /* Retry txn */
  694. continue;
  695. }
  696. if (0 != retval) {
  697. LDAPDebug( LDAP_DEBUG_TRACE,
  698. "delete (adding %s) failed, err=%d %s\n",
  699. SLAPI_ATTR_UNIQUEID, retval,
  700. (msg = dblayer_strerror( retval )) ? msg : "" );
  701. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  702. DEL_SET_ERROR(ldap_result_code,
  703. LDAP_OPERATIONS_ERROR, retry_count);
  704. goto error_return;
  705. }
  706. retval = index_addordel_string(be, SLAPI_ATTR_NSCP_ENTRYDN,
  707. slapi_sdn_get_ndn(&nscpEntrySDN),
  708. tombstone->ep_id, BE_INDEX_ADD, &txn);
  709. if (DB_LOCK_DEADLOCK == retval) {
  710. LDAPDebug( LDAP_DEBUG_ARGS,
  711. "delete (adding %s) DB_LOCK_DEADLOCK\n",
  712. SLAPI_ATTR_NSCP_ENTRYDN, 0, 0 );
  713. /* Retry txn */
  714. continue;
  715. }
  716. if (0 != retval) {
  717. LDAPDebug( LDAP_DEBUG_TRACE,
  718. "delete (adding %s) failed, err=%d %s\n",
  719. SLAPI_ATTR_NSCP_ENTRYDN, retval,
  720. (msg = dblayer_strerror( retval )) ? msg : "" );
  721. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  722. DEL_SET_ERROR(ldap_result_code,
  723. LDAP_OPERATIONS_ERROR, retry_count);
  724. goto error_return;
  725. }
  726. /* add a new usn to the entryusn index */
  727. entryusn_str = slapi_entry_attr_get_charptr(tombstone->ep_entry,
  728. SLAPI_ATTR_ENTRYUSN);
  729. if (entryusn_str) {
  730. retval = index_addordel_string(be, SLAPI_ATTR_ENTRYUSN,
  731. entryusn_str, tombstone->ep_id, BE_INDEX_ADD, &txn);
  732. slapi_ch_free_string(&entryusn_str);
  733. if (DB_LOCK_DEADLOCK == retval) {
  734. LDAPDebug( LDAP_DEBUG_ARGS,
  735. "delete (adding %s) DB_LOCK_DEADLOCK\n",
  736. SLAPI_ATTR_ENTRYUSN, 0, 0 );
  737. /* Retry txn */
  738. continue;
  739. }
  740. if (0 != retval) {
  741. LDAPDebug( LDAP_DEBUG_TRACE,
  742. "delete (adding %s) failed, err=%d %s\n",
  743. SLAPI_ATTR_ENTRYUSN, retval,
  744. (msg = dblayer_strerror( retval )) ? msg : "" );
  745. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  746. DEL_SET_ERROR(ldap_result_code,
  747. LDAP_OPERATIONS_ERROR, retry_count);
  748. goto error_return;
  749. }
  750. }
  751. if (entryrdn_get_switch()) /* subtree-rename: on */
  752. {
  753. Slapi_Attr *attr;
  754. Slapi_Value **svals;
  755. /* To maintain tombstonenumsubordinates,
  756. * parentid is needed for tombstone, as well. */
  757. slapi_entry_attr_find(tombstone->ep_entry, LDBM_PARENTID_STR,
  758. &attr);
  759. if (attr) {
  760. svals = attr_get_present_values(attr);
  761. retval = index_addordel_values_sv(be, LDBM_PARENTID_STR,
  762. svals, NULL, e->ep_id,
  763. BE_INDEX_ADD, &txn);
  764. if (DB_LOCK_DEADLOCK == retval) {
  765. LDAPDebug0Args( LDAP_DEBUG_ARGS,
  766. "delete (updating " LDBM_PARENTID_STR ") DB_LOCK_DEADLOCK\n");
  767. /* Retry txn */
  768. continue;
  769. }
  770. if ( retval ) {
  771. LDAPDebug( LDAP_DEBUG_TRACE,
  772. "delete (deleting %s) failed, err=%d %s\n",
  773. LDBM_PARENTID_STR, retval,
  774. (msg = dblayer_strerror( retval )) ? msg : "" );
  775. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  776. DEL_SET_ERROR(ldap_result_code,
  777. LDAP_OPERATIONS_ERROR, retry_count);
  778. goto error_return;
  779. }
  780. }
  781. retval = entryrdn_index_entry(be, e, BE_INDEX_DEL, &txn);
  782. if (DB_LOCK_DEADLOCK == retval) {
  783. LDAPDebug0Args( LDAP_DEBUG_ARGS,
  784. "delete (deleting entryrdn) DB_LOCK_DEADLOCK\n");
  785. /* Retry txn */
  786. continue;
  787. }
  788. if (0 != retval) {
  789. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  790. "delete (deleting entryrdn) failed, err=%d %s\n",
  791. retval,
  792. (msg = dblayer_strerror( retval )) ? msg : "" );
  793. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  794. DEL_SET_ERROR(ldap_result_code,
  795. LDAP_OPERATIONS_ERROR, retry_count);
  796. goto error_return;
  797. }
  798. retval = entryrdn_index_entry(be, tombstone, BE_INDEX_ADD, &txn);
  799. if (DB_LOCK_DEADLOCK == retval) {
  800. LDAPDebug0Args( LDAP_DEBUG_ARGS,
  801. "adding (adding tombstone entryrdn) DB_LOCK_DEADLOCK\n");
  802. /* Retry txn */
  803. continue;
  804. }
  805. if (0 != retval) {
  806. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  807. "adding (adding tombstone entryrdn) failed, err=%d %s\n",
  808. retval,
  809. (msg = dblayer_strerror( retval )) ? msg : "" );
  810. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  811. DEL_SET_ERROR(ldap_result_code,
  812. LDAP_OPERATIONS_ERROR, retry_count);
  813. goto error_return;
  814. }
  815. }
  816. } /* create_tombstone_entry */
  817. else if (delete_tombstone_entry)
  818. {
  819. /*
  820. * We need to remove the Tombstone entry from the remaining indexes:
  821. * objectclass=nsTombstone, nsUniqueID, nscpEntryDN
  822. */
  823. char *nscpedn = NULL;
  824. retval = index_addordel_string(be, SLAPI_ATTR_OBJECTCLASS,
  825. SLAPI_ATTR_VALUE_TOMBSTONE, e->ep_id,
  826. BE_INDEX_DEL, &txn);
  827. if (DB_LOCK_DEADLOCK == retval) {
  828. LDAPDebug( LDAP_DEBUG_ARGS,
  829. "delete (deleting %s) DB_LOCK_DEADLOCK\n",
  830. SLAPI_ATTR_VALUE_TOMBSTONE, 0, 0 );
  831. /* Retry txn */
  832. continue;
  833. }
  834. if (0 != retval) {
  835. LDAPDebug( LDAP_DEBUG_TRACE,
  836. "delete (deleting %s) failed, err=%d %s\n",
  837. SLAPI_ATTR_VALUE_TOMBSTONE, retval,
  838. (msg = dblayer_strerror( retval )) ? msg : "" );
  839. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  840. DEL_SET_ERROR(ldap_result_code,
  841. LDAP_OPERATIONS_ERROR, retry_count);
  842. goto error_return;
  843. }
  844. retval = index_addordel_string(be, SLAPI_ATTR_UNIQUEID,
  845. slapi_entry_get_uniqueid(e->ep_entry),
  846. e->ep_id, BE_INDEX_DEL, &txn);
  847. if (DB_LOCK_DEADLOCK == retval) {
  848. LDAPDebug( LDAP_DEBUG_ARGS,
  849. "delete (deleting %s) DB_LOCK_DEADLOCK\n",
  850. SLAPI_ATTR_UNIQUEID, 0, 0 );
  851. /* Retry txn */
  852. continue;
  853. }
  854. if (0 != retval) {
  855. LDAPDebug( LDAP_DEBUG_TRACE,
  856. "delete (deleting %s) failed, err=%d %s\n",
  857. SLAPI_ATTR_UNIQUEID, retval,
  858. (msg = dblayer_strerror( retval )) ? msg : "" );
  859. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  860. DEL_SET_ERROR(ldap_result_code,
  861. LDAP_OPERATIONS_ERROR, retry_count);
  862. goto error_return;
  863. }
  864. nscpedn = slapi_entry_attr_get_charptr(e->ep_entry,
  865. SLAPI_ATTR_NSCP_ENTRYDN);
  866. if (nscpedn) {
  867. retval = index_addordel_string(be, SLAPI_ATTR_NSCP_ENTRYDN,
  868. nscpedn, e->ep_id, BE_INDEX_DEL, &txn);
  869. slapi_ch_free((void **)&nscpedn);
  870. if (DB_LOCK_DEADLOCK == retval) {
  871. LDAPDebug( LDAP_DEBUG_ARGS,
  872. "delete (deleting %s) DB_LOCK_DEADLOCK\n",
  873. SLAPI_ATTR_NSCP_ENTRYDN, 0, 0 );
  874. /* Retry txn */
  875. continue;
  876. }
  877. if (0 != retval) {
  878. LDAPDebug( LDAP_DEBUG_TRACE,
  879. "delete (deleting %s) failed, err=%d %s\n",
  880. SLAPI_ATTR_NSCP_ENTRYDN, retval,
  881. (msg = dblayer_strerror( retval )) ? msg : "" );
  882. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  883. DEL_SET_ERROR(ldap_result_code,
  884. LDAP_OPERATIONS_ERROR, retry_count);
  885. goto error_return;
  886. }
  887. }
  888. /* delete usn from the entryusn index */
  889. entryusn_str = slapi_entry_attr_get_charptr(e->ep_entry,
  890. SLAPI_ATTR_ENTRYUSN);
  891. if (entryusn_str) {
  892. retval = index_addordel_string(be, SLAPI_ATTR_ENTRYUSN,
  893. entryusn_str, e->ep_id,
  894. BE_INDEX_DEL|BE_INDEX_EQUALITY, &txn);
  895. slapi_ch_free_string(&entryusn_str);
  896. if (DB_LOCK_DEADLOCK == retval) {
  897. LDAPDebug( LDAP_DEBUG_ARGS,
  898. "delete (deleting %s) DB_LOCK_DEADLOCK\n",
  899. SLAPI_ATTR_ENTRYUSN, 0, 0 );
  900. /* Retry txn */
  901. continue;
  902. }
  903. if (0 != retval) {
  904. LDAPDebug( LDAP_DEBUG_TRACE,
  905. "delete (deleting %s) failed, err=%d %s\n",
  906. SLAPI_ATTR_ENTRYUSN, retval,
  907. (msg = dblayer_strerror( retval )) ? msg : "" );
  908. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  909. DEL_SET_ERROR(ldap_result_code,
  910. LDAP_OPERATIONS_ERROR, retry_count);
  911. goto error_return;
  912. }
  913. }
  914. if (entryrdn_get_switch()) /* subtree-rename: on */
  915. {
  916. retval = entryrdn_index_entry(be, e, BE_INDEX_DEL, &txn);
  917. if (DB_LOCK_DEADLOCK == retval) {
  918. LDAPDebug0Args( LDAP_DEBUG_ARGS,
  919. "delete (deleting entryrdn) DB_LOCK_DEADLOCK\n");
  920. /* Retry txn */
  921. continue;
  922. }
  923. if (0 != retval) {
  924. LDAPDebug2Args( LDAP_DEBUG_TRACE,
  925. "delete (deleting entryrdn) failed, err=%d %s\n",
  926. retval,
  927. (msg = dblayer_strerror( retval )) ? msg : "" );
  928. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  929. DEL_SET_ERROR(ldap_result_code,
  930. LDAP_OPERATIONS_ERROR, retry_count);
  931. goto error_return;
  932. }
  933. }
  934. } /* delete_tombstone_entry */
  935. if (parent_found) {
  936. /* Push out the db modifications from the parent entry */
  937. retval = modify_update_all(be,pb,&parent_modify_c,&txn);
  938. if (DB_LOCK_DEADLOCK == retval)
  939. {
  940. LDAPDebug( LDAP_DEBUG_ARGS, "del 4 DEADLOCK\n", 0, 0, 0 );
  941. /* Retry txn */
  942. continue;
  943. }
  944. if (0 != retval) {
  945. LDAPDebug( LDAP_DEBUG_TRACE, "delete 3 BAD, err=%d %s\n",
  946. retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  947. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  948. DEL_SET_ERROR(ldap_result_code,
  949. LDAP_OPERATIONS_ERROR, retry_count);
  950. goto error_return;
  951. }
  952. }
  953. /*
  954. * first check if searchentry needs to be removed
  955. * Remove the entry from the Virtual List View indexes.
  956. */
  957. if (!delete_tombstone_entry && !is_ruv &&
  958. !vlv_delete_search_entry(pb,e->ep_entry,inst)) {
  959. retval = vlv_update_all_indexes(&txn, be, pb, e, NULL);
  960. if (DB_LOCK_DEADLOCK == retval)
  961. {
  962. LDAPDebug( LDAP_DEBUG_ARGS, "delete DEADLOCK vlv_update_index\n", 0, 0, 0 );
  963. /* Retry txn */
  964. continue;
  965. }
  966. if (retval != 0 ) {
  967. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  968. DEL_SET_ERROR(ldap_result_code,
  969. LDAP_OPERATIONS_ERROR, retry_count);
  970. goto error_return;
  971. }
  972. }
  973. if (!is_ruv && !is_fixup_operation && !delete_tombstone_entry && !NO_RUV_UPDATE(li)) {
  974. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  975. if (-1 == ruv_c_init) {
  976. LDAPDebug( LDAP_DEBUG_ANY,
  977. "ldbm_back_delete: ldbm_txn_ruv_modify_context "
  978. "failed to construct RUV modify context\n",
  979. 0, 0, 0);
  980. ldap_result_code= LDAP_OPERATIONS_ERROR;
  981. retval = 0;
  982. goto error_return;
  983. }
  984. }
  985. if (ruv_c_init) {
  986. retval = modify_update_all( be, pb, &ruv_c, &txn );
  987. if (DB_LOCK_DEADLOCK == retval) {
  988. /* Abort and re-try */
  989. continue;
  990. }
  991. if (0 != retval) {
  992. LDAPDebug( LDAP_DEBUG_ANY,
  993. "modify_update_all failed, err=%d %s\n", retval,
  994. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  995. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  996. disk_full = 1;
  997. ldap_result_code= LDAP_OPERATIONS_ERROR;
  998. goto error_return;
  999. }
  1000. }
  1001. if (retval == 0 ) {
  1002. break;
  1003. }
  1004. }
  1005. if (retry_count == RETRY_TIMES) {
  1006. /* Failed */
  1007. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in delete\n", 0, 0, 0 );
  1008. ldap_result_code= LDAP_BUSY;
  1009. retval = -1;
  1010. goto error_return;
  1011. }
  1012. /* call the transaction post delete plugins just before the commit */
  1013. if (plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN)) {
  1014. LDAPDebug0Args( LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN plugin "
  1015. "returned error code\n" );
  1016. if (!ldap_result_code) {
  1017. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1018. }
  1019. if (!ldap_result_code) {
  1020. LDAPDebug0Args( LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN plugin "
  1021. "returned error code but did not set SLAPI_RESULT_CODE\n" );
  1022. ldap_result_code = LDAP_OPERATIONS_ERROR;
  1023. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1024. }
  1025. if (!opreturn) {
  1026. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  1027. }
  1028. if (!retval) {
  1029. retval = -1;
  1030. }
  1031. if (!opreturn) {
  1032. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &retval );
  1033. }
  1034. goto error_return;
  1035. }
  1036. /* Release SERIAL LOCK */
  1037. retval = dblayer_txn_commit(be, &txn);
  1038. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  1039. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  1040. if (0 != retval)
  1041. {
  1042. if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
  1043. ldap_result_code= LDAP_OPERATIONS_ERROR;
  1044. goto error_return;
  1045. }
  1046. /* delete from cache and clean up */
  1047. if (e) {
  1048. if (e_in_cache) {
  1049. CACHE_REMOVE(&inst->inst_cache, e);
  1050. }
  1051. cache_unlock_entry(&inst->inst_cache, e);
  1052. CACHE_RETURN(&inst->inst_cache, &e);
  1053. e = NULL;
  1054. }
  1055. if (parent_found)
  1056. {
  1057. /* Replace the old parent entry with the newly modified one */
  1058. modify_switch_entries( &parent_modify_c,be);
  1059. }
  1060. if (ruv_c_init) {
  1061. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  1062. ldap_result_code= LDAP_OPERATIONS_ERROR;
  1063. LDAPDebug( LDAP_DEBUG_ANY,
  1064. "ldbm_back_delete: modify_switch_entries failed\n", 0, 0, 0);
  1065. retval = -1;
  1066. goto error_return;
  1067. }
  1068. }
  1069. rc= 0;
  1070. goto common_return;
  1071. error_return:
  1072. if (inst && tombstone_in_cache)
  1073. {
  1074. CACHE_REMOVE( &inst->inst_cache, tombstone );
  1075. CACHE_RETURN( &inst->inst_cache, &tombstone );
  1076. tombstone = NULL;
  1077. tombstone_in_cache = 0;
  1078. }
  1079. else
  1080. {
  1081. backentry_free( &tombstone );
  1082. }
  1083. if (retval == DB_RUNRECOVERY) {
  1084. dblayer_remember_disk_filled(li);
  1085. ldbm_nasty("Delete",79,retval);
  1086. disk_full = 1;
  1087. }
  1088. if (disk_full) {
  1089. rc= return_on_disk_full(li);
  1090. goto diskfull_return;
  1091. }
  1092. else
  1093. rc= SLAPI_FAIL_GENERAL;
  1094. /* It is safer not to abort when the transaction is not started. */
  1095. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  1096. /* make sure SLAPI_RESULT_CODE and SLAPI_PLUGIN_OPRETURN are set */
  1097. int val = 0;
  1098. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &val);
  1099. if (!val) {
  1100. if (!ldap_result_code) {
  1101. ldap_result_code = LDAP_OPERATIONS_ERROR;
  1102. }
  1103. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1104. }
  1105. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &val );
  1106. if (!val) {
  1107. opreturn = retval;
  1108. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &retval );
  1109. }
  1110. /* call the transaction post delete plugins just before the abort */
  1111. if (plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN)) {
  1112. LDAPDebug1Arg( LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN plugin "
  1113. "returned error code %d\n", retval );
  1114. if (!ldap_result_code) {
  1115. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1116. }
  1117. if (!opreturn) {
  1118. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &opreturn );
  1119. }
  1120. if (!opreturn) {
  1121. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval );
  1122. }
  1123. }
  1124. /* Release SERIAL LOCK */
  1125. dblayer_txn_abort(be, &txn); /* abort crashes in case disk full */
  1126. /* txn is no longer valid - reset the txn pointer to the parent */
  1127. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  1128. }
  1129. common_return:
  1130. if (orig_entry) {
  1131. /* NOTE: #define SLAPI_DELETE_BEPREOP_ENTRY SLAPI_ENTRY_PRE_OP */
  1132. /* so if orig_entry is NULL, we will wipe out SLAPI_ENTRY_PRE_OP
  1133. for the post op plugins */
  1134. slapi_pblock_set( pb, SLAPI_DELETE_BEPREOP_ENTRY, orig_entry );
  1135. }
  1136. if (inst && tombstone_in_cache)
  1137. {
  1138. CACHE_RETURN( &inst->inst_cache, &tombstone );
  1139. tombstone = NULL;
  1140. tombstone_in_cache = 0;
  1141. }
  1142. else
  1143. {
  1144. backentry_free( &tombstone );
  1145. }
  1146. /* result code could be used in the bepost plugin functions. */
  1147. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1148. /*
  1149. * The bepostop is called even if the operation fails,
  1150. * but not if the operation is purging tombstones.
  1151. */
  1152. if (!delete_tombstone_entry) {
  1153. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_DELETE_FN);
  1154. }
  1155. /* Need to return to cache after post op plugins are called */
  1156. if (inst) {
  1157. if (retval && e) { /* error case */
  1158. cache_unlock_entry( &inst->inst_cache, e );
  1159. CACHE_RETURN( &inst->inst_cache, &e );
  1160. }
  1161. if (inst->inst_ref_count) {
  1162. slapi_counter_decrement(inst->inst_ref_count);
  1163. }
  1164. }
  1165. if (ruv_c_init) {
  1166. modify_term(&ruv_c, be);
  1167. }
  1168. diskfull_return:
  1169. if(ldap_result_code!=-1) {
  1170. if (not_an_error) {
  1171. /* This is mainly used by urp. Solved conflict is not an error.
  1172. * And we don't want the supplier to halt sending the updates. */
  1173. ldap_result_code = LDAP_SUCCESS;
  1174. }
  1175. slapi_send_ldap_result( pb, ldap_result_code, NULL, ldap_result_message, 0, NULL );
  1176. }
  1177. modify_term(&parent_modify_c,be);
  1178. if (rc == 0 && opcsn && !is_fixup_operation && !delete_tombstone_entry)
  1179. {
  1180. /* URP Naming Collision
  1181. * When an entry is deleted by a replicated delete operation
  1182. * we must check for entries that have had a naming collision
  1183. * with this entry. Now that this name has been given up, one
  1184. * of those entries can take over the name.
  1185. */
  1186. slapi_pblock_set(pb, SLAPI_URP_NAMING_COLLISION_DN, slapi_ch_strdup(dn));
  1187. }
  1188. if (free_delete_existing_entry) {
  1189. done_with_pblock_entry(pb, SLAPI_DELETE_EXISTING_ENTRY);
  1190. } else { /* owned by someone else */
  1191. slapi_pblock_set(pb, SLAPI_DELETE_EXISTING_ENTRY, NULL);
  1192. }
  1193. backentry_free(&original_tombstone);
  1194. slapi_ch_free((void**)&errbuf);
  1195. slapi_sdn_done(&nscpEntrySDN);
  1196. slapi_ch_free_string(&e_uniqueid);
  1197. if (pb->pb_conn)
  1198. {
  1199. slapi_log_error (SLAPI_LOG_TRACE, "ldbm_back_delete", "leave conn=%" NSPRIu64 " op=%d\n", pb->pb_conn->c_connid, operation->o_opid);
  1200. }
  1201. return rc;
  1202. }