ldbm_delete.c 46 KB

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