ldbm_add.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. /* add.c - ldap ldbm back-end add routine */
  43. #include "back-ldbm.h"
  44. extern char *numsubordinates;
  45. extern char *hassubordinates;
  46. static void delete_update_entrydn_operational_attributes(struct backentry *ep);
  47. #define ADD_SET_ERROR(rc, error, count) \
  48. { \
  49. (rc) = (error); \
  50. (count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
  51. }
  52. /* in order to find the parent, we must have either the parent dn or uniqueid
  53. This function will return true if either are set, or false otherwise */
  54. static int
  55. have_parent_address(const Slapi_DN *parentsdn, const char *parentuniqueid)
  56. {
  57. if (parentuniqueid && parentuniqueid[0]) {
  58. return 1; /* have parent uniqueid */
  59. }
  60. if (parentsdn && !slapi_sdn_isempty(parentsdn)) {
  61. return 1; /* have parent dn */
  62. }
  63. return 0; /* have no address */
  64. }
  65. int
  66. ldbm_back_add( Slapi_PBlock *pb )
  67. {
  68. backend *be;
  69. struct ldbminfo *li;
  70. ldbm_instance *inst;
  71. Slapi_Entry *e = NULL;
  72. struct backentry *tombstoneentry = NULL;
  73. struct backentry *addingentry = NULL;
  74. struct backentry *parententry = NULL;
  75. struct backentry *originalentry = NULL;
  76. struct backentry *tmpentry = NULL;
  77. ID pid;
  78. int isroot;
  79. char *errbuf= NULL;
  80. back_txn txn;
  81. back_txnid parent_txn;
  82. int retval = -1;
  83. char *msg;
  84. int managedsait;
  85. int ldap_result_code = LDAP_SUCCESS;
  86. char *ldap_result_message = NULL;
  87. char *ldap_result_matcheddn = NULL;
  88. int retry_count = 0;
  89. int disk_full = 0;
  90. modify_context parent_modify_c = {0};
  91. modify_context ruv_c = {0};
  92. int parent_found = 0;
  93. int ruv_c_init = 0;
  94. int rc;
  95. int addingentry_id_assigned= 0;
  96. Slapi_DN *sdn = NULL;
  97. Slapi_DN parentsdn;
  98. Slapi_Operation *operation;
  99. int dblock_acquired= 0;
  100. int is_replicated_operation= 0;
  101. int is_resurect_operation= 0;
  102. int is_tombstone_operation= 0;
  103. int is_fixup_operation= 0;
  104. int is_ruv = 0; /* True if the current entry is RUV */
  105. CSN *opcsn = NULL;
  106. entry_address addr = {0};
  107. int parent_switched = 0;
  108. int noabort = 1;
  109. const char *dn = NULL;
  110. slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
  111. slapi_pblock_get( pb, SLAPI_ADD_ENTRY, &e );
  112. slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
  113. slapi_pblock_get( pb, SLAPI_MANAGEDSAIT, &managedsait );
  114. slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
  115. slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
  116. slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation );
  117. slapi_pblock_get( pb, SLAPI_BACKEND, &be);
  118. is_resurect_operation= operation_is_flag_set(operation,OP_FLAG_RESURECT_ENTRY);
  119. is_tombstone_operation= operation_is_flag_set(operation,OP_FLAG_TOMBSTONE_ENTRY);
  120. is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
  121. is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
  122. inst = (ldbm_instance *) be->be_instance_info;
  123. /* sdn & parentsdn need to be initialized before "goto *_return" */
  124. slapi_sdn_init(&parentsdn);
  125. /* Get rid of ldbm backend attributes that you are not allowed to specify yourself */
  126. slapi_entry_delete_values( e, hassubordinates, NULL );
  127. slapi_entry_delete_values( e, numsubordinates, NULL );
  128. dblayer_txn_init(li,&txn);
  129. /* the calls to perform searches require the parent txn if any
  130. so set txn to the parent_txn until we begin the child transaction */
  131. if (parent_txn) {
  132. txn.back_txn_txn = parent_txn;
  133. } else {
  134. parent_txn = txn.back_txn_txn;
  135. slapi_pblock_set( pb, SLAPI_TXN, parent_txn );
  136. }
  137. /* The dblock serializes writes to the database,
  138. * which reduces deadlocking in the db code,
  139. * which means that we run faster.
  140. *
  141. * But, this lock is re-enterant for the fixup
  142. * operations that the URP code in the Replication
  143. * plugin generates.
  144. */
  145. if(SERIALLOCK(li) && (!is_fixup_operation || is_ruv))
  146. {
  147. dblayer_lock_backend(be);
  148. dblock_acquired= 1;
  149. }
  150. rc= 0;
  151. /*
  152. * We are about to pass the last abandon test, so from now on we are
  153. * committed to finish this operation. Set status to "will complete"
  154. * before we make our last abandon check to avoid race conditions in
  155. * the code that processes abandon operations.
  156. */
  157. if (operation) {
  158. operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
  159. }
  160. if ( slapi_op_abandoned( pb ) ) {
  161. ldap_result_code = -1; /* needs to distinguish from "success" */
  162. goto error_return;
  163. }
  164. if (!is_tombstone_operation) {
  165. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
  166. }
  167. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_UNIQUEID_ENTRY);
  168. rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
  169. while(rc!=0)
  170. {
  171. /* JCM - copying entries can be expensive... should optimize */
  172. /*
  173. * Some present state information is passed through the PBlock to the
  174. * backend pre-op plugin. To ensure a consistent snapshot of this state
  175. * we wrap the reading of the entry with the dblock.
  176. */
  177. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_UNIQUEID_ENTRY))
  178. {
  179. /* Check if an entry with the intended uniqueid already exists. */
  180. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_UNIQUEID_ENTRY); /* Could be through this multiple times */
  181. addr.udn = NULL;
  182. addr.sdn = NULL;
  183. addr.uniqueid = (char*)slapi_entry_get_uniqueid(e); /* jcm - cast away const */
  184. ldap_result_code= get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_EXISTING_UNIQUEID_ENTRY, !is_replicated_operation);
  185. }
  186. if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY))
  187. {
  188. slapi_pblock_get( pb, SLAPI_ADD_TARGET_SDN, &sdn );
  189. if (NULL == sdn)
  190. {
  191. LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm_back_add: FETCH_EXISTING_DN_ENTRY is set, but target dn is NULL.\n");
  192. ldap_result_code = LDAP_OPERATIONS_ERROR;
  193. goto error_return;
  194. }
  195. /* not need to check the dn syntax as this is a replicated op */
  196. if(!is_replicated_operation){
  197. ldap_result_code = slapi_dn_syntax_check(pb, slapi_sdn_get_dn(sdn), 1);
  198. if (ldap_result_code)
  199. {
  200. ldap_result_code = LDAP_INVALID_DN_SYNTAX;
  201. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  202. goto error_return;
  203. }
  204. }
  205. /*
  206. * If the parent is conflict, slapi_sdn_get_backend_parent does not support it.
  207. * That is, adding children to a conflict entry is not allowed.
  208. */
  209. slapi_sdn_get_backend_parent(sdn, &parentsdn, pb->pb_backend);
  210. /* Check if an entry with the intended DN already exists. */
  211. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_DN_ENTRY); /* Could be through this multiple times */
  212. addr.sdn = sdn;
  213. addr.udn = NULL;
  214. addr.uniqueid = NULL;
  215. ldap_result_code= get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_EXISTING_DN_ENTRY, !is_replicated_operation);
  216. if ((ldap_result_code == LDAP_OPERATIONS_ERROR) || (ldap_result_code==LDAP_INVALID_DN_SYNTAX)) {
  217. goto error_return;
  218. }
  219. }
  220. /* if we can find the parent by dn or uniqueid, and the operation has requested the parent
  221. then get it */
  222. if (have_parent_address(&parentsdn, operation->o_params.p.p_add.parentuniqueid) &&
  223. slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY)) {
  224. done_with_pblock_entry(pb,SLAPI_ADD_PARENT_ENTRY); /* Could be through this multiple times */
  225. addr.sdn = &parentsdn;
  226. addr.udn = NULL;
  227. addr.uniqueid = operation->o_params.p.p_add.parentuniqueid;
  228. ldap_result_code = get_copy_of_entry(pb, &addr, &txn, SLAPI_ADD_PARENT_ENTRY,
  229. !is_replicated_operation);
  230. }
  231. /* Call the Backend Pre Add plugins */
  232. ldap_result_code = LDAP_SUCCESS;
  233. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  234. rc = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_ADD_FN);
  235. if (rc == -1) {
  236. int opreturn = 0;
  237. /*
  238. * Plugin indicated some kind of failure,
  239. * or that this Operation became a No-Op.
  240. */
  241. if (!ldap_result_code) {
  242. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  243. }
  244. if (!ldap_result_code) {
  245. LDAPDebug0Args(LDAP_DEBUG_ANY,
  246. "ldbm_back_add: SLAPI_PLUGIN_BE_PRE_ADD_FN returned error but did not set SLAPI_RESULT_CODE\n");
  247. ldap_result_code = LDAP_OPERATIONS_ERROR;
  248. }
  249. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  250. if (!opreturn) {
  251. /* make sure opreturn is set for the postop plugins */
  252. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &rc);
  253. }
  254. goto error_return;
  255. }
  256. /*
  257. * (rc!=-1 && rc!= 0) means that the plugin changed things, so we go around
  258. * the loop once again to get the new present state.
  259. */
  260. /* JCMREPL - Warning: A Plugin could cause an infinite loop by always returning a result code that requires some action. */
  261. }
  262. /*
  263. * Originally (in the U-M LDAP 3.3 code), there was a comment near this
  264. * code about a race condition. The race was that a 2nd entry could be
  265. * added between the time when we check for an already existing entry
  266. * and the cache_add_entry_lock() call below. A race condition no
  267. * longer exists, because now we keep the parent entry locked for
  268. * the duration of the old race condition's window of opportunity.
  269. */
  270. /*
  271. * Fetch the parent entry and acquire the cache lock.
  272. */
  273. if(have_parent_address(&parentsdn, operation->o_params.p.p_add.parentuniqueid))
  274. {
  275. addr.sdn = &parentsdn;
  276. addr.udn = NULL;
  277. addr.uniqueid = operation->o_params.p.p_add.parentuniqueid;
  278. parententry = find_entry2modify_only(pb,be,&addr,&txn);
  279. if (parententry && parententry->ep_entry) {
  280. if (!operation->o_params.p.p_add.parentuniqueid){
  281. /* Set the parentuniqueid now */
  282. operation->o_params.p.p_add.parentuniqueid = slapi_ch_strdup(slapi_entry_get_uniqueid(parententry->ep_entry));
  283. }
  284. if (slapi_sdn_isempty(&parentsdn) ||
  285. slapi_sdn_compare(&parentsdn, slapi_entry_get_sdn(parententry->ep_entry))) {
  286. /* Set the parentsdn now */
  287. slapi_sdn_set_dn_byval(&parentsdn, slapi_entry_get_dn_const(parententry->ep_entry));
  288. }
  289. } else {
  290. LDAPDebug2Args(LDAP_DEBUG_BACKLDBM, "find_entry2modify_only returned NULL parententry pdn: %s, uniqueid: %s\n",
  291. slapi_sdn_get_dn(&parentsdn), addr.uniqueid?addr.uniqueid:"none");
  292. }
  293. modify_init(&parent_modify_c,parententry);
  294. }
  295. /* Check if the entry we have been asked to add already exists */
  296. {
  297. Slapi_Entry *entry;
  298. slapi_pblock_get(pb, SLAPI_ADD_EXISTING_DN_ENTRY, &entry);
  299. if (entry) {
  300. if (is_resurect_operation) {
  301. Slapi_Entry *uniqentry;
  302. slapi_pblock_get(pb, SLAPI_ADD_EXISTING_UNIQUEID_ENTRY, &uniqentry);
  303. if (uniqentry == entry) {
  304. /*
  305. * adding entry having the uniqueid exists.
  306. * No need to resurrect.
  307. */
  308. ldap_result_code = LDAP_SUCCESS;
  309. } else {
  310. /*
  311. * The entry having the DN already exists.
  312. * But the
  313. */
  314. if (uniqentry) {
  315. if (PL_strcmp(slapi_entry_get_uniqueid(entry),
  316. slapi_entry_get_uniqueid(uniqentry))) {
  317. /* Not match; conflict. */
  318. ldap_result_code = LDAP_ALREADY_EXISTS;
  319. } else {
  320. /* Same entry; no need to resurrect. */
  321. ldap_result_code = LDAP_SUCCESS;
  322. }
  323. } else {
  324. ldap_result_code = LDAP_ALREADY_EXISTS;
  325. }
  326. }
  327. } else {
  328. /* The entry already exists */
  329. ldap_result_code = LDAP_ALREADY_EXISTS;
  330. }
  331. goto error_return;
  332. }
  333. else
  334. {
  335. /*
  336. * did not find the entry - this is good, since we're
  337. * trying to add it, but we have to check whether the
  338. * entry we did match has a referral we should return
  339. * instead. we do this only if managedsait is not on.
  340. */
  341. if (!managedsait && !is_tombstone_operation && !is_resurect_operation)
  342. {
  343. int err= 0;
  344. Slapi_DN ancestorsdn;
  345. struct backentry *ancestorentry;
  346. slapi_sdn_init(&ancestorsdn);
  347. ancestorentry= dn2ancestor(pb->pb_backend,sdn,&ancestorsdn,&txn,&err);
  348. slapi_sdn_done(&ancestorsdn);
  349. if ( ancestorentry != NULL )
  350. {
  351. int sentreferral= check_entry_for_referral(pb, ancestorentry->ep_entry, backentry_get_ndn(ancestorentry), "ldbm_back_add");
  352. CACHE_RETURN( &inst->inst_cache, &ancestorentry );
  353. if(sentreferral)
  354. {
  355. ldap_result_code= -1; /* The result was sent by check_entry_for_referral */
  356. goto error_return;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. /* no need to check the schema as this is a replication add */
  363. if(!is_replicated_operation){
  364. if ((operation_is_flag_set(operation,OP_FLAG_ACTION_SCHEMA_CHECK)) && slapi_entry_schema_check(pb, e) != 0)
  365. {
  366. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0, 0);
  367. ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
  368. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  369. goto error_return;
  370. }
  371. /* Check attribute syntax */
  372. if (slapi_entry_syntax_check(pb, e, 0) != 0)
  373. {
  374. LDAPDebug(LDAP_DEBUG_TRACE, "entry failed syntax check\n", 0, 0, 0);
  375. ldap_result_code = LDAP_INVALID_SYNTAX;
  376. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  377. goto error_return;
  378. }
  379. }
  380. opcsn = operation_get_csn (operation);
  381. if(is_resurect_operation)
  382. {
  383. char *reason = NULL;
  384. /*
  385. * When we resurect a tombstone we must use its UniqueID
  386. * to find the tombstone entry and lock it down in the cache.
  387. */
  388. addr.udn = NULL;
  389. addr.sdn = NULL;
  390. addr.uniqueid = (char *)slapi_entry_get_uniqueid(e); /* jcm - cast away const */
  391. tombstoneentry = find_entry2modify( pb, be, &addr, &txn );
  392. if ( tombstoneentry==NULL )
  393. {
  394. ldap_result_code= -1;
  395. goto error_return; /* error result sent by find_entry2modify() */
  396. }
  397. addingentry = backentry_dup( tombstoneentry );
  398. if ( addingentry==NULL )
  399. {
  400. ldap_result_code = LDAP_OPERATIONS_ERROR;
  401. goto error_return;
  402. }
  403. /*
  404. * To resurect a tombstone we must fix its DN and remove the
  405. * parent UniqueID that we stashed in there.
  406. *
  407. * The entry comes back to life as a Glue entry, so we add the
  408. * magic objectclass.
  409. */
  410. if (NULL == sdn) {
  411. LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm_back_add: Null target dn\n");
  412. ldap_result_code = LDAP_OPERATIONS_ERROR;
  413. goto error_return;
  414. }
  415. slapi_entry_set_sdn(addingentry->ep_entry, sdn); /* The DN is passed into the entry. */
  416. /* not just e_sdn, e_rsdn needs to be updated. */
  417. slapi_rdn_set_all_dn(slapi_entry_get_srdn(addingentry->ep_entry),
  418. slapi_entry_get_dn_const(addingentry->ep_entry));
  419. /* LPREPL: the DN is normalized...Somehow who should get a not normalized one */
  420. addingentry->ep_id = slapi_entry_attr_get_ulong(addingentry->ep_entry,"entryid");
  421. slapi_entry_attr_delete(addingentry->ep_entry, SLAPI_ATTR_VALUE_PARENT_UNIQUEID);
  422. slapi_entry_delete_string(addingentry->ep_entry, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  423. /* Now also remove the nscpEntryDN */
  424. if (slapi_entry_attr_delete(addingentry->ep_entry, SLAPI_ATTR_NSCP_ENTRYDN) != 0){
  425. LDAPDebug(LDAP_DEBUG_REPL, "Resurrection of %s - Couldn't remove %s\n",
  426. slapi_sdn_get_dn(sdn), SLAPI_ATTR_NSCP_ENTRYDN, 0);
  427. }
  428. /* And copy the reason from e */
  429. reason = slapi_entry_attr_get_charptr(e, "nsds5ReplConflict");
  430. if (reason) {
  431. if (!slapi_entry_attr_hasvalue(addingentry->ep_entry, "nsds5ReplConflict", reason)) {
  432. slapi_entry_add_string(addingentry->ep_entry, "nsds5ReplConflict", reason);
  433. LDAPDebug(LDAP_DEBUG_REPL, "Resurrection of %s - Added Conflict reason %s\n",
  434. slapi_sdn_get_dn(sdn), reason, 0);
  435. }
  436. slapi_ch_free((void **)&reason);
  437. }
  438. /* Clear the Tombstone Flag in the entry */
  439. slapi_entry_clear_flag(addingentry->ep_entry, SLAPI_ENTRY_FLAG_TOMBSTONE);
  440. /* make sure the objectclass
  441. - does not contain any duplicate values
  442. - has CSNs for the new values we added
  443. */
  444. {
  445. Slapi_Attr *sa = NULL;
  446. Slapi_Value sv;
  447. const struct berval *svbv = NULL;
  448. /* add the extensibleobject objectclass with csn if not present */
  449. slapi_entry_attr_find(addingentry->ep_entry, SLAPI_ATTR_OBJECTCLASS, &sa);
  450. slapi_value_init_string(&sv, "extensibleobject");
  451. svbv = slapi_value_get_berval(&sv);
  452. if (slapi_attr_value_find(sa, svbv)) { /* not found, so add it */
  453. if (opcsn) {
  454. value_update_csn(&sv, CSN_TYPE_VALUE_UPDATED, opcsn);
  455. }
  456. slapi_attr_add_value(sa, &sv);
  457. }
  458. value_done(&sv);
  459. /* add the glue objectclass with csn if not present */
  460. slapi_value_init_string(&sv, "glue");
  461. svbv = slapi_value_get_berval(&sv);
  462. if (slapi_attr_value_find(sa, svbv)) { /* not found, so add it */
  463. if (opcsn) {
  464. value_update_csn(&sv, CSN_TYPE_VALUE_UPDATED, opcsn);
  465. }
  466. slapi_attr_add_value(sa, &sv);
  467. }
  468. value_done(&sv);
  469. }
  470. }
  471. else
  472. {
  473. /*
  474. * Try to add the entry to the cache, assign it a new entryid
  475. * and mark it locked. This should only fail if the entry
  476. * already exists.
  477. */
  478. /*
  479. * next_id will add this id to the list of ids that are pending
  480. * id2entry indexing.
  481. */
  482. addingentry = backentry_init( e );
  483. if ( ( addingentry->ep_id = next_id( be ) ) >= MAXID ) {
  484. LDAPDebug( LDAP_DEBUG_ANY,
  485. "add: maximum ID reached, cannot add entry to "
  486. "backend '%s'", be->be_name, 0, 0 );
  487. ldap_result_code = LDAP_OPERATIONS_ERROR;
  488. goto error_return;
  489. }
  490. addingentry_id_assigned= 1;
  491. if (!is_fixup_operation)
  492. {
  493. if ( opcsn == NULL && operation->o_csngen_handler )
  494. {
  495. /*
  496. * Current op is a user request. Opcsn will be assigned
  497. * if the dn is in an updatable replica.
  498. */
  499. opcsn = entry_assign_operation_csn ( pb, e, parententry ? parententry->ep_entry : NULL );
  500. }
  501. if ( opcsn != NULL )
  502. {
  503. entry_set_csn (e, opcsn);
  504. entry_add_dncsn (e, opcsn);
  505. entry_add_rdn_csn (e, opcsn);
  506. entry_set_maxcsn (e, opcsn);
  507. }
  508. }
  509. if (is_tombstone_operation)
  510. {
  511. /* Directly add the entry as a tombstone */
  512. /*
  513. * 1) If the entry has an existing DN, change it to be
  514. * "nsuniqueid=<uniqueid>, <old dn>"
  515. * 2) Add the objectclass value "tombstone" and arrange for only
  516. * that value to be indexed.
  517. * 3) If the parent entry was found, set the nsparentuniqueid
  518. * attribute to be the unique id of that parent.
  519. */
  520. char *untombstoned_dn = slapi_entry_get_dn(e);
  521. char *tombstoned_dn = NULL;
  522. if (NULL == untombstoned_dn)
  523. {
  524. untombstoned_dn = "";
  525. }
  526. tombstoned_dn = compute_entry_tombstone_dn(untombstoned_dn, addr.uniqueid);
  527. /*
  528. * This needs to be done before slapi_entry_set_dn call,
  529. * because untombstoned_dn is released in slapi_entry_set_dn.
  530. */
  531. if (entryrdn_get_switch())
  532. {
  533. Slapi_RDN srdn = {0};
  534. rc = slapi_rdn_init_all_dn(&srdn, tombstoned_dn);
  535. if (rc) {
  536. LDAPDebug1Arg( LDAP_DEBUG_TRACE,
  537. "ldbm_back_add (tombstone_operation): failed to "
  538. "decompose %s to Slapi_RDN\n", tombstoned_dn);
  539. } else {
  540. slapi_entry_set_srdn(e, &srdn);
  541. slapi_rdn_done(&srdn);
  542. }
  543. }
  544. slapi_entry_set_dn(addingentry->ep_entry, tombstoned_dn);
  545. /* Work around pb with slapi_entry_add_string (defect 522327)
  546. * doesn't check duplicate values */
  547. if (!slapi_entry_attr_hasvalue(addingentry->ep_entry,
  548. SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE)) {
  549. slapi_entry_add_string(addingentry->ep_entry,
  550. SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE);
  551. slapi_entry_set_flag(addingentry->ep_entry,
  552. SLAPI_ENTRY_FLAG_TOMBSTONE);
  553. }
  554. if (NULL != operation->o_params.p.p_add.parentuniqueid)
  555. {
  556. slapi_entry_add_string(addingentry->ep_entry,
  557. SLAPI_ATTR_VALUE_PARENT_UNIQUEID,
  558. operation->o_params.p.p_add.parentuniqueid);
  559. }
  560. }
  561. }
  562. /*
  563. * Get the parent dn and see if the corresponding entry exists.
  564. * If the parent does not exist, only allow the "root" user to
  565. * add the entry.
  566. */
  567. if ( !slapi_sdn_isempty(&parentsdn) )
  568. {
  569. /* This is getting the parent */
  570. if (NULL == parententry)
  571. {
  572. /* Here means that we didn't find the parent */
  573. int err = 0;
  574. Slapi_DN ancestorsdn;
  575. struct backentry *ancestorentry;
  576. LDAPDebug1Arg(LDAP_DEBUG_BACKLDBM, "ldbm_add: Parent \"%s\" does not exist. "
  577. "It might be a conflict entry.\n", slapi_sdn_get_dn(&parentsdn));
  578. slapi_sdn_init(&ancestorsdn);
  579. ancestorentry = dn2ancestor(be, &parentsdn, &ancestorsdn, &txn, &err );
  580. CACHE_RETURN( &inst->inst_cache, &ancestorentry );
  581. ldap_result_code= LDAP_NO_SUCH_OBJECT;
  582. ldap_result_matcheddn= slapi_ch_strdup((char *)slapi_sdn_get_dn(&ancestorsdn)); /* jcm - cast away const. */
  583. slapi_sdn_done(&ancestorsdn);
  584. goto error_return;
  585. }
  586. ldap_result_code = plugin_call_acl_plugin(pb, e, NULL, NULL, SLAPI_ACL_ADD,
  587. ACLPLUGIN_ACCESS_DEFAULT, &errbuf );
  588. if ( ldap_result_code != LDAP_SUCCESS )
  589. {
  590. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "no access to parent, pdn = %s\n",
  591. slapi_sdn_get_dn(&parentsdn));
  592. ldap_result_message= errbuf;
  593. goto error_return;
  594. }
  595. pid = parententry->ep_id;
  596. /* We may need to adjust the DN since parent could be a resrected conflict entry... */
  597. if (!slapi_sdn_isparent(slapi_entry_get_sdn_const(parententry->ep_entry),
  598. slapi_entry_get_sdn_const(addingentry->ep_entry))) {
  599. Slapi_DN adjustedsdn = {0};
  600. char *adjusteddn = slapi_ch_smprintf("%s,%s",
  601. slapi_entry_get_rdn_const(addingentry->ep_entry),
  602. slapi_entry_get_dn_const(parententry->ep_entry));
  603. LDAPDebug2Args(LDAP_DEBUG_BACKLDBM, "ldbm_add: adjusting dn: %s --> %s\n",
  604. slapi_entry_get_dn(addingentry->ep_entry), adjusteddn);
  605. slapi_sdn_set_normdn_passin(&adjustedsdn, adjusteddn);
  606. slapi_entry_set_sdn(addingentry->ep_entry, &adjustedsdn);
  607. /* not just e_sdn, e_rsdn needs to be updated. */
  608. slapi_rdn_set_all_dn(slapi_entry_get_srdn(addingentry->ep_entry), adjusteddn);
  609. slapi_sdn_done(&adjustedsdn);
  610. }
  611. }
  612. else
  613. { /* no parent */
  614. if (!isroot && !is_replicated_operation)
  615. {
  616. LDAPDebug0Args(LDAP_DEBUG_TRACE, "no parent & not root\n");
  617. ldap_result_code= LDAP_INSUFFICIENT_ACCESS;
  618. goto error_return;
  619. }
  620. parententry = NULL;
  621. pid = 0;
  622. }
  623. if(is_resurect_operation)
  624. {
  625. add_update_entrydn_operational_attributes(addingentry);
  626. }
  627. else if (is_tombstone_operation)
  628. {
  629. /* Remove the entrydn operational attributes from the addingentry */
  630. delete_update_entrydn_operational_attributes(addingentry);
  631. }
  632. else
  633. {
  634. /*
  635. * add the parentid, entryid and entrydn operational attributes
  636. */
  637. add_update_entry_operational_attributes(addingentry, pid);
  638. }
  639. /* Tentatively add the entry to the cache. We do this after adding any
  640. * operational attributes to ensure that the cache is sized correctly. */
  641. if ( cache_add_tentative( &inst->inst_cache, addingentry, NULL ) < 0 )
  642. {
  643. LDAPDebug1Arg(LDAP_DEBUG_CACHE, "cache_add_tentative concurrency detected: %s\n",
  644. slapi_entry_get_dn_const(addingentry->ep_entry));
  645. ldap_result_code= LDAP_ALREADY_EXISTS;
  646. goto error_return;
  647. }
  648. /*
  649. * Before we add the entry, find out if the syntax of the aci
  650. * aci attribute values are correct or not. We don't want to
  651. * the entry if the syntax is incorrect.
  652. */
  653. if ( plugin_call_acl_verify_syntax (pb, addingentry->ep_entry, &errbuf) != 0 ) {
  654. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "ACL syntax error: %s\n",
  655. slapi_entry_get_dn_const(addingentry->ep_entry));
  656. ldap_result_code= LDAP_INVALID_SYNTAX;
  657. ldap_result_message= errbuf;
  658. goto error_return;
  659. }
  660. /* Having decided that we're really going to do the operation, let's modify
  661. the in-memory state of the parent to reflect the new child (update
  662. subordinate count specifically */
  663. if (parententry)
  664. {
  665. retval = parent_update_on_childchange(&parent_modify_c,
  666. is_resurect_operation?PARENTUPDATE_RESURECT:PARENTUPDATE_ADD,
  667. NULL);
  668. /* The modify context now contains info needed later */
  669. if (retval) {
  670. LDAPDebug2Args(LDAP_DEBUG_BACKLDBM, "parent_update_on_childchange: %s, rc=%d\n",
  671. slapi_entry_get_dn_const(addingentry->ep_entry), retval);
  672. ldap_result_code= LDAP_OPERATIONS_ERROR;
  673. goto error_return;
  674. }
  675. parent_found = 1;
  676. parententry = NULL;
  677. }
  678. if ( (originalentry = backentry_dup(addingentry )) == NULL ) {
  679. ldap_result_code= LDAP_OPERATIONS_ERROR;
  680. goto error_return;
  681. }
  682. /*
  683. * So, we believe that no code up till here actually added anything
  684. * to persistent store. From now on, we're transacted
  685. */
  686. txn.back_txn_txn = NULL; /* ready to create the child transaction */
  687. for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++) {
  688. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  689. dblayer_txn_abort(li,&txn);
  690. noabort = 1;
  691. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  692. /* must duplicate addingentry before returning it to cache,
  693. * which could free the entry. */
  694. if ( (tmpentry = backentry_dup( addingentry )) == NULL ) {
  695. ldap_result_code= LDAP_OPERATIONS_ERROR;
  696. goto error_return;
  697. }
  698. if (cache_is_in_cache(&inst->inst_cache, addingentry)) {
  699. /* addingentry is in cache. Remove it once. */
  700. retval = CACHE_REMOVE(&inst->inst_cache, addingentry);
  701. if (retval) {
  702. LDAPDebug1Arg(LDAP_DEBUG_CACHE, "ldbm_add: cache_remove %s failed.\n",
  703. slapi_entry_get_dn_const(addingentry->ep_entry));
  704. }
  705. }
  706. CACHE_RETURN(&inst->inst_cache, &addingentry);
  707. slapi_pblock_set( pb, SLAPI_ADD_ENTRY, originalentry->ep_entry );
  708. addingentry = originalentry;
  709. originalentry = tmpentry;
  710. tmpentry = NULL;
  711. /* Adding the resetted addingentry to the cache. */
  712. if (cache_add_tentative(&inst->inst_cache, addingentry, NULL) < 0) {
  713. LDAPDebug1Arg(LDAP_DEBUG_CACHE, "cache_add_tentative concurrency detected: %s\n",
  714. slapi_entry_get_dn_const(addingentry->ep_entry));
  715. ldap_result_code = LDAP_ALREADY_EXISTS;
  716. goto error_return;
  717. }
  718. if (ruv_c_init) {
  719. /* reset the ruv txn stuff */
  720. modify_term(&ruv_c, be);
  721. ruv_c_init = 0;
  722. }
  723. /* We're re-trying */
  724. LDAPDebug0Args(LDAP_DEBUG_BACKLDBM, "Add Retrying Transaction\n");
  725. #ifndef LDBM_NO_BACKOFF_DELAY
  726. {
  727. PRIntervalTime interval;
  728. interval = PR_MillisecondsToInterval(slapi_rand() % 100);
  729. DS_Sleep(interval);
  730. }
  731. #endif
  732. }
  733. retval = dblayer_txn_begin(li,parent_txn,&txn);
  734. if (0 != retval) {
  735. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  736. disk_full = 1;
  737. ldap_result_code= LDAP_OPERATIONS_ERROR;
  738. goto diskfull_return;
  739. }
  740. ldap_result_code= LDAP_OPERATIONS_ERROR;
  741. goto error_return;
  742. }
  743. noabort = 0;
  744. /* stash the transaction for plugins */
  745. slapi_pblock_set(pb, SLAPI_TXN, txn.back_txn_txn);
  746. /* call the transaction pre add plugins just after creating the transaction */
  747. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN))) {
  748. int opreturn = 0;
  749. LDAPDebug1Arg(LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN plugin "
  750. "returned error code %d\n", retval );
  751. if (!ldap_result_code) {
  752. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  753. }
  754. if (!ldap_result_code) {
  755. ldap_result_code = LDAP_OPERATIONS_ERROR;
  756. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  757. }
  758. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  759. if (!opreturn) {
  760. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  761. }
  762. LDAPDebug1Arg(LDAP_DEBUG_ANY, "SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN plugin failed: %d",
  763. ldap_result_code ? ldap_result_code : retval);
  764. goto error_return;
  765. }
  766. retval = id2entry_add( be, addingentry, &txn );
  767. if (DB_LOCK_DEADLOCK == retval)
  768. {
  769. LDAPDebug( LDAP_DEBUG_ARGS, "add 1 DEADLOCK\n", 0, 0, 0 );
  770. /* Retry txn */
  771. continue;
  772. }
  773. if (retval) {
  774. LDAPDebug(LDAP_DEBUG_TRACE, "id2entry_add(%s) failed, err=%d %s\n",
  775. slapi_entry_get_dn_const(addingentry->ep_entry),
  776. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  777. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  778. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  779. disk_full = 1;
  780. goto diskfull_return;
  781. }
  782. goto error_return;
  783. }
  784. if(is_resurect_operation)
  785. {
  786. retval = index_addordel_string(be,SLAPI_ATTR_OBJECTCLASS,SLAPI_ATTR_VALUE_TOMBSTONE,addingentry->ep_id,BE_INDEX_DEL,&txn);
  787. if (DB_LOCK_DEADLOCK == retval) {
  788. LDAPDebug( LDAP_DEBUG_ARGS, "add 2 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  789. /* Retry txn */
  790. continue;
  791. }
  792. if (retval) {
  793. LDAPDebug(LDAP_DEBUG_TRACE, "index_addordel_string TOMBSTONE (%s), err=%d %s\n",
  794. slapi_entry_get_dn_const(addingentry->ep_entry),
  795. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  796. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  797. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  798. disk_full = 1;
  799. goto diskfull_return;
  800. }
  801. goto error_return;
  802. }
  803. retval = index_addordel_string(be,SLAPI_ATTR_UNIQUEID,slapi_entry_get_uniqueid(addingentry->ep_entry),addingentry->ep_id,BE_INDEX_DEL,&txn);
  804. if (DB_LOCK_DEADLOCK == retval) {
  805. LDAPDebug( LDAP_DEBUG_ARGS, "add 3 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  806. /* Retry txn */
  807. continue;
  808. }
  809. if (0 != retval) {
  810. LDAPDebug(LDAP_DEBUG_TRACE, "index_addordel_string UNIQUEID (%s), err=%d %s\n",
  811. slapi_entry_get_dn_const(addingentry->ep_entry),
  812. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  813. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  814. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  815. disk_full = 1;
  816. goto diskfull_return;
  817. }
  818. goto error_return;
  819. }
  820. retval = index_addordel_string(be,
  821. SLAPI_ATTR_NSCP_ENTRYDN,
  822. slapi_sdn_get_ndn(sdn),
  823. addingentry->ep_id,
  824. BE_INDEX_DEL, &txn);
  825. if (DB_LOCK_DEADLOCK == retval) {
  826. LDAPDebug( LDAP_DEBUG_ARGS, "add 4 DB_LOCK_DEADLOCK\n", 0, 0, 0 );
  827. /* Retry txn */
  828. continue;
  829. }
  830. if (0 != retval) {
  831. LDAPDebug(LDAP_DEBUG_TRACE, "index_addordel_string ENTRYDN (%s), err=%d %s\n",
  832. slapi_entry_get_dn_const(addingentry->ep_entry),
  833. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  834. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  835. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  836. disk_full = 1;
  837. goto diskfull_return;
  838. }
  839. goto error_return;
  840. }
  841. /* Need to delete the entryrdn index of the resurrected tombstone... */
  842. if (entryrdn_get_switch()) { /* subtree-rename: on */
  843. if (tombstoneentry) {
  844. retval = entryrdn_index_entry(be, tombstoneentry, BE_INDEX_DEL, &txn);
  845. if (retval) {
  846. LDAPDebug(LDAP_DEBUG_ANY, "Resurrecting %s: failed to remove entryrdn index, err=%d %s\n",
  847. slapi_entry_get_dn_const(tombstoneentry->ep_entry),
  848. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  849. goto error_return;
  850. }
  851. }
  852. }
  853. }
  854. if (is_tombstone_operation)
  855. {
  856. retval = index_addordel_entry( be, addingentry, BE_INDEX_ADD | BE_INDEX_TOMBSTONE, &txn );
  857. }
  858. else
  859. {
  860. retval = index_addordel_entry( be, addingentry, BE_INDEX_ADD, &txn );
  861. }
  862. if (DB_LOCK_DEADLOCK == retval)
  863. {
  864. LDAPDebug( LDAP_DEBUG_ARGS, "add 5 DEADLOCK\n", 0, 0, 0 );
  865. /* retry txn */
  866. continue;
  867. }
  868. if (retval) {
  869. LDAPDebug2Args(LDAP_DEBUG_ANY, "add: attempt to index %lu failed; rc=%d\n",
  870. (u_long)addingentry->ep_id, retval);
  871. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  872. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  873. disk_full = 1;
  874. goto diskfull_return;
  875. }
  876. goto error_return;
  877. }
  878. if (parent_found) {
  879. /* Push out the db modifications from the parent entry */
  880. retval = modify_update_all(be,pb,&parent_modify_c,&txn);
  881. if (DB_LOCK_DEADLOCK == retval)
  882. {
  883. LDAPDebug( LDAP_DEBUG_ARGS, "add 6 DEADLOCK\n", 0, 0, 0 );
  884. /* Retry txn */
  885. continue;
  886. }
  887. if (retval) {
  888. LDAPDebug(LDAP_DEBUG_BACKLDBM, "modify_update_all: %s (%lu) failed; rc=%d\n",
  889. slapi_entry_get_dn(addingentry->ep_entry), (u_long)addingentry->ep_id, retval);
  890. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  891. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  892. disk_full = 1;
  893. goto diskfull_return;
  894. }
  895. goto error_return;
  896. }
  897. }
  898. /*
  899. * Update the Virtual List View indexes
  900. */
  901. if (!is_ruv)
  902. {
  903. retval= vlv_update_all_indexes(&txn, be, pb, NULL, addingentry);
  904. if (DB_LOCK_DEADLOCK == retval) {
  905. LDAPDebug(LDAP_DEBUG_ARGS,
  906. "add DEADLOCK vlv_update_index\n", 0, 0, 0 );
  907. /* Retry txn */
  908. continue;
  909. }
  910. if (retval) {
  911. LDAPDebug2Args(LDAP_DEBUG_TRACE,
  912. "vlv_update_index failed, err=%d %s\n",
  913. retval, (msg = dblayer_strerror( retval )) ? msg : "");
  914. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  915. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  916. disk_full = 1;
  917. goto diskfull_return;
  918. }
  919. goto error_return;
  920. }
  921. }
  922. if (!is_ruv && !is_fixup_operation) {
  923. ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
  924. if (-1 == ruv_c_init) {
  925. LDAPDebug( LDAP_DEBUG_ANY,
  926. "ldbm_back_add: ldbm_txn_ruv_modify_context "
  927. "failed to construct RUV modify context\n",
  928. 0, 0, 0);
  929. ldap_result_code= LDAP_OPERATIONS_ERROR;
  930. retval = 0;
  931. goto error_return;
  932. }
  933. }
  934. if (ruv_c_init) {
  935. retval = modify_update_all( be, pb, &ruv_c, &txn );
  936. if (DB_LOCK_DEADLOCK == retval) {
  937. /* Abort and re-try */
  938. continue;
  939. }
  940. if (0 != retval) {
  941. LDAPDebug( LDAP_DEBUG_ANY,
  942. "modify_update_all failed, err=%d %s\n", retval,
  943. (msg = dblayer_strerror( retval )) ? msg : "", 0 );
  944. if (LDBM_OS_ERR_IS_DISKFULL(retval))
  945. disk_full = 1;
  946. ldap_result_code= LDAP_OPERATIONS_ERROR;
  947. goto error_return;
  948. }
  949. }
  950. if (retval == 0) {
  951. break;
  952. }
  953. }
  954. if (retry_count == RETRY_TIMES) {
  955. /* Failed */
  956. LDAPDebug( LDAP_DEBUG_ANY, "Retry count exceeded in add\n", 0, 0, 0 );
  957. ldap_result_code= LDAP_BUSY;
  958. goto error_return;
  959. }
  960. /*
  961. * At this point, everything's cool, and the only thing which
  962. * can go wrong is a transaction commit failure.
  963. */
  964. slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, NULL );
  965. slapi_pblock_set( pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup( addingentry->ep_entry ));
  966. if(is_resurect_operation)
  967. {
  968. /*
  969. * We can now switch the tombstone entry with the real entry.
  970. */
  971. retval = cache_replace(&inst->inst_cache, tombstoneentry, addingentry);
  972. if (retval) {
  973. /* This happens if the dn of addingentry already exists */
  974. ADD_SET_ERROR(ldap_result_code, LDAP_ALREADY_EXISTS, retry_count);
  975. LDAPDebug2Args(LDAP_DEBUG_CACHE, "ldap_add: cache_replace concurrency detected: %s (rc: %d)\n",
  976. slapi_entry_get_dn_const(addingentry->ep_entry), retval);
  977. retval = -1;
  978. goto error_return;
  979. }
  980. /*
  981. * The tombstone was locked down in the cache... we can
  982. * get rid of the entry in the cache now.
  983. * We cannot expect tombstoneentry exists from now on.
  984. */
  985. if (entryrdn_get_switch()) { /* subtree-rename: on */
  986. /* since the op was successful, delete the tombstone dn from the dn cache */
  987. struct backdn *bdn = dncache_find_id(&inst->inst_dncache,
  988. tombstoneentry->ep_id);
  989. if (bdn) { /* in the dncache, remove it. */
  990. CACHE_REMOVE(&inst->inst_dncache, bdn);
  991. CACHE_RETURN(&inst->inst_dncache, &bdn);
  992. }
  993. }
  994. }
  995. if (parent_found)
  996. {
  997. /* switch the parent entry copy into play */
  998. modify_switch_entries(&parent_modify_c,be);
  999. parent_switched = 1;
  1000. }
  1001. if (ruv_c_init) {
  1002. if (modify_switch_entries(&ruv_c, be) != 0 ) {
  1003. ldap_result_code= LDAP_OPERATIONS_ERROR;
  1004. LDAPDebug0Args(LDAP_DEBUG_ANY,
  1005. "ldbm_back_add: modify_switch_entries failed\n");
  1006. goto error_return;
  1007. }
  1008. }
  1009. /* call the transaction post add plugins just before the commit */
  1010. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_ADD_FN))) {
  1011. int opreturn = 0;
  1012. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_ADD_FN plugin "
  1013. "returned error code %d\n", retval );
  1014. if (!ldap_result_code) {
  1015. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1016. }
  1017. if (!ldap_result_code) {
  1018. ldap_result_code = LDAP_OPERATIONS_ERROR;
  1019. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1020. }
  1021. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  1022. if (!opreturn) {
  1023. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval);
  1024. }
  1025. goto error_return;
  1026. }
  1027. retval = dblayer_txn_commit(li,&txn);
  1028. /* after commit - txn is no longer valid - replace SLAPI_TXN with parent */
  1029. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  1030. if (0 != retval)
  1031. {
  1032. ADD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
  1033. if (LDBM_OS_ERR_IS_DISKFULL(retval)) {
  1034. disk_full = 1;
  1035. goto diskfull_return;
  1036. }
  1037. goto error_return;
  1038. }
  1039. rc= 0;
  1040. goto common_return;
  1041. error_return:
  1042. if ( addingentry_id_assigned )
  1043. {
  1044. next_id_return( be, addingentry->ep_id );
  1045. }
  1046. if (rc == DB_RUNRECOVERY) {
  1047. dblayer_remember_disk_filled(li);
  1048. ldbm_nasty("Add",80,rc);
  1049. disk_full = 1;
  1050. } else if (0 == rc) {
  1051. rc = SLAPI_FAIL_GENERAL;
  1052. }
  1053. if (parent_switched){
  1054. /*
  1055. * Restore the old parent entry, switch the new with the original.
  1056. * Otherwise the numsubordinate count will be off, and could later
  1057. * be written to disk.
  1058. */
  1059. modify_unswitch_entries( &parent_modify_c,be);
  1060. }
  1061. diskfull_return:
  1062. if (disk_full) {
  1063. if ( addingentry )
  1064. {
  1065. if (inst && cache_is_in_cache(&inst->inst_cache, addingentry)) {
  1066. CACHE_REMOVE(&inst->inst_cache, addingentry);
  1067. /* tell frontend not to free this entry */
  1068. slapi_pblock_set(pb, SLAPI_ADD_ENTRY, NULL);
  1069. }
  1070. else if (!cache_has_otherref(&inst->inst_cache, addingentry))
  1071. {
  1072. if (!is_resurect_operation) { /* if resurect, tombstoneentry is dupped. */
  1073. backentry_clear_entry(addingentry); /* e is released in the frontend */
  1074. }
  1075. }
  1076. CACHE_RETURN( &inst->inst_cache, &addingentry );
  1077. }
  1078. rc = return_on_disk_full(li);
  1079. } else {
  1080. /* It is safer not to abort when the transaction is not started. */
  1081. if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn)) {
  1082. /* make sure SLAPI_RESULT_CODE and SLAPI_PLUGIN_OPRETURN are set */
  1083. int val = 0;
  1084. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &val);
  1085. if (!val) {
  1086. if (!ldap_result_code) {
  1087. ldap_result_code = LDAP_OPERATIONS_ERROR;
  1088. }
  1089. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1090. }
  1091. slapi_pblock_get( pb, SLAPI_PLUGIN_OPRETURN, &val );
  1092. if (!val) {
  1093. val = -1;
  1094. slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, &val );
  1095. }
  1096. /* call the transaction post add plugins just before the abort */
  1097. if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_ADD_FN))) {
  1098. int opreturn = 0;
  1099. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_POST_ADD_FN plugin "
  1100. "returned error code %d\n", retval );
  1101. if (!ldap_result_code) {
  1102. slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1103. }
  1104. slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  1105. if (!opreturn) {
  1106. opreturn = -1;
  1107. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
  1108. }
  1109. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
  1110. }
  1111. if ( addingentry ) {
  1112. if (inst && cache_is_in_cache(&inst->inst_cache, addingentry)) {
  1113. CACHE_REMOVE(&inst->inst_cache, addingentry);
  1114. /* tell frontend not to free this entry */
  1115. slapi_pblock_set(pb, SLAPI_ADD_ENTRY, NULL);
  1116. }
  1117. else if (!cache_has_otherref(&inst->inst_cache, addingentry))
  1118. {
  1119. if (!is_resurect_operation) { /* if resurect, tombstoneentry is dupped. */
  1120. backentry_clear_entry(addingentry); /* e is released in the frontend */
  1121. }
  1122. }
  1123. CACHE_RETURN( &inst->inst_cache, &addingentry );
  1124. }
  1125. if (!noabort) {
  1126. dblayer_txn_abort(li,&txn); /* abort crashes in case disk full */
  1127. }
  1128. /* txn is no longer valid - reset the txn pointer to the parent */
  1129. slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
  1130. } else {
  1131. if ( addingentry ) {
  1132. if (inst && cache_is_in_cache(&inst->inst_cache, addingentry)) {
  1133. CACHE_REMOVE(&inst->inst_cache, addingentry);
  1134. /* tell frontend not to free this entry */
  1135. slapi_pblock_set(pb, SLAPI_ADD_ENTRY, NULL);
  1136. }
  1137. else if (!cache_has_otherref(&inst->inst_cache, addingentry))
  1138. {
  1139. if (!is_resurect_operation) { /* if resurect, tombstoneentry is dupped. */
  1140. backentry_clear_entry(addingentry); /* e is released in the frontend */
  1141. }
  1142. }
  1143. CACHE_RETURN( &inst->inst_cache, &addingentry );
  1144. }
  1145. }
  1146. rc = SLAPI_FAIL_GENERAL;
  1147. }
  1148. common_return:
  1149. if (inst) {
  1150. if (tombstoneentry && cache_is_in_cache(&inst->inst_cache, tombstoneentry)) {
  1151. cache_unlock_entry(&inst->inst_cache, tombstoneentry);
  1152. CACHE_RETURN(&inst->inst_cache, &tombstoneentry);
  1153. }
  1154. if (addingentry) {
  1155. if ((0 == retval) && entryrdn_get_switch()) { /* subtree-rename: on */
  1156. /* since adding the entry to the entry cache was successful,
  1157. * let's add the dn to dncache, if not yet done. */
  1158. struct backdn *bdn = dncache_find_id(&inst->inst_dncache,
  1159. addingentry->ep_id);
  1160. if (bdn) { /* already in the dncache */
  1161. CACHE_RETURN(&inst->inst_dncache, &bdn);
  1162. } else { /* not in the dncache yet */
  1163. Slapi_DN *addingsdn = slapi_sdn_dup(slapi_entry_get_sdn(addingentry->ep_entry));
  1164. if (addingsdn) {
  1165. bdn = backdn_init(addingsdn, addingentry->ep_id, 0);
  1166. if (bdn) {
  1167. CACHE_ADD( &inst->inst_dncache, bdn, NULL );
  1168. CACHE_RETURN(&inst->inst_dncache, &bdn);
  1169. slapi_log_error(SLAPI_LOG_CACHE, "ldbm_back_add",
  1170. "set %s to dn cache\n", dn);
  1171. }
  1172. }
  1173. }
  1174. }
  1175. CACHE_RETURN( &inst->inst_cache, &addingentry );
  1176. }
  1177. }
  1178. /* bepost op needs to know this result */
  1179. slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
  1180. /* JCMREPL - The bepostop is called even if the operation fails. */
  1181. plugin_call_plugins (pb, SLAPI_PLUGIN_BE_POST_ADD_FN);
  1182. if (ruv_c_init) {
  1183. modify_term(&ruv_c, be);
  1184. }
  1185. modify_term(&parent_modify_c,be);
  1186. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_DN_ENTRY);
  1187. done_with_pblock_entry(pb,SLAPI_ADD_EXISTING_UNIQUEID_ENTRY);
  1188. done_with_pblock_entry(pb,SLAPI_ADD_PARENT_ENTRY);
  1189. if(dblock_acquired)
  1190. {
  1191. dblayer_unlock_backend(be);
  1192. }
  1193. if(ldap_result_code!=-1)
  1194. {
  1195. slapi_send_ldap_result( pb, ldap_result_code, ldap_result_matcheddn, ldap_result_message, 0, NULL );
  1196. }
  1197. backentry_free(&originalentry);
  1198. backentry_free(&tmpentry);
  1199. slapi_sdn_done(&parentsdn);
  1200. slapi_ch_free( (void**)&ldap_result_matcheddn );
  1201. slapi_ch_free( (void**)&errbuf );
  1202. return rc;
  1203. }
  1204. /*
  1205. * add the parentid, entryid and entrydn, operational attributes.
  1206. *
  1207. * Note: This is called from the ldif2ldbm code.
  1208. */
  1209. void
  1210. add_update_entry_operational_attributes(struct backentry *ep, ID pid)
  1211. {
  1212. struct berval bv;
  1213. struct berval *bvp[2];
  1214. char buf[40]; /* Enough for an EntryID */
  1215. bvp[0] = &bv;
  1216. bvp[1] = NULL;
  1217. /* parentid */
  1218. /* If the pid is 0, then the entry does not have a parent. It
  1219. * may be the case that the entry is a suffix. In any case,
  1220. * the parentid attribute should only be added if the entry
  1221. * has a parent. */
  1222. if (pid != 0) {
  1223. sprintf( buf, "%lu", (u_long)pid );
  1224. bv.bv_val = buf;
  1225. bv.bv_len = strlen( buf );
  1226. entry_replace_values( ep->ep_entry, LDBM_PARENTID_STR, bvp );
  1227. }
  1228. /* entryid */
  1229. sprintf( buf, "%lu", (u_long)ep->ep_id );
  1230. bv.bv_val = buf;
  1231. bv.bv_len = strlen( buf );
  1232. entry_replace_values( ep->ep_entry, "entryid", bvp );
  1233. /* add the entrydn operational attribute to the entry. */
  1234. add_update_entrydn_operational_attributes(ep);
  1235. }
  1236. /*
  1237. * add the entrydn operational attribute to the entry.
  1238. */
  1239. void
  1240. add_update_entrydn_operational_attributes(struct backentry *ep)
  1241. {
  1242. struct berval bv;
  1243. struct berval *bvp[2];
  1244. /* entrydn */
  1245. bvp[0] = &bv;
  1246. bvp[1] = NULL;
  1247. bv.bv_val = (void*)backentry_get_ndn(ep);
  1248. bv.bv_len = strlen( bv.bv_val );
  1249. entry_replace_values_with_flags( ep->ep_entry, LDBM_ENTRYDN_STR, bvp,
  1250. SLAPI_ATTR_FLAG_NORMALIZED_CIS );
  1251. }
  1252. /*
  1253. * delete the entrydn operational attributes from the entry.
  1254. */
  1255. static void
  1256. delete_update_entrydn_operational_attributes(struct backentry *ep)
  1257. {
  1258. /* entrydn */
  1259. slapi_entry_attr_delete( ep->ep_entry, LDBM_ENTRYDN_STR);
  1260. }