ldbm_add.c 42 KB

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