xref: /titanic_50/usr/src/lib/libnisdb/db_dictionary.cc (revision 8d0852b7f8176b2c281cc944010af2b0fecf9fb2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	db_dictionary.cc
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "db_headers.h"
307c478bd9Sstevel@tonic-gate #include "db_entry.h"
317c478bd9Sstevel@tonic-gate #include "db_dictionary.h"
327c478bd9Sstevel@tonic-gate #include "db_dictlog.h"
337c478bd9Sstevel@tonic-gate #include "db_vers.h"
347c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
357c478bd9Sstevel@tonic-gate #include "nisdb_rw.h"
367c478bd9Sstevel@tonic-gate #include "ldap_parse.h"
377c478bd9Sstevel@tonic-gate #include "ldap_map.h"
387c478bd9Sstevel@tonic-gate #include "nis_hashitem.h"
397c478bd9Sstevel@tonic-gate #include "ldap_util.h"
407c478bd9Sstevel@tonic-gate #include "nis_db.h"
417c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <malloc.h>
467c478bd9Sstevel@tonic-gate #ifdef TDRPC
477c478bd9Sstevel@tonic-gate #include <sysent.h>
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <syslog.h>
517c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate typedef bool_t	(*db_func)(XDR *, db_table_desc *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern db_dictionary *InUseDictionary;
567c478bd9Sstevel@tonic-gate extern db_dictionary *FreeDictionary;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* *************** dictionary version ****************** */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	DB_MAGIC 0x12340000
617c478bd9Sstevel@tonic-gate #define	DB_MAJOR 0
627c478bd9Sstevel@tonic-gate #define	DB_MINOR 10
637c478bd9Sstevel@tonic-gate #define	DB_VERSION_0_9	(DB_MAGIC|(DB_MAJOR<<8)|9)
647c478bd9Sstevel@tonic-gate #define	DB_ORIG_VERSION	DB_VERSION_0_9
657c478bd9Sstevel@tonic-gate #define	DB_CURRENT_VERSION (DB_MAGIC|DB_MAJOR<<8|DB_MINOR)
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate vers db_update_version;   /* Note 'global' for all dbs. */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	INMEMORY_ONLY 1
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Checks for valid version.  For now, there are two:
737c478bd9Sstevel@tonic-gate  * DB_VERSION_ORIG was the ORIGINAL one with major = 0, minor = 9
747c478bd9Sstevel@tonic-gate  * DB_CURRENT_VERSION is the latest one with changes in the database format
757c478bd9Sstevel@tonic-gate  *	for entry objects and the change in the dictionary format.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Our current implementation can support both versions.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate static inline bool_t
db_valid_version(u_int vers)807c478bd9Sstevel@tonic-gate db_valid_version(u_int vers)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	return ((vers == DB_CURRENT_VERSION) || (vers == DB_ORIG_VERSION));
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static char *
db_version_str(u_int vers)867c478bd9Sstevel@tonic-gate db_version_str(u_int vers)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	static char vstr[128];
897c478bd9Sstevel@tonic-gate 	u_int d_major =  (vers&0x0000ff00)>>8;
907c478bd9Sstevel@tonic-gate 	u_int d_minor =  (vers&0x000000ff);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	sprintf(vstr, "SunSoft, SSM, Version %d.%d", d_major, d_minor);
937c478bd9Sstevel@tonic-gate 	return (vstr);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Special XDR version that checks for a valid version number.
987c478bd9Sstevel@tonic-gate  * If we don't find an acceptable version, exit immediately instead
997c478bd9Sstevel@tonic-gate  * of continuing to xdr rest of dictionary, which might lead to
1007c478bd9Sstevel@tonic-gate  * a core dump if the formats between versions are incompatible.
1017c478bd9Sstevel@tonic-gate  * In the future, there might be a switch to determine versions
1027c478bd9Sstevel@tonic-gate  * and their corresponding XDR routines for the rest of the dictionary.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate extern "C" {
1057c478bd9Sstevel@tonic-gate bool_t
xdr_db_dict_version(XDR * xdrs,db_dict_version * objp)1067c478bd9Sstevel@tonic-gate xdr_db_dict_version(XDR *xdrs, db_dict_version *objp)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
1097c478bd9Sstevel@tonic-gate 		if (!xdr_u_int(xdrs, (u_int*) objp) ||
1107c478bd9Sstevel@tonic-gate 		    !db_valid_version(((u_int) *objp))) {
1117c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1127c478bd9Sstevel@tonic-gate 	"db_dictionary: invalid dictionary format! Expecting %s",
1137c478bd9Sstevel@tonic-gate 				db_version_str(DB_CURRENT_VERSION));
1147c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1157c478bd9Sstevel@tonic-gate 	"db_dictionary: invalid dictionary format! Expecting %s\n",
1167c478bd9Sstevel@tonic-gate 				db_version_str(DB_CURRENT_VERSION));
1177c478bd9Sstevel@tonic-gate 			exit(1);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 	} else if (!xdr_u_int(xdrs, (u_int*) objp))
1207c478bd9Sstevel@tonic-gate 		return (FALSE);
1217c478bd9Sstevel@tonic-gate 	return (TRUE);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate void
make_zero(vers * v)1257c478bd9Sstevel@tonic-gate make_zero(vers* v)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	v->zero();
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* ******************* dictionary data structures *************** */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* Delete contents of single db_table_desc pointed to by 'current.' */
1377c478bd9Sstevel@tonic-gate static void
delete_table_desc(db_table_desc * current)1387c478bd9Sstevel@tonic-gate delete_table_desc(db_table_desc *current)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	if (current->table_name != NULL) delete current->table_name;
1417c478bd9Sstevel@tonic-gate 	if (current->scheme != NULL) delete current->scheme;
1427c478bd9Sstevel@tonic-gate 	if (current->database != NULL) delete current->database;
1437c478bd9Sstevel@tonic-gate 	delete current;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* Create new table descriptor using given table name and table_object. */
1477c478bd9Sstevel@tonic-gate db_status
create_table_desc(char * tab,table_obj * zdesc,db_table_desc ** answer)1487c478bd9Sstevel@tonic-gate db_dictionary::create_table_desc(char *tab, table_obj* zdesc,
1497c478bd9Sstevel@tonic-gate 				db_table_desc** answer)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	db_table_desc *newtab;
1527c478bd9Sstevel@tonic-gate 	if ((newtab = new db_table_desc) == NULL) {
1537c478bd9Sstevel@tonic-gate 		FATAL3(
1547c478bd9Sstevel@tonic-gate 	    "db_dictionary::add_table: could not allocate space for new table",
1557c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	newtab->database = NULL;
1597c478bd9Sstevel@tonic-gate 	newtab->table_name = NULL;
1607c478bd9Sstevel@tonic-gate 	newtab->next = NULL;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if ((newtab->scheme = new db_scheme(zdesc)) == NULL) {
1637c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1647c478bd9Sstevel@tonic-gate 		FATAL3(
1657c478bd9Sstevel@tonic-gate 	"db_dictionary::add_table: could not allocate space for scheme",
1667c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (newtab->scheme->numkeys() == 0) {
1707c478bd9Sstevel@tonic-gate 		WARNING(
1717c478bd9Sstevel@tonic-gate 	"db_dictionary::add_table: could not translate table_obj to scheme");
1727c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1737c478bd9Sstevel@tonic-gate 		return (DB_BADOBJECT);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if ((newtab->table_name = strdup(tab)) == NULL) {
1777c478bd9Sstevel@tonic-gate 		delete_table_desc(newtab);
1787c478bd9Sstevel@tonic-gate 		FATAL3(
1797c478bd9Sstevel@tonic-gate 	    "db_dictionary::add_table: could not allocate space for table name",
1807c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (answer)
1847c478bd9Sstevel@tonic-gate 		*answer = newtab;
1857c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* Delete list of db_table_desc pointed to by 'head.' */
1907c478bd9Sstevel@tonic-gate static void
delete_bucket(db_table_desc * head)1917c478bd9Sstevel@tonic-gate delete_bucket(db_table_desc *head)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	db_table_desc * nextone, *current;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	for (current = head; current != NULL; current = nextone) {
1967c478bd9Sstevel@tonic-gate 		nextone = current->next;	// remember next
1977c478bd9Sstevel@tonic-gate 		delete_table_desc(current);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate static void
delete_dictionary(db_dict_desc * dict)2027c478bd9Sstevel@tonic-gate delete_dictionary(db_dict_desc *dict)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	db_table_desc* bucket;
2057c478bd9Sstevel@tonic-gate 	int i;
2067c478bd9Sstevel@tonic-gate 	if (dict) {
2077c478bd9Sstevel@tonic-gate 		if (dict->tables.tables_val) {
2087c478bd9Sstevel@tonic-gate 			/* delete each bucket */
2097c478bd9Sstevel@tonic-gate 			for (i = 0; i < dict->tables.tables_len; i++)
2107c478bd9Sstevel@tonic-gate 				bucket = dict->tables.tables_val[i];
2117c478bd9Sstevel@tonic-gate 				if (bucket)
2127c478bd9Sstevel@tonic-gate 					delete_bucket(bucket);
2137c478bd9Sstevel@tonic-gate 			/* delete table */
2147c478bd9Sstevel@tonic-gate 			delete dict->tables.tables_val;
2157c478bd9Sstevel@tonic-gate 		}
2167c478bd9Sstevel@tonic-gate 		/* delete dictionary */
2177c478bd9Sstevel@tonic-gate 		delete dict;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /* Relocate bucket starting with this entry to new hashtable 'new_tab'. */
2227c478bd9Sstevel@tonic-gate static void
relocate_bucket(db_table_desc * bucket,db_table_desc_p * new_tab,unsigned long hashsize)2237c478bd9Sstevel@tonic-gate relocate_bucket(db_table_desc* bucket,
2247c478bd9Sstevel@tonic-gate 		db_table_desc_p *new_tab, unsigned long hashsize)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	db_table_desc_p np, next_np, *hp;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = next_np) {
2297c478bd9Sstevel@tonic-gate 		next_np = np->next;
2307c478bd9Sstevel@tonic-gate 		hp = &new_tab[np->hashval % hashsize];
2317c478bd9Sstevel@tonic-gate 		np->next = *hp;
2327c478bd9Sstevel@tonic-gate 		*hp = np;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Return pointer to entry with same hash value and table_name
2387c478bd9Sstevel@tonic-gate  * as those supplied.  Returns NULL if not found.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate static db_status
enumerate_bucket(db_table_desc * bucket,db_status (* func)(db_table_desc *))2417c478bd9Sstevel@tonic-gate enumerate_bucket(db_table_desc* bucket, db_status(*func)(db_table_desc *))
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	db_table_desc_p np;
2447c478bd9Sstevel@tonic-gate 	db_status status;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = np->next) {
2477c478bd9Sstevel@tonic-gate 		status = (func)(np);
2487c478bd9Sstevel@tonic-gate 		if (status != DB_SUCCESS)
2497c478bd9Sstevel@tonic-gate 			return (status);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Return pointer to entry with same hash value and table_name
2577c478bd9Sstevel@tonic-gate  * as those supplied.  Returns NULL if not found.
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate static db_table_desc_p
search_bucket(db_table_desc * bucket,unsigned long hval,char * target)2607c478bd9Sstevel@tonic-gate search_bucket(db_table_desc* bucket, unsigned long hval, char *target)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	db_table_desc_p np;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	for (np = bucket; np != NULL; np = np->next) {
2657c478bd9Sstevel@tonic-gate 		if (np->hashval == hval &&
2667c478bd9Sstevel@tonic-gate 		    strcmp(np->table_name, target) == 0) {
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	return (np);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * Remove entry with the specified hashvalue and target table name.
2767c478bd9Sstevel@tonic-gate  * Returns 'TRUE' if successful, FALSE otherwise.
2777c478bd9Sstevel@tonic-gate  * If the entry being removed is at the head of the list, then
2787c478bd9Sstevel@tonic-gate  * the head is updated to reflect the removal. The storage for the
2797c478bd9Sstevel@tonic-gate  * entry is freed if desired.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate static bool_t
remove_from_bucket(db_table_desc_p bucket,db_table_desc_p * head,unsigned long hval,char * target,bool_t free_storage)2827c478bd9Sstevel@tonic-gate remove_from_bucket(db_table_desc_p bucket,
2837c478bd9Sstevel@tonic-gate 		db_table_desc_p *head, unsigned long hval, char *target,
2847c478bd9Sstevel@tonic-gate 		bool_t free_storage)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	db_table_desc_p np, dp;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* Search for it in the bucket */
2897c478bd9Sstevel@tonic-gate 	for (dp = np = bucket; np != NULL; np = np->next) {
2907c478bd9Sstevel@tonic-gate 		if (np->hashval == hval &&
2917c478bd9Sstevel@tonic-gate 		    strcmp(np->table_name, target) == 0) {
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 		} else {
2947c478bd9Sstevel@tonic-gate 			dp = np;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (np == NULL)
2997c478bd9Sstevel@tonic-gate 		return (FALSE);	// cannot delete if it is not there
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (dp == np) {
3027c478bd9Sstevel@tonic-gate 		*head = np->next;	// deleting head of bucket
3037c478bd9Sstevel@tonic-gate 	} else {
3047c478bd9Sstevel@tonic-gate 		dp->next = np->next;	// deleting interior link
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	if (free_storage)
3077c478bd9Sstevel@tonic-gate 		delete_table_desc(np);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (TRUE);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Add given entry to the bucket pointed to by 'bucket'.
3157c478bd9Sstevel@tonic-gate  * If an entry with the same table_name is found, no addition
3167c478bd9Sstevel@tonic-gate  * is done.  The entry is added to the head of the bucket.
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate static bool_t
add_to_bucket(db_table_desc_p bucket,db_table_desc ** head,db_table_desc_p td)3197c478bd9Sstevel@tonic-gate add_to_bucket(db_table_desc_p bucket, db_table_desc **head, db_table_desc_p td)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	db_table_desc_p curr, prev;
3227c478bd9Sstevel@tonic-gate 	register char *target_name;
3237c478bd9Sstevel@tonic-gate 	unsigned long target_hval;
3247c478bd9Sstevel@tonic-gate 	target_name = td->table_name;
3257c478bd9Sstevel@tonic-gate 	target_hval = td->hashval;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* Search for it in the bucket */
3287c478bd9Sstevel@tonic-gate 	for (prev = curr = bucket; curr != NULL; curr = curr->next) {
3297c478bd9Sstevel@tonic-gate 		if (curr->hashval == target_hval &&
3307c478bd9Sstevel@tonic-gate 		    strcmp(curr->table_name, target_name) == 0) {
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 		} else {
3337c478bd9Sstevel@tonic-gate 			prev = curr;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (curr != NULL)
3387c478bd9Sstevel@tonic-gate 		return (FALSE);  /* duplicates not allowed */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	curr = *head;
3417c478bd9Sstevel@tonic-gate 	*head = td;
3427c478bd9Sstevel@tonic-gate 	td->next = curr;
3437c478bd9Sstevel@tonic-gate 	return (TRUE);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /* Print bucket starting with this entry. */
3487c478bd9Sstevel@tonic-gate static void
print_bucket(db_table_desc * head)3497c478bd9Sstevel@tonic-gate print_bucket(db_table_desc *head)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	db_table_desc *np;
3527c478bd9Sstevel@tonic-gate 	for (np = head; np != NULL; np = np->next) {
3537c478bd9Sstevel@tonic-gate 		printf("%s: %d\n", np->table_name, np->hashval);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static db_status
print_table(db_table_desc * tbl)3587c478bd9Sstevel@tonic-gate print_table(db_table_desc *tbl)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	if (tbl == NULL)
3617c478bd9Sstevel@tonic-gate 		return (DB_BADTABLE);
3627c478bd9Sstevel@tonic-gate 	printf("%s: %d\n", tbl->table_name, tbl->hashval);
3637c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int hashsizes[] = {		/* hashtable sizes */
3687c478bd9Sstevel@tonic-gate 	11,
3697c478bd9Sstevel@tonic-gate 	53,
3707c478bd9Sstevel@tonic-gate 	113,
3717c478bd9Sstevel@tonic-gate 	337,
3727c478bd9Sstevel@tonic-gate 	977,
3737c478bd9Sstevel@tonic-gate 	2053,
3747c478bd9Sstevel@tonic-gate 	4073,
3757c478bd9Sstevel@tonic-gate 	8011,
3767c478bd9Sstevel@tonic-gate 	16001,
3777c478bd9Sstevel@tonic-gate 	0
3787c478bd9Sstevel@tonic-gate };
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate // prevents wrap around numbers from being passed
3817c478bd9Sstevel@tonic-gate #define	CALLOC_LIMIT 536870911
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate /* Returns the next size to use for the hash table */
3847c478bd9Sstevel@tonic-gate static unsigned int
get_next_hashsize(long unsigned oldsize)3857c478bd9Sstevel@tonic-gate get_next_hashsize(long unsigned oldsize)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	long unsigned newsize, n;
3887c478bd9Sstevel@tonic-gate 	if (oldsize == 0)
3897c478bd9Sstevel@tonic-gate 		newsize = hashsizes[0];
3907c478bd9Sstevel@tonic-gate 	else {
3917c478bd9Sstevel@tonic-gate 		for (n = 0; newsize = hashsizes[n++]; )
3927c478bd9Sstevel@tonic-gate 			if (oldsize == newsize) {
3937c478bd9Sstevel@tonic-gate 				newsize = hashsizes[n];	/* get next size */
3947c478bd9Sstevel@tonic-gate 				break;
3957c478bd9Sstevel@tonic-gate 			}
3967c478bd9Sstevel@tonic-gate 		if (newsize == 0)
3977c478bd9Sstevel@tonic-gate 			newsize = oldsize * 2 + 1;	/* just double */
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 	return (newsize);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Grow the current hashtable upto the next size.
4047c478bd9Sstevel@tonic-gate  * The contents of the existing hashtable is copied to the new one and
4057c478bd9Sstevel@tonic-gate  * relocated according to its hashvalue relative to the new size.
4067c478bd9Sstevel@tonic-gate  * Old table is deleted after the relocation.
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate static void
grow_dictionary(db_dict_desc_p dd)4097c478bd9Sstevel@tonic-gate grow_dictionary(db_dict_desc_p dd)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	unsigned int oldsize, i, new_size;
4127c478bd9Sstevel@tonic-gate 	db_table_desc_p * oldtab, *newtab;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	oldsize = dd->tables.tables_len;
4157c478bd9Sstevel@tonic-gate 	oldtab = dd->tables.tables_val;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	new_size = get_next_hashsize(oldsize);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (new_size > CALLOC_LIMIT) {
4207c478bd9Sstevel@tonic-gate 		FATAL("db_dictionary::grow: table size exceeds calloc limit",
4217c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if ((newtab = (db_table_desc_p*)
4257c478bd9Sstevel@tonic-gate 		calloc((unsigned int) new_size,
4267c478bd9Sstevel@tonic-gate 			sizeof (db_table_desc_p))) == NULL) {
4277c478bd9Sstevel@tonic-gate 		FATAL("db_dictionary::grow: cannot allocate space",
4287c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (oldtab != NULL) {		// must transfer contents of old to new
4327c478bd9Sstevel@tonic-gate 		for (i = 0; i < oldsize; i++) {
4337c478bd9Sstevel@tonic-gate 			relocate_bucket(oldtab[i], newtab, new_size);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 		delete oldtab;		// delete old hashtable
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	dd->tables.tables_val = newtab;
4397c478bd9Sstevel@tonic-gate 	dd->tables.tables_len = new_size;
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate #define	HASHSHIFT	3
4437c478bd9Sstevel@tonic-gate #define	HASHMASK	0x1f
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static u_int
get_hashval(char * value)4467c478bd9Sstevel@tonic-gate get_hashval(char *value)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	int i, len;
4497c478bd9Sstevel@tonic-gate 	u_int hval = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	len = strlen(value);
4527c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
4537c478bd9Sstevel@tonic-gate 		hval = ((hval<<HASHSHIFT)^hval);
4547c478bd9Sstevel@tonic-gate 		hval += (value[i] & HASHMASK);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	return (hval);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate static db_status
enumerate_dictionary(db_dict_desc * dd,db_status (* func)(db_table_desc *))4617c478bd9Sstevel@tonic-gate enumerate_dictionary(db_dict_desc *dd, db_status (*func) (db_table_desc*))
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	int i;
4647c478bd9Sstevel@tonic-gate 	db_table_desc *bucket;
4657c478bd9Sstevel@tonic-gate 	db_status status;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (dd == NULL)
4687c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	for (i = 0; i < dd->tables.tables_len; i++) {
4717c478bd9Sstevel@tonic-gate 		bucket = dd->tables.tables_val[i];
4727c478bd9Sstevel@tonic-gate 		if (bucket) {
4737c478bd9Sstevel@tonic-gate 			status = enumerate_bucket(bucket, func);
4747c478bd9Sstevel@tonic-gate 			if (status != DB_SUCCESS)
4757c478bd9Sstevel@tonic-gate 				return (status);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate  * Look up target table_name in hashtable and return its db_table_desc.
4857c478bd9Sstevel@tonic-gate  * Return NULL if not found.
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate static db_table_desc *
search_dictionary(db_dict_desc * dd,char * target)4887c478bd9Sstevel@tonic-gate search_dictionary(db_dict_desc *dd, char *target)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	register unsigned long hval;
4917c478bd9Sstevel@tonic-gate 	unsigned long bucket;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (target == NULL || dd == NULL || dd->tables.tables_len == 0)
4947c478bd9Sstevel@tonic-gate 		return (NULL);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
4977c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	db_table_desc_p fst = dd->tables.tables_val[bucket];
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	if (fst != NULL)
5027c478bd9Sstevel@tonic-gate 		return (search_bucket(fst, hval, target));
5037c478bd9Sstevel@tonic-gate 	else
5047c478bd9Sstevel@tonic-gate 		return (NULL);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /*
5087c478bd9Sstevel@tonic-gate  * Remove the entry with the target table_name from the dictionary.
5097c478bd9Sstevel@tonic-gate  * If successful, return DB_SUCCESS; otherwise DB_NOTUNIQUE if target
5107c478bd9Sstevel@tonic-gate  * is null; DB_NOTFOUND if entry is not found.
5117c478bd9Sstevel@tonic-gate  * If successful, decrement count of number of entries in hash table.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static db_status
remove_from_dictionary(db_dict_desc * dd,char * target,bool_t remove_storage)5147c478bd9Sstevel@tonic-gate remove_from_dictionary(db_dict_desc *dd, char *target, bool_t remove_storage)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	register unsigned long hval;
5177c478bd9Sstevel@tonic-gate 	unsigned long bucket;
5187c478bd9Sstevel@tonic-gate 	register db_table_desc *fst;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (target == NULL)
5217c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
5227c478bd9Sstevel@tonic-gate 	if (dd == NULL || dd->tables.tables_len == 0)
5237c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5247c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
5257c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
5267c478bd9Sstevel@tonic-gate 	fst = dd->tables.tables_val[bucket];
5277c478bd9Sstevel@tonic-gate 	if (fst == NULL)
5287c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5297c478bd9Sstevel@tonic-gate 	if (remove_from_bucket(fst, &dd->tables.tables_val[bucket],
5307c478bd9Sstevel@tonic-gate 			hval, target, remove_storage)) {
5317c478bd9Sstevel@tonic-gate 		--(dd->count);
5327c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
5337c478bd9Sstevel@tonic-gate 	} else
5347c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * Add a new db_table_desc to the dictionary.
5397c478bd9Sstevel@tonic-gate  * Return DB_NOTUNIQUE, if entry with identical table_name
5407c478bd9Sstevel@tonic-gate  * already exists.  If entry is added, return DB_SUCCESS.
5417c478bd9Sstevel@tonic-gate  * Increment count of number of entries in index table and grow table
5427c478bd9Sstevel@tonic-gate  * if number of entries equals size of table.
5437c478bd9Sstevel@tonic-gate  *
5447c478bd9Sstevel@tonic-gate  * Inputs: db_dict_desc_p dd	pointer to dictionary to add to.
5457c478bd9Sstevel@tonic-gate  *	   db_table_desc *td	pointer to table entry to be added. The
5467c478bd9Sstevel@tonic-gate  * 				db_table_desc.next field will be altered
5477c478bd9Sstevel@tonic-gate  *				without regard to it's current setting.
5487c478bd9Sstevel@tonic-gate  *				This means that if next points to a list of
5497c478bd9Sstevel@tonic-gate  *				table entries, they may be either linked into
5507c478bd9Sstevel@tonic-gate  *				the dictionary unexpectly or cut off (leaked).
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate static db_status
add_to_dictionary(db_dict_desc_p dd,db_table_desc * td)5537c478bd9Sstevel@tonic-gate add_to_dictionary(db_dict_desc_p dd, db_table_desc *td)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	register unsigned long hval;
5567c478bd9Sstevel@tonic-gate 	char *target;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (dd == NULL)
5597c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (td == NULL)
5627c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
5637c478bd9Sstevel@tonic-gate 	target = td->table_name;
5647c478bd9Sstevel@tonic-gate 	if (target == NULL)
5657c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	hval = get_hashval(target);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (dd->tables.tables_val == NULL)
5707c478bd9Sstevel@tonic-gate 		grow_dictionary(dd);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	db_table_desc_p fst;
5737c478bd9Sstevel@tonic-gate 	unsigned long bucket;
5747c478bd9Sstevel@tonic-gate 	bucket = hval % dd->tables.tables_len;
5757c478bd9Sstevel@tonic-gate 	fst = dd->tables.tables_val[bucket];
5767c478bd9Sstevel@tonic-gate 	td->hashval = hval;
5777c478bd9Sstevel@tonic-gate 	if (fst == NULL)  { /* Empty bucket */
5787c478bd9Sstevel@tonic-gate 		dd->tables.tables_val[bucket] = td;
5797c478bd9Sstevel@tonic-gate 	} else if (!add_to_bucket(fst, &dd->tables.tables_val[bucket], td)) {
5807c478bd9Sstevel@tonic-gate 			return (DB_NOTUNIQUE);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/* increase hash table size if number of entries equals table size */
5847c478bd9Sstevel@tonic-gate 	if (++(dd->count) > dd->tables.tables_len)
5857c478bd9Sstevel@tonic-gate 		grow_dictionary(dd);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate /* ******************* pickling routines for dictionary ******************* */
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate /* Does the actual writing to/from file specific for db_dict_desc structure. */
5947c478bd9Sstevel@tonic-gate static bool_t
transfer_aux(XDR * x,pptr tbl)5957c478bd9Sstevel@tonic-gate transfer_aux(XDR* x, pptr tbl)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	return (xdr_db_dict_desc_p(x, (db_dict_desc_p *) tbl));
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate class pickle_dict_desc: public pickle_file {
6017c478bd9Sstevel@tonic-gate     public:
pickle_dict_desc(char * f,pickle_mode m)6027c478bd9Sstevel@tonic-gate 	pickle_dict_desc(char *f, pickle_mode m) : pickle_file(f, m) {}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/* Transfers db_dict_desc structure pointed to by dp to/from file. */
transfer(db_dict_desc_p * dp)6057c478bd9Sstevel@tonic-gate 	int transfer(db_dict_desc_p * dp)
6067c478bd9Sstevel@tonic-gate 		{ return (pickle_file::transfer((pptr) dp, &transfer_aux)); }
6077c478bd9Sstevel@tonic-gate };
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /* ************************ dictionary methods *************************** */
6107c478bd9Sstevel@tonic-gate 
db_dictionary()6117c478bd9Sstevel@tonic-gate db_dictionary::db_dictionary()
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	dictionary = NULL;
6147c478bd9Sstevel@tonic-gate 	initialized = FALSE;
6157c478bd9Sstevel@tonic-gate 	filename = NULL;
6167c478bd9Sstevel@tonic-gate 	tmpfilename = NULL;
6177c478bd9Sstevel@tonic-gate 	logfilename = NULL;
6187c478bd9Sstevel@tonic-gate 	logfile = NULL;
6197c478bd9Sstevel@tonic-gate 	logfile_opened = FALSE;
6207c478bd9Sstevel@tonic-gate 	changed = FALSE;
6217c478bd9Sstevel@tonic-gate 	INITRW(dict);
6227c478bd9Sstevel@tonic-gate 	READLOCKOK(dict);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * This routine clones an entire hash bucket chain. If you clone a
6277c478bd9Sstevel@tonic-gate  * data dictionary entry with the ->next pointer set, you will get a
6287c478bd9Sstevel@tonic-gate  * clone of that entry, as well as the entire linked list. This can cause
6297c478bd9Sstevel@tonic-gate  * pain if you then pass the cloned bucket to routines such as
6307c478bd9Sstevel@tonic-gate  * add_to_dictionary(), which do not expect nor handle dictionary hash
6317c478bd9Sstevel@tonic-gate  * entries with the ->next pointer set. You might either get duplicate
6327c478bd9Sstevel@tonic-gate  * entires or lose entries. If you wish to clone the entire bucket chain
6337c478bd9Sstevel@tonic-gate  * and add it to a new dictionary, loop through the db_table_desc->next list
6347c478bd9Sstevel@tonic-gate  * and call add_to_dictionary() for each item.
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate int
db_clone_bucket(db_table_desc * bucket,db_table_desc ** clone)6377c478bd9Sstevel@tonic-gate db_dictionary::db_clone_bucket(db_table_desc *bucket, db_table_desc **clone)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	u_long		size;
6407c478bd9Sstevel@tonic-gate 	XDR		xdrs;
6417c478bd9Sstevel@tonic-gate 	char		*bufin = NULL;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::db_clone_bucket");
6447c478bd9Sstevel@tonic-gate 	db_func use_this = xdr_db_table_desc;
6457c478bd9Sstevel@tonic-gate 	size = xdr_sizeof((xdrproc_t) use_this, (void *) bucket);
6467c478bd9Sstevel@tonic-gate 	bufin = (char *) calloc(1, (size_t) size * sizeof (char));
6477c478bd9Sstevel@tonic-gate 	if (!bufin) {
6487c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6497c478bd9Sstevel@tonic-gate 			"db_dictionary::insert_modified_table: out of memory");
6507c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::insert_modified_table: out of memory",
6517c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, bufin, (size_t) size, XDR_ENCODE);
6547c478bd9Sstevel@tonic-gate 	if (!xdr_db_table_desc(&xdrs, bucket)) {
6557c478bd9Sstevel@tonic-gate 		free(bufin);
6567c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6577c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6587c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed");
6597c478bd9Sstevel@tonic-gate 		FATAL3(
6607c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed.",
6617c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, 0);
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 	*clone = (db_table_desc *) calloc(1, (size_t) size * sizeof (char));
6647c478bd9Sstevel@tonic-gate 	if (!*clone) {
6657c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6667c478bd9Sstevel@tonic-gate 		free(bufin);
6677c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6687c478bd9Sstevel@tonic-gate 			"db_dictionary::insert_modified_table: out of memory");
6697c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::insert_modified_table: out of memory",
6707c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdrs, bufin, (size_t) size, XDR_DECODE);
6747c478bd9Sstevel@tonic-gate 	if (!xdr_db_table_desc(&xdrs, *clone)) {
6757c478bd9Sstevel@tonic-gate 		free(bufin);
6767c478bd9Sstevel@tonic-gate 		free(*clone);
6777c478bd9Sstevel@tonic-gate 		xdr_destroy(&xdrs);
6787c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_MEMORY_LIMIT,
6797c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed");
6807c478bd9Sstevel@tonic-gate 		FATAL3(
6817c478bd9Sstevel@tonic-gate 		"db_dictionary::insert_modified_table: xdr encode failed.",
6827c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, 0);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 	free(bufin);
6857c478bd9Sstevel@tonic-gate 	xdr_destroy(&xdrs);
6867c478bd9Sstevel@tonic-gate 	READUNLOCK(this, DB_LOCK_ERROR, "ru db_dictionary::db_clone_bucket");
6877c478bd9Sstevel@tonic-gate 	return (1);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate int
change_table_name(db_table_desc * clone,char * tok,char * repl)6927c478bd9Sstevel@tonic-gate db_dictionary::change_table_name(db_table_desc *clone, char *tok, char *repl)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	char 	*newname;
6957c478bd9Sstevel@tonic-gate 	char	*loc_end, *loc_beg;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::change_table_name");
6987c478bd9Sstevel@tonic-gate 	while (clone) {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * Special case for a tok="". This is used for the
7017c478bd9Sstevel@tonic-gate 		 * nisrestore(1M), when restoring a replica in another
7027c478bd9Sstevel@tonic-gate 		 * domain. This routine is used to change the datafile
7037c478bd9Sstevel@tonic-gate 		 * names in the data.dict (see bugid #4031273). This will not
7047c478bd9Sstevel@tonic-gate 		 * effect massage_dict(), since it never generates an empty
7057c478bd9Sstevel@tonic-gate 		 * string for tok.
7067c478bd9Sstevel@tonic-gate 		 */
7077c478bd9Sstevel@tonic-gate 		if (strlen(tok) == 0) {
7087c478bd9Sstevel@tonic-gate 			strcat(clone->table_name, repl);
7097c478bd9Sstevel@tonic-gate 			clone = clone->next;
7107c478bd9Sstevel@tonic-gate 			continue;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 		newname = (char *) calloc(1, sizeof (char) *
7137c478bd9Sstevel@tonic-gate 				strlen(clone->table_name) +
7147c478bd9Sstevel@tonic-gate 				strlen(repl) - strlen(tok) + 1);
7157c478bd9Sstevel@tonic-gate 		if (!newname) {
7167c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_MEMORY_LIMIT,
7177c478bd9Sstevel@tonic-gate 			"db_dictionary::change_table_name: out of memory");
7187c478bd9Sstevel@tonic-gate 		    FATAL3("db_dictionary::change_table_name: out of memory.",
7197c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, 0);
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 		if (loc_beg = strstr(clone->table_name, tok)) {
7227c478bd9Sstevel@tonic-gate 			loc_end = loc_beg + strlen(tok);
7237c478bd9Sstevel@tonic-gate 			int s = loc_beg - clone->table_name;
7247c478bd9Sstevel@tonic-gate 			memcpy(newname, clone->table_name, s);
7257c478bd9Sstevel@tonic-gate 			strcat(newname + s, repl);
7267c478bd9Sstevel@tonic-gate 			strcat(newname, loc_end);
7277c478bd9Sstevel@tonic-gate 			free(clone->table_name);
7287c478bd9Sstevel@tonic-gate 			clone->table_name = newname;
7297c478bd9Sstevel@tonic-gate 		} else {
7307c478bd9Sstevel@tonic-gate 			free(newname);
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 		clone = clone->next;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR,
7357c478bd9Sstevel@tonic-gate 			"wu db_dictionary::change_table_name");
7367c478bd9Sstevel@tonic-gate 	return (1);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate #ifdef	curdict
7417c478bd9Sstevel@tonic-gate #undef	curdict
7427c478bd9Sstevel@tonic-gate #endif
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate  * A function to initialize the temporary dictionary from the real
7457c478bd9Sstevel@tonic-gate  * dictionary.
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate bool_t
inittemp(char * dictname,db_dictionary & curdict)7487c478bd9Sstevel@tonic-gate db_dictionary::inittemp(char *dictname, db_dictionary& curdict)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	int status;
7517c478bd9Sstevel@tonic-gate 	db_table_desc_p	*newtab;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	db_shutdown();
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "w db_dictionary::inittemp");
7567c478bd9Sstevel@tonic-gate 	if (initialized) {
7577c478bd9Sstevel@tonic-gate 		/* Someone else got in between db_shutdown() and lock() */
7587c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE, "wu db_dictionary::inittemp");
7597c478bd9Sstevel@tonic-gate 		return (TRUE);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	pickle_dict_desc f(dictname, PICKLE_READ);
7637c478bd9Sstevel@tonic-gate 	filename = strdup(dictname);
7647c478bd9Sstevel@tonic-gate 	if (filename == NULL) {
7657c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7667c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
7677c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: could not allocate space",
7687c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	int len = strlen(filename);
7717c478bd9Sstevel@tonic-gate 	tmpfilename = new char[len+5];
7727c478bd9Sstevel@tonic-gate 	if (tmpfilename == NULL) {
7737c478bd9Sstevel@tonic-gate 		delete filename;
7747c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7757c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
7767c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: could not allocate space",
7777c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	logfilename = new char[len+5];
7807c478bd9Sstevel@tonic-gate 	if (logfilename == NULL) {
7817c478bd9Sstevel@tonic-gate 		delete filename;
7827c478bd9Sstevel@tonic-gate 		delete tmpfilename;
7837c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
7847c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space");
7857c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::inittemp: cannot allocate space",
7867c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	sprintf(tmpfilename, "%s.tmp", filename);
7907c478bd9Sstevel@tonic-gate 	sprintf(logfilename, "%s.log", filename);
7917c478bd9Sstevel@tonic-gate 	unlink(tmpfilename);  /* get rid of partial checkpoints */
7927c478bd9Sstevel@tonic-gate 	dictionary = NULL;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if ((status = f.transfer(&dictionary)) < 0) {
7957c478bd9Sstevel@tonic-gate 		initialized = FALSE;
7967c478bd9Sstevel@tonic-gate 	} else if (status == 1) { /* no dictionary exists, create one */
7977c478bd9Sstevel@tonic-gate 		dictionary = new db_dict_desc;
7987c478bd9Sstevel@tonic-gate 		if (dictionary == NULL) {
7997c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8007c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space");
8017c478bd9Sstevel@tonic-gate 			FATAL3(
8027c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: could not allocate space",
8037c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		dictionary->tables.tables_len =
8067c478bd9Sstevel@tonic-gate 				curdict.dictionary->tables.tables_len;
8077c478bd9Sstevel@tonic-gate 		if ((newtab = (db_table_desc_p *) calloc(
8087c478bd9Sstevel@tonic-gate 			(unsigned int) dictionary->tables.tables_len,
8097c478bd9Sstevel@tonic-gate 			sizeof (db_table_desc_p))) == NULL) {
8107c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, FALSE,
8117c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space");
8127c478bd9Sstevel@tonic-gate 			FATAL3(
8137c478bd9Sstevel@tonic-gate 			"db_dictionary::inittemp: cannot allocate space",
8147c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, 0);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 		dictionary->tables.tables_val = newtab;
8177c478bd9Sstevel@tonic-gate 		dictionary->count = 0;
8187c478bd9Sstevel@tonic-gate 		dictionary->impl_vers = curdict.dictionary->impl_vers;
8197c478bd9Sstevel@tonic-gate 		initialized = TRUE;
8207c478bd9Sstevel@tonic-gate 	} else	/* dictionary loaded successfully */
8217c478bd9Sstevel@tonic-gate 		initialized = TRUE;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (initialized == TRUE) {
8247c478bd9Sstevel@tonic-gate 		changed = FALSE;
8257c478bd9Sstevel@tonic-gate 		reset_log();
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, FALSE, "wu db_dictionary::inittemp");
8297c478bd9Sstevel@tonic-gate 	return (initialized);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * This method replaces the token string specified with the replacment
8357c478bd9Sstevel@tonic-gate  * string specified. It assumes that at least one and only one instance of
8367c478bd9Sstevel@tonic-gate  * the token exists. It is the responsibility of the caller to ensure that
8377c478bd9Sstevel@tonic-gate  * the above assumption stays valid.
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate db_status
massage_dict(char * newdictname,char * tok,char * repl)8407c478bd9Sstevel@tonic-gate db_dictionary::massage_dict(char *newdictname, char *tok, char *repl)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	int		retval;
8437c478bd9Sstevel@tonic-gate 	u_int		i, tbl_count;
8447c478bd9Sstevel@tonic-gate 	db_status	status;
8457c478bd9Sstevel@tonic-gate 	db_table_desc 	*bucket, *np, *clone, *next_np;
8467c478bd9Sstevel@tonic-gate 	char		tail[NIS_MAXNAMELEN];
8477c478bd9Sstevel@tonic-gate 	db_dictionary	*tmpptr;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::massage_dict");
8507c478bd9Sstevel@tonic-gate 	if (dictionary == NULL) {
8517c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8527c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: uninitialized dictionary file");
8537c478bd9Sstevel@tonic-gate 		FATAL3(
8547c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: uninitialized dictionary file.",
8557c478bd9Sstevel@tonic-gate 		DB_INTERNAL_ERROR, DB_INTERNAL_ERROR);
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	if ((tbl_count = dictionary->count) == 0) {
8597c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_SUCCESS,
8607c478bd9Sstevel@tonic-gate 				"wu db_dictionary::massage_dict");
8617c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* First checkpoint */
8657c478bd9Sstevel@tonic-gate 	if ((status = checkpoint()) != DB_SUCCESS) {
8667c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, status, "wu db_dictionary::massage_dict");
8677c478bd9Sstevel@tonic-gate 		return (status);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate #ifdef DEBUG
8717c478bd9Sstevel@tonic-gate 	enumerate_dictionary(dictionary, &print_table);
8727c478bd9Sstevel@tonic-gate #endif
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/* Initialize the free dictionary so that we can start populating it */
8757c478bd9Sstevel@tonic-gate 	FreeDictionary->inittemp(newdictname, *this);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	for (i = 0; i < dictionary->tables.tables_len; i++) {
8787c478bd9Sstevel@tonic-gate 		bucket = dictionary->tables.tables_val[i];
8797c478bd9Sstevel@tonic-gate 		if (bucket) {
8807c478bd9Sstevel@tonic-gate 			np = bucket;
8817c478bd9Sstevel@tonic-gate 			while (np != NULL) {
8827c478bd9Sstevel@tonic-gate 				next_np = np->next;
8837c478bd9Sstevel@tonic-gate 				retval = db_clone_bucket(np, &clone);
8847c478bd9Sstevel@tonic-gate 				if (retval != 1) {
8857c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8867c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
8877c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
8887c478bd9Sstevel@tonic-gate 				}
8897c478bd9Sstevel@tonic-gate 				if (change_table_name(clone, tok, repl) == -1) {
8907c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
8917c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
8927c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
8937c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
8947c478bd9Sstevel@tonic-gate 				}
8957c478bd9Sstevel@tonic-gate 				/*
8967c478bd9Sstevel@tonic-gate 				 * We know we don't have a log file, so we will
8977c478bd9Sstevel@tonic-gate 				 * just add to the in-memory database and dump
8987c478bd9Sstevel@tonic-gate 				 * all of it once we are done.
8997c478bd9Sstevel@tonic-gate 				 */
9007c478bd9Sstevel@tonic-gate 				status = add_to_dictionary
9017c478bd9Sstevel@tonic-gate 						(FreeDictionary->dictionary,
9027c478bd9Sstevel@tonic-gate 						clone);
9037c478bd9Sstevel@tonic-gate 				if (status != DB_SUCCESS) {
9047c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
9057c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9067c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
9077c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
9087c478bd9Sstevel@tonic-gate 				}
9097c478bd9Sstevel@tonic-gate 				status = remove_from_dictionary(dictionary,
9107c478bd9Sstevel@tonic-gate 							np->table_name, TRUE);
9117c478bd9Sstevel@tonic-gate 				if (status != DB_SUCCESS) {
9127c478bd9Sstevel@tonic-gate 					delete_table_desc(clone);
9137c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9147c478bd9Sstevel@tonic-gate 					"wu db_dictionary::massage_dict");
9157c478bd9Sstevel@tonic-gate 					return (DB_INTERNAL_ERROR);
9167c478bd9Sstevel@tonic-gate 				}
9177c478bd9Sstevel@tonic-gate 				np = next_np;
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (FreeDictionary->dump() != DB_SUCCESS) {
9237c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9247c478bd9Sstevel@tonic-gate 				"wu db_dictionary::massage_dict");
9257c478bd9Sstevel@tonic-gate 		FATAL3(
9267c478bd9Sstevel@tonic-gate 		"db_dictionary::massage_dict: Unable to dump new dictionary.",
9277c478bd9Sstevel@tonic-gate 		DB_INTERNAL_ERROR, DB_INTERNAL_ERROR);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * Now, shutdown the inuse dictionary and update the FreeDictionary
9327c478bd9Sstevel@tonic-gate 	 * and InUseDictionary pointers as well. Also, delete the old dictionary
9337c478bd9Sstevel@tonic-gate 	 * file.
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	unlink(filename); /* There shouldn't be a tmpfile or logfile */
9367c478bd9Sstevel@tonic-gate 	db_shutdown();
9377c478bd9Sstevel@tonic-gate 	tmpptr = InUseDictionary;
9387c478bd9Sstevel@tonic-gate 	InUseDictionary = FreeDictionary;
9397c478bd9Sstevel@tonic-gate 	FreeDictionary = tmpptr;
9407c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::massage_dict");
9417c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate db_status
merge_dict(db_dictionary & tempdict,char * tok,char * repl)9467c478bd9Sstevel@tonic-gate db_dictionary::merge_dict(db_dictionary& tempdict, char *tok, char *repl)
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	db_status	dbstat = DB_SUCCESS;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl = NULL, *clone = NULL, *next_td = NULL;
9527c478bd9Sstevel@tonic-gate 	int		retval, i;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::merge_dict");
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	for (i = 0; i < tempdict.dictionary->tables.tables_len; ++i) {
9577c478bd9Sstevel@tonic-gate 		tbl = tempdict.dictionary->tables.tables_val[i];
9587c478bd9Sstevel@tonic-gate 		if (!tbl)
9597c478bd9Sstevel@tonic-gate 			continue;
9607c478bd9Sstevel@tonic-gate 		retval = db_clone_bucket(tbl, &clone);
9617c478bd9Sstevel@tonic-gate 		if (retval != 1) {
9627c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9637c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9647c478bd9Sstevel@tonic-gate 			return (DB_INTERNAL_ERROR);
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		while (clone) {
9677c478bd9Sstevel@tonic-gate 			next_td = clone->next;
9687c478bd9Sstevel@tonic-gate 			clone->next = NULL;
9697c478bd9Sstevel@tonic-gate 			if ((tok) &&
9707c478bd9Sstevel@tonic-gate 				(change_table_name(clone, tok, repl) == -1)) {
9717c478bd9Sstevel@tonic-gate 				delete_table_desc(clone);
9727c478bd9Sstevel@tonic-gate 				if (next_td)
9737c478bd9Sstevel@tonic-gate 					delete_table_desc(next_td);
9747c478bd9Sstevel@tonic-gate 				WRITEUNLOCK(this, DB_INTERNAL_ERROR,
9757c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9767c478bd9Sstevel@tonic-gate 				return (DB_INTERNAL_ERROR);
9777c478bd9Sstevel@tonic-gate 			}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 			dbstat = add_to_dictionary(dictionary, clone);
9807c478bd9Sstevel@tonic-gate 			if (dbstat == DB_NOTUNIQUE) {
9817c478bd9Sstevel@tonic-gate 				/* Overide */
9827c478bd9Sstevel@tonic-gate 				dbstat = remove_from_dictionary(dictionary,
9837c478bd9Sstevel@tonic-gate 						clone->table_name, TRUE);
9847c478bd9Sstevel@tonic-gate 				if (dbstat != DB_SUCCESS) {
9857c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, dbstat,
9867c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9877c478bd9Sstevel@tonic-gate 					return (dbstat);
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 				dbstat = add_to_dictionary(dictionary,
9907c478bd9Sstevel@tonic-gate 								clone);
9917c478bd9Sstevel@tonic-gate 			} else {
9927c478bd9Sstevel@tonic-gate 				if (dbstat != DB_SUCCESS) {
9937c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, dbstat,
9947c478bd9Sstevel@tonic-gate 					"wu db_dictionary::merge_dict");
9957c478bd9Sstevel@tonic-gate 					return (dbstat);
9967c478bd9Sstevel@tonic-gate 				}
9977c478bd9Sstevel@tonic-gate 			}
9987c478bd9Sstevel@tonic-gate 			clone = next_td;
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * If we were successful in merging the dictionaries, then mark the
10037c478bd9Sstevel@tonic-gate  * dictionary changed, so that it will be properly checkpointed and
10047c478bd9Sstevel@tonic-gate  * dumped to disk.
10057c478bd9Sstevel@tonic-gate  */
10067c478bd9Sstevel@tonic-gate 	if (dbstat == DB_SUCCESS)
10077c478bd9Sstevel@tonic-gate 		changed = TRUE;
10087c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::merge_dict");
10097c478bd9Sstevel@tonic-gate 	return (dbstat);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate int
copyfile(char * infile,char * outfile)10137c478bd9Sstevel@tonic-gate db_dictionary::copyfile(char *infile, char *outfile)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl = NULL;
10167c478bd9Sstevel@tonic-gate 	db	*dbase;
10177c478bd9Sstevel@tonic-gate 	int	ret;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::copyfile");
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * We need to hold the read-lock until the dump() is done.
10227c478bd9Sstevel@tonic-gate 	 * However, we must avoid the lock migration (read -> write)
10237c478bd9Sstevel@tonic-gate 	 * that would happen in find_table() if the db must be loaded.
10247c478bd9Sstevel@tonic-gate 	 * Hence, look first look for an already loaded db.
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	dbase  = find_table(infile, &tbl, TRUE, TRUE, FALSE);
10277c478bd9Sstevel@tonic-gate 	if (dbase == NULL) {
10287c478bd9Sstevel@tonic-gate 		/* Release the read-lock, and try again, allowing load */
10297c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_LOCK_ERROR, "ru db_dictionary::copyfile");
10307c478bd9Sstevel@tonic-gate 		dbase  = find_table(infile, &tbl, TRUE, TRUE, TRUE);
10317c478bd9Sstevel@tonic-gate 		if (dbase == NULL)
10327c478bd9Sstevel@tonic-gate 			return (DB_NOTFOUND);
10337c478bd9Sstevel@tonic-gate 		/*
10347c478bd9Sstevel@tonic-gate 		 * Read-lock again, and get a 'tbl' we can use since we're
10357c478bd9Sstevel@tonic-gate 		 * still holding the lock.
10367c478bd9Sstevel@tonic-gate 		 */
10377c478bd9Sstevel@tonic-gate 		READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::copyfile");
10387c478bd9Sstevel@tonic-gate 		dbase  = find_table(infile, &tbl, TRUE, TRUE, FALSE);
10397c478bd9Sstevel@tonic-gate 		if (dbase == NULL) {
10407c478bd9Sstevel@tonic-gate 			READUNLOCK(this, DB_NOTFOUND,
10417c478bd9Sstevel@tonic-gate 					"ru db_dictionary::copyfile");
10427c478bd9Sstevel@tonic-gate 			return (DB_NOTFOUND);
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 	ret = tbl->database->dump(outfile) ? DB_SUCCESS : DB_INTERNAL_ERROR;
10467c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_dictionary::copyfile");
10477c478bd9Sstevel@tonic-gate 	return (ret);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate bool_t
extract_entries(db_dictionary & tempdict,char ** fs,int fscnt)10527c478bd9Sstevel@tonic-gate db_dictionary::extract_entries(db_dictionary& tempdict, char **fs, int fscnt)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	int		i, retval;
10557c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl, *clone;
10567c478bd9Sstevel@tonic-gate 	db_table_desc	tbl_ent;
10577c478bd9Sstevel@tonic-gate 	db_status	dbstat;
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "r db_dictionary::extract_entries");
10607c478bd9Sstevel@tonic-gate 	for (i = 0; i < fscnt; ++i) {
10617c478bd9Sstevel@tonic-gate 		tbl = find_table_desc(fs[i]);
10627c478bd9Sstevel@tonic-gate 		if (!tbl) {
10637c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
10647c478bd9Sstevel@tonic-gate 				"extract_entries: no dictionary entry for %s",
10657c478bd9Sstevel@tonic-gate 				fs[i]);
10667c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10677c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10687c478bd9Sstevel@tonic-gate 			return (FALSE);
10697c478bd9Sstevel@tonic-gate 		} else {
10707c478bd9Sstevel@tonic-gate 			tbl_ent.table_name = tbl->table_name;
10717c478bd9Sstevel@tonic-gate 			tbl_ent.hashval = tbl->hashval;
10727c478bd9Sstevel@tonic-gate 			tbl_ent.scheme = tbl->scheme;
10737c478bd9Sstevel@tonic-gate 			tbl_ent.database = tbl->database;
10747c478bd9Sstevel@tonic-gate 			tbl_ent.next = NULL;
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 		retval = db_clone_bucket(&tbl_ent, &clone);
10777c478bd9Sstevel@tonic-gate 		if (retval != 1) {
10787c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG,
10797c478bd9Sstevel@tonic-gate 			"extract_entries: unable to clone entry for %s",
10807c478bd9Sstevel@tonic-gate 			fs[i]);
10817c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10827c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10837c478bd9Sstevel@tonic-gate 			return (FALSE);
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 		dbstat = add_to_dictionary(tempdict.dictionary, clone);
10867c478bd9Sstevel@tonic-gate 		if (dbstat != DB_SUCCESS) {
10877c478bd9Sstevel@tonic-gate 			delete_table_desc(clone);
10887c478bd9Sstevel@tonic-gate 			READUNLOCK(this, FALSE,
10897c478bd9Sstevel@tonic-gate 					"ru db_dictionary::extract_entries");
10907c478bd9Sstevel@tonic-gate 			return (FALSE);
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	if (tempdict.dump() != DB_SUCCESS) {
10947c478bd9Sstevel@tonic-gate 		READUNLOCK(this, FALSE,
10957c478bd9Sstevel@tonic-gate 				"ru db_dictionary::extract_entries");
10967c478bd9Sstevel@tonic-gate 		return (FALSE);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	READUNLOCK(this, FALSE,
10997c478bd9Sstevel@tonic-gate 			"ru db_dictionary::extract_entries");
11007c478bd9Sstevel@tonic-gate 	return (TRUE);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate /*
11057c478bd9Sstevel@tonic-gate  * Initialize dictionary from contents in 'file'.
11067c478bd9Sstevel@tonic-gate  * If there is already information in this dictionary, it is removed.
11077c478bd9Sstevel@tonic-gate  * Therefore, regardless of whether the load from the file succeeds,
11087c478bd9Sstevel@tonic-gate  * the contents of this dictionary will be altered.  Returns
11097c478bd9Sstevel@tonic-gate  * whether table has been initialized successfully.
11107c478bd9Sstevel@tonic-gate  */
11117c478bd9Sstevel@tonic-gate bool_t
init(char * file)11127c478bd9Sstevel@tonic-gate db_dictionary::init(char *file)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	int status;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	WRITELOCK(this, FALSE, "w db_dictionary::init");
11177c478bd9Sstevel@tonic-gate 	db_shutdown();
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	pickle_dict_desc f(file, PICKLE_READ);
11207c478bd9Sstevel@tonic-gate 	filename = strdup(file);
11217c478bd9Sstevel@tonic-gate 	if (filename == NULL) {
11227c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11237c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11247c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11257c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	int len = strlen(filename);
11287c478bd9Sstevel@tonic-gate 	tmpfilename = new char[len+5];
11297c478bd9Sstevel@tonic-gate 	if (tmpfilename == NULL) {
11307c478bd9Sstevel@tonic-gate 		delete filename;
11317c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11327c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11337c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11347c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	logfilename = new char[len+5];
11377c478bd9Sstevel@tonic-gate 	if (logfilename == NULL) {
11387c478bd9Sstevel@tonic-gate 		delete filename;
11397c478bd9Sstevel@tonic-gate 		delete tmpfilename;
11407c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11417c478bd9Sstevel@tonic-gate 				"db_dictionary::init: cannot allocate space");
11427c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: cannot allocate space",
11437c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	sprintf(tmpfilename, "%s.tmp", filename);
11477c478bd9Sstevel@tonic-gate 	sprintf(logfilename, "%s.log", filename);
11487c478bd9Sstevel@tonic-gate 	unlink(tmpfilename);  /* get rid of partial checkpoints */
11497c478bd9Sstevel@tonic-gate 	dictionary = NULL;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/* load dictionary */
11527c478bd9Sstevel@tonic-gate 	if ((status = f.transfer(&dictionary)) < 0) {
11537c478bd9Sstevel@tonic-gate 	    initialized = FALSE;
11547c478bd9Sstevel@tonic-gate 	} else if (status == 1) {  /* no dictionary exists, create one */
11557c478bd9Sstevel@tonic-gate 	    dictionary = new db_dict_desc;
11567c478bd9Sstevel@tonic-gate 	    if (dictionary == NULL) {
11577c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, FALSE,
11587c478bd9Sstevel@tonic-gate 			"db_dictionary::init: could not allocate space");
11597c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::init: could not allocate space",
11607c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, FALSE);
11617c478bd9Sstevel@tonic-gate 	    }
11627c478bd9Sstevel@tonic-gate 	    dictionary->tables.tables_len = 0;
11637c478bd9Sstevel@tonic-gate 	    dictionary->tables.tables_val = NULL;
11647c478bd9Sstevel@tonic-gate 	    dictionary->count = 0;
11657c478bd9Sstevel@tonic-gate 	    dictionary->impl_vers = DB_CURRENT_VERSION;
11667c478bd9Sstevel@tonic-gate 	    initialized = TRUE;
11677c478bd9Sstevel@tonic-gate 	} else  /* dictionary loaded successfully */
11687c478bd9Sstevel@tonic-gate 	    initialized = TRUE;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (initialized == TRUE) {
11717c478bd9Sstevel@tonic-gate 	    int num_changes = 0;
11727c478bd9Sstevel@tonic-gate 	    changed = FALSE;
11737c478bd9Sstevel@tonic-gate 	    reset_log();
11747c478bd9Sstevel@tonic-gate 	    if ((num_changes = incorporate_log(logfilename)) < 0)
11757c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
11767c478bd9Sstevel@tonic-gate 			"incorporation of dictionary logfile '%s' failed",
11777c478bd9Sstevel@tonic-gate 			logfilename);
11787c478bd9Sstevel@tonic-gate 	    changed = (num_changes > 0);
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, initialized, "wu db_dictionary::init");
11827c478bd9Sstevel@tonic-gate 	return (initialized);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate  * Execute log entry 'j' on the dictionary identified by 'dict' if the
11877c478bd9Sstevel@tonic-gate  * version of j is later than that of the dictionary.  If 'j' is executed,
11887c478bd9Sstevel@tonic-gate  * 'count' is incremented and the dictionary's verison is updated to
11897c478bd9Sstevel@tonic-gate  * that of 'j'.
11907c478bd9Sstevel@tonic-gate  * Returns TRUE always for valid log entries; FALSE otherwise.
11917c478bd9Sstevel@tonic-gate  */
11927c478bd9Sstevel@tonic-gate static bool_t
apply_log_entry(db_dictlog_entry * j,char * dictchar,int * count)11937c478bd9Sstevel@tonic-gate apply_log_entry(db_dictlog_entry *j, char *dictchar, int *count)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	db_dictionary *dict = (db_dictionary*) dictchar;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	WRITELOCK(dict, FALSE, "w apply_log_entry");
11987c478bd9Sstevel@tonic-gate 	if (db_update_version.earlier_than(j->get_version())) {
11997c478bd9Sstevel@tonic-gate 		++ *count;
12007c478bd9Sstevel@tonic-gate #ifdef DEBUG
12017c478bd9Sstevel@tonic-gate 		j->print();
12027c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12037c478bd9Sstevel@tonic-gate 		switch (j->get_action()) {
12047c478bd9Sstevel@tonic-gate 		case DB_ADD_TABLE:
12057c478bd9Sstevel@tonic-gate 			dict->add_table_aux(j->get_table_name(),
12067c478bd9Sstevel@tonic-gate 				j->get_table_object(), INMEMORY_ONLY);
12077c478bd9Sstevel@tonic-gate 			// ignore status
12087c478bd9Sstevel@tonic-gate 			break;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		case DB_REMOVE_TABLE:
12117c478bd9Sstevel@tonic-gate 			dict->delete_table_aux(j->get_table_name(),
12127c478bd9Sstevel@tonic-gate 							INMEMORY_ONLY);
12137c478bd9Sstevel@tonic-gate 			// ignore status
12147c478bd9Sstevel@tonic-gate 			break;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		default:
12177c478bd9Sstevel@tonic-gate 			WARNING("db::apply_log_entry: unknown action_type");
12187c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(dict, FALSE, "wu apply_log_entry");
12197c478bd9Sstevel@tonic-gate 			return (FALSE);
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		db_update_version.assign(j->get_version());
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(dict, TRUE, "wu apply_log_entry");
12257c478bd9Sstevel@tonic-gate 	return (TRUE);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate int
incorporate_log(char * file_name)12297c478bd9Sstevel@tonic-gate db_dictionary::incorporate_log(char *file_name)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	db_dictlog f(file_name, PICKLE_READ);
12327c478bd9Sstevel@tonic-gate 	int	ret;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	WRITELOCK(this, -1, "w db_dictionary::incorporate_log");
12357c478bd9Sstevel@tonic-gate 	setNoWriteThrough();
12367c478bd9Sstevel@tonic-gate 	ret = f.execute_on_log(&(apply_log_entry), (char *) this);
12377c478bd9Sstevel@tonic-gate 	clearNoWriteThrough();
12387c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, -1, "wu db_dictionary::incorporate_log");
12397c478bd9Sstevel@tonic-gate 	return (ret);
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate /* Frees memory of filename and tables.  Has no effect on disk storage. */
12447c478bd9Sstevel@tonic-gate db_status
db_shutdown()12457c478bd9Sstevel@tonic-gate db_dictionary::db_shutdown()
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::db_shutdown");
12487c478bd9Sstevel@tonic-gate 	if (!initialized) {
12497c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
12507c478bd9Sstevel@tonic-gate 				"wu db_dictionary::db_shutdown");
12517c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS); /* DB_NOTFOUND? */
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (filename) {
12557c478bd9Sstevel@tonic-gate 		delete filename;
12567c478bd9Sstevel@tonic-gate 		filename = NULL;
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	if (tmpfilename) {
12597c478bd9Sstevel@tonic-gate 		delete tmpfilename;
12607c478bd9Sstevel@tonic-gate 		tmpfilename = NULL;
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 	if (logfilename) {
12637c478bd9Sstevel@tonic-gate 		delete logfilename;
12647c478bd9Sstevel@tonic-gate 		logfilename = NULL;
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 	if (dictionary) {
12677c478bd9Sstevel@tonic-gate 		delete_dictionary(dictionary);
12687c478bd9Sstevel@tonic-gate 		dictionary = NULL;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 	initialized = FALSE;
12717c478bd9Sstevel@tonic-gate 	changed = FALSE;
12727c478bd9Sstevel@tonic-gate 	reset_log();
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::db_shutdown");
12757c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * Dump contents of this dictionary (minus the database representations)
12807c478bd9Sstevel@tonic-gate  * to its file. Returns 0 if operation succeeds, -1 otherwise.
12817c478bd9Sstevel@tonic-gate  */
12827c478bd9Sstevel@tonic-gate int
dump()12837c478bd9Sstevel@tonic-gate db_dictionary::dump()
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	int status;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	READLOCK(this, -1, "r db_dictionary::dump");
12887c478bd9Sstevel@tonic-gate 	if (!initialized) {
12897c478bd9Sstevel@tonic-gate 		READUNLOCK(this, -1, "ru db_dictionary::dump");
12907c478bd9Sstevel@tonic-gate 		return (-1);
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	unlink(tmpfilename);  /* get rid of partial dumps */
12947c478bd9Sstevel@tonic-gate 	pickle_dict_desc f(tmpfilename, PICKLE_WRITE);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	status = f.transfer(&dictionary); 	/* dump table descs */
12977c478bd9Sstevel@tonic-gate 	if (status != 0) {
12987c478bd9Sstevel@tonic-gate 		WARNING("db_dictionary::dump: could not write out dictionary");
12997c478bd9Sstevel@tonic-gate 	} else if (rename(tmpfilename, filename) < 0) {
13007c478bd9Sstevel@tonic-gate 		WARNING_M("db_dictionary::dump: could not rename temp file: ");
13017c478bd9Sstevel@tonic-gate 		status = -1;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	READUNLOCK(this, -1, "ru db_dictionary::dump");
13057c478bd9Sstevel@tonic-gate 	return (status);
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate /*
13097c478bd9Sstevel@tonic-gate  * Write out in-memory copy of dictionary to file.
13107c478bd9Sstevel@tonic-gate  * 1.  Update major version.
13117c478bd9Sstevel@tonic-gate  * 2.  Dump contents to temporary file.
13127c478bd9Sstevel@tonic-gate  * 3.  Rename temporary file to real dictionary file.
13137c478bd9Sstevel@tonic-gate  * 4.  Remove log file.
13147c478bd9Sstevel@tonic-gate  * A checkpoint is done only if it has changed since the previous checkpoint.
13157c478bd9Sstevel@tonic-gate  * Returns DB_SUCCESS if checkpoint was successful; error code otherwise
13167c478bd9Sstevel@tonic-gate  */
13177c478bd9Sstevel@tonic-gate db_status
checkpoint()13187c478bd9Sstevel@tonic-gate db_dictionary::checkpoint()
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::checkpoint");
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	if (changed == FALSE) {
13237c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
13247c478bd9Sstevel@tonic-gate 				"wu db_dictionary::checkpoint");
13257c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	vers *oldv = new vers(db_update_version);	// copy
13297c478bd9Sstevel@tonic-gate 	vers * newv = db_update_version.nextmajor();	// get next version
13307c478bd9Sstevel@tonic-gate 	db_update_version.assign(newv);			// update version
13317c478bd9Sstevel@tonic-gate 	delete newv;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (dump() != 0) {
13347c478bd9Sstevel@tonic-gate 		WARNING_M(
13357c478bd9Sstevel@tonic-gate 		    "db_dictionary::checkpoint: could not dump dictionary: ");
13367c478bd9Sstevel@tonic-gate 		db_update_version.assign(oldv);  // rollback
13377c478bd9Sstevel@tonic-gate 		delete oldv;
13387c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_INTERNAL_ERROR,
13397c478bd9Sstevel@tonic-gate 			"wu db_dictionary::checkpoint");
13407c478bd9Sstevel@tonic-gate 		return (DB_INTERNAL_ERROR);
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	unlink(logfilename);	/* should do atomic rename and log delete */
13437c478bd9Sstevel@tonic-gate 	reset_log();		/* should check for what? */
13447c478bd9Sstevel@tonic-gate 	delete oldv;
13457c478bd9Sstevel@tonic-gate 	changed = FALSE;
13467c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::checkpoint");
13477c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate /* close existing logfile and delete its structure */
13517c478bd9Sstevel@tonic-gate int
reset_log()13527c478bd9Sstevel@tonic-gate db_dictionary::reset_log()
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate 	WRITELOCK(this, -1, "w db_dictionary::reset_log");
13557c478bd9Sstevel@tonic-gate 	/* try to close old log file */
13567c478bd9Sstevel@tonic-gate 	/* doesnot matter since we do synchronous writes only */
13577c478bd9Sstevel@tonic-gate 	if (logfile != NULL) {
13587c478bd9Sstevel@tonic-gate 		if (logfile_opened == TRUE) {
13597c478bd9Sstevel@tonic-gate 			if (logfile->close() < 0) {
13607c478bd9Sstevel@tonic-gate 				WARNING_M(
13617c478bd9Sstevel@tonic-gate 			"db_dictionary::reset_log: could not close log file: ");
13627c478bd9Sstevel@tonic-gate 			}
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 		delete logfile;
13657c478bd9Sstevel@tonic-gate 		logfile = NULL;
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 	logfile_opened = FALSE;
13687c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, -1, "wu db_dictionary::reset_log");
13697c478bd9Sstevel@tonic-gate 	return (0);
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate /* close existing logfile, but leave its structure if exists */
13737c478bd9Sstevel@tonic-gate int
close_log()13747c478bd9Sstevel@tonic-gate db_dictionary::close_log()
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	WRITELOCK(this, -1, "w db_dictionary::close_log");
13777c478bd9Sstevel@tonic-gate 	if (logfile != NULL && logfile_opened == TRUE) {
13787c478bd9Sstevel@tonic-gate 		logfile->close();
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 	logfile_opened = FALSE;
13817c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, -1, "wu db_dictionary::close_log");
13827c478bd9Sstevel@tonic-gate 	return (0);
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate /* open logfile, creating its structure if it does not exist */
13867c478bd9Sstevel@tonic-gate int
open_log()13877c478bd9Sstevel@tonic-gate db_dictionary::open_log()
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	WRITELOCK(this, -1, "w db_dictionary::open_log");
13907c478bd9Sstevel@tonic-gate 	if (logfile == NULL) {
13917c478bd9Sstevel@tonic-gate 		if ((logfile = new db_dictlog(logfilename, PICKLE_APPEND)) ==
13927c478bd9Sstevel@tonic-gate 				NULL) {
13937c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, -1, "wu db_dictionary::open_log");
13947c478bd9Sstevel@tonic-gate 			FATAL3(
13957c478bd9Sstevel@tonic-gate 			"db_dictionary::reset_log: cannot allocate space",
13967c478bd9Sstevel@tonic-gate 				DB_MEMORY_LIMIT, -1);
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	if (logfile_opened == TRUE) {
14017c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, -1, "wu db_dictionary::open_log");
14027c478bd9Sstevel@tonic-gate 		return (0);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	if ((logfile->open()) == NULL) {
14067c478bd9Sstevel@tonic-gate 		WARNING_M("db_dictionary::open_log: could not open log file: ");
14077c478bd9Sstevel@tonic-gate 		delete logfile;
14087c478bd9Sstevel@tonic-gate 		logfile = NULL;
14097c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, -1, "wu db_dictionary::open_log");
14107c478bd9Sstevel@tonic-gate 		return (-1);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	logfile_opened = TRUE;
14147c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, -1, "wu db_dictionary::open_log");
14157c478bd9Sstevel@tonic-gate 	return (0);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate /*
14197c478bd9Sstevel@tonic-gate  * closes any open log files for all tables in dictionary or 'tab'.
14207c478bd9Sstevel@tonic-gate  * "tab" is an optional argument.
14217c478bd9Sstevel@tonic-gate  */
14227c478bd9Sstevel@tonic-gate static int close_standby_list();
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate db_status
db_standby(char * tab)14257c478bd9Sstevel@tonic-gate db_dictionary::db_standby(char *tab)
14267c478bd9Sstevel@tonic-gate {
14277c478bd9Sstevel@tonic-gate 	db_table_desc *tbl;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::db_standby");
14307c478bd9Sstevel@tonic-gate 	if (!initialized) {
14317c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_BADDICTIONARY,
14327c478bd9Sstevel@tonic-gate 				"wu db_dictionary::db_standby");
14337c478bd9Sstevel@tonic-gate 		return (DB_BADDICTIONARY);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if (tab == NULL) {
14377c478bd9Sstevel@tonic-gate 	    close_log();  // close dictionary log
14387c478bd9Sstevel@tonic-gate 	    close_standby_list();
14397c478bd9Sstevel@tonic-gate 	    WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::db_standby");
14407c478bd9Sstevel@tonic-gate 	    return (DB_SUCCESS);
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if ((tbl = find_table_desc(tab)) == NULL) {
14447c478bd9Sstevel@tonic-gate 	    WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::db_standby");
14457c478bd9Sstevel@tonic-gate 	    return (DB_BADTABLE);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (tbl->database != NULL)
14497c478bd9Sstevel@tonic-gate 	    tbl->database->close_log();
14507c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::db_standby");
14517c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate /*
14557c478bd9Sstevel@tonic-gate  * Returns db_table_desc of table name 'tab'.  'prev', if supplied,
14567c478bd9Sstevel@tonic-gate  * is set to the entry located ahead of 'tab's entry in the dictionary.
14577c478bd9Sstevel@tonic-gate  */
14587c478bd9Sstevel@tonic-gate db_table_desc*
find_table_desc(char * tab)14597c478bd9Sstevel@tonic-gate db_dictionary::find_table_desc(char *tab)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	db_table_desc	*ret;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_dictionary::find_table_desc");
14647c478bd9Sstevel@tonic-gate 	if (initialized)
14657c478bd9Sstevel@tonic-gate 		ret = search_dictionary(dictionary, tab);
14667c478bd9Sstevel@tonic-gate 	else
14677c478bd9Sstevel@tonic-gate 		ret = NULL;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "r db_dictionary::find_table_desc");
14707c478bd9Sstevel@tonic-gate 	return (ret);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate db_table_desc *
find_table_desc(char * tab,bool_t searchDeferred)14747c478bd9Sstevel@tonic-gate db_dictionary::find_table_desc(char *tab, bool_t searchDeferred) {
14757c478bd9Sstevel@tonic-gate 	db_table_desc	*ret = NULL;
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_dictionary::find_table_desc_d");
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	/* If desired, look in the deferred dictionary first */
14807c478bd9Sstevel@tonic-gate 	if (initialized && searchDeferred && deferred.dictionary != NULL)
14817c478bd9Sstevel@tonic-gate 		ret = search_dictionary(deferred.dictionary, tab);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	/* No result yet => search the "normal" dictionary */
14847c478bd9Sstevel@tonic-gate 	if (ret == NULL)
14857c478bd9Sstevel@tonic-gate 		ret = find_table_desc(tab);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "r db_dictionary::find_table_desc_d");
14887c478bd9Sstevel@tonic-gate 	return (ret);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate db *
find_table(char * tab,db_table_desc ** where)14927c478bd9Sstevel@tonic-gate db_dictionary::find_table(char *tab, db_table_desc **where) {
14937c478bd9Sstevel@tonic-gate 	/* Most operations should use the deferred dictionary if it exists */
14947c478bd9Sstevel@tonic-gate 	return (find_table(tab, where, TRUE, TRUE, TRUE));
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate db *
find_table(char * tab,db_table_desc ** where,bool_t searchDeferred)14987c478bd9Sstevel@tonic-gate db_dictionary::find_table(char *tab, db_table_desc **where,
14997c478bd9Sstevel@tonic-gate 				bool_t searchDeferred) {
15007c478bd9Sstevel@tonic-gate 	return (find_table(tab, where, searchDeferred, TRUE, TRUE));
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate db *
find_table(char * tab,db_table_desc ** where,bool_t searchDeferred,bool_t doLDAP,bool_t doLoad)15047c478bd9Sstevel@tonic-gate db_dictionary::find_table(char *tab, db_table_desc **where,
15057c478bd9Sstevel@tonic-gate 				bool_t searchDeferred, bool_t doLDAP,
15067c478bd9Sstevel@tonic-gate 				bool_t doLoad) {
15077c478bd9Sstevel@tonic-gate 	db			*res;
15087c478bd9Sstevel@tonic-gate 	int			lstat;
15097c478bd9Sstevel@tonic-gate 	db_status		dstat;
1510*8d0852b7SRichard Lowe 	const char		*myself = "db_dictionary::find_table";
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	res = find_table_noLDAP(tab, where, searchDeferred, doLoad);
15137c478bd9Sstevel@tonic-gate 	/* If found, or shouldn't try LDAP, we're done */
15147c478bd9Sstevel@tonic-gate 	if (res != 0 || !doLDAP)
15157c478bd9Sstevel@tonic-gate 		return (res);
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	/* See if we can retrieve the object from LDAP */
15187c478bd9Sstevel@tonic-gate 	dstat = dbCreateFromLDAP(tab, &lstat);
15197c478bd9Sstevel@tonic-gate 	if (dstat != DB_SUCCESS) {
15207c478bd9Sstevel@tonic-gate 		if (dstat == DB_NOTFOUND) {
15217c478bd9Sstevel@tonic-gate 			if (lstat != LDAP_SUCCESS) {
15227c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_INFO,
15237c478bd9Sstevel@tonic-gate 					"%s: LDAP error for \"%s\": %s",
15247c478bd9Sstevel@tonic-gate 					myself, NIL(tab),
15257c478bd9Sstevel@tonic-gate 					ldap_err2string(lstat));
15267c478bd9Sstevel@tonic-gate 			}
15277c478bd9Sstevel@tonic-gate 		} else {
15287c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOTIMECHECK, LOG_INFO,
15297c478bd9Sstevel@tonic-gate 				"%s: DB error %d for \"%s\"",
15307c478bd9Sstevel@tonic-gate 				myself, dstat, NIL(tab));
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 		return (0);
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	/* Try the dictionary again */
15367c478bd9Sstevel@tonic-gate 	res = find_table_noLDAP(tab, where, searchDeferred, doLoad);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	return (res);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate /*
15427c478bd9Sstevel@tonic-gate  * Return database structure of table named by 'tab'.
15437c478bd9Sstevel@tonic-gate  * If 'where' is set, set it to the table_desc of 'tab.'
15447c478bd9Sstevel@tonic-gate  * If the database is loaded in from stable store if it has not been loaded.
15457c478bd9Sstevel@tonic-gate  * If it cannot be loaded, it is initialized using the scheme stored in
15467c478bd9Sstevel@tonic-gate  * the table_desc.  NULL is returned if the initialization fails.
15477c478bd9Sstevel@tonic-gate  */
15487c478bd9Sstevel@tonic-gate db *
find_table_noLDAP(char * tab,db_table_desc ** where,bool_t searchDeferred,bool_t doLoad)15497c478bd9Sstevel@tonic-gate db_dictionary::find_table_noLDAP(char *tab, db_table_desc **where,
15507c478bd9Sstevel@tonic-gate 				bool_t searchDeferred, bool_t doLoad)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	if (!initialized)
15537c478bd9Sstevel@tonic-gate 		return (NULL);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	db_table_desc* tbl;
15567c478bd9Sstevel@tonic-gate 	db *dbase = NULL;
15577c478bd9Sstevel@tonic-gate 	int		lret;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_dictionary::find_table");
15607c478bd9Sstevel@tonic-gate 	tbl = find_table_desc(tab, searchDeferred);
15617c478bd9Sstevel@tonic-gate 	if (tbl == NULL) {
15627c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_dictionary::find_table");
15637c478bd9Sstevel@tonic-gate 		return (NULL);		// not found
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	if (tbl->database != NULL || !doLoad) {
15677c478bd9Sstevel@tonic-gate 		if (tbl->database && where) *where = tbl;
15687c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_dictionary::find_table");
15697c478bd9Sstevel@tonic-gate 		return (tbl->database);  // return handle
15707c478bd9Sstevel@tonic-gate 	}
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	READUNLOCK(this, NULL, "ru db_dictionary::find_table");
15737c478bd9Sstevel@tonic-gate 	WRITELOCK(this, NULL, "w db_dictionary::find_table");
15747c478bd9Sstevel@tonic-gate 	/* Re-check; some other thread might have loaded the db */
15757c478bd9Sstevel@tonic-gate 	if (tbl->database != NULL) {
15767c478bd9Sstevel@tonic-gate 		if (where) *where = tbl;
15777c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL, "wu db_dictionary::find_table");
15787c478bd9Sstevel@tonic-gate 		return (tbl->database);  // return handle
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	// need to load in/init database
15827c478bd9Sstevel@tonic-gate 	dbase = new db(tab);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (dbase == NULL) {
15857c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL,
15867c478bd9Sstevel@tonic-gate 			"db_dictionary::find_table: could not allocate space");
15877c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::find_table: could not allocate space",
15887c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, NULL);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	/*
15927c478bd9Sstevel@tonic-gate 	 * Lock the newly created 'dbase', so we can release the general
15937c478bd9Sstevel@tonic-gate 	 * db_dictionary lock.
15947c478bd9Sstevel@tonic-gate 	 */
15957c478bd9Sstevel@tonic-gate 	WRITELOCKNR(dbase, lret, "w dbase db_dictionary::find_table");
15967c478bd9Sstevel@tonic-gate 	if (lret != 0) {
15977c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL,
15987c478bd9Sstevel@tonic-gate 			"db_dictionary::find_table: could not lock dbase");
15997c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::find_table: could not lock dbase",
16007c478bd9Sstevel@tonic-gate 			DB_LOCK_ERROR, NULL);
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 	/* Assign tbl->database, and then release the 'this' lock */
16037c478bd9Sstevel@tonic-gate 	tbl->database = dbase;
16047c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, NULL, "wu db_dictionary::find_table");
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (dbase->load()) {			// try to load in database
16077c478bd9Sstevel@tonic-gate 		if (where) *where = tbl;
16087c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(dbase, dbase, "wu dbase db_dictionary::find_table");
16097c478bd9Sstevel@tonic-gate 		return (dbase);
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	delete dbase;
16137c478bd9Sstevel@tonic-gate 	tbl->database = NULL;
16147c478bd9Sstevel@tonic-gate 	WARNING("db_dictionary::find_table: could not load database");
16157c478bd9Sstevel@tonic-gate 	return (NULL);
16167c478bd9Sstevel@tonic-gate }
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate /* Log action to be taken on the  dictionary and update db_update_version. */
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate db_status
log_action(int action,char * tab,table_obj * tobj)16217c478bd9Sstevel@tonic-gate db_dictionary::log_action(int action, char *tab, table_obj *tobj)
16227c478bd9Sstevel@tonic-gate {
16237c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::log_action");
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	vers *newv = db_update_version.nextminor();
16267c478bd9Sstevel@tonic-gate 	db_dictlog_entry le(action, newv, tab, tobj);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (open_log() < 0) {
16297c478bd9Sstevel@tonic-gate 		delete newv;
16307c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_STORAGE_LIMIT,
16317c478bd9Sstevel@tonic-gate 				"wu db_dictionary::log_action");
16327c478bd9Sstevel@tonic-gate 		return (DB_STORAGE_LIMIT);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if (logfile->append(&le) < 0) {
16367c478bd9Sstevel@tonic-gate 		WARNING_M("db::log_action: could not add log entry: ");
16377c478bd9Sstevel@tonic-gate 		close_log();
16387c478bd9Sstevel@tonic-gate 		delete newv;
16397c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_STORAGE_LIMIT,
16407c478bd9Sstevel@tonic-gate 				"wu db_dictionary::log_action");
16417c478bd9Sstevel@tonic-gate 		return (DB_STORAGE_LIMIT);
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	db_update_version.assign(newv);
16457c478bd9Sstevel@tonic-gate 	delete newv;
16467c478bd9Sstevel@tonic-gate 	changed = TRUE;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, DB_LOCK_ERROR, "wu db_dictionary::log_action");
16497c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate // For a complete 'delete' operation, we want the following behaviour:
16537c478bd9Sstevel@tonic-gate // 1. If there is an entry in the log, the physical table exists and is
16547c478bd9Sstevel@tonic-gate //    stable.
16557c478bd9Sstevel@tonic-gate // 2. If there is no entry in the log, the physical table may or may not
16567c478bd9Sstevel@tonic-gate //    exist.
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate db_status
delete_table_aux(char * tab,int mode)16597c478bd9Sstevel@tonic-gate db_dictionary::delete_table_aux(char *tab, int mode)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	db_status	ret;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::delete_table_aux");
16647c478bd9Sstevel@tonic-gate 	if (!initialized) {
16657c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
16667c478bd9Sstevel@tonic-gate 				"wu db_dictionary::delete_table_aux");
16677c478bd9Sstevel@tonic-gate 		return (DB_BADDICTIONARY);
16687c478bd9Sstevel@tonic-gate 	}
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	db_table_desc *tbl;
16717c478bd9Sstevel@tonic-gate 	if ((tbl = find_table_desc(tab)) == NULL) { // table not found
16727c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
16737c478bd9Sstevel@tonic-gate 				"wu db_dictionary::delete_table_aux");
16747c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	if (mode != INMEMORY_ONLY) {
16787c478bd9Sstevel@tonic-gate 		int need_free = 0;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 		// Update log.
16817c478bd9Sstevel@tonic-gate 		db_status status = log_action(DB_REMOVE_TABLE, tab);
16827c478bd9Sstevel@tonic-gate 		if (status != DB_SUCCESS) {
16837c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, status,
16847c478bd9Sstevel@tonic-gate 				"wu db_dictionary::delete_table_aux");
16857c478bd9Sstevel@tonic-gate 			return (status);
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		// Remove physical structures
16897c478bd9Sstevel@tonic-gate 		db *dbase = tbl->database;
16907c478bd9Sstevel@tonic-gate 		if (dbase == NULL) {	// need to get desc to access files
16917c478bd9Sstevel@tonic-gate 			dbase = new db(tab);
16927c478bd9Sstevel@tonic-gate 			need_free = 1;
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 		if (dbase == NULL) {
16957c478bd9Sstevel@tonic-gate 			WARNING(
16967c478bd9Sstevel@tonic-gate 		"db_dictionary::delete_table: could not create db structure");
16977c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_MEMORY_LIMIT,
16987c478bd9Sstevel@tonic-gate 					"wu db_dictionary::delete_table_aux");
16997c478bd9Sstevel@tonic-gate 			return (DB_MEMORY_LIMIT);
17007c478bd9Sstevel@tonic-gate 		}
17017c478bd9Sstevel@tonic-gate 		dbase->remove_files();	// remove physical files
17027c478bd9Sstevel@tonic-gate 		if (need_free)
17037c478bd9Sstevel@tonic-gate 			delete dbase;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	// Remove in-memory structures
17077c478bd9Sstevel@tonic-gate 	ret = remove_from_dictionary(dictionary, tab, TRUE);
17087c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_dictionary::delete_table_aux");
17097c478bd9Sstevel@tonic-gate 	return (ret);
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate /*
17137c478bd9Sstevel@tonic-gate  * Delete table with given name 'tab' from dictionary.
17147c478bd9Sstevel@tonic-gate  * Returns error code if table does not exist or if dictionary has not been
17157c478bd9Sstevel@tonic-gate  * initialized.   Dictionary is updated to stable store if deletion is
17167c478bd9Sstevel@tonic-gate  * successful.  Fatal error occurs if dictionary cannot be saved.
17177c478bd9Sstevel@tonic-gate  * Returns DB_SUCCESS if dictionary has been updated successfully.
17187c478bd9Sstevel@tonic-gate  * Note that the files associated with the table are also removed.
17197c478bd9Sstevel@tonic-gate  */
17207c478bd9Sstevel@tonic-gate db_status
delete_table(char * tab)17217c478bd9Sstevel@tonic-gate db_dictionary::delete_table(char *tab)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 	return (delete_table_aux(tab, !INMEMORY_ONLY));
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate // For a complete 'add' operation, we want the following behaviour:
17277c478bd9Sstevel@tonic-gate // 1. If there is an entry in the log, then the physical table exists and
17287c478bd9Sstevel@tonic-gate //    has been initialized properly.
17297c478bd9Sstevel@tonic-gate // 2. If there is no entry in the log, the physical table may or may not
17307c478bd9Sstevel@tonic-gate //    exist.  In this case, we don't really care because we cannot get at
17317c478bd9Sstevel@tonic-gate //    it.  The next time we add a table with the same name to the dictionary,
17327c478bd9Sstevel@tonic-gate //    it will be initialized properly.
17337c478bd9Sstevel@tonic-gate // This mode is used when the table is first created.
17347c478bd9Sstevel@tonic-gate //
17357c478bd9Sstevel@tonic-gate // For an INMEMORY_ONLY operation, only the internal structure is created and
17367c478bd9Sstevel@tonic-gate // updated.  This mode is used when the database gets loaded and the internal
17377c478bd9Sstevel@tonic-gate // dictionary gets updated from the log entries.
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate db_status
add_table_aux(char * tab,table_obj * tobj,int mode)17407c478bd9Sstevel@tonic-gate db_dictionary::add_table_aux(char *tab, table_obj* tobj, int mode)
17417c478bd9Sstevel@tonic-gate {
17427c478bd9Sstevel@tonic-gate 	db_status	ret;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::add_table_aux");
17457c478bd9Sstevel@tonic-gate 	if (!initialized) {
17467c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
17477c478bd9Sstevel@tonic-gate 				"wu db_dictionary::add_table_aux");
17487c478bd9Sstevel@tonic-gate 		return (DB_BADDICTIONARY);
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	if (find_table_desc(tab) != NULL) {
17527c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
17537c478bd9Sstevel@tonic-gate 				"wu db_dictionary::add_table_aux");
17547c478bd9Sstevel@tonic-gate 		return (DB_NOTUNIQUE);		// table already exists
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	// create data structures for table
17587c478bd9Sstevel@tonic-gate 	db_table_desc *new_table = 0;
17597c478bd9Sstevel@tonic-gate 	db_status status = create_table_desc(tab, tobj, &new_table);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	if (status != DB_SUCCESS) {
17627c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_LOCK_ERROR,
17637c478bd9Sstevel@tonic-gate 				"wu db_dictionary::add_table_aux");
17647c478bd9Sstevel@tonic-gate 		return (status);
17657c478bd9Sstevel@tonic-gate 	}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (mode != INMEMORY_ONLY) {
17687c478bd9Sstevel@tonic-gate 		// create physical structures for table
17697c478bd9Sstevel@tonic-gate 		new_table->database = new db(tab);
17707c478bd9Sstevel@tonic-gate 		if (new_table->database == NULL) {
17717c478bd9Sstevel@tonic-gate 			delete_table_desc(new_table);
17727c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_MEMORY_LIMIT,
17737c478bd9Sstevel@tonic-gate 		"db_dictionary::add_table: could not allocate space for db");
17747c478bd9Sstevel@tonic-gate 			FATAL3(
17757c478bd9Sstevel@tonic-gate 		    "db_dictionary::add_table: could not allocate space for db",
17767c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 		if (new_table->database->init(new_table->scheme) == 0) {
17797c478bd9Sstevel@tonic-gate 			WARNING(
17807c478bd9Sstevel@tonic-gate 	"db_dictionary::add_table: could not initialize database from scheme");
17817c478bd9Sstevel@tonic-gate 			new_table->database->remove_files();
17827c478bd9Sstevel@tonic-gate 			delete_table_desc(new_table);
17837c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, DB_STORAGE_LIMIT,
17847c478bd9Sstevel@tonic-gate 				"wu db_dictionary::add_table_aux");
17857c478bd9Sstevel@tonic-gate 			return (DB_STORAGE_LIMIT);
17867c478bd9Sstevel@tonic-gate 		}
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 		// update 'external' copy of dictionary
17897c478bd9Sstevel@tonic-gate 		status = log_action(DB_ADD_TABLE, tab, tobj);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		if (status != DB_SUCCESS) {
17927c478bd9Sstevel@tonic-gate 			new_table->database->remove_files();
17937c478bd9Sstevel@tonic-gate 			delete_table_desc(new_table);
17947c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, status,
17957c478bd9Sstevel@tonic-gate 					"wu db_dictionary::add_table_aux");
17967c478bd9Sstevel@tonic-gate 			return (status);
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	// finally, update in-memory copy of dictionary
18017c478bd9Sstevel@tonic-gate 	ret = add_to_dictionary(dictionary, new_table);
18027c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_dictionary::add_table_aux");
18037c478bd9Sstevel@tonic-gate 	return (ret);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate /*
18077c478bd9Sstevel@tonic-gate  * Add table with given name 'tab' and description 'zdesc' to dictionary.
18087c478bd9Sstevel@tonic-gate  * Returns errror code if table already exists, or if no memory can be found
18097c478bd9Sstevel@tonic-gate  * to store the descriptor, or if dictionary has not been intialized.
18107c478bd9Sstevel@tonic-gate  * Dictionary is updated to stable store if addition is successful.
18117c478bd9Sstevel@tonic-gate  * Fatal error occurs if dictionary cannot be saved.
18127c478bd9Sstevel@tonic-gate  * Returns DB_SUCCESS if dictionary has been updated successfully.
18137c478bd9Sstevel@tonic-gate */
18147c478bd9Sstevel@tonic-gate db_status
add_table(char * tab,table_obj * tobj)18157c478bd9Sstevel@tonic-gate db_dictionary::add_table(char *tab, table_obj* tobj)
18167c478bd9Sstevel@tonic-gate {
18177c478bd9Sstevel@tonic-gate 	return (add_table_aux(tab, tobj, !INMEMORY_ONLY));
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate /*
18217c478bd9Sstevel@tonic-gate  * Translate given NIS attribute list to a db_query structure.
18227c478bd9Sstevel@tonic-gate  * Return FALSE if dictionary has not been initialized, or
18237c478bd9Sstevel@tonic-gate  * table does not have a scheme (which should be a fatal error?).
18247c478bd9Sstevel@tonic-gate  */
18257c478bd9Sstevel@tonic-gate db_query*
translate_to_query(db_table_desc * tbl,int numattrs,nis_attr * attrlist)18267c478bd9Sstevel@tonic-gate db_dictionary::translate_to_query(db_table_desc* tbl, int numattrs,
18277c478bd9Sstevel@tonic-gate 				nis_attr* attrlist)
18287c478bd9Sstevel@tonic-gate {
18297c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_dictionary::translate_to_query");
18307c478bd9Sstevel@tonic-gate 	if (!initialized ||
18317c478bd9Sstevel@tonic-gate 		tbl->scheme == NULL || numattrs == 0 || attrlist == NULL) {
18327c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_dictionary::translate_to_query");
18337c478bd9Sstevel@tonic-gate 		return (NULL);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	db_query *q = new db_query(tbl->scheme, numattrs, attrlist);
18377c478bd9Sstevel@tonic-gate 	if (q == NULL) {
18387c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL,
18397c478bd9Sstevel@tonic-gate 			"db_dictionary::translate: could not allocate space");
18407c478bd9Sstevel@tonic-gate 		FATAL3("db_dictionary::translate: could not allocate space",
18417c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, NULL);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (q->size() == 0) {
18457c478bd9Sstevel@tonic-gate 		delete q;
18467c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru db_dictionary::translate_to_query");
18477c478bd9Sstevel@tonic-gate 		return (NULL);
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 	READUNLOCK(this, NULL, "ru db_dictionary::translate_to_query");
18507c478bd9Sstevel@tonic-gate 	return (q);
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate static db_table_names gt_answer;
18547c478bd9Sstevel@tonic-gate static int gt_posn;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate static db_status
get_table_name(db_table_desc * tbl)18577c478bd9Sstevel@tonic-gate get_table_name(db_table_desc* tbl)
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	if (tbl)
18607c478bd9Sstevel@tonic-gate 		return (DB_BADTABLE);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	if (gt_posn < gt_answer.db_table_names_len)
18637c478bd9Sstevel@tonic-gate 		gt_answer.db_table_names_val[gt_posn++] =
18647c478bd9Sstevel@tonic-gate 			strdup(tbl->table_name);
18657c478bd9Sstevel@tonic-gate 	else
18667c478bd9Sstevel@tonic-gate 		return (DB_BADTABLE);
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * Return the names of tables in this dictionary.
18747c478bd9Sstevel@tonic-gate  * XXX This routine is used only for testing only;
18757c478bd9Sstevel@tonic-gate  *	if to be used for real, need to free memory sensibly, or
18767c478bd9Sstevel@tonic-gate  *	caller of get_table_names should have freed them.
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate db_table_names*
get_table_names()18797c478bd9Sstevel@tonic-gate db_dictionary::get_table_names()
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r db_dictionary::get_table_names");
18827c478bd9Sstevel@tonic-gate 	gt_answer.db_table_names_len = dictionary->count;
18837c478bd9Sstevel@tonic-gate 	gt_answer.db_table_names_val = new db_table_namep[dictionary->count];
18847c478bd9Sstevel@tonic-gate 	gt_posn = 0;
18857c478bd9Sstevel@tonic-gate 	if ((gt_answer.db_table_names_val) == NULL) {
18867c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL,
18877c478bd9Sstevel@tonic-gate 	"db_dictionary::get_table_names: could not allocate space for names");
18887c478bd9Sstevel@tonic-gate 		FATAL3(
18897c478bd9Sstevel@tonic-gate 	"db_dictionary::get_table_names: could not allocate space for names",
18907c478bd9Sstevel@tonic-gate 		DB_MEMORY_LIMIT, NULL);
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	enumerate_dictionary(dictionary, &get_table_name);
18947c478bd9Sstevel@tonic-gate 	READUNLOCK(this, NULL, "ru db_dictionary::get_table_names");
18957c478bd9Sstevel@tonic-gate 	return (&gt_answer);
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate static db_status
db_checkpoint_aux(db_table_desc * current)18997c478bd9Sstevel@tonic-gate db_checkpoint_aux(db_table_desc *current)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate 	db *dbase;
19027c478bd9Sstevel@tonic-gate 	int status;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	if (current == NULL)
19057c478bd9Sstevel@tonic-gate 		return (DB_BADTABLE);
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	if (current->database == NULL) {  /* need to load it in */
19087c478bd9Sstevel@tonic-gate 		dbase = new db(current->table_name);
19097c478bd9Sstevel@tonic-gate 		if (dbase == NULL) {
19107c478bd9Sstevel@tonic-gate 			FATAL3(
19117c478bd9Sstevel@tonic-gate 		    "db_dictionary::db_checkpoint: could not allocate space",
19127c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, DB_MEMORY_LIMIT);
19137c478bd9Sstevel@tonic-gate 		}
19147c478bd9Sstevel@tonic-gate 		if (dbase->load() == 0) {
19157c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
19167c478bd9Sstevel@tonic-gate 			"db_dictionary::db_checkpoint: could not load table %s",
19177c478bd9Sstevel@tonic-gate 							current->table_name);
19187c478bd9Sstevel@tonic-gate 			delete dbase;
19197c478bd9Sstevel@tonic-gate 			return (DB_BADTABLE);
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 		status = dbase->checkpoint();
19227c478bd9Sstevel@tonic-gate 		delete dbase;  // unload
19237c478bd9Sstevel@tonic-gate 	} else
19247c478bd9Sstevel@tonic-gate 	    status = current->database->checkpoint();
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	if (status == 0)
19277c478bd9Sstevel@tonic-gate 		return (DB_STORAGE_LIMIT);
19287c478bd9Sstevel@tonic-gate 	return (DB_SUCCESS);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate /* Like db_checkpoint_aux except only stops on LIMIT errors */
19327c478bd9Sstevel@tonic-gate static db_status
db_checkpoint_aux_cont(db_table_desc * current)19337c478bd9Sstevel@tonic-gate db_checkpoint_aux_cont(db_table_desc *current)
19347c478bd9Sstevel@tonic-gate {
19357c478bd9Sstevel@tonic-gate 	db_status status = db_checkpoint_aux(current);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	if (status == DB_STORAGE_LIMIT || status == DB_MEMORY_LIMIT)
19387c478bd9Sstevel@tonic-gate 		return (status);
19397c478bd9Sstevel@tonic-gate 	else
19407c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate db_status
db_checkpoint(char * tab)19447c478bd9Sstevel@tonic-gate db_dictionary::db_checkpoint(char *tab)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate 	db_table_desc *tbl;
19477c478bd9Sstevel@tonic-gate 	db_status	ret;
19487c478bd9Sstevel@tonic-gate 	bool_t		init;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::db_checkpoint");
19517c478bd9Sstevel@tonic-gate 	init = initialized;
19527c478bd9Sstevel@tonic-gate 	READUNLOCK(this, DB_LOCK_ERROR, "ru db_dictionary::db_checkpoint");
19537c478bd9Sstevel@tonic-gate 	if (!init)
19547c478bd9Sstevel@tonic-gate 		return (DB_BADDICTIONARY);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	checkpoint();	// checkpoint dictionary first
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	READLOCK(this, DB_LOCK_ERROR, "r db_dictionary::db_checkpoint");
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	if (tab == NULL) {
19617c478bd9Sstevel@tonic-gate 	    ret = enumerate_dictionary(dictionary, &db_checkpoint_aux_cont);
19627c478bd9Sstevel@tonic-gate 	    READUNLOCK(this, ret, "ru db_dictionary::db_checkpoint");
19637c478bd9Sstevel@tonic-gate 	    return (ret);
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if ((tbl = find_table_desc(tab)) == NULL) {
19677c478bd9Sstevel@tonic-gate 		READUNLOCK(this, DB_LOCK_ERROR,
19687c478bd9Sstevel@tonic-gate 				"ru db_dictionary::db_checkpoint");
19697c478bd9Sstevel@tonic-gate 	    return (DB_BADTABLE);
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	ret = db_checkpoint_aux(tbl);
19737c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru db_dictionary::db_checkpoint");
19747c478bd9Sstevel@tonic-gate 	return (ret);
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate /* *********************** db_standby **************************** */
19787c478bd9Sstevel@tonic-gate /* Deal with list of tables that need to be 'closed' */
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate #define	OPENED_DBS_CHUNK	12
19817c478bd9Sstevel@tonic-gate static db	**db_standby_list;
19827c478bd9Sstevel@tonic-gate static uint_t	db_standby_size = 0;
19837c478bd9Sstevel@tonic-gate static uint_t	db_standby_count = 0;
19847c478bd9Sstevel@tonic-gate DECLMUTEXLOCK(db_standby_list);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate /*
19877c478bd9Sstevel@tonic-gate  * Returns 1 if all databases on the list could be closed, 0
19887c478bd9Sstevel@tonic-gate  * otherwise.
19897c478bd9Sstevel@tonic-gate  */
19907c478bd9Sstevel@tonic-gate static int
close_standby_list()19917c478bd9Sstevel@tonic-gate close_standby_list()
19927c478bd9Sstevel@tonic-gate {
19937c478bd9Sstevel@tonic-gate 	db		*database;
19947c478bd9Sstevel@tonic-gate 	int		i, ret;
1995*8d0852b7SRichard Lowe 	const char	*myself = "close_standby_list";
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	MUTEXLOCK(db_standby_list, "close_standby_list");
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (db_standby_count == 0) {
20007c478bd9Sstevel@tonic-gate 		MUTEXUNLOCK(db_standby_list, "close_standby_list");
20017c478bd9Sstevel@tonic-gate 		return (1);
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	for (i = 0, ret = 0; i < db_standby_size; i++) {
20057c478bd9Sstevel@tonic-gate 		if ((database = db_standby_list[i])) {
20067c478bd9Sstevel@tonic-gate 			/*
20077c478bd9Sstevel@tonic-gate 			 * In order to avoid a potential dead-lock, we
20087c478bd9Sstevel@tonic-gate 			 * check to see if close_log() would be able to
20097c478bd9Sstevel@tonic-gate 			 * lock the db; if not, just skip the db.
20107c478bd9Sstevel@tonic-gate 			 */
20117c478bd9Sstevel@tonic-gate 			int	lockok;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 			TRYWRITELOCK(database, lockok,
20147c478bd9Sstevel@tonic-gate 				"try w db_dictionary::close_standby_list");
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 			if (lockok == 0) {
20177c478bd9Sstevel@tonic-gate 				database->close_log(1);
20187c478bd9Sstevel@tonic-gate 				db_standby_list[i] = (db*)NULL;
20197c478bd9Sstevel@tonic-gate 				--db_standby_count;
20207c478bd9Sstevel@tonic-gate 				WRITEUNLOCK(database, db_standby_count == 0,
20217c478bd9Sstevel@tonic-gate 					"db_dictionary::close_standby_list");
20227c478bd9Sstevel@tonic-gate 				if (db_standby_count == 0) {
20237c478bd9Sstevel@tonic-gate 					ret = 1;
20247c478bd9Sstevel@tonic-gate 					break;
20257c478bd9Sstevel@tonic-gate 				}
20267c478bd9Sstevel@tonic-gate 			} else if (lockok != EBUSY) {
20277c478bd9Sstevel@tonic-gate 				logmsg(MSG_NOTIMECHECK, LOG_INFO,
20287c478bd9Sstevel@tonic-gate 					"%s: try-lock error %d",
20297c478bd9Sstevel@tonic-gate 					myself, lockok);
20307c478bd9Sstevel@tonic-gate 			} /* else it's EBUSY; skip to the next one */
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	MUTEXUNLOCK(db_standby_list, "close_standby_list");
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	return (ret);
20377c478bd9Sstevel@tonic-gate }
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate /*
20407c478bd9Sstevel@tonic-gate  * Add given database to list of databases that have been opened for updates.
20417c478bd9Sstevel@tonic-gate  * If size of list exceeds maximum, close opened databases first.
20427c478bd9Sstevel@tonic-gate  */
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate int
add_to_standby_list(db * database)20457c478bd9Sstevel@tonic-gate add_to_standby_list(db* database)
20467c478bd9Sstevel@tonic-gate {
20477c478bd9Sstevel@tonic-gate 	int		i;
2048*8d0852b7SRichard Lowe 	const char	*myself = "add_to_standby_list";
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	MUTEXLOCK(db_standby_list, "add_to_standby_list");
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if (database == 0) {
20537c478bd9Sstevel@tonic-gate 		MUTEXUNLOCK(db_standby_list, "add_to_standby_list");
20547c478bd9Sstevel@tonic-gate 		return (1);
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	/* Try to keep the list below OPENED_DBS_CHUNK */
20587c478bd9Sstevel@tonic-gate 	if (db_standby_count >= OPENED_DBS_CHUNK) {
20597c478bd9Sstevel@tonic-gate 		MUTEXUNLOCK(db_standby_list, "add_to_standby_list");
20607c478bd9Sstevel@tonic-gate 		close_standby_list();
20617c478bd9Sstevel@tonic-gate 		MUTEXLOCK(db_standby_list, "add_to_standby_list");
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (db_standby_count >= db_standby_size) {
20657c478bd9Sstevel@tonic-gate 		db	**ndsl = (db **)realloc(db_standby_list,
20667c478bd9Sstevel@tonic-gate 					(db_standby_size+OPENED_DBS_CHUNK) *
20677c478bd9Sstevel@tonic-gate 						sizeof (ndsl[0]));
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 		if (ndsl == 0) {
20707c478bd9Sstevel@tonic-gate 			MUTEXUNLOCK(db_standby_list, "add_to_standby_list");
20717c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOMEM, LOG_ERR,
20727c478bd9Sstevel@tonic-gate 				"%s: realloc(%d) => NULL",
20737c478bd9Sstevel@tonic-gate 				myself, (db_standby_size+OPENED_DBS_CHUNK) *
20747c478bd9Sstevel@tonic-gate 				sizeof (ndsl[0]));
20757c478bd9Sstevel@tonic-gate 			return (0);
20767c478bd9Sstevel@tonic-gate 		}
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 		db_standby_list = ndsl;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		for (i = db_standby_size; i < db_standby_size+OPENED_DBS_CHUNK;
20817c478bd9Sstevel@tonic-gate 				i++)
20827c478bd9Sstevel@tonic-gate 			db_standby_list[i] = 0;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 		db_standby_size += OPENED_DBS_CHUNK;
20857c478bd9Sstevel@tonic-gate 	}
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	for (i = 0; i < db_standby_size; i++) {
20887c478bd9Sstevel@tonic-gate 		if (db_standby_list[i] == (db*)NULL) {
20897c478bd9Sstevel@tonic-gate 			db_standby_list[i] = database;
20907c478bd9Sstevel@tonic-gate 			++db_standby_count;
20917c478bd9Sstevel@tonic-gate 			MUTEXUNLOCK(db_standby_list, "add_to_standby_list");
20927c478bd9Sstevel@tonic-gate 			return (1);
20937c478bd9Sstevel@tonic-gate 		}
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	MUTEXUNLOCK(db_standby_list, "add_to_standby_list");
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	return (0);
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate int
remove_from_standby_list(db * database)21027c478bd9Sstevel@tonic-gate remove_from_standby_list(db* database)
21037c478bd9Sstevel@tonic-gate {
21047c478bd9Sstevel@tonic-gate 	int i;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	MUTEXLOCK(db_standby_list, "remove_from_standby_list");
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	if (database == 0) {
21097c478bd9Sstevel@tonic-gate 		MUTEXUNLOCK(db_standby_list, "remove_from_standby_list");
21107c478bd9Sstevel@tonic-gate 		return (1);
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	for (i = 0; i < db_standby_size; i++) {
21147c478bd9Sstevel@tonic-gate 		if ((database == db_standby_list[i])) {
21157c478bd9Sstevel@tonic-gate 			db_standby_list[i] = (db*)NULL;
21167c478bd9Sstevel@tonic-gate 			--db_standby_count;
21177c478bd9Sstevel@tonic-gate 			MUTEXUNLOCK(db_standby_list,
21187c478bd9Sstevel@tonic-gate 					"remove_from_standby_list");
21197c478bd9Sstevel@tonic-gate 			return (1);
21207c478bd9Sstevel@tonic-gate 		}
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	MUTEXUNLOCK(db_standby_list, "remove_from_standby_list");
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	return (0);
21267c478bd9Sstevel@tonic-gate }
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate /* Release space for copied dictionary */
21297c478bd9Sstevel@tonic-gate static void
db_release_dictionary(db_dict_desc_p d)21307c478bd9Sstevel@tonic-gate db_release_dictionary(db_dict_desc_p d) {
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	int	i;
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	if (d != NULL) {
21357c478bd9Sstevel@tonic-gate 		for (i = 0; i < d->tables.tables_len; i++) {
21367c478bd9Sstevel@tonic-gate 			db_table_desc_p	n, t = d->tables.tables_val[i];
21377c478bd9Sstevel@tonic-gate 			while (t != NULL) {
21387c478bd9Sstevel@tonic-gate 				n = t->next;
21397c478bd9Sstevel@tonic-gate 				delete_table_desc(t);
21407c478bd9Sstevel@tonic-gate 				t = n;
21417c478bd9Sstevel@tonic-gate 			}
21427c478bd9Sstevel@tonic-gate 		}
21437c478bd9Sstevel@tonic-gate 		delete d;
21447c478bd9Sstevel@tonic-gate 	}
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	return;
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate /*
21507c478bd9Sstevel@tonic-gate  * Make a copy of the dictionary
21517c478bd9Sstevel@tonic-gate  */
21527c478bd9Sstevel@tonic-gate db_dict_desc_p
db_copy_dictionary(void)21537c478bd9Sstevel@tonic-gate db_dictionary::db_copy_dictionary(void) {
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	db_dict_desc_p	tmp;
21567c478bd9Sstevel@tonic-gate 	int		i, ok = 1, count = 0;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	WRITELOCK(this, NULL, "db_dictionary::db_copy_dictionary w");
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	if (dictionary == NULL) {
21617c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL,
21627c478bd9Sstevel@tonic-gate 			"db_dictionary::db_copy_dictionary wu");
21637c478bd9Sstevel@tonic-gate 		return (NULL);
21647c478bd9Sstevel@tonic-gate 	}
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	tmp = new db_dict_desc;
21677c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {
21687c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL,
21697c478bd9Sstevel@tonic-gate 			"db_dictionary::db_copy_dictionary wu: no memory");
21707c478bd9Sstevel@tonic-gate 		return (NULL);
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	tmp->tables.tables_val = (db_table_desc_p *)calloc(
21747c478bd9Sstevel@tonic-gate 						tmp->tables.tables_len,
21757c478bd9Sstevel@tonic-gate 					sizeof (tmp->tables.tables_val[0]));
21767c478bd9Sstevel@tonic-gate 	if (tmp->tables.tables_val == NULL) {
21777c478bd9Sstevel@tonic-gate 		delete tmp;
21787c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, NULL,
21797c478bd9Sstevel@tonic-gate 			"db_dictionary::db_copy_dictionary wu: no memory");
21807c478bd9Sstevel@tonic-gate 		return (NULL);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	tmp->impl_vers = dictionary->impl_vers;
21847c478bd9Sstevel@tonic-gate 	tmp->tables.tables_len = 0;
21857c478bd9Sstevel@tonic-gate 	tmp->count = 0;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	/* For each table ... */
21887c478bd9Sstevel@tonic-gate 	for (i = 0; ok && i < dictionary->tables.tables_len; i++) {
21897c478bd9Sstevel@tonic-gate 		db_table_desc_p	tbl = NULL,
21907c478bd9Sstevel@tonic-gate 				t = dictionary->tables.tables_val[i];
21917c478bd9Sstevel@tonic-gate 		/* ... and each bucket in the chain ... */
21927c478bd9Sstevel@tonic-gate 		while (ok && t != NULL) {
21937c478bd9Sstevel@tonic-gate 			db_table_desc_p		n, savenext = t->next;
21947c478bd9Sstevel@tonic-gate 			t->next = NULL;
21957c478bd9Sstevel@tonic-gate 			if (db_clone_bucket(t, &n)) {
21967c478bd9Sstevel@tonic-gate 				if (tbl != NULL) {
21977c478bd9Sstevel@tonic-gate 					tbl->next = n;
21987c478bd9Sstevel@tonic-gate 				} else {
21997c478bd9Sstevel@tonic-gate 					tmp->tables.tables_val[i] = n;
22007c478bd9Sstevel@tonic-gate 				}
22017c478bd9Sstevel@tonic-gate 				tbl = n;
22027c478bd9Sstevel@tonic-gate 				tmp->count++;
22037c478bd9Sstevel@tonic-gate 			} else {
22047c478bd9Sstevel@tonic-gate 				ok = 0;
22057c478bd9Sstevel@tonic-gate 			}
22067c478bd9Sstevel@tonic-gate 			t->next = savenext;
22077c478bd9Sstevel@tonic-gate 		}
22087c478bd9Sstevel@tonic-gate 		tmp->tables.tables_len++;
22097c478bd9Sstevel@tonic-gate 	}
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	if (ok) {
22127c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
22137c478bd9Sstevel@tonic-gate 		if ((tmp->tables.tables_len !=
22147c478bd9Sstevel@tonic-gate 				dictionary->tables.tables_len) ||
22157c478bd9Sstevel@tonic-gate 			(tmp->count != dictionary->count))
22167c478bd9Sstevel@tonic-gate 			abort();
22177c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
22187c478bd9Sstevel@tonic-gate 	} else {
22197c478bd9Sstevel@tonic-gate 		db_release_dictionary(tmp);
22207c478bd9Sstevel@tonic-gate 		tmp = NULL;
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	return (tmp);
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate /*
22277c478bd9Sstevel@tonic-gate  * Set deferred commit mode. To do this, we make a copy of the table
22287c478bd9Sstevel@tonic-gate  * (structures and data), and put that on the deferred dictionary list.
22297c478bd9Sstevel@tonic-gate  * This list is used for lookups during a resync, so clients continue
22307c478bd9Sstevel@tonic-gate  * to see the pre-resync data. Meanwhile, any changes (including table
22317c478bd9Sstevel@tonic-gate  * deletes) are done to the (temporarily hidden to clients) table in
22327c478bd9Sstevel@tonic-gate  * the normal dictionary.
22337c478bd9Sstevel@tonic-gate  */
22347c478bd9Sstevel@tonic-gate db_status
defer(char * table)22357c478bd9Sstevel@tonic-gate db_dictionary::defer(char *table) {
22367c478bd9Sstevel@tonic-gate 	db_status	ret = DB_SUCCESS;
22377c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::defer");
22387c478bd9Sstevel@tonic-gate 	db_table_desc	*tbl = find_table_desc(table);
22397c478bd9Sstevel@tonic-gate 	int		res;
2240*8d0852b7SRichard Lowe 	const char	*myself = "db_dictionary::defer";
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if (tbl != NULL) {
22437c478bd9Sstevel@tonic-gate 		db_table_desc	*clone, *savenext = tbl->next;
22447c478bd9Sstevel@tonic-gate 		/*
22457c478bd9Sstevel@tonic-gate 		 * Only want to clone one db_table_desc, so temporarily
22467c478bd9Sstevel@tonic-gate 		 * unlink the tail.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		tbl->next = NULL;
22497c478bd9Sstevel@tonic-gate 		res = db_clone_bucket(tbl, &clone);
22507c478bd9Sstevel@tonic-gate 		/* Restore link to tail */
22517c478bd9Sstevel@tonic-gate 		tbl->next = savenext;
22527c478bd9Sstevel@tonic-gate 		if (res == 1) {
22537c478bd9Sstevel@tonic-gate 			db_status	stat;
22547c478bd9Sstevel@tonic-gate 			if (deferred.dictionary == NULL) {
22557c478bd9Sstevel@tonic-gate 				deferred.dictionary = new db_dict_desc;
22567c478bd9Sstevel@tonic-gate 				if (deferred.dictionary == NULL) {
22577c478bd9Sstevel@tonic-gate 					WRITEUNLOCK(this, DB_MEMORY_LIMIT,
22587c478bd9Sstevel@tonic-gate 						"wu db_dictionary::defer");
22597c478bd9Sstevel@tonic-gate 					return (DB_MEMORY_LIMIT);
22607c478bd9Sstevel@tonic-gate 				}
22617c478bd9Sstevel@tonic-gate 				deferred.dictionary->tables.tables_len = 0;
22627c478bd9Sstevel@tonic-gate 				deferred.dictionary->tables.tables_val = NULL;
22637c478bd9Sstevel@tonic-gate 				deferred.dictionary->count = 0;
22647c478bd9Sstevel@tonic-gate 				deferred.dictionary->impl_vers =
22657c478bd9Sstevel@tonic-gate 							DB_CURRENT_VERSION;
22667c478bd9Sstevel@tonic-gate 			}
22677c478bd9Sstevel@tonic-gate 			ret = DB_SUCCESS;
22687c478bd9Sstevel@tonic-gate 			/* Initialize and load the database for the clone */
22697c478bd9Sstevel@tonic-gate 			if (clone->database == 0) {
22707c478bd9Sstevel@tonic-gate 				clone->database = new db(table);
22717c478bd9Sstevel@tonic-gate 				if (clone->database != 0) {
22727c478bd9Sstevel@tonic-gate 					if (clone->database->load()) {
22737c478bd9Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK,
22747c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
22757c478bd9Sstevel@tonic-gate 							LOG_WARNING,
22767c478bd9Sstevel@tonic-gate #else
22777c478bd9Sstevel@tonic-gate 							LOG_INFO,
22787c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
22797c478bd9Sstevel@tonic-gate 					"%s: Clone DB for \"%s\" loaded",
22807c478bd9Sstevel@tonic-gate 							myself, NIL(table));
22817c478bd9Sstevel@tonic-gate 					} else {
22827c478bd9Sstevel@tonic-gate 						logmsg(MSG_NOTIMECHECK, LOG_ERR,
22837c478bd9Sstevel@tonic-gate 					"%s: Error loading clone DB for \"%s\"",
22847c478bd9Sstevel@tonic-gate 							myself, NIL(table));
22857c478bd9Sstevel@tonic-gate 						delete clone->database;
22867c478bd9Sstevel@tonic-gate 						clone->database = 0;
22877c478bd9Sstevel@tonic-gate 						ret = DB_INTERNAL_ERROR;
22887c478bd9Sstevel@tonic-gate 					}
22897c478bd9Sstevel@tonic-gate 				} else {
22907c478bd9Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_ERR,
22917c478bd9Sstevel@tonic-gate 					"%s: Unable to clone DB for \"%s\"",
22927c478bd9Sstevel@tonic-gate 						myself, NIL(table));
22937c478bd9Sstevel@tonic-gate 					ret = DB_MEMORY_LIMIT;
22947c478bd9Sstevel@tonic-gate 				}
22957c478bd9Sstevel@tonic-gate 			}
22967c478bd9Sstevel@tonic-gate 			if (clone->database != 0) {
22977c478bd9Sstevel@tonic-gate 				clone->database->markDeferred();
22987c478bd9Sstevel@tonic-gate 				stat = add_to_dictionary(deferred.dictionary,
22997c478bd9Sstevel@tonic-gate 							clone);
23007c478bd9Sstevel@tonic-gate 				ret = stat;
23017c478bd9Sstevel@tonic-gate 				if (stat != DB_SUCCESS) {
23027c478bd9Sstevel@tonic-gate 					delete clone->database;
23037c478bd9Sstevel@tonic-gate 					clone->database = 0;
23047c478bd9Sstevel@tonic-gate 					delete clone;
23057c478bd9Sstevel@tonic-gate 					if (stat == DB_NOTUNIQUE) {
23067c478bd9Sstevel@tonic-gate 						/* Already deferred */
23077c478bd9Sstevel@tonic-gate 						ret = DB_SUCCESS;
23087c478bd9Sstevel@tonic-gate 					}
23097c478bd9Sstevel@tonic-gate 				}
23107c478bd9Sstevel@tonic-gate 			} else {
23117c478bd9Sstevel@tonic-gate 				delete clone;
23127c478bd9Sstevel@tonic-gate 				/* Return value already set above */
23137c478bd9Sstevel@tonic-gate 			}
23147c478bd9Sstevel@tonic-gate 		} else {
23157c478bd9Sstevel@tonic-gate 			ret = DB_INTERNAL_ERROR;
23167c478bd9Sstevel@tonic-gate 		}
23177c478bd9Sstevel@tonic-gate 	} else {
23187c478bd9Sstevel@tonic-gate 		ret = DB_NOTFOUND;
23197c478bd9Sstevel@tonic-gate 	}
23207c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_dictionary::defer");
23217c478bd9Sstevel@tonic-gate 	return (ret);
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate /*
23257c478bd9Sstevel@tonic-gate  * Unset deferred commit mode and roll back changes; doesn't recover the
23267c478bd9Sstevel@tonic-gate  * disk data, but that's OK, since we only want to be able to continue
23277c478bd9Sstevel@tonic-gate  * serving the table until we can try a full dump again.
23287c478bd9Sstevel@tonic-gate  *
23297c478bd9Sstevel@tonic-gate  * The rollback is done by removing (and deleting) the updated table from
23307c478bd9Sstevel@tonic-gate  * the dictionary, and then moving the saved table from the deferred
23317c478bd9Sstevel@tonic-gate  * dictionary list to the actual one.
23327c478bd9Sstevel@tonic-gate  */
23337c478bd9Sstevel@tonic-gate db_status
rollback(char * table)23347c478bd9Sstevel@tonic-gate db_dictionary::rollback(char *table) {
23357c478bd9Sstevel@tonic-gate 	db_status	ret = DB_SUCCESS;
23367c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::rollback");
23377c478bd9Sstevel@tonic-gate 	db_table_desc	*old = search_dictionary(deferred.dictionary, table);
23387c478bd9Sstevel@tonic-gate 	db_table_desc	*upd = search_dictionary(dictionary, table);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	if (old == NULL) {
23417c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, DB_NOTFOUND, "wu db_dictionary::rollback");
23427c478bd9Sstevel@tonic-gate 		return (DB_NOTFOUND);
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	/*
23467c478bd9Sstevel@tonic-gate 	 * Remove old incarnation from deferred dictionary. We already hold
23477c478bd9Sstevel@tonic-gate 	 * a pointer ('old') to it, so don't delete.
23487c478bd9Sstevel@tonic-gate 	 */
23497c478bd9Sstevel@tonic-gate 	ret = remove_from_dictionary(deferred.dictionary, table, FALSE);
23507c478bd9Sstevel@tonic-gate 	if (ret != DB_SUCCESS) {
23517c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
23527c478bd9Sstevel@tonic-gate 		abort();
23537c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
23547c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, ret, "wu db_dictionary::rollback");
23557c478bd9Sstevel@tonic-gate 		return (ret);
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	if (old->database != 0)
23597c478bd9Sstevel@tonic-gate 		old->database->unmarkDeferred();
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	/*
23627c478bd9Sstevel@tonic-gate 	 * Remove updated incarnation from dictionary. If 'upd' is NULL,
23637c478bd9Sstevel@tonic-gate 	 * the table has been removed while we were in deferred mode, and
23647c478bd9Sstevel@tonic-gate 	 * that's OK; we just need to retain the old incarnation.
23657c478bd9Sstevel@tonic-gate 	 */
23667c478bd9Sstevel@tonic-gate 	if (upd != NULL) {
23677c478bd9Sstevel@tonic-gate 		ret = remove_from_dictionary(dictionary, table, FALSE);
23687c478bd9Sstevel@tonic-gate 		if (ret != DB_SUCCESS) {
23697c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
23707c478bd9Sstevel@tonic-gate 			abort();
23717c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
23727c478bd9Sstevel@tonic-gate 			/*
23737c478bd9Sstevel@tonic-gate 			 * Cut our losses; delete old incarnation, and leave
23747c478bd9Sstevel@tonic-gate 			 * updated one in place.
23757c478bd9Sstevel@tonic-gate 			 */
23767c478bd9Sstevel@tonic-gate 			delete_table_desc(old);
23777c478bd9Sstevel@tonic-gate 			WRITEUNLOCK(this, ret, "wu db_dictionary::rollback");
23787c478bd9Sstevel@tonic-gate 			return (ret);
23797c478bd9Sstevel@tonic-gate 		}
23807c478bd9Sstevel@tonic-gate 		/* Throw away updates */
23817c478bd9Sstevel@tonic-gate 		delete_table_desc(upd);
23827c478bd9Sstevel@tonic-gate 	}
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	/* (Re-)insert old incarnation in the dictionary */
23857c478bd9Sstevel@tonic-gate 	ret = add_to_dictionary(dictionary, old);
23867c478bd9Sstevel@tonic-gate 	if (ret != DB_SUCCESS) {
23877c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
23887c478bd9Sstevel@tonic-gate 		abort();
23897c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
23907c478bd9Sstevel@tonic-gate 		/* At least avoid memory leak */
23917c478bd9Sstevel@tonic-gate 		delete_table_desc(old);
23927c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
23937c478bd9Sstevel@tonic-gate 	"db_dictionary::rollback: rollback error %d for \"%s\"", ret, table);
23947c478bd9Sstevel@tonic-gate 	}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_dictionary::rollback");
23977c478bd9Sstevel@tonic-gate 	return (ret);
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate /*
24017c478bd9Sstevel@tonic-gate  * Commit changes. Done by simply removing and deleting the pre-resync
24027c478bd9Sstevel@tonic-gate  * data from the deferred dictionary.
24037c478bd9Sstevel@tonic-gate  */
24047c478bd9Sstevel@tonic-gate db_status
commit(char * table)24057c478bd9Sstevel@tonic-gate db_dictionary::commit(char *table) {
24067c478bd9Sstevel@tonic-gate 	db_status	ret = DB_SUCCESS;
24077c478bd9Sstevel@tonic-gate 	WRITELOCK(this, DB_LOCK_ERROR, "w db_dictionary::commit");
24087c478bd9Sstevel@tonic-gate 	db_table_desc	*old = search_dictionary(deferred.dictionary, table);
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if (old == NULL) {
24117c478bd9Sstevel@tonic-gate 		/* Fine (we hope); nothing to do */
24127c478bd9Sstevel@tonic-gate 		WRITEUNLOCK(this, ret, "wu db_dictionary::commit");
24137c478bd9Sstevel@tonic-gate 		return (DB_SUCCESS);
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	ret = remove_from_dictionary(deferred.dictionary, table, FALSE);
24177c478bd9Sstevel@tonic-gate 	if (ret == DB_SUCCESS)
24187c478bd9Sstevel@tonic-gate 		delete_table_desc(old);
24197c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
24207c478bd9Sstevel@tonic-gate 	else
24217c478bd9Sstevel@tonic-gate 		abort();
24227c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	WRITEUNLOCK(this, ret, "wu db_dictionary::commit");
24257c478bd9Sstevel@tonic-gate 	return (ret);
24267c478bd9Sstevel@tonic-gate }
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate /*
24297c478bd9Sstevel@tonic-gate  * The noWriteThrough flag is used to prevent modifies/updates to LDAP
24307c478bd9Sstevel@tonic-gate  * while we're incorporating log data into the in-memory tables.
24317c478bd9Sstevel@tonic-gate  */
24327c478bd9Sstevel@tonic-gate void
setNoWriteThrough(void)24337c478bd9Sstevel@tonic-gate db_dictionary::setNoWriteThrough(void) {
24347c478bd9Sstevel@tonic-gate 	ASSERTWHELD(this->dict);
24357c478bd9Sstevel@tonic-gate 	noWriteThrough.flag++;
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate void
clearNoWriteThrough(void)24397c478bd9Sstevel@tonic-gate db_dictionary::clearNoWriteThrough(void) {
24407c478bd9Sstevel@tonic-gate 	ASSERTWHELD(this->dict);
24417c478bd9Sstevel@tonic-gate 	if (noWriteThrough.flag > 0)
24427c478bd9Sstevel@tonic-gate 		noWriteThrough.flag--;
24437c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
24447c478bd9Sstevel@tonic-gate 	else
24457c478bd9Sstevel@tonic-gate 		abort();
24467c478bd9Sstevel@tonic-gate #endif	/* NISDB_LDAP_DEBUG */
24477c478bd9Sstevel@tonic-gate }
2448