ldbm_add.c 42 KB

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