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