xref: /titanic_51/usr/src/cmd/fs.d/nfs/nfslog/dbtab.c (revision 3582b7c1ba378300f8cf7361514e9287401900fe)
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
5*3582b7c1Sgt29601  * Common Development and Distribution License (the "License").
6*3582b7c1Sgt29601  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3582b7c1Sgt29601  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Code to maintain the runtime and on-disk filehandle mapping table for
307c478bd9Sstevel@tonic-gate  * nfslog.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <assert.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <ctype.h>
367c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <stddef.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <strings.h>
417c478bd9Sstevel@tonic-gate #include <syslog.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <dirent.h>
447c478bd9Sstevel@tonic-gate #include <ndbm.h>
457c478bd9Sstevel@tonic-gate #include <time.h>
467c478bd9Sstevel@tonic-gate #include <libintl.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
497c478bd9Sstevel@tonic-gate #include <nfs/nfs_log.h>
507c478bd9Sstevel@tonic-gate #include "fhtab.h"
517c478bd9Sstevel@tonic-gate #include "nfslogd.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	ROUNDUP32(val)		(((val) + 3) & ~3)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * It is important that this string not match the length of the
577c478bd9Sstevel@tonic-gate  * file handle key length NFS_FHMAXDATA
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate #define	DB_VERSION_STRING	"NFSLOG_DB_VERSION"
607c478bd9Sstevel@tonic-gate #define	DB_VERSION		"1"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	MAX_PRUNE_REC_CNT	100000
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate fhandle_t	public_fh = { 0 };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct db_list {
677c478bd9Sstevel@tonic-gate 	fsid_t		fsid;		/* filesystem fsid */
687c478bd9Sstevel@tonic-gate 	char		*path;		/* dbm filepair path */
697c478bd9Sstevel@tonic-gate 	DBM		*db;		/* open dbm database */
707c478bd9Sstevel@tonic-gate 	bool_t		getall;		/* TRUE if all dbm for prefix open */
717c478bd9Sstevel@tonic-gate 	struct db_list	*next;		/* next db */
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct db_list *db_fs_list = NULL;
75*3582b7c1Sgt29601 static	char	err_str[] = "DB I/O error has occurred";
767c478bd9Sstevel@tonic-gate struct link_keys {
777c478bd9Sstevel@tonic-gate 	fh_secondary_key	lnkey;
787c478bd9Sstevel@tonic-gate 	int			lnsize;
797c478bd9Sstevel@tonic-gate 	struct link_keys	*next;
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate extern int debug;
827c478bd9Sstevel@tonic-gate extern time_t mapping_update_interval;
837c478bd9Sstevel@tonic-gate extern time_t prune_timeout;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static int fill_link_key(char *linkkey, fhandle_t *dfh, char *name);
867c478bd9Sstevel@tonic-gate static struct db_list *db_get_db(char *fhpath, fsid_t *fsid, int *errorp,
877c478bd9Sstevel@tonic-gate 	int create_flag);
887c478bd9Sstevel@tonic-gate static struct db_list *db_get_all_databases(char *fhpath, bool_t getall);
897c478bd9Sstevel@tonic-gate static void debug_print_fhlist(FILE *fp, fhlist_ent *fhrecp);
907c478bd9Sstevel@tonic-gate static void debug_print_linkinfo(FILE *fp, linkinfo_ent *fhrecp);
917c478bd9Sstevel@tonic-gate static void debug_print_key(FILE *fp, char *str1, char *str2, char *key,
927c478bd9Sstevel@tonic-gate 	int ksize);
937c478bd9Sstevel@tonic-gate static void debug_print_key_and_data(FILE *fp, char *str1, char *str2,
947c478bd9Sstevel@tonic-gate 	char *key, int ksize, char *data, int dsize);
957c478bd9Sstevel@tonic-gate static int store_record(struct db_list *dbp, void *keyaddr, int keysize,
967c478bd9Sstevel@tonic-gate 	void *dataaddr, int datasize, char *str);
977c478bd9Sstevel@tonic-gate static void *fetch_record(struct db_list *dbp, void *keyaddr, int keysize,
987c478bd9Sstevel@tonic-gate 	void *dataaddr, int *errorp, char *str);
997c478bd9Sstevel@tonic-gate static int delete_record(struct db_list *dbp, void *keyaddr, int keysize,
1007c478bd9Sstevel@tonic-gate 	char *str);
1017c478bd9Sstevel@tonic-gate static int db_update_fhrec(struct db_list *dbp, void *keyaddr, int keysize,
1027c478bd9Sstevel@tonic-gate 	fhlist_ent *fhrecp, char *str);
1037c478bd9Sstevel@tonic-gate static int db_update_linkinfo(struct db_list *dbp, void *keyaddr, int keysize,
1047c478bd9Sstevel@tonic-gate 	linkinfo_ent *linkp, char *str);
1057c478bd9Sstevel@tonic-gate static fhlist_ent *create_primary_struct(struct db_list *dbp, fhandle_t *dfh,
1067c478bd9Sstevel@tonic-gate 	char *name, fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp,
1077c478bd9Sstevel@tonic-gate 	int *errorp);
1087c478bd9Sstevel@tonic-gate static fhlist_ent *db_add_primary(struct db_list *dbp, fhandle_t *dfh,
1097c478bd9Sstevel@tonic-gate 	char *name, fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp,
1107c478bd9Sstevel@tonic-gate 	int *errorp);
1117c478bd9Sstevel@tonic-gate static linkinfo_ent *get_next_link(struct db_list *dbp, char *linkkey,
1127c478bd9Sstevel@tonic-gate 	int *linksizep, linkinfo_ent *linkp, void **cookiep,
1137c478bd9Sstevel@tonic-gate 	int *errorp, char *msg);
1147c478bd9Sstevel@tonic-gate static void free_link_cookies(void *cookie);
1157c478bd9Sstevel@tonic-gate static void add_mc_path(struct db_list *dbp, fhandle_t *dfh, char *name,
1167c478bd9Sstevel@tonic-gate 	fhlist_ent *fhrecp, linkinfo_ent *linkp, int *errorp);
1177c478bd9Sstevel@tonic-gate static linkinfo_ent *create_link_struct(struct db_list *dbp, fhandle_t *dfh,
1187c478bd9Sstevel@tonic-gate 	char *name, fhlist_ent *fhrecp, int *errorp);
1197c478bd9Sstevel@tonic-gate static int db_add_secondary(struct db_list *dbp, fhandle_t *dfh, char *name,
1207c478bd9Sstevel@tonic-gate 	fhandle_t *fh, fhlist_ent *fhrecp);
1217c478bd9Sstevel@tonic-gate static linkinfo_ent *update_next_link(struct db_list *dbp, char *nextkey,
1227c478bd9Sstevel@tonic-gate 	int nextsize, char *prevkey, int prevsize, int *errorp);
1237c478bd9Sstevel@tonic-gate static int update_prev_link(struct db_list *dbp, char *nextkey, int nextsize,
1247c478bd9Sstevel@tonic-gate 	char *prevkey, int prevsize);
1257c478bd9Sstevel@tonic-gate static linkinfo_ent *update_linked_list(struct db_list *dbp, char *nextkey,
1267c478bd9Sstevel@tonic-gate 	int nextsize, char *prevkey, int prevsize, int *errorp);
1277c478bd9Sstevel@tonic-gate static int db_update_primary_new_head(struct db_list *dbp,
1287c478bd9Sstevel@tonic-gate 	linkinfo_ent *dellinkp, linkinfo_ent *nextlinkp, fhlist_ent *fhrecp);
1297c478bd9Sstevel@tonic-gate static int delete_link_by_key(struct db_list *dbp, char *linkkey,
1307c478bd9Sstevel@tonic-gate 	int *linksizep, int *errorp, char *errstr);
1317c478bd9Sstevel@tonic-gate static int delete_link(struct db_list *dbp, fhandle_t *dfh, char *name,
1327c478bd9Sstevel@tonic-gate 	char *nextlinkkey, int *nextlinksizep, int *errorp, char *errstr);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * The following functions do the actual database I/O. Currently use DBM.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * The "db_*" functions are functions that access the database using
1407c478bd9Sstevel@tonic-gate  * database-specific calls. Currently the only database supported is
1417c478bd9Sstevel@tonic-gate  * dbm. Because of the limitations of this database, in particular when
1427c478bd9Sstevel@tonic-gate  * it comes to manipulating records with the same key, or using multiple keys,
1437c478bd9Sstevel@tonic-gate  * the following design decisions have been made:
1447c478bd9Sstevel@tonic-gate  *
1457c478bd9Sstevel@tonic-gate  *	Each file system has a separate dbm file, which are kept open as
1467c478bd9Sstevel@tonic-gate  *		accessed, listed in a linked list.
1477c478bd9Sstevel@tonic-gate  *	Two possible access mode are available for each file - either by
1487c478bd9Sstevel@tonic-gate  *		file handle, or by directory file handle and name. Since
1497c478bd9Sstevel@tonic-gate  *		dbm does not allow multiple keys, we will have a primary
1507c478bd9Sstevel@tonic-gate  *		and secondary key for each file/link.
1517c478bd9Sstevel@tonic-gate  *	The primary key is the pair (inode,gen) which can be obtained
1527c478bd9Sstevel@tonic-gate  *		from the file handle. This points to a record with
1537c478bd9Sstevel@tonic-gate  *		the full file handle and the secondary key (dfh-key,name)
1547c478bd9Sstevel@tonic-gate  *		for one of the links.
1557c478bd9Sstevel@tonic-gate  *	The secondary key is the pair (dfh-key,name) where dfh-key is
1567c478bd9Sstevel@tonic-gate  *		the primary key for the directory and the name is the
1577c478bd9Sstevel@tonic-gate  *		link name. It points to a record that contains the primary
1587c478bd9Sstevel@tonic-gate  *		key for the file and to the previous and next hard link
1597c478bd9Sstevel@tonic-gate  *		found for this file (if they exist).
1607c478bd9Sstevel@tonic-gate  *
1617c478bd9Sstevel@tonic-gate  * Summary of operations:
1627c478bd9Sstevel@tonic-gate  *	Adding a new file: Create the primary record and secondary (link)
1637c478bd9Sstevel@tonic-gate  *		record and add both to the database. The link record
1647c478bd9Sstevel@tonic-gate  *		would have prev and next links set to NULL.
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  *	Adding a link to a file in the database: Add the link record,
1677c478bd9Sstevel@tonic-gate  *		to the head of the links list (i.e. prev = NULL, next =
1687c478bd9Sstevel@tonic-gate  *		secondary key recorded in the primary record). Update
1697c478bd9Sstevel@tonic-gate  *		the primary record to point to the new link, and the
1707c478bd9Sstevel@tonic-gate  *		secondary record for the old head of list to point to new.
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  *	Deleting a file: Delete the link record. If it is the last link
1737c478bd9Sstevel@tonic-gate  *		then mark the primary record as deleted but don't delete
1747c478bd9Sstevel@tonic-gate  *		that one from the database (in case some clients still
1757c478bd9Sstevel@tonic-gate  *		hold the file handle). If there are other links, and the
1767c478bd9Sstevel@tonic-gate  *		deleted link is the head of the list (in the primary
1777c478bd9Sstevel@tonic-gate  *		record), update the primary record with the new head.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *	Renaming a file: Add the new link and then delete the old one.
1807c478bd9Sstevel@tonic-gate  *
1817c478bd9Sstevel@tonic-gate  *	Lookup by file handle (read, write, lookup, etc.) - fetch primary rec.
1827c478bd9Sstevel@tonic-gate  *	Lookup by dir info (delete, link, rename) - fetch secondary rec.
1837c478bd9Sstevel@tonic-gate  *
1847c478bd9Sstevel@tonic-gate  *	XXX NOTE: The code is written single-threaded. To make it multi-
1857c478bd9Sstevel@tonic-gate  *	threaded, the following considerations must be made:
1867c478bd9Sstevel@tonic-gate  *	1. Changes/access to the db list must be atomic.
1877c478bd9Sstevel@tonic-gate  *	2. Changes/access for a specific file handle must be atomic
1887c478bd9Sstevel@tonic-gate  *	   (example: deleting a link may affect up to 4 separate database
1897c478bd9Sstevel@tonic-gate  *	   entries: the deleted link, the prev and next links if exist,
1907c478bd9Sstevel@tonic-gate  *	   and the filehandle entry, if it points to the deleted link -
1917c478bd9Sstevel@tonic-gate  *	   these changes must be atomic).
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Create a link key given directory fh and name
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate static int
1987c478bd9Sstevel@tonic-gate fill_link_key(char *linkkey, fhandle_t *dfh, char *name)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	int	linksize, linksize32;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	(void) memcpy(linkkey, &dfh->fh_data, dfh->fh_len);
2037c478bd9Sstevel@tonic-gate 	(void) strcpy(&linkkey[dfh->fh_len], name);
2047c478bd9Sstevel@tonic-gate 	linksize = dfh->fh_len + strlen(name) + 1;
2057c478bd9Sstevel@tonic-gate 	linksize32 = ROUNDUP32(linksize);
2067c478bd9Sstevel@tonic-gate 	if (linksize32 > linksize)
2077c478bd9Sstevel@tonic-gate 		bzero(&linkkey[linksize], linksize32 - linksize);
2087c478bd9Sstevel@tonic-gate 	return (linksize32);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * db_get_db - gets the database for the filesystem, or creates one
2137c478bd9Sstevel@tonic-gate  * if none exists. Return the pointer for the database in *dbpp if success.
2147c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static struct db_list *
2177c478bd9Sstevel@tonic-gate db_get_db(char *fhpath, fsid_t *fsid, int *errorp, int create_flag)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	struct db_list	*p, *newp;
2207c478bd9Sstevel@tonic-gate 	char		fsidstr[30];
2217c478bd9Sstevel@tonic-gate 	datum		key, data;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	*errorp = 0;
2247c478bd9Sstevel@tonic-gate 	for (p = db_fs_list;
2257c478bd9Sstevel@tonic-gate 		(p != NULL) && memcmp(&p->fsid, fsid, sizeof (*fsid));
2267c478bd9Sstevel@tonic-gate 		p = p->next);
2277c478bd9Sstevel@tonic-gate 	if (p != NULL) {
2287c478bd9Sstevel@tonic-gate 		/* Found it */
2297c478bd9Sstevel@tonic-gate 		return (p);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	/* Create it */
2327c478bd9Sstevel@tonic-gate 	if ((newp = calloc(1, sizeof (*newp))) == NULL) {
2337c478bd9Sstevel@tonic-gate 		*errorp = errno;
2347c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
2357c478bd9Sstevel@tonic-gate 			"db_get_db: malloc db failed: Error %s"),
2367c478bd9Sstevel@tonic-gate 			strerror(*errorp));
2377c478bd9Sstevel@tonic-gate 		return (NULL);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 	(void) sprintf(fsidstr, "%08x%08x", fsid->val[0], fsid->val[1]);
2407c478bd9Sstevel@tonic-gate 	if ((newp->path = malloc(strlen(fhpath) + 2 + strlen(fsidstr)))
2417c478bd9Sstevel@tonic-gate 		== NULL) {
2427c478bd9Sstevel@tonic-gate 		*errorp = errno;
2437c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
2447c478bd9Sstevel@tonic-gate 			"db_get_db: malloc dbpath failed: Error %s"),
2457c478bd9Sstevel@tonic-gate 			strerror(*errorp));
2467c478bd9Sstevel@tonic-gate 		goto err_exit;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	(void) sprintf(newp->path, "%s.%s", fhpath, fsidstr);
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * The open mode is masked by UMASK.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if ((newp->db = dbm_open(newp->path, create_flag | O_RDWR, 0666))
2537c478bd9Sstevel@tonic-gate 		== NULL) {
2547c478bd9Sstevel@tonic-gate 		*errorp = errno;
2557c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
2567c478bd9Sstevel@tonic-gate 			"db_get_db: dbm_open db '%s' failed: Error %s"),
2577c478bd9Sstevel@tonic-gate 			newp->path, strerror(*errorp));
2587c478bd9Sstevel@tonic-gate 		if (*errorp == 0)	/* should not happen but may */
2597c478bd9Sstevel@tonic-gate 			*errorp = -1;
2607c478bd9Sstevel@tonic-gate 		goto err_exit;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * Add the version identifier (have to check first in the
2647c478bd9Sstevel@tonic-gate 	 * case the db exists)
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	key.dptr = DB_VERSION_STRING;
2677c478bd9Sstevel@tonic-gate 	key.dsize = strlen(DB_VERSION_STRING);
2687c478bd9Sstevel@tonic-gate 	data = dbm_fetch(newp->db, key);
2697c478bd9Sstevel@tonic-gate 	if (data.dptr == NULL) {
2707c478bd9Sstevel@tonic-gate 		data.dptr = DB_VERSION;
2717c478bd9Sstevel@tonic-gate 		data.dsize = strlen(DB_VERSION);
2727c478bd9Sstevel@tonic-gate 		(void) dbm_store(newp->db, key, data, DBM_INSERT);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	(void) memcpy(&newp->fsid, fsid, sizeof (*fsid));
2767c478bd9Sstevel@tonic-gate 	newp->next = db_fs_list;
2777c478bd9Sstevel@tonic-gate 	db_fs_list = newp;
2787c478bd9Sstevel@tonic-gate 	if (debug > 1) {
2797c478bd9Sstevel@tonic-gate 		(void) printf("db_get_db: db %s opened\n", newp->path);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	return (newp);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate err_exit:
2847c478bd9Sstevel@tonic-gate 	if (newp != NULL) {
2857c478bd9Sstevel@tonic-gate 		if (newp->db != NULL) {
2867c478bd9Sstevel@tonic-gate 			dbm_close(newp->db);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		if (newp->path != NULL) {
2897c478bd9Sstevel@tonic-gate 			free(newp->path);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		free(newp);
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 	return (NULL);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * db_get_all_databases - gets the database for any filesystem. This is used
2987c478bd9Sstevel@tonic-gate  * when any database will do - typically to retrieve the path for the
2997c478bd9Sstevel@tonic-gate  * public filesystem. If any database is open - return the first one,
3007c478bd9Sstevel@tonic-gate  * otherwise, search for it using fhpath. If getall is TRUE, open all
3017c478bd9Sstevel@tonic-gate  * matching databases, and mark them (to indicate that all such were opened).
3027c478bd9Sstevel@tonic-gate  * Return the pointer for a matching database if success.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static struct db_list *
3057c478bd9Sstevel@tonic-gate db_get_all_databases(char *fhpath, bool_t getall)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	char		*dirptr, *fhdir, *fhpathname;
3087c478bd9Sstevel@tonic-gate 	int		len, error;
3097c478bd9Sstevel@tonic-gate 	DIR		*dirp;
3107c478bd9Sstevel@tonic-gate 	struct dirent	*dp;
3117c478bd9Sstevel@tonic-gate 	fsid_t		fsid;
3127c478bd9Sstevel@tonic-gate 	struct db_list	*dbp, *ret_dbp;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	for (dbp = db_fs_list; dbp != NULL; dbp = dbp->next) {
3157c478bd9Sstevel@tonic-gate 		if (strncmp(fhpath, dbp->path, strlen(fhpath)) == 0)
3167c478bd9Sstevel@tonic-gate 			break;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 	if (dbp != NULL) {
3197c478bd9Sstevel@tonic-gate 		/*
3207c478bd9Sstevel@tonic-gate 		 * if one database for that prefix is open, and  either only
3217c478bd9Sstevel@tonic-gate 		 * one is needed, or already opened all such databases,
3227c478bd9Sstevel@tonic-gate 		 * return here without exhaustive search
3237c478bd9Sstevel@tonic-gate 		 */
3247c478bd9Sstevel@tonic-gate 		if (!getall || dbp->getall)
3257c478bd9Sstevel@tonic-gate 			return (dbp);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	if ((fhdir = strdup(fhpath)) == NULL) {
3287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
3297c478bd9Sstevel@tonic-gate 			"db_get_all_databases: strdup '%s' Error '%s*'"),
3307c478bd9Sstevel@tonic-gate 			fhpath, strerror(errno));
3317c478bd9Sstevel@tonic-gate 		return (NULL);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	fhpathname = NULL;
3347c478bd9Sstevel@tonic-gate 	ret_dbp = NULL;
3357c478bd9Sstevel@tonic-gate 	if ((dirptr = strrchr(fhdir, '/')) == NULL) {
3367c478bd9Sstevel@tonic-gate 		/* no directory */
3377c478bd9Sstevel@tonic-gate 		goto exit;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if ((fhpathname = strdup(&dirptr[1])) == NULL) {
3407c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
3417c478bd9Sstevel@tonic-gate 			"db_get_all_databases: strdup '%s' Error '%s*'"),
3427c478bd9Sstevel@tonic-gate 			&dirptr[1], strerror(errno));
3437c478bd9Sstevel@tonic-gate 		goto exit;
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	/* Terminate fhdir string at last '/' */
3467c478bd9Sstevel@tonic-gate 	dirptr[1] = '\0';
3477c478bd9Sstevel@tonic-gate 	/* Search the directory */
3487c478bd9Sstevel@tonic-gate 	if (debug > 2) {
3497c478bd9Sstevel@tonic-gate 		(void) printf("db_get_all_databases: search '%s' for '%s*'\n",
3507c478bd9Sstevel@tonic-gate 			fhdir, fhpathname);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(fhdir)) == NULL) {
3537c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
3547c478bd9Sstevel@tonic-gate 			"db_get_all_databases: opendir '%s' Error '%s*'"),
3557c478bd9Sstevel@tonic-gate 			fhdir, strerror(errno));
3567c478bd9Sstevel@tonic-gate 		goto exit;
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	len = strlen(fhpathname);
3597c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
3607c478bd9Sstevel@tonic-gate 		if (strncmp(fhpathname, dp->d_name, len) == 0) {
3617c478bd9Sstevel@tonic-gate 			dirptr = &dp->d_name[len + 1];
3627c478bd9Sstevel@tonic-gate 			if (*(dirptr - 1) != '.') {
3637c478bd9Sstevel@tonic-gate 				continue;
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			(void) sscanf(dirptr, "%08lx%08lx",
3667c478bd9Sstevel@tonic-gate 			    (ulong_t *)&fsid.val[0], (ulong_t *)&fsid.val[1]);
3677c478bd9Sstevel@tonic-gate 			dbp = db_get_db(fhpath, &fsid, &error, 0);
3687c478bd9Sstevel@tonic-gate 			if (dbp != NULL) {
3697c478bd9Sstevel@tonic-gate 				ret_dbp = dbp;
3707c478bd9Sstevel@tonic-gate 				if (!getall)
3717c478bd9Sstevel@tonic-gate 					break;
3727c478bd9Sstevel@tonic-gate 				dbp->getall = TRUE;
3737c478bd9Sstevel@tonic-gate 			}
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
3777c478bd9Sstevel@tonic-gate exit:
3787c478bd9Sstevel@tonic-gate 	if (fhpathname != NULL)
3797c478bd9Sstevel@tonic-gate 		free(fhpathname);
3807c478bd9Sstevel@tonic-gate 	if (fhdir != NULL)
3817c478bd9Sstevel@tonic-gate 		free(fhdir);
3827c478bd9Sstevel@tonic-gate 	return (ret_dbp);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static void
3867c478bd9Sstevel@tonic-gate debug_print_key(FILE *fp, char *str1, char *str2, char *key, int ksize)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s: %s key (%d) ", str1, str2, ksize);
3897c478bd9Sstevel@tonic-gate 	debug_opaque_print(fp, key, ksize);
3907c478bd9Sstevel@tonic-gate 	/* may be inode,name - try to print the fields */
3917c478bd9Sstevel@tonic-gate 	if (ksize >= NFS_FHMAXDATA) {
3927c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, ": inode ");
3937c478bd9Sstevel@tonic-gate 		debug_opaque_print(fp, &key[2], sizeof (int));
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, ", gen ");
3957c478bd9Sstevel@tonic-gate 		debug_opaque_print(fp, &key[2 + sizeof (int)], sizeof (int));
3967c478bd9Sstevel@tonic-gate 		if (ksize > NFS_FHMAXDATA) {
3977c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, ", name '%s'", &key[NFS_FHMAXDATA]);
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\n");
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static void
4047c478bd9Sstevel@tonic-gate debug_print_linkinfo(FILE *fp, linkinfo_ent *linkp)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	if (linkp == NULL)
4077c478bd9Sstevel@tonic-gate 		return;
4087c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "linkinfo:\ndfh: ");
4097c478bd9Sstevel@tonic-gate 	debug_opaque_print(fp, (void *)&linkp->dfh, sizeof (linkp->dfh));
4107c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\nname: '%s'", LN_NAME(linkp));
4117c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\nmtime 0x%x, atime 0x%x, flags 0x%x, reclen %d\n",
4127c478bd9Sstevel@tonic-gate 		linkp->mtime, linkp->atime, linkp->flags, linkp->reclen);
4137c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "offsets: fhkey %d, name %d, next %d, prev %d\n",
4147c478bd9Sstevel@tonic-gate 		linkp->fhkey_offset, linkp->name_offset, linkp->next_offset,
4157c478bd9Sstevel@tonic-gate 		linkp->prev_offset);
4167c478bd9Sstevel@tonic-gate 	debug_print_key(fp, "fhkey", "", LN_FHKEY(linkp), LN_FHKEY_LEN(linkp));
4177c478bd9Sstevel@tonic-gate 	debug_print_key(fp, "next", "", LN_NEXT(linkp), LN_NEXT_LEN(linkp));
4187c478bd9Sstevel@tonic-gate 	debug_print_key(fp, "prev", "", LN_PREV(linkp), LN_PREV_LEN(linkp));
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static void
4227c478bd9Sstevel@tonic-gate debug_print_fhlist(FILE *fp, fhlist_ent *fhrecp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	if (fhrecp == NULL)
4257c478bd9Sstevel@tonic-gate 		return;
4267c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "fhrec:\nfh: ");
4277c478bd9Sstevel@tonic-gate 	debug_opaque_print(fp, (void *)&fhrecp->fh, sizeof (fhrecp->fh));
4287c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "name '%s', dfh: ", fhrecp->name);
4297c478bd9Sstevel@tonic-gate 	debug_opaque_print(fp, (void *)&fhrecp->dfh, sizeof (fhrecp->dfh));
4307c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\nmtime 0x%x, atime 0x%x, flags 0x%x, reclen %d\n",
4317c478bd9Sstevel@tonic-gate 		fhrecp->mtime, fhrecp->atime, fhrecp->flags, fhrecp->reclen);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static void
4357c478bd9Sstevel@tonic-gate debug_print_key_and_data(FILE *fp, char *str1, char *str2, char *key,
4367c478bd9Sstevel@tonic-gate 	int ksize, char *data, int dsize)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	debug_print_key(fp, str1, str2, key, ksize);
4397c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, " ==> (%p,%d)\n", (void *)data, dsize);
4407c478bd9Sstevel@tonic-gate 	if (ksize > NFS_FHMAXDATA) {
4417c478bd9Sstevel@tonic-gate 		linkinfo_ent inf;
4427c478bd9Sstevel@tonic-gate 		/* probably a link struct */
4437c478bd9Sstevel@tonic-gate 		(void) memcpy(&inf, data, sizeof (linkinfo_ent));
4447c478bd9Sstevel@tonic-gate 		debug_print_linkinfo(fp, &inf);
4457c478bd9Sstevel@tonic-gate 	} else if (ksize == NFS_FHMAXDATA) {
4467c478bd9Sstevel@tonic-gate 		fhlist_ent inf;
4477c478bd9Sstevel@tonic-gate 		/* probably an fhlist struct */
4487c478bd9Sstevel@tonic-gate 		(void) memcpy(&inf, data, sizeof (linkinfo_ent));
4497c478bd9Sstevel@tonic-gate 		debug_print_fhlist(fp, &inf);
4507c478bd9Sstevel@tonic-gate 	} else {
4517c478bd9Sstevel@tonic-gate 		/* don't know... */
4527c478bd9Sstevel@tonic-gate 		debug_opaque_print(fp, data, dsize);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*
4577c478bd9Sstevel@tonic-gate  * store_record - store the record in the database and return 0 for success
4587c478bd9Sstevel@tonic-gate  * or error code otherwise.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate static int
4617c478bd9Sstevel@tonic-gate store_record(struct db_list *dbp, void *keyaddr, int keysize, void *dataaddr,
4627c478bd9Sstevel@tonic-gate 	int datasize, char *str)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	datum	key, data;
4657c478bd9Sstevel@tonic-gate 	int	error;
4667c478bd9Sstevel@tonic-gate 	char	*errfmt = "store_record: dbm_store failed, Error: %s\n";
4677c478bd9Sstevel@tonic-gate 	char	*err;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	errno = 0;
4707c478bd9Sstevel@tonic-gate 	key.dptr = keyaddr;
4717c478bd9Sstevel@tonic-gate 	key.dsize = keysize;
4727c478bd9Sstevel@tonic-gate 	data.dptr = dataaddr;
4737c478bd9Sstevel@tonic-gate 	data.dsize = datasize;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (debug > 2) {
4767c478bd9Sstevel@tonic-gate 		debug_print_key_and_data(stdout, str, "dbm_store:\n    ",
4777c478bd9Sstevel@tonic-gate 			key.dptr, key.dsize, data.dptr, data.dsize);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	if (dbm_store(dbp->db, key, data, DBM_REPLACE) < 0) {
4807c478bd9Sstevel@tonic-gate 		/* Could not store */
4817c478bd9Sstevel@tonic-gate 		error = dbm_error(dbp->db);
4827c478bd9Sstevel@tonic-gate 		dbm_clearerr(dbp->db);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		if (error) {
4857c478bd9Sstevel@tonic-gate 			if (errno)
4867c478bd9Sstevel@tonic-gate 				err = strerror(errno);
4877c478bd9Sstevel@tonic-gate 			else {
4887c478bd9Sstevel@tonic-gate 				err = err_str;
4897c478bd9Sstevel@tonic-gate 				errno = EIO;
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 		} else { /* should not happen but sometimes does */
4927c478bd9Sstevel@tonic-gate 			err = err_str;
4937c478bd9Sstevel@tonic-gate 			errno = -1;
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 		if (debug) {
4967c478bd9Sstevel@tonic-gate 			debug_print_key(stderr, str, "store_record:"
4977c478bd9Sstevel@tonic-gate 				"dbm_store:\n", key.dptr, key.dsize);
4987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, errfmt, err);
4997c478bd9Sstevel@tonic-gate 		} else
5007c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(errfmt), err);
5017c478bd9Sstevel@tonic-gate 		return (errno);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	return (0);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * fetch_record - fetch the record from the database and return 0 for success
5087c478bd9Sstevel@tonic-gate  * and errno for failure.
5097c478bd9Sstevel@tonic-gate  * dataaddr is an optional valid address for the result. If dataaddr
5107c478bd9Sstevel@tonic-gate  * is non-null, then that memory is already alloc'd. Else, alloc it, and
5117c478bd9Sstevel@tonic-gate  * the caller must free the returned struct when done.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate static void *
5147c478bd9Sstevel@tonic-gate fetch_record(struct db_list *dbp, void *keyaddr, int keysize, void *dataaddr,
5157c478bd9Sstevel@tonic-gate 	int *errorp, char *str)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	datum	key, data;
5187c478bd9Sstevel@tonic-gate 	char	*errfmt = "fetch_record: dbm_fetch failed, Error: %s\n";
5197c478bd9Sstevel@tonic-gate 	char	*err;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	errno = 0;
5227c478bd9Sstevel@tonic-gate 	*errorp = 0;
5237c478bd9Sstevel@tonic-gate 	key.dptr = keyaddr;
5247c478bd9Sstevel@tonic-gate 	key.dsize = keysize;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	data = dbm_fetch(dbp->db, key);
5277c478bd9Sstevel@tonic-gate 	if (data.dptr == NULL) {
528*3582b7c1Sgt29601 		/* see if there is a database error */
529*3582b7c1Sgt29601 		if (dbm_error(dbp->db)) {
530*3582b7c1Sgt29601 			/* clear and report the database error */
5317c478bd9Sstevel@tonic-gate 			dbm_clearerr(dbp->db);
532*3582b7c1Sgt29601 			*errorp = EIO;
533*3582b7c1Sgt29601 			err = strerror(*errorp);
534*3582b7c1Sgt29601 			syslog(LOG_ERR, gettext(errfmt), err);
535*3582b7c1Sgt29601 		} else {
536*3582b7c1Sgt29601 			/* primary record not in database */
537*3582b7c1Sgt29601 			*errorp = ENOENT;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 		if (debug > 3) {
540*3582b7c1Sgt29601 			err = strerror(*errorp);
5417c478bd9Sstevel@tonic-gate 			debug_print_key(stderr, str, "fetch_record:"
5427c478bd9Sstevel@tonic-gate 				"dbm_fetch:\n", key.dptr, key.dsize);
5437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, errfmt, err);
544*3582b7c1Sgt29601 		}
5457c478bd9Sstevel@tonic-gate 		return (NULL);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* copy to local struct because dbm may return non-aligned pointers */
5497c478bd9Sstevel@tonic-gate 	if ((dataaddr == NULL) &&
5507c478bd9Sstevel@tonic-gate 	    ((dataaddr = malloc(data.dsize)) == NULL)) {
5517c478bd9Sstevel@tonic-gate 		*errorp = errno;
5527c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
5537c478bd9Sstevel@tonic-gate 			"%s: dbm_fetch - malloc %ld: Error %s"),
5547c478bd9Sstevel@tonic-gate 			str, data.dsize, strerror(*errorp));
5557c478bd9Sstevel@tonic-gate 		return (NULL);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	(void) memcpy(dataaddr, data.dptr, data.dsize);
5587c478bd9Sstevel@tonic-gate 	if (debug > 3) {
5597c478bd9Sstevel@tonic-gate 		debug_print_key_and_data(stdout, str, "fetch_record:"
5607c478bd9Sstevel@tonic-gate 			"dbm_fetch:\n", key.dptr, key.dsize,
5617c478bd9Sstevel@tonic-gate 			dataaddr, data.dsize);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 	*errorp = 0;
5647c478bd9Sstevel@tonic-gate 	return (dataaddr);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * delete_record - delete the record from the database and return 0 for success
5697c478bd9Sstevel@tonic-gate  * or error code for failure.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static int
5727c478bd9Sstevel@tonic-gate delete_record(struct db_list *dbp, void *keyaddr, int keysize, char *str)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	datum	key;
5757c478bd9Sstevel@tonic-gate 	int	error = 0;
5767c478bd9Sstevel@tonic-gate 	char	*errfmt = "delete_record: dbm_delete failed, Error: %s\n";
5777c478bd9Sstevel@tonic-gate 	char	*err;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	errno = 0;
5807c478bd9Sstevel@tonic-gate 	key.dptr = keyaddr;
5817c478bd9Sstevel@tonic-gate 	key.dsize = keysize;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (debug > 2) {
5847c478bd9Sstevel@tonic-gate 		debug_print_key(stdout, str, "delete_record:"
5857c478bd9Sstevel@tonic-gate 			"dbm_delete:\n", key.dptr, key.dsize);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 	if (dbm_delete(dbp->db, key) < 0) {
5887c478bd9Sstevel@tonic-gate 		error = dbm_error(dbp->db);
5897c478bd9Sstevel@tonic-gate 		dbm_clearerr(dbp->db);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if (error) {
5927c478bd9Sstevel@tonic-gate 			if (errno)
5937c478bd9Sstevel@tonic-gate 				err = strerror(errno);
5947c478bd9Sstevel@tonic-gate 			else {
5957c478bd9Sstevel@tonic-gate 				err = err_str;
5967c478bd9Sstevel@tonic-gate 				errno = EIO;
5977c478bd9Sstevel@tonic-gate 			}
5987c478bd9Sstevel@tonic-gate 		} else { /* should not happen but sometimes does */
5997c478bd9Sstevel@tonic-gate 			err = err_str;
6007c478bd9Sstevel@tonic-gate 			errno = -1;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 		if (debug) {
6037c478bd9Sstevel@tonic-gate 			debug_print_key(stderr, str, "delete_record:"
6047c478bd9Sstevel@tonic-gate 				"dbm_delete:\n", key.dptr, key.dsize);
6057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, errfmt, err);
6067c478bd9Sstevel@tonic-gate 		} else
6077c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(errfmt), err);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 	return (errno);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * db_update_fhrec - puts fhrec in db with updated atime if more than
6147c478bd9Sstevel@tonic-gate  * mapping_update_interval seconds passed. Return 0 if success, error otherwise.
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate static int
6177c478bd9Sstevel@tonic-gate db_update_fhrec(struct db_list *dbp, void *keyaddr, int keysize,
6187c478bd9Sstevel@tonic-gate 	fhlist_ent *fhrecp, char *str)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	time_t	cur_time = time(0);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (difftime(cur_time, fhrecp->atime) >= mapping_update_interval) {
6237c478bd9Sstevel@tonic-gate 		fhrecp->atime = cur_time;
6247c478bd9Sstevel@tonic-gate 		return (store_record(dbp, keyaddr, keysize,
6257c478bd9Sstevel@tonic-gate 				fhrecp, fhrecp->reclen, str));
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 	return (0);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * db_update_linkinfo - puts linkinfo in db with updated atime if more than
6327c478bd9Sstevel@tonic-gate  * mapping_update_interval seconds passed. Return 0 if success, error otherwise.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate static int
6357c478bd9Sstevel@tonic-gate db_update_linkinfo(struct db_list *dbp, void *keyaddr, int keysize,
6367c478bd9Sstevel@tonic-gate 	linkinfo_ent *linkp, char *str)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	time_t	cur_time = time(0);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (difftime(cur_time, linkp->atime) >= mapping_update_interval) {
6417c478bd9Sstevel@tonic-gate 		linkp->atime = cur_time;
6427c478bd9Sstevel@tonic-gate 		return (store_record(dbp, keyaddr, keysize,
6437c478bd9Sstevel@tonic-gate 				linkp, linkp->reclen, str));
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	return (0);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * create_primary_struct - add primary record to the database.
6507c478bd9Sstevel@tonic-gate  * Database must be open when this function is called.
6517c478bd9Sstevel@tonic-gate  * If success, return the added database entry. fhrecp may be used to
6527c478bd9Sstevel@tonic-gate  * provide an existing memory area, else malloc it. If failed, *errorp
6537c478bd9Sstevel@tonic-gate  * contains the error code and return NULL.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate static fhlist_ent *
6567c478bd9Sstevel@tonic-gate create_primary_struct(struct db_list *dbp, fhandle_t *dfh, char *name,
6577c478bd9Sstevel@tonic-gate 	fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp, int *errorp)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	int		reclen, reclen1;
6607c478bd9Sstevel@tonic-gate 	fhlist_ent	*new_fhrecp = fhrecp;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	reclen1 = offsetof(fhlist_ent, name) + strlen(name) + 1;
6637c478bd9Sstevel@tonic-gate 	reclen = ROUNDUP32(reclen1);
6647c478bd9Sstevel@tonic-gate 	if (fhrecp == NULL) {	/* allocated the memory */
6657c478bd9Sstevel@tonic-gate 		if ((new_fhrecp = malloc(reclen)) == NULL) {
6667c478bd9Sstevel@tonic-gate 			*errorp = errno;
6677c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
6687c478bd9Sstevel@tonic-gate 				"create_primary_struct: malloc %d Error %s"),
6697c478bd9Sstevel@tonic-gate 				reclen, strerror(*errorp));
6707c478bd9Sstevel@tonic-gate 			return (NULL);
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	/* Fill in the fields */
6747c478bd9Sstevel@tonic-gate 	(void) memcpy(&new_fhrecp->fh, fh, sizeof (*fh));
6757c478bd9Sstevel@tonic-gate 	(void) memcpy(&new_fhrecp->dfh, dfh, sizeof (*dfh));
6767c478bd9Sstevel@tonic-gate 	new_fhrecp->flags = flags;
6777c478bd9Sstevel@tonic-gate 	if (dfh == &public_fh)
6787c478bd9Sstevel@tonic-gate 		new_fhrecp->flags |= PUBLIC_PATH;
6797c478bd9Sstevel@tonic-gate 	else
6807c478bd9Sstevel@tonic-gate 		new_fhrecp->flags &= ~PUBLIC_PATH;
6817c478bd9Sstevel@tonic-gate 	new_fhrecp->mtime = time(0);
6827c478bd9Sstevel@tonic-gate 	new_fhrecp->atime = new_fhrecp->mtime;
6837c478bd9Sstevel@tonic-gate 	(void) strcpy(new_fhrecp->name, name);
6847c478bd9Sstevel@tonic-gate 	if (reclen1 < reclen) {
6857c478bd9Sstevel@tonic-gate 		bzero((char *)((uintptr_t)new_fhrecp + reclen1),
6867c478bd9Sstevel@tonic-gate 			reclen - reclen1);
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	new_fhrecp->reclen = reclen;
6897c478bd9Sstevel@tonic-gate 	*errorp = store_record(dbp, &fh->fh_data, fh->fh_len, new_fhrecp,
6907c478bd9Sstevel@tonic-gate 			new_fhrecp->reclen, "create_primary_struct");
6917c478bd9Sstevel@tonic-gate 	if (*errorp != 0) {
6927c478bd9Sstevel@tonic-gate 		/* Could not store */
6937c478bd9Sstevel@tonic-gate 		if (fhrecp == NULL)	/* caller did not supply pointer */
6947c478bd9Sstevel@tonic-gate 			free(new_fhrecp);
6957c478bd9Sstevel@tonic-gate 		return (NULL);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	return (new_fhrecp);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * db_add_primary - add primary record to the database.
7027c478bd9Sstevel@tonic-gate  * If record already in and live, return it (even if for a different link).
7037c478bd9Sstevel@tonic-gate  * If in database but marked deleted, replace it. If not in database, add it.
7047c478bd9Sstevel@tonic-gate  * Database must be open when this function is called.
7057c478bd9Sstevel@tonic-gate  * If success, return the added database entry. fhrecp may be used to
7067c478bd9Sstevel@tonic-gate  * provide an existing memory area, else malloc it. If failed, *errorp
7077c478bd9Sstevel@tonic-gate  * contains the error code and return NULL.
7087c478bd9Sstevel@tonic-gate  */
7097c478bd9Sstevel@tonic-gate static fhlist_ent *
7107c478bd9Sstevel@tonic-gate db_add_primary(struct db_list *dbp, fhandle_t *dfh, char *name, fhandle_t *fh,
7117c478bd9Sstevel@tonic-gate 	uint_t flags, fhlist_ent *fhrecp, int *errorp)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	fhlist_ent	*new_fhrecp;
7147c478bd9Sstevel@tonic-gate 	fh_primary_key	fhkey;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (debug > 2)
7177c478bd9Sstevel@tonic-gate 		(void) printf("db_add_primary entered: name '%s'\n", name);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	bcopy(&fh->fh_data, fhkey, fh->fh_len);
7207c478bd9Sstevel@tonic-gate 	new_fhrecp = fetch_record(dbp, fhkey, fh->fh_len, (void *)fhrecp,
7217c478bd9Sstevel@tonic-gate 			errorp, "db_add_primary");
7227c478bd9Sstevel@tonic-gate 	if (new_fhrecp != NULL) {
7237c478bd9Sstevel@tonic-gate 		/* primary record is in the database */
7247c478bd9Sstevel@tonic-gate 		/* Update atime if needed */
7257c478bd9Sstevel@tonic-gate 		*errorp = db_update_fhrec(dbp, fhkey, fh->fh_len, new_fhrecp,
7267c478bd9Sstevel@tonic-gate 				"db_add_primary put fhrec");
7277c478bd9Sstevel@tonic-gate 		if (debug > 2)
7287c478bd9Sstevel@tonic-gate 			(void) printf("db_add_primary exits(2): name '%s'\n",
7297c478bd9Sstevel@tonic-gate 				name);
7307c478bd9Sstevel@tonic-gate 		return (new_fhrecp);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	/* primary record not in database - create it */
7337c478bd9Sstevel@tonic-gate 	new_fhrecp = create_primary_struct(dbp, dfh, name, fh, flags,
7347c478bd9Sstevel@tonic-gate 			fhrecp, errorp);
7357c478bd9Sstevel@tonic-gate 	if (new_fhrecp == NULL) {
7367c478bd9Sstevel@tonic-gate 		/* Could not store */
7377c478bd9Sstevel@tonic-gate 		if (debug > 2)
7387c478bd9Sstevel@tonic-gate 			(void) printf(
7397c478bd9Sstevel@tonic-gate 				"db_add_primary exits(1): name '%s' Error %s\n",
7407c478bd9Sstevel@tonic-gate 				name, ((*errorp >= 0) ? strerror(*errorp) :
7417c478bd9Sstevel@tonic-gate 					"Unknown"));
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		return (NULL);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 	if (debug > 2)
7467c478bd9Sstevel@tonic-gate 		(void) printf("db_add_primary exits(0): name '%s'\n", name);
7477c478bd9Sstevel@tonic-gate 	return (new_fhrecp);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate /*
7517c478bd9Sstevel@tonic-gate  * get_next_link - get and check the next link in the chain.
7527c478bd9Sstevel@tonic-gate  * Re-use space if linkp param non-null. Also set *linkkey and *linksizep
7537c478bd9Sstevel@tonic-gate  * to values for next link (*linksizep set to 0 if last link).
7547c478bd9Sstevel@tonic-gate  * cookie is used to detect corrupted link entries XXXXXXX
7557c478bd9Sstevel@tonic-gate  * Return the link pointer or NULL if none.
7567c478bd9Sstevel@tonic-gate  */
7577c478bd9Sstevel@tonic-gate static linkinfo_ent *
7587c478bd9Sstevel@tonic-gate get_next_link(struct db_list *dbp, char *linkkey, int *linksizep,
7597c478bd9Sstevel@tonic-gate 	linkinfo_ent *linkp, void **cookiep, int *errorp, char *msg)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	int	linksize, nextsize;
7627c478bd9Sstevel@tonic-gate 	char	*nextkey;
7637c478bd9Sstevel@tonic-gate 	linkinfo_ent *new_linkp = linkp;
7647c478bd9Sstevel@tonic-gate 	struct link_keys *lnp;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	linksize = *linksizep;
7677c478bd9Sstevel@tonic-gate 	if (linksize == 0)
7687c478bd9Sstevel@tonic-gate 		return (NULL);
7697c478bd9Sstevel@tonic-gate 	*linksizep = 0;
7707c478bd9Sstevel@tonic-gate 	new_linkp = fetch_record(dbp, linkkey, linksize, (void *)linkp,
7717c478bd9Sstevel@tonic-gate 			errorp, msg);
7727c478bd9Sstevel@tonic-gate 	if (new_linkp == NULL)
7737c478bd9Sstevel@tonic-gate 		return (NULL);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/* Set linkkey to point to next record */
7767c478bd9Sstevel@tonic-gate 	nextsize = LN_NEXT_LEN(new_linkp);
7777c478bd9Sstevel@tonic-gate 	if (nextsize == 0)
7787c478bd9Sstevel@tonic-gate 		return (new_linkp);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* Add this key to the cookie list */
7817c478bd9Sstevel@tonic-gate 	if ((lnp = malloc(sizeof (struct link_keys))) == NULL) {
7827c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("get_next_key: malloc error %s\n"),
7837c478bd9Sstevel@tonic-gate 			strerror(errno));
7847c478bd9Sstevel@tonic-gate 		if ((new_linkp != NULL) && (linkp == NULL))
7857c478bd9Sstevel@tonic-gate 			free(new_linkp);
7867c478bd9Sstevel@tonic-gate 		return (NULL);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	(void) memcpy(lnp->lnkey, linkkey, linksize);
7897c478bd9Sstevel@tonic-gate 	lnp->lnsize = linksize;
7907c478bd9Sstevel@tonic-gate 	lnp->next = *(struct link_keys **)cookiep;
7917c478bd9Sstevel@tonic-gate 	*cookiep = (void *)lnp;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	/* Make sure record does not point to itself or other internal loops */
7947c478bd9Sstevel@tonic-gate 	nextkey = LN_NEXT(new_linkp);
7957c478bd9Sstevel@tonic-gate 	for (; lnp != NULL; lnp = lnp->next) {
7967c478bd9Sstevel@tonic-gate 		if ((nextsize == lnp->lnsize) && (memcmp(
7977c478bd9Sstevel@tonic-gate 			lnp->lnkey, nextkey, nextsize) == 0)) {
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 			/*
8007c478bd9Sstevel@tonic-gate 			 * XXX This entry's next pointer points to
8017c478bd9Sstevel@tonic-gate 			 * itself. This is only a work-around, remove
8027c478bd9Sstevel@tonic-gate 			 * this check once bug 4203186 is fixed.
8037c478bd9Sstevel@tonic-gate 			 */
8047c478bd9Sstevel@tonic-gate 			if (debug) {
8057c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
8067c478bd9Sstevel@tonic-gate 				"%s: get_next_link: last record invalid.\n",
8077c478bd9Sstevel@tonic-gate 					msg);
8087c478bd9Sstevel@tonic-gate 				debug_print_key_and_data(stderr, msg,
8097c478bd9Sstevel@tonic-gate 					"invalid rec:\n ", linkkey, linksize,
8107c478bd9Sstevel@tonic-gate 					(char *)new_linkp, new_linkp->reclen);
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 			/* Return as if this is the last link */
8137c478bd9Sstevel@tonic-gate 			return (new_linkp);
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 	(void) memcpy(linkkey, nextkey, nextsize);
8177c478bd9Sstevel@tonic-gate 	*linksizep = nextsize;
8187c478bd9Sstevel@tonic-gate 	return (new_linkp);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * free_link_cookies - free the cookie list
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate static void
8257c478bd9Sstevel@tonic-gate free_link_cookies(void *cookie)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	struct link_keys *dellnp, *lnp;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	lnp = (struct link_keys *)cookie;
8307c478bd9Sstevel@tonic-gate 	while (lnp != NULL) {
8317c478bd9Sstevel@tonic-gate 		dellnp = lnp;
8327c478bd9Sstevel@tonic-gate 		lnp = lnp->next;
8337c478bd9Sstevel@tonic-gate 		free(dellnp);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate  * add_mc_path - add a mc link to a file that has other links. Add it at end
8397c478bd9Sstevel@tonic-gate  * of linked list. Called when it's known there are other links.
8407c478bd9Sstevel@tonic-gate  */
8417c478bd9Sstevel@tonic-gate static void
8427c478bd9Sstevel@tonic-gate add_mc_path(struct db_list *dbp, fhandle_t *dfh, char *name,
8437c478bd9Sstevel@tonic-gate 	fhlist_ent *fhrecp, linkinfo_ent *linkp, int *errorp)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	fh_secondary_key	linkkey;
8467c478bd9Sstevel@tonic-gate 	int			linksize, len;
8477c478bd9Sstevel@tonic-gate 	linkinfo_ent		lastlink, *lastlinkp;
8487c478bd9Sstevel@tonic-gate 	void			*cookie;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, &fhrecp->dfh, fhrecp->name);
8517c478bd9Sstevel@tonic-gate 	cookie = NULL;
8527c478bd9Sstevel@tonic-gate 	do {
8537c478bd9Sstevel@tonic-gate 		lastlinkp = get_next_link(dbp, linkkey, &linksize, &lastlink,
8547c478bd9Sstevel@tonic-gate 				&cookie, errorp, "add_mc_path");
8557c478bd9Sstevel@tonic-gate 	} while (linksize > 0);
8567c478bd9Sstevel@tonic-gate 	free_link_cookies(cookie);
8577c478bd9Sstevel@tonic-gate 	/* reached end of list */
8587c478bd9Sstevel@tonic-gate 	if (lastlinkp == NULL) {
8597c478bd9Sstevel@tonic-gate 		/* nothing to do */
8607c478bd9Sstevel@tonic-gate 		if (debug > 1) {
8617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "add_mc_path link is null\n");
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		return;
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 	/* Add new link after last link */
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * next - link key for the next in the list - add at end so null.
8687c478bd9Sstevel@tonic-gate 	 * prev - link key for the previous link in the list.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	linkp->prev_offset = linkp->next_offset;	/* aligned */
8717c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(LN_PREV(linkp), &lastlinkp->dfh,
8727c478bd9Sstevel@tonic-gate 				LN_NAME(lastlinkp));
8737c478bd9Sstevel@tonic-gate 	linkp->reclen = linkp->prev_offset + linksize;	/* aligned */
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	/* Add the link information to the database */
8767c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, dfh, name);
8777c478bd9Sstevel@tonic-gate 	*errorp = store_record(dbp, linkkey, linksize,
8787c478bd9Sstevel@tonic-gate 			linkp, linkp->reclen, "add_mc_path");
8797c478bd9Sstevel@tonic-gate 	if (*errorp != 0)
8807c478bd9Sstevel@tonic-gate 		return;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* Now update previous last link to point forward to new link */
8837c478bd9Sstevel@tonic-gate 	/* Copy prev link out since it's going to be overwritten */
8847c478bd9Sstevel@tonic-gate 	linksize = LN_PREV_LEN(lastlinkp);
8857c478bd9Sstevel@tonic-gate 	(void) memcpy(linkkey, LN_PREV(lastlinkp), linksize);
8867c478bd9Sstevel@tonic-gate 	/* Update previous last link to point to new one */
8877c478bd9Sstevel@tonic-gate 	len = fill_link_key(LN_NEXT(lastlinkp), dfh, name);
8887c478bd9Sstevel@tonic-gate 	lastlinkp->prev_offset = lastlinkp->next_offset + len;	/* aligned */
8897c478bd9Sstevel@tonic-gate 	(void) memcpy(LN_PREV(lastlinkp), linkkey, linksize);
8907c478bd9Sstevel@tonic-gate 	lastlinkp->reclen = lastlinkp->prev_offset + linksize;
8917c478bd9Sstevel@tonic-gate 	/* Update the link information to the database */
8927c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, &lastlinkp->dfh, LN_NAME(lastlinkp));
8937c478bd9Sstevel@tonic-gate 	*errorp = store_record(dbp, linkkey, linksize,
8947c478bd9Sstevel@tonic-gate 			lastlinkp, lastlinkp->reclen, "add_mc_path prev");
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate  * create_link_struct - create the secondary struct.
8997c478bd9Sstevel@tonic-gate  * (dfh,name) is the secondary key, fhrec is the primary record for the file
9007c478bd9Sstevel@tonic-gate  * and linkpp is a place holder for the record (could be null).
9017c478bd9Sstevel@tonic-gate  * Insert the record to the database.
9027c478bd9Sstevel@tonic-gate  * Return 0 if success, error otherwise.
9037c478bd9Sstevel@tonic-gate  */
9047c478bd9Sstevel@tonic-gate static linkinfo_ent *
9057c478bd9Sstevel@tonic-gate create_link_struct(struct db_list *dbp, fhandle_t *dfh, char *name,
9067c478bd9Sstevel@tonic-gate 	fhlist_ent *fhrecp, int *errorp)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	fh_secondary_key	linkkey;
9097c478bd9Sstevel@tonic-gate 	int			len, linksize;
9107c478bd9Sstevel@tonic-gate 	linkinfo_ent		*linkp;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	if ((linkp = malloc(sizeof (linkinfo_ent))) == NULL) {
9137c478bd9Sstevel@tonic-gate 		*errorp = errno;
9147c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
9157c478bd9Sstevel@tonic-gate 			"create_link_struct: malloc failed: Error %s"),
9167c478bd9Sstevel@tonic-gate 			strerror(*errorp));
9177c478bd9Sstevel@tonic-gate 		return (NULL);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	if (dfh == &public_fh)
9207c478bd9Sstevel@tonic-gate 		linkp->flags |= PUBLIC_PATH;
9217c478bd9Sstevel@tonic-gate 	else
9227c478bd9Sstevel@tonic-gate 		linkp->flags &= ~PUBLIC_PATH;
9237c478bd9Sstevel@tonic-gate 	(void) memcpy(&linkp->dfh, dfh, sizeof (*dfh));
9247c478bd9Sstevel@tonic-gate 	linkp->mtime = time(0);
9257c478bd9Sstevel@tonic-gate 	linkp->atime = linkp->mtime;
9267c478bd9Sstevel@tonic-gate 	/* Calculate offsets of variable fields */
9277c478bd9Sstevel@tonic-gate 	/* fhkey - primary key (inode/gen) */
9287c478bd9Sstevel@tonic-gate 	/* name - component name (in directory dfh) */
9297c478bd9Sstevel@tonic-gate 	linkp->fhkey_offset = ROUNDUP32(offsetof(linkinfo_ent, varbuf));
9307c478bd9Sstevel@tonic-gate 	len = fill_link_key(LN_FHKEY(linkp), &fhrecp->fh, name);
9317c478bd9Sstevel@tonic-gate 	linkp->name_offset = linkp->fhkey_offset + fhrecp->fh.fh_len;
9327c478bd9Sstevel@tonic-gate 	linkp->next_offset = linkp->fhkey_offset + len;	/* aligned */
9337c478bd9Sstevel@tonic-gate 	/*
9347c478bd9Sstevel@tonic-gate 	 * next - link key for the next link in the list - NULL if it's
9357c478bd9Sstevel@tonic-gate 	 * the first link. If this is the public fs, only one link allowed.
9367c478bd9Sstevel@tonic-gate 	 * Avoid setting a multi-component path as primary path,
9377c478bd9Sstevel@tonic-gate 	 * unless no choice.
9387c478bd9Sstevel@tonic-gate 	 */
9397c478bd9Sstevel@tonic-gate 	len = 0;
9407c478bd9Sstevel@tonic-gate 	if (memcmp(&fhrecp->dfh, dfh, sizeof (*dfh)) ||
9417c478bd9Sstevel@tonic-gate 	    strcmp(fhrecp->name, name)) {
9427c478bd9Sstevel@tonic-gate 		/* different link than the one that's in the record */
9437c478bd9Sstevel@tonic-gate 		if (dfh == &public_fh) {
9447c478bd9Sstevel@tonic-gate 			/* parent is public fh - either multi-comp or root */
9457c478bd9Sstevel@tonic-gate 			if (memcmp(&fhrecp->fh, &public_fh,
9467c478bd9Sstevel@tonic-gate 				sizeof (public_fh))) {
9477c478bd9Sstevel@tonic-gate 				/* multi-comp path */
9487c478bd9Sstevel@tonic-gate 				add_mc_path(dbp, dfh, name, fhrecp, linkp,
9497c478bd9Sstevel@tonic-gate 						errorp);
9507c478bd9Sstevel@tonic-gate 				if (*errorp != 0) {
9517c478bd9Sstevel@tonic-gate 					free(linkp);
9527c478bd9Sstevel@tonic-gate 					return (NULL);
9537c478bd9Sstevel@tonic-gate 				}
9547c478bd9Sstevel@tonic-gate 				return (linkp);
9557c478bd9Sstevel@tonic-gate 			}
9567c478bd9Sstevel@tonic-gate 		} else {
9577c478bd9Sstevel@tonic-gate 			/* new link to a file with a different one already */
9587c478bd9Sstevel@tonic-gate 			len = fill_link_key(LN_NEXT(linkp), &fhrecp->dfh,
9597c478bd9Sstevel@tonic-gate 				fhrecp->name);
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	/*
9637c478bd9Sstevel@tonic-gate 	 * prev - link key for the previous link in the list - since we
9647c478bd9Sstevel@tonic-gate 	 * always insert at the front of the list, it's always initially NULL.
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	linkp->prev_offset = linkp->next_offset + len;	/* aligned */
9677c478bd9Sstevel@tonic-gate 	linkp->reclen = linkp->prev_offset;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/* Add the link information to the database */
9707c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, dfh, name);
9717c478bd9Sstevel@tonic-gate 	*errorp = store_record(dbp, linkkey, linksize, linkp, linkp->reclen,
9727c478bd9Sstevel@tonic-gate 			"create_link_struct");
9737c478bd9Sstevel@tonic-gate 	if (*errorp != 0) {
9747c478bd9Sstevel@tonic-gate 		free(linkp);
9757c478bd9Sstevel@tonic-gate 		return (NULL);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 	return (linkp);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * db_add_secondary - add secondary record to the database (for the directory
9827c478bd9Sstevel@tonic-gate  * information).
9837c478bd9Sstevel@tonic-gate  * Assumes this is a new link, not yet in the database, and that the primary
9847c478bd9Sstevel@tonic-gate  * record is already in.
9857c478bd9Sstevel@tonic-gate  * If fhrecp is non-null, then fhrecp is the primary record.
9867c478bd9Sstevel@tonic-gate  * Database must be open when this function is called.
9877c478bd9Sstevel@tonic-gate  * Return 0 if success, error code otherwise.
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate static int
9907c478bd9Sstevel@tonic-gate db_add_secondary(struct db_list *dbp, fhandle_t *dfh, char *name,
9917c478bd9Sstevel@tonic-gate 	fhandle_t *fh, fhlist_ent *fhrecp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	int			nextsize, len, error;
9947c478bd9Sstevel@tonic-gate 	linkinfo_ent		nextlink, *newlinkp, *nextlinkp;
9957c478bd9Sstevel@tonic-gate 	uint_t			fhflags;
9967c478bd9Sstevel@tonic-gate 	char			*nextaddr;
9977c478bd9Sstevel@tonic-gate 	fhlist_ent		*new_fhrecp = fhrecp;
9987c478bd9Sstevel@tonic-gate 	fh_primary_key		fhkey;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	if (debug > 2)
10017c478bd9Sstevel@tonic-gate 		(void) printf("db_add_secondary entered: name '%s'\n", name);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	bcopy(&fh->fh_data, fhkey, fh->fh_len);
10047c478bd9Sstevel@tonic-gate 	if (fhrecp == NULL) {
10057c478bd9Sstevel@tonic-gate 		/* Fetch the primary record */
10067c478bd9Sstevel@tonic-gate 		new_fhrecp = fetch_record(dbp, fhkey, fh->fh_len, NULL,
10077c478bd9Sstevel@tonic-gate 				&error, "db_add_secondary primary");
10087c478bd9Sstevel@tonic-gate 		if (new_fhrecp == NULL) {
10097c478bd9Sstevel@tonic-gate 			return (error);
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	/* Update fhrec atime if needed */
10137c478bd9Sstevel@tonic-gate 	error = db_update_fhrec(dbp, fhkey, fh->fh_len, new_fhrecp,
10147c478bd9Sstevel@tonic-gate 			"db_add_secondary primary");
10157c478bd9Sstevel@tonic-gate 	fhflags = new_fhrecp->flags;
10167c478bd9Sstevel@tonic-gate 	/* now create and insert the secondary record */
10177c478bd9Sstevel@tonic-gate 	newlinkp = create_link_struct(dbp, dfh, name, new_fhrecp, &error);
10187c478bd9Sstevel@tonic-gate 	if (fhrecp == NULL) {
10197c478bd9Sstevel@tonic-gate 		free(new_fhrecp);
10207c478bd9Sstevel@tonic-gate 		new_fhrecp = NULL;
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 	if (newlinkp == NULL) {
10237c478bd9Sstevel@tonic-gate 		if (debug > 2)
10247c478bd9Sstevel@tonic-gate 			(void) printf("create_link_struct '%s' Error %s\n",
10257c478bd9Sstevel@tonic-gate 				name, ((error >= 0) ? strerror(error) :
10267c478bd9Sstevel@tonic-gate 					"Unknown"));
10277c478bd9Sstevel@tonic-gate 		return (error);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	nextsize = LN_NEXT_LEN(newlinkp);
10307c478bd9Sstevel@tonic-gate 	if (nextsize == 0) {
10317c478bd9Sstevel@tonic-gate 		/* No next - can exit now */
10327c478bd9Sstevel@tonic-gate 		if (debug > 2)
10337c478bd9Sstevel@tonic-gate 			(void) printf("db_add_secondary: no next link\n");
10347c478bd9Sstevel@tonic-gate 		free(newlinkp);
10357c478bd9Sstevel@tonic-gate 		return (0);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * Update the linked list to point to new head: replace head of
10407c478bd9Sstevel@tonic-gate 	 * list in the primary record, then update previous secondary record
10417c478bd9Sstevel@tonic-gate 	 * to point to new head
10427c478bd9Sstevel@tonic-gate 	 */
10437c478bd9Sstevel@tonic-gate 	new_fhrecp = create_primary_struct(dbp, dfh, name, fh, fhflags,
10447c478bd9Sstevel@tonic-gate 			new_fhrecp, &error);
10457c478bd9Sstevel@tonic-gate 	if (new_fhrecp == NULL) {
10467c478bd9Sstevel@tonic-gate 		if (debug > 2)
10477c478bd9Sstevel@tonic-gate 			(void) printf(
10487c478bd9Sstevel@tonic-gate 				"db_add_secondary: replace primary failed\n");
10497c478bd9Sstevel@tonic-gate 		free(newlinkp);
10507c478bd9Sstevel@tonic-gate 		return (error);
10517c478bd9Sstevel@tonic-gate 	} else if (fhrecp == NULL) {
10527c478bd9Sstevel@tonic-gate 		free(new_fhrecp);
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	/*
10567c478bd9Sstevel@tonic-gate 	 * newlink is the new head of the list, with its "next" pointing to
10577c478bd9Sstevel@tonic-gate 	 * the old head, and its "prev" pointing to NULL. We now need to
10587c478bd9Sstevel@tonic-gate 	 * modify the "next" entry to have its "prev" point to the new entry.
10597c478bd9Sstevel@tonic-gate 	 */
10607c478bd9Sstevel@tonic-gate 	nextaddr = LN_NEXT(newlinkp);
10617c478bd9Sstevel@tonic-gate 	if (debug > 2) {
10627c478bd9Sstevel@tonic-gate 		debug_print_key(stderr, "db_add_secondary", "next key\n    ",
10637c478bd9Sstevel@tonic-gate 			nextaddr, nextsize);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 	/* Get the next link entry from the database */
10667c478bd9Sstevel@tonic-gate 	nextlinkp = fetch_record(dbp, nextaddr, nextsize, (void *)&nextlink,
10677c478bd9Sstevel@tonic-gate 			&error, "db_add_secondary next link");
10687c478bd9Sstevel@tonic-gate 	if (nextlinkp == NULL) {
10697c478bd9Sstevel@tonic-gate 		if (debug > 2)
10707c478bd9Sstevel@tonic-gate 			(void) printf(
10717c478bd9Sstevel@tonic-gate 				"db_add_secondary: fetch next link failed\n");
10727c478bd9Sstevel@tonic-gate 		free(newlinkp);
10737c478bd9Sstevel@tonic-gate 		return (error);
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	/*
10777c478bd9Sstevel@tonic-gate 	 * since the "prev" field is the only field to be changed, and it's
10787c478bd9Sstevel@tonic-gate 	 * the last in the link record, we only need to modify it (and reclen).
10797c478bd9Sstevel@tonic-gate 	 * Re-use link to update the next record.
10807c478bd9Sstevel@tonic-gate 	 */
10817c478bd9Sstevel@tonic-gate 	len = fill_link_key(LN_PREV(nextlinkp), dfh, name);
10827c478bd9Sstevel@tonic-gate 	nextlinkp->reclen = nextlinkp->prev_offset + len;
10837c478bd9Sstevel@tonic-gate 	error = store_record(dbp, nextaddr, nextsize, nextlinkp,
10847c478bd9Sstevel@tonic-gate 			nextlinkp->reclen, "db_add_secondary");
10857c478bd9Sstevel@tonic-gate 	if (debug > 2)
10867c478bd9Sstevel@tonic-gate 		(void) printf(
10877c478bd9Sstevel@tonic-gate 			"db_add_secondary exits(%d): name '%s'\n", error, name);
10887c478bd9Sstevel@tonic-gate 	free(newlinkp);
10897c478bd9Sstevel@tonic-gate 	return (error);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate /*
10937c478bd9Sstevel@tonic-gate  * Update the next link to point to the new prev.
10947c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
10957c478bd9Sstevel@tonic-gate  * If successful, and nextlinkpp is non-null,
10967c478bd9Sstevel@tonic-gate  * *nextlinkpp contains the record for the next link, since
10977c478bd9Sstevel@tonic-gate  * we may will it if the primary record should be updated.
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate static linkinfo_ent *
11007c478bd9Sstevel@tonic-gate update_next_link(struct db_list *dbp, char *nextkey, int nextsize,
11017c478bd9Sstevel@tonic-gate 	char *prevkey, int prevsize, int *errorp)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	linkinfo_ent	*nextlinkp, *linkp1;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if ((nextlinkp = malloc(sizeof (linkinfo_ent))) == NULL) {
11067c478bd9Sstevel@tonic-gate 		*errorp = errno;
11077c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, gettext(
11087c478bd9Sstevel@tonic-gate 			"update_next_link: malloc next Error %s"),
11097c478bd9Sstevel@tonic-gate 			strerror(*errorp));
11107c478bd9Sstevel@tonic-gate 		return (NULL);
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 	linkp1 = nextlinkp;
11137c478bd9Sstevel@tonic-gate 	nextlinkp = fetch_record(dbp, nextkey, nextsize, nextlinkp,
11147c478bd9Sstevel@tonic-gate 			errorp, "update next");
11157c478bd9Sstevel@tonic-gate 	/* if there is no next record - ok */
11167c478bd9Sstevel@tonic-gate 	if (nextlinkp == NULL) {
11177c478bd9Sstevel@tonic-gate 		/* Return no error */
11187c478bd9Sstevel@tonic-gate 		*errorp = 0;
11197c478bd9Sstevel@tonic-gate 		free(linkp1);
11207c478bd9Sstevel@tonic-gate 		return (NULL);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	/* Set its prev to the prev of the deleted record */
11237c478bd9Sstevel@tonic-gate 	nextlinkp->reclen = ROUNDUP32(nextlinkp->reclen -
11247c478bd9Sstevel@tonic-gate 				LN_PREV_LEN(nextlinkp) + prevsize);
11257c478bd9Sstevel@tonic-gate 	/* Change the len and set prev */
11267c478bd9Sstevel@tonic-gate 	if (prevsize > 0) {
11277c478bd9Sstevel@tonic-gate 		(void) memcpy(LN_PREV(nextlinkp), prevkey, prevsize);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	/* No other changes needed because prev is last field */
11307c478bd9Sstevel@tonic-gate 	*errorp = store_record(dbp, nextkey, nextsize, nextlinkp,
11317c478bd9Sstevel@tonic-gate 			nextlinkp->reclen, "update_next");
11327c478bd9Sstevel@tonic-gate 	if (*errorp != 0) {
11337c478bd9Sstevel@tonic-gate 		free(nextlinkp);
11347c478bd9Sstevel@tonic-gate 		nextlinkp = NULL;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	return (nextlinkp);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /*
11407c478bd9Sstevel@tonic-gate  * Update the prev link to point to the new next.
11417c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
11427c478bd9Sstevel@tonic-gate  */
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate update_prev_link(struct db_list *dbp, char *nextkey, int nextsize,
11457c478bd9Sstevel@tonic-gate 	char *prevkey, int prevsize)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	linkinfo_ent	prevlink, *prevlinkp;
11487c478bd9Sstevel@tonic-gate 	int		diff, error;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/* Update its next to the given one */
11517c478bd9Sstevel@tonic-gate 	prevlinkp = fetch_record(dbp, prevkey, prevsize, &prevlink, &error,
11527c478bd9Sstevel@tonic-gate 			"update prev");
11537c478bd9Sstevel@tonic-gate 	/* if error there is no next record - ok */
11547c478bd9Sstevel@tonic-gate 	if (prevlinkp == NULL) {
11557c478bd9Sstevel@tonic-gate 		return (0);
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 	diff = nextsize - LN_NEXT_LEN(prevlinkp);
11587c478bd9Sstevel@tonic-gate 	prevlinkp->reclen = ROUNDUP32(prevlinkp->reclen + diff);
11597c478bd9Sstevel@tonic-gate 	/* Change the len and set next - may push prev */
11607c478bd9Sstevel@tonic-gate 	if (diff != 0) {
11617c478bd9Sstevel@tonic-gate 		char	*ptr = LN_PREV(prevlinkp);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		prevlinkp->prev_offset += diff;
11647c478bd9Sstevel@tonic-gate 		(void) memcpy(LN_PREV(prevlinkp), ptr, LN_PREV_LEN(prevlinkp));
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 	if (nextsize > 0) {
11677c478bd9Sstevel@tonic-gate 		(void) memcpy(LN_NEXT(prevlinkp), nextkey, nextsize);
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 	/* Store updated record */
11707c478bd9Sstevel@tonic-gate 	error = store_record(dbp, prevkey, prevsize, prevlinkp,
11717c478bd9Sstevel@tonic-gate 			prevlinkp->reclen, "update_prev");
11727c478bd9Sstevel@tonic-gate 	return (error);
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate /*
11767c478bd9Sstevel@tonic-gate  * update_linked_list - update the next link to point back to prev, and vice
11777c478bd9Sstevel@tonic-gate  * versa. Normally called by delete_link to drop the deleted link from the
11787c478bd9Sstevel@tonic-gate  * linked list of hard links for the file. next and prev are the keys of next
11797c478bd9Sstevel@tonic-gate  * and previous links for the deleted link in the list (could be NULL).
11807c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
11817c478bd9Sstevel@tonic-gate  * If successful, and nextlinkpp is non-null,
11827c478bd9Sstevel@tonic-gate  * return the record for the next link, since
11837c478bd9Sstevel@tonic-gate  * if the primary record should be updated we'll need it. In this case,
11847c478bd9Sstevel@tonic-gate  * actually allocate the space for it because we can't tell otherwise.
11857c478bd9Sstevel@tonic-gate  */
11867c478bd9Sstevel@tonic-gate static linkinfo_ent *
11877c478bd9Sstevel@tonic-gate update_linked_list(struct db_list *dbp, char *nextkey, int nextsize,
11887c478bd9Sstevel@tonic-gate 	char *prevkey, int prevsize, int *errorp)
11897c478bd9Sstevel@tonic-gate {
11907c478bd9Sstevel@tonic-gate 	linkinfo_ent	*nextlinkp = NULL;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	*errorp = 0;
11937c478bd9Sstevel@tonic-gate 	if (nextsize > 0) {
11947c478bd9Sstevel@tonic-gate 		nextlinkp = update_next_link(dbp, nextkey, nextsize,
11957c478bd9Sstevel@tonic-gate 				prevkey, prevsize, errorp);
11967c478bd9Sstevel@tonic-gate 		if (nextlinkp == NULL) {
11977c478bd9Sstevel@tonic-gate 			/* not an error if no next link */
11987c478bd9Sstevel@tonic-gate 			if (*errorp != 0) {
11997c478bd9Sstevel@tonic-gate 				if (debug > 1) {
12007c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
12017c478bd9Sstevel@tonic-gate 						"update_next_link Error %s\n",
12027c478bd9Sstevel@tonic-gate 					((*errorp >= 0) ? strerror(*errorp) :
12037c478bd9Sstevel@tonic-gate 						"Unknown"));
12047c478bd9Sstevel@tonic-gate 				}
12057c478bd9Sstevel@tonic-gate 				return (NULL);
12067c478bd9Sstevel@tonic-gate 			}
12077c478bd9Sstevel@tonic-gate 		}
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate 	if (prevsize > 0) {
12107c478bd9Sstevel@tonic-gate 		*errorp = update_prev_link(dbp, nextkey, nextsize,
12117c478bd9Sstevel@tonic-gate 				prevkey, prevsize);
12127c478bd9Sstevel@tonic-gate 		if (*errorp != 0) {
12137c478bd9Sstevel@tonic-gate 			if (debug > 1) {
12147c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
12157c478bd9Sstevel@tonic-gate 					"update_prev_link Error %s\n",
12167c478bd9Sstevel@tonic-gate 					((*errorp >= 0) ? strerror(*errorp) :
12177c478bd9Sstevel@tonic-gate 					"Unknown"));
12187c478bd9Sstevel@tonic-gate 			}
12197c478bd9Sstevel@tonic-gate 			if (nextlinkp != NULL)
12207c478bd9Sstevel@tonic-gate 				free(nextlinkp);
12217c478bd9Sstevel@tonic-gate 			nextlinkp = NULL;
12227c478bd9Sstevel@tonic-gate 		}
12237c478bd9Sstevel@tonic-gate 	}
12247c478bd9Sstevel@tonic-gate 	return (nextlinkp);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate  * db_update_primary_new_head - Update a primary record that the head of
12297c478bd9Sstevel@tonic-gate  * the list is deleted. Similar to db_add_primary, but the primary record
12307c478bd9Sstevel@tonic-gate  * must exist, and is always replaced with one pointing to the new link,
12317c478bd9Sstevel@tonic-gate  * unless it does not point to the deleted link. If the link we deleted
12327c478bd9Sstevel@tonic-gate  * was the last link, the delete the primary record as well.
12337c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate static int
12367c478bd9Sstevel@tonic-gate db_update_primary_new_head(struct db_list *dbp, linkinfo_ent *dellinkp,
12377c478bd9Sstevel@tonic-gate 	linkinfo_ent *nextlinkp, fhlist_ent *fhrecp)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	int			error;
12407c478bd9Sstevel@tonic-gate 	char			*name, *next_name;
12417c478bd9Sstevel@tonic-gate 	fhandle_t		*dfh;
12427c478bd9Sstevel@tonic-gate 	fh_primary_key		fhkey;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	dfh = &dellinkp->dfh;
12457c478bd9Sstevel@tonic-gate 	name = LN_NAME(dellinkp);
12467c478bd9Sstevel@tonic-gate 	/* If the deleted link was not the head of the list, we are done */
12477c478bd9Sstevel@tonic-gate 	if (memcmp(&fhrecp->dfh, dfh, sizeof (*dfh)) ||
12487c478bd9Sstevel@tonic-gate 	    strcmp(fhrecp->name, name)) {
12497c478bd9Sstevel@tonic-gate 		/* should never be here... */
12507c478bd9Sstevel@tonic-gate 		if (debug > 1) {
12517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
12527c478bd9Sstevel@tonic-gate 				"db_update_primary_new_head: primary "
12537c478bd9Sstevel@tonic-gate 				"is for [%s,", name);
12547c478bd9Sstevel@tonic-gate 			debug_opaque_print(stderr, (void *)dfh, sizeof (*dfh));
12557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "], not [%s,", fhrecp->name);
12567c478bd9Sstevel@tonic-gate 			debug_opaque_print(stderr, (void *)&fhrecp->dfh,
12577c478bd9Sstevel@tonic-gate 				sizeof (fhrecp->dfh));
12587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "]\n");
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate 		return (0);	/* not head of list so done */
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 	/* Set the head to nextkey if exists. Otherwise, mark file as deleted */
12637c478bd9Sstevel@tonic-gate 	bcopy(&fhrecp->fh.fh_data, fhkey, fhrecp->fh.fh_len);
12647c478bd9Sstevel@tonic-gate 	if (nextlinkp == NULL) {
12657c478bd9Sstevel@tonic-gate 		/* last link */
12667c478bd9Sstevel@tonic-gate 		/* remove primary record from database */
12677c478bd9Sstevel@tonic-gate 		(void) delete_record(dbp,
12687c478bd9Sstevel@tonic-gate 			fhkey, fhrecp->fh.fh_len,
12697c478bd9Sstevel@tonic-gate 			"db_update_primary_new_head: fh delete");
12707c478bd9Sstevel@tonic-gate 		return (0);
12717c478bd9Sstevel@tonic-gate 	} else {
12727c478bd9Sstevel@tonic-gate 		/*
12737c478bd9Sstevel@tonic-gate 		 * There are still "live" links, so update the primary record.
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		next_name = LN_NAME(nextlinkp);
12767c478bd9Sstevel@tonic-gate 		fhrecp->reclen = ROUNDUP32(offsetof(fhlist_ent, name) +
12777c478bd9Sstevel@tonic-gate 					strlen(next_name) + 1);
12787c478bd9Sstevel@tonic-gate 		/* Replace link data with the info for the next link */
12797c478bd9Sstevel@tonic-gate 		(void) memcpy(&fhrecp->dfh, &nextlinkp->dfh,
12807c478bd9Sstevel@tonic-gate 			sizeof (nextlinkp->dfh));
12817c478bd9Sstevel@tonic-gate 		(void) strcpy(fhrecp->name, next_name);
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 	/* not last link */
12847c478bd9Sstevel@tonic-gate 	fhrecp->mtime = time(0);
12857c478bd9Sstevel@tonic-gate 	fhrecp->atime = fhrecp->mtime;
12867c478bd9Sstevel@tonic-gate 	error = store_record(dbp,
12877c478bd9Sstevel@tonic-gate 			fhkey, fhrecp->fh.fh_len, fhrecp,
12887c478bd9Sstevel@tonic-gate 			fhrecp->reclen, "db_update_primary_new_head: fh");
12897c478bd9Sstevel@tonic-gate 	return (error);
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate /*
12937c478bd9Sstevel@tonic-gate  * Exported functions
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate  * db_add - add record to the database. If dfh, fh and name are all here,
12987c478bd9Sstevel@tonic-gate  * add both primary and secondary records. If fh is not available, don't
12997c478bd9Sstevel@tonic-gate  * add anything...
13007c478bd9Sstevel@tonic-gate  * Assumes this is a new file, not yet in the database and that the record
13017c478bd9Sstevel@tonic-gate  * for fh is already in.
13027c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate int
13057c478bd9Sstevel@tonic-gate db_add(char *fhpath, fhandle_t *dfh, char *name, fhandle_t *fh, uint_t flags)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	struct db_list	*dbp = NULL;
13087c478bd9Sstevel@tonic-gate 	fhlist_ent	fhrec, *fhrecp;
13097c478bd9Sstevel@tonic-gate 	int		error = 0;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	if (fh == NULL) {
13127c478bd9Sstevel@tonic-gate 		/* nothing to add */
13137c478bd9Sstevel@tonic-gate 		return (EINVAL);
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 	if (fh == &public_fh) {
13167c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, FALSE);
13177c478bd9Sstevel@tonic-gate 	} else {
13187c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &fh->fh_fsid, &error, O_CREAT);
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	for (; dbp != NULL; dbp = ((fh != &public_fh) ? NULL : dbp->next)) {
13217c478bd9Sstevel@tonic-gate 		if (debug > 3) {
13227c478bd9Sstevel@tonic-gate 			(void) printf("db_add: name '%s', db '%s'\n",
13237c478bd9Sstevel@tonic-gate 				name, dbp->path);
13247c478bd9Sstevel@tonic-gate 		}
13257c478bd9Sstevel@tonic-gate 		fhrecp = db_add_primary(dbp, dfh, name, fh, flags,
13267c478bd9Sstevel@tonic-gate 				&fhrec, &error);
13277c478bd9Sstevel@tonic-gate 		if (fhrecp == NULL) {
13287c478bd9Sstevel@tonic-gate 			continue;
13297c478bd9Sstevel@tonic-gate 		}
13307c478bd9Sstevel@tonic-gate 		if ((dfh == NULL) || (name == NULL)) {
13317c478bd9Sstevel@tonic-gate 			/* Can't add link information */
13327c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, gettext(
13337c478bd9Sstevel@tonic-gate 				"db_add: dfh %p, name %p - invalid"),
13347c478bd9Sstevel@tonic-gate 				(void *)dfh, (void *)name);
13357c478bd9Sstevel@tonic-gate 			error = EINVAL;
13367c478bd9Sstevel@tonic-gate 			continue;
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 		if (fh == &public_fh) {
13397c478bd9Sstevel@tonic-gate 			while ((fhrecp != NULL) && strcmp(name, fhrecp->name)) {
13407c478bd9Sstevel@tonic-gate 				/* Replace the public fh rather than add link */
13417c478bd9Sstevel@tonic-gate 				error = db_delete_link(fhpath, dfh,
13427c478bd9Sstevel@tonic-gate 						fhrecp->name);
13437c478bd9Sstevel@tonic-gate 				fhrecp = db_add_primary(dbp, dfh, name, fh,
13447c478bd9Sstevel@tonic-gate 						flags, &fhrec, &error);
13457c478bd9Sstevel@tonic-gate 			}
13467c478bd9Sstevel@tonic-gate 			if (fhrecp == NULL) {
13477c478bd9Sstevel@tonic-gate 				continue;
13487c478bd9Sstevel@tonic-gate 			}
13497c478bd9Sstevel@tonic-gate 		}
13507c478bd9Sstevel@tonic-gate 		error = db_add_secondary(dbp, dfh, name, fh, fhrecp);
13517c478bd9Sstevel@tonic-gate 		if (fhrecp != &fhrec) {
13527c478bd9Sstevel@tonic-gate 			free(fhrecp);
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 	return (error);
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate /*
13597c478bd9Sstevel@tonic-gate  * db_lookup - search the database for the file identified by fh.
13607c478bd9Sstevel@tonic-gate  * Return the entry in *fhrecpp if found, or NULL with error set otherwise.
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate fhlist_ent *
13637c478bd9Sstevel@tonic-gate db_lookup(char *fhpath, fhandle_t *fh, fhlist_ent *fhrecp, int *errorp)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	struct db_list	*dbp;
13667c478bd9Sstevel@tonic-gate 	fh_primary_key	fhkey;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) || (fh == NULL) || (errorp == NULL)) {
13697c478bd9Sstevel@tonic-gate 		if (errorp != NULL)
13707c478bd9Sstevel@tonic-gate 			*errorp = EINVAL;
13717c478bd9Sstevel@tonic-gate 		return (NULL);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate 	*errorp = 0;
13747c478bd9Sstevel@tonic-gate 	if (fh == &public_fh) {
13757c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, FALSE);
13767c478bd9Sstevel@tonic-gate 	} else {
13777c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &fh->fh_fsid, errorp, O_CREAT);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	if (dbp == NULL) {
13807c478bd9Sstevel@tonic-gate 		/* Could not get or create database */
13817c478bd9Sstevel@tonic-gate 		return (NULL);
13827c478bd9Sstevel@tonic-gate 	}
13837c478bd9Sstevel@tonic-gate 	bcopy(&fh->fh_data, fhkey, fh->fh_len);
13847c478bd9Sstevel@tonic-gate 	fhrecp = fetch_record(dbp, fhkey, fh->fh_len, fhrecp,
13857c478bd9Sstevel@tonic-gate 			errorp, "db_lookup");
13867c478bd9Sstevel@tonic-gate 	/* Update fhrec atime if needed */
13877c478bd9Sstevel@tonic-gate 	if (fhrecp != NULL) {
13887c478bd9Sstevel@tonic-gate 		*errorp = db_update_fhrec(dbp, fhkey, fh->fh_len, fhrecp,
13897c478bd9Sstevel@tonic-gate 				"db_lookup");
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 	return (fhrecp);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * db_lookup_link - search the database for the file identified by (dfh,name).
13967c478bd9Sstevel@tonic-gate  * If the link was found, use it to search for the primary record.
13977c478bd9Sstevel@tonic-gate  * Return 0 and set the entry in *fhrecpp if found, return error otherwise.
13987c478bd9Sstevel@tonic-gate  */
13997c478bd9Sstevel@tonic-gate fhlist_ent *
14007c478bd9Sstevel@tonic-gate db_lookup_link(char *fhpath, fhandle_t *dfh, char *name, fhlist_ent *fhrecp,
14017c478bd9Sstevel@tonic-gate 	int *errorp)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	struct db_list		*dbp;
14047c478bd9Sstevel@tonic-gate 	fh_secondary_key	linkkey;
14057c478bd9Sstevel@tonic-gate 	linkinfo_ent		*linkp;
14067c478bd9Sstevel@tonic-gate 	int			linksize, fhkeysize;
14077c478bd9Sstevel@tonic-gate 	char			*fhkey;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) || (dfh == NULL) || (name == NULL) ||
14107c478bd9Sstevel@tonic-gate 		(errorp == NULL)) {
14117c478bd9Sstevel@tonic-gate 		if (errorp != NULL)
14127c478bd9Sstevel@tonic-gate 			*errorp = EINVAL;
14137c478bd9Sstevel@tonic-gate 		return (NULL);
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 	*errorp = 0;
14167c478bd9Sstevel@tonic-gate 	if (dfh == &public_fh) {
14177c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, FALSE);
14187c478bd9Sstevel@tonic-gate 	} else {
14197c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &dfh->fh_fsid, errorp, O_CREAT);
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 	if (dbp == NULL) {
14227c478bd9Sstevel@tonic-gate 		/* Could not get or create database */
14237c478bd9Sstevel@tonic-gate 		return (NULL);
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 	/* Get the link record */
14267c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, dfh, name);
14277c478bd9Sstevel@tonic-gate 	linkp = fetch_record(dbp, linkkey, linksize, NULL, errorp,
14287c478bd9Sstevel@tonic-gate 			"db_lookup_link link");
14297c478bd9Sstevel@tonic-gate 	if (linkp != NULL) {
14307c478bd9Sstevel@tonic-gate 		/* Now use link to search for fh entry */
14317c478bd9Sstevel@tonic-gate 		fhkeysize = LN_FHKEY_LEN(linkp);
14327c478bd9Sstevel@tonic-gate 		fhkey = LN_FHKEY(linkp);
14337c478bd9Sstevel@tonic-gate 		fhrecp = fetch_record(dbp, fhkey, fhkeysize,
14347c478bd9Sstevel@tonic-gate 				(void *)fhrecp, errorp, "db_lookup_link fh");
14357c478bd9Sstevel@tonic-gate 		/* Update fhrec atime if needed */
14367c478bd9Sstevel@tonic-gate 		if (fhrecp != NULL) {
14377c478bd9Sstevel@tonic-gate 			*errorp = db_update_fhrec(dbp, fhkey, fhkeysize, fhrecp,
14387c478bd9Sstevel@tonic-gate 				"db_lookup_link fhrec");
14397c478bd9Sstevel@tonic-gate 		}
14407c478bd9Sstevel@tonic-gate 		/* Update link atime if needed */
14417c478bd9Sstevel@tonic-gate 		*errorp = db_update_linkinfo(dbp, linkkey, linksize, linkp,
14427c478bd9Sstevel@tonic-gate 			"db_lookup_link link");
14437c478bd9Sstevel@tonic-gate 		free(linkp);
14447c478bd9Sstevel@tonic-gate 	} else {
14457c478bd9Sstevel@tonic-gate 		fhrecp = NULL;
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 	return (fhrecp);
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate /*
14517c478bd9Sstevel@tonic-gate  * delete_link - delete the requested link from the database. If it's the
14527c478bd9Sstevel@tonic-gate  * last link in the database for that file then remove the primary record
14537c478bd9Sstevel@tonic-gate  * as well. *errorp contains the returned error code.
14547c478bd9Sstevel@tonic-gate  * Return ENOENT if link not in database and 0 otherwise.
14557c478bd9Sstevel@tonic-gate  */
14567c478bd9Sstevel@tonic-gate static int
14577c478bd9Sstevel@tonic-gate delete_link_by_key(struct db_list *dbp, char *linkkey, int *linksizep,
14587c478bd9Sstevel@tonic-gate 	int *errorp, char *errstr)
14597c478bd9Sstevel@tonic-gate {
14607c478bd9Sstevel@tonic-gate 	int			nextsize, prevsize, fhkeysize, linksize;
14617c478bd9Sstevel@tonic-gate 	char			*nextkey, *prevkey, *fhkey;
14627c478bd9Sstevel@tonic-gate 	linkinfo_ent		*dellinkp, *nextlinkp;
14637c478bd9Sstevel@tonic-gate 	fhlist_ent		*fhrecp, fhrec;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	*errorp = 0;
14667c478bd9Sstevel@tonic-gate 	linksize = *linksizep;
14677c478bd9Sstevel@tonic-gate 	/* Get the link record */
14687c478bd9Sstevel@tonic-gate 	dellinkp = fetch_record(dbp, linkkey, linksize, NULL, errorp, errstr);
14697c478bd9Sstevel@tonic-gate 	if (dellinkp == NULL) {
14707c478bd9Sstevel@tonic-gate 		/*
14717c478bd9Sstevel@tonic-gate 		 * Link not in database.
14727c478bd9Sstevel@tonic-gate 		 */
14737c478bd9Sstevel@tonic-gate 		if (debug > 2) {
14747c478bd9Sstevel@tonic-gate 			debug_print_key(stderr, errstr,
14757c478bd9Sstevel@tonic-gate 				"link not in database\n",
14767c478bd9Sstevel@tonic-gate 				linkkey, linksize);
14777c478bd9Sstevel@tonic-gate 		}
14787c478bd9Sstevel@tonic-gate 		*linksizep = 0;
14797c478bd9Sstevel@tonic-gate 		return (ENOENT);
14807c478bd9Sstevel@tonic-gate 	}
14817c478bd9Sstevel@tonic-gate 	/*
14827c478bd9Sstevel@tonic-gate 	 * Possibilities:
14837c478bd9Sstevel@tonic-gate 	 * 1. Normal case - only one link to delete: the link next and
14847c478bd9Sstevel@tonic-gate 	 *    prev should be NULL, and fhrec's name/dfh are same
14857c478bd9Sstevel@tonic-gate 	 *    as the link. Remove the link and fhrec.
14867c478bd9Sstevel@tonic-gate 	 * 2. Multiple hard links, and the deleted link is the head of
14877c478bd9Sstevel@tonic-gate 	 *    the list. Remove the link and replace the link key in
14887c478bd9Sstevel@tonic-gate 	 *    the primary record to point to the new head.
14897c478bd9Sstevel@tonic-gate 	 * 3. Multiple hard links, and the deleted link is not the
14907c478bd9Sstevel@tonic-gate 	 *    head of the list (not the same as in fhrec) - just
14917c478bd9Sstevel@tonic-gate 	 *    delete the link and update the previous and next records
14927c478bd9Sstevel@tonic-gate 	 *    in the links linked list.
14937c478bd9Sstevel@tonic-gate 	 */
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/* Get next and prev keys for linked list updates */
14967c478bd9Sstevel@tonic-gate 	nextsize = LN_NEXT_LEN(dellinkp);
14977c478bd9Sstevel@tonic-gate 	nextkey = ((nextsize > 0) ? LN_NEXT(dellinkp) : NULL);
14987c478bd9Sstevel@tonic-gate 	prevsize = LN_PREV_LEN(dellinkp);
14997c478bd9Sstevel@tonic-gate 	prevkey = ((prevsize > 0) ? LN_PREV(dellinkp) : NULL);
15007c478bd9Sstevel@tonic-gate 	/* Update the linked list for the file */
15017c478bd9Sstevel@tonic-gate 	nextlinkp = update_linked_list(dbp, nextkey, nextsize,
15027c478bd9Sstevel@tonic-gate 			prevkey, prevsize, errorp);
15037c478bd9Sstevel@tonic-gate 	if ((nextlinkp == NULL) && (*errorp != 0)) {
15047c478bd9Sstevel@tonic-gate 		free(dellinkp);
15057c478bd9Sstevel@tonic-gate 		*linksizep = 0;
15067c478bd9Sstevel@tonic-gate 		return (0);
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 	/* Delete link record */
15097c478bd9Sstevel@tonic-gate 	*errorp = delete_record(dbp, linkkey, linksize, errstr);
15107c478bd9Sstevel@tonic-gate 	/* Get the primary key */
15117c478bd9Sstevel@tonic-gate 	fhkeysize = LN_FHKEY_LEN(dellinkp);
15127c478bd9Sstevel@tonic-gate 	fhkey = LN_FHKEY(dellinkp);
15137c478bd9Sstevel@tonic-gate 	fhrecp = fetch_record(dbp, fhkey, fhkeysize,
15147c478bd9Sstevel@tonic-gate 		&fhrec, errorp, errstr);
15157c478bd9Sstevel@tonic-gate 	if (fhrecp == NULL) {
15167c478bd9Sstevel@tonic-gate 		/* Should never happen */
15177c478bd9Sstevel@tonic-gate 		if (debug > 1) {
15187c478bd9Sstevel@tonic-gate 			debug_print_key(stderr, errstr,
15197c478bd9Sstevel@tonic-gate 				"fetch primary for ", linkkey, linksize);
15207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, " Error %s\n",
15217c478bd9Sstevel@tonic-gate 			((*errorp >= 0) ? strerror(*errorp) : "Unknown"));
15227c478bd9Sstevel@tonic-gate 		}
15237c478bd9Sstevel@tonic-gate 	} else if ((*errorp == 0) && (prevsize <= 0)) {
15247c478bd9Sstevel@tonic-gate 		/* This is the head of the list update primary record */
15257c478bd9Sstevel@tonic-gate 		*errorp = db_update_primary_new_head(dbp, dellinkp,
15267c478bd9Sstevel@tonic-gate 				nextlinkp, fhrecp);
15277c478bd9Sstevel@tonic-gate 	} else {
15287c478bd9Sstevel@tonic-gate 		/* Update fhrec atime if needed */
15297c478bd9Sstevel@tonic-gate 		*errorp = db_update_fhrec(dbp, fhkey, fhkeysize, fhrecp,
15307c478bd9Sstevel@tonic-gate 				errstr);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	*linksizep = nextsize;
15337c478bd9Sstevel@tonic-gate 	if (nextsize > 0)
15347c478bd9Sstevel@tonic-gate 		(void) memcpy(linkkey, nextkey, nextsize);
15357c478bd9Sstevel@tonic-gate 	if (nextlinkp != NULL)
15367c478bd9Sstevel@tonic-gate 		free(nextlinkp);
15377c478bd9Sstevel@tonic-gate 	free(dellinkp);
15387c478bd9Sstevel@tonic-gate 	return (0);
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate /*
15427c478bd9Sstevel@tonic-gate  * delete_link - delete the requested link from the database. If it's the
15437c478bd9Sstevel@tonic-gate  * last link in the database for that file then remove the primary record
15447c478bd9Sstevel@tonic-gate  * as well. If nextlinkkey/sizep are non-null, copy the key and key size of
15457c478bd9Sstevel@tonic-gate  * the next link in the chain into them (this would save a dbm_fetch op).
15467c478bd9Sstevel@tonic-gate  * Return ENOENT if link not in database and 0 otherwise, with *errorp
15477c478bd9Sstevel@tonic-gate  * containing the returned error if any from the delete_link ops.
15487c478bd9Sstevel@tonic-gate  */
15497c478bd9Sstevel@tonic-gate static int
15507c478bd9Sstevel@tonic-gate delete_link(struct db_list *dbp, fhandle_t *dfh, char *name,
15517c478bd9Sstevel@tonic-gate 	char *nextlinkkey, int *nextlinksizep, int *errorp, char *errstr)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	int	linkerr;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	*errorp = 0;
15567c478bd9Sstevel@tonic-gate 	if ((nextlinkkey != NULL) && (nextlinksizep != NULL)) {
15577c478bd9Sstevel@tonic-gate 		*nextlinksizep = fill_link_key(nextlinkkey, dfh, name);
15587c478bd9Sstevel@tonic-gate 		linkerr = delete_link_by_key(dbp, nextlinkkey, nextlinksizep,
15597c478bd9Sstevel@tonic-gate 				errorp, errstr);
15607c478bd9Sstevel@tonic-gate 	} else {
15617c478bd9Sstevel@tonic-gate 		int			linksize;
15627c478bd9Sstevel@tonic-gate 		fh_secondary_key	linkkey;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		linksize = fill_link_key(linkkey, dfh, name);
15657c478bd9Sstevel@tonic-gate 		linkerr = delete_link_by_key(dbp, linkkey, &linksize,
15667c478bd9Sstevel@tonic-gate 				errorp, errstr);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 	return (linkerr);
15697c478bd9Sstevel@tonic-gate }
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate /*
15727c478bd9Sstevel@tonic-gate  * db_delete_link - search the database for the file system for link.
15737c478bd9Sstevel@tonic-gate  * Delete the link from the database. If this is the "primary" link,
15747c478bd9Sstevel@tonic-gate  * set the primary record for the next link. If it's the last one,
15757c478bd9Sstevel@tonic-gate  * delete the primary record.
15767c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
15777c478bd9Sstevel@tonic-gate  */
15787c478bd9Sstevel@tonic-gate int
15797c478bd9Sstevel@tonic-gate db_delete_link(char *fhpath, fhandle_t *dfh, char *name)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	struct db_list		*dbp;
15827c478bd9Sstevel@tonic-gate 	int			error = 0;
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) || (dfh == NULL) || (name == NULL)) {
15857c478bd9Sstevel@tonic-gate 		return (EINVAL);
15867c478bd9Sstevel@tonic-gate 	}
15877c478bd9Sstevel@tonic-gate 	if (dfh == &public_fh) {
15887c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, TRUE);
15897c478bd9Sstevel@tonic-gate 	} else {
15907c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &dfh->fh_fsid, &error, O_CREAT);
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 	for (; dbp != NULL; dbp = ((dfh == &public_fh) ? dbp->next : NULL)) {
15937c478bd9Sstevel@tonic-gate 		(void) delete_link(dbp, dfh, name, NULL, NULL, &error,
15947c478bd9Sstevel@tonic-gate 			"db_delete_link link");
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 	return (error);
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate #ifdef DEBUG
16007c478bd9Sstevel@tonic-gate /*
16017c478bd9Sstevel@tonic-gate  * db_delete - Deletes the fhrec corresponding to the fh. Use only
16027c478bd9Sstevel@tonic-gate  * for repairing the fhtable, not for normal handling.
16037c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
16047c478bd9Sstevel@tonic-gate  */
16057c478bd9Sstevel@tonic-gate int
16067c478bd9Sstevel@tonic-gate db_delete(char *fhpath, fhandle_t *fh)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	struct db_list		*dbp;
16097c478bd9Sstevel@tonic-gate 	int			error = 0;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) || (fh == NULL)) {
16127c478bd9Sstevel@tonic-gate 		return (EINVAL);
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 	if (fh == &public_fh) {
16157c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, TRUE);
16167c478bd9Sstevel@tonic-gate 	} else {
16177c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &fh->fh_fsid, &error, O_CREAT);
16187c478bd9Sstevel@tonic-gate 	}
16197c478bd9Sstevel@tonic-gate 	for (; dbp != NULL; dbp = ((fh == &public_fh) ? dbp->next : NULL)) {
16207c478bd9Sstevel@tonic-gate 		/* Get the link record */
16217c478bd9Sstevel@tonic-gate 		(void) delete_record(dbp, &fh->fh_data, fh->fh_len,
16227c478bd9Sstevel@tonic-gate 			"db_delete: fh delete");
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	return (error);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate /*
16297c478bd9Sstevel@tonic-gate  * db_rename_link - search the database for the file system for link.
16307c478bd9Sstevel@tonic-gate  * Add the new link and delete the old link from the database.
16317c478bd9Sstevel@tonic-gate  * Return 0 for success, error code otherwise.
16327c478bd9Sstevel@tonic-gate  */
16337c478bd9Sstevel@tonic-gate int
16347c478bd9Sstevel@tonic-gate db_rename_link(char *fhpath, fhandle_t *from_dfh, char *from_name,
16357c478bd9Sstevel@tonic-gate 	fhandle_t *to_dfh, char *to_name)
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate 	int			error;
16387c478bd9Sstevel@tonic-gate 	struct db_list		*dbp;
16397c478bd9Sstevel@tonic-gate 	fhlist_ent		fhrec, *fhrecp;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) || (from_dfh == NULL) || (from_name == NULL) ||
16427c478bd9Sstevel@tonic-gate 		(to_dfh == NULL) || (to_name == NULL)) {
16437c478bd9Sstevel@tonic-gate 		return (EINVAL);
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate 	if (from_dfh == &public_fh) {
16467c478bd9Sstevel@tonic-gate 		dbp = db_get_all_databases(fhpath, FALSE);
16477c478bd9Sstevel@tonic-gate 	} else {
16487c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, &from_dfh->fh_fsid, &error, O_CREAT);
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 	for (; dbp != NULL;
16517c478bd9Sstevel@tonic-gate 		dbp = ((from_dfh != &public_fh) ? NULL : dbp->next)) {
16527c478bd9Sstevel@tonic-gate 		/* find existing link */
16537c478bd9Sstevel@tonic-gate 		fhrecp = db_lookup_link(fhpath, from_dfh, from_name, &fhrec,
16547c478bd9Sstevel@tonic-gate 				&error);
16557c478bd9Sstevel@tonic-gate 		if (fhrecp == NULL) {
16567c478bd9Sstevel@tonic-gate 			/* Could not find the link */
16577c478bd9Sstevel@tonic-gate 			continue;
16587c478bd9Sstevel@tonic-gate 		}
16597c478bd9Sstevel@tonic-gate 		/* Delete the old link (if last primary record not deleted) */
16607c478bd9Sstevel@tonic-gate 		error = db_delete_link(fhpath, from_dfh, from_name);
16617c478bd9Sstevel@tonic-gate 		if (error == 0) {
16627c478bd9Sstevel@tonic-gate 			error = db_add(fhpath, to_dfh, to_name, &fhrecp->fh,
16637c478bd9Sstevel@tonic-gate 					fhrecp->flags);
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 	return (error);
16677c478bd9Sstevel@tonic-gate }
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate /*
16707c478bd9Sstevel@tonic-gate  * db_print_all_keys: prints all keys for a given filesystem. If fsidp is
16717c478bd9Sstevel@tonic-gate  * NULL, print for all filesystems covered by fhpath.
16727c478bd9Sstevel@tonic-gate  */
16737c478bd9Sstevel@tonic-gate void
16747c478bd9Sstevel@tonic-gate db_print_all_keys(char *fhpath, fsid_t *fsidp, FILE *fp)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	struct db_list	*dbp;
16777c478bd9Sstevel@tonic-gate 	datum		key;
16787c478bd9Sstevel@tonic-gate 	int		error, len;
16797c478bd9Sstevel@tonic-gate 	char		strkey[NFS_FHMAXDATA + MAXNAMELEN];
16807c478bd9Sstevel@tonic-gate 	db_record	rec;
16817c478bd9Sstevel@tonic-gate 	void		*ptr;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if ((fhpath == NULL) ||
16847c478bd9Sstevel@tonic-gate 	    ((fsidp != NULL) && (fsidp == &public_fh.fh_fsid)))
16857c478bd9Sstevel@tonic-gate 		return;
16867c478bd9Sstevel@tonic-gate 	if (fsidp == NULL) {
16877c478bd9Sstevel@tonic-gate 		(void) db_get_all_databases(fhpath, TRUE);
16887c478bd9Sstevel@tonic-gate 		dbp = db_fs_list;
16897c478bd9Sstevel@tonic-gate 	} else {
16907c478bd9Sstevel@tonic-gate 		dbp = db_get_db(fhpath, fsidp, &error, 0);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	if (dbp == NULL) {
16937c478bd9Sstevel@tonic-gate 		/* Could not get or create database */
16947c478bd9Sstevel@tonic-gate 		return;
16957c478bd9Sstevel@tonic-gate 	}
16967c478bd9Sstevel@tonic-gate 	len = strlen(fhpath);
16977c478bd9Sstevel@tonic-gate 	for (; dbp != NULL; dbp = ((fsidp != NULL) ? NULL : dbp->next)) {
16987c478bd9Sstevel@tonic-gate 		if (strncmp(fhpath, dbp->path, len))
16997c478bd9Sstevel@tonic-gate 			continue;
17007c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
17017c478bd9Sstevel@tonic-gate 			"\nStart print database for fsid 0x%x 0x%x\n",
17027c478bd9Sstevel@tonic-gate 			dbp->fsid.val[0], dbp->fsid.val[1]);
17037c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "=============================\n");
17047c478bd9Sstevel@tonic-gate 		for (key = dbm_firstkey(dbp->db); key.dptr != NULL;
17057c478bd9Sstevel@tonic-gate 			key = dbm_nextkey(dbp->db)) {
17067c478bd9Sstevel@tonic-gate 			(void) memcpy(strkey, key.dptr, key.dsize);
17077c478bd9Sstevel@tonic-gate 			debug_print_key(fp, "", "", strkey, key.dsize);
17087c478bd9Sstevel@tonic-gate 			if (debug < 2)
17097c478bd9Sstevel@tonic-gate 				continue;
17107c478bd9Sstevel@tonic-gate 			ptr = fetch_record(dbp, key.dptr, key.dsize,
17117c478bd9Sstevel@tonic-gate 					(void *)&rec, &error, "db_prt_keys");
17127c478bd9Sstevel@tonic-gate 			if (ptr == NULL)
17137c478bd9Sstevel@tonic-gate 				continue;
17147c478bd9Sstevel@tonic-gate 			if (key.dsize == NFS_FHMAXDATA) {
17157c478bd9Sstevel@tonic-gate 				/* fhrec */
17167c478bd9Sstevel@tonic-gate 				debug_print_fhlist(fp, &rec.fhlist_rec);
17177c478bd9Sstevel@tonic-gate 			} else if (key.dsize > NFS_FHMAXDATA) {
17187c478bd9Sstevel@tonic-gate 				/* linkinfo */
17197c478bd9Sstevel@tonic-gate 				debug_print_linkinfo(fp, &rec.link_rec);
17207c478bd9Sstevel@tonic-gate 			}
17217c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "-----------------------------\n");
17227c478bd9Sstevel@tonic-gate 		}
17237c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "End print database for fsid 0x%x 0x%x\n",
17247c478bd9Sstevel@tonic-gate 			dbp->fsid.val[0], dbp->fsid.val[1]);
17257c478bd9Sstevel@tonic-gate 	}
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate void
17297c478bd9Sstevel@tonic-gate debug_opaque_print(FILE *fp, void *buf, int size)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	int		bufoffset = 0;
17327c478bd9Sstevel@tonic-gate 	char		debug_str[200];
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	if ((buf == NULL) || (size <= 0))
17357c478bd9Sstevel@tonic-gate 		return;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	nfslog_opaque_print_buf(buf, size, debug_str, &bufoffset, 200);
17387c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, debug_str);
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate /*
17427c478bd9Sstevel@tonic-gate  * links_timedout() takes a primary records and searches all of its
17437c478bd9Sstevel@tonic-gate  * links to see if they all have access times that are older than
17447c478bd9Sstevel@tonic-gate  * the 'prune_timeout' value.  TRUE if all links are old and FALSE
17457c478bd9Sstevel@tonic-gate  * if there is just one link that has an access time which is recent.
17467c478bd9Sstevel@tonic-gate  */
17477c478bd9Sstevel@tonic-gate static int
17487c478bd9Sstevel@tonic-gate links_timedout(struct db_list *pdb, fhlist_ent *pfe, time_t ts)
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate 	fh_secondary_key	linkkey;
17517c478bd9Sstevel@tonic-gate 	linkinfo_ent		*linkp, link_st;
17527c478bd9Sstevel@tonic-gate 	int			error;
17537c478bd9Sstevel@tonic-gate 	int			linksize;
17547c478bd9Sstevel@tonic-gate 	void			*cookie;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/* Get the link record */
17577c478bd9Sstevel@tonic-gate 	linksize = fill_link_key(linkkey, &pfe->dfh, pfe->name);
17587c478bd9Sstevel@tonic-gate 	cookie = NULL;
17597c478bd9Sstevel@tonic-gate 	do {
17607c478bd9Sstevel@tonic-gate 		linkp = get_next_link(pdb, linkkey, &linksize, &link_st,
17617c478bd9Sstevel@tonic-gate 				&cookie, &error, "links_timedout");
17627c478bd9Sstevel@tonic-gate 		if ((linkp != NULL) &&
17637c478bd9Sstevel@tonic-gate 			(difftime(ts, linkp->atime) <= prune_timeout)) {
17647c478bd9Sstevel@tonic-gate 			/* update primary record to have an uptodate time */
17657c478bd9Sstevel@tonic-gate 			pfe = fetch_record(pdb, (void *)&pfe->fh.fh_data,
17667c478bd9Sstevel@tonic-gate 					pfe->fh.fh_len, NULL, &error,
17677c478bd9Sstevel@tonic-gate 					"links_timedout");
17687c478bd9Sstevel@tonic-gate 			if (pfe == NULL) {
17697c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, gettext(
17707c478bd9Sstevel@tonic-gate 				"links_timedout: fetch fhrec error %s\n"),
17717c478bd9Sstevel@tonic-gate 				strerror(error));
17727c478bd9Sstevel@tonic-gate 			} else {
17737c478bd9Sstevel@tonic-gate 				if (difftime(pfe->atime, linkp->atime) < 0) {
17747c478bd9Sstevel@tonic-gate 					/* update fhrec atime */
17757c478bd9Sstevel@tonic-gate 					pfe->atime = linkp->atime;
17767c478bd9Sstevel@tonic-gate 					(void) store_record(pdb,
17777c478bd9Sstevel@tonic-gate 						(void *)&pfe->fh.fh_data,
17787c478bd9Sstevel@tonic-gate 						pfe->fh.fh_len, pfe,
17797c478bd9Sstevel@tonic-gate 						pfe->reclen, "links_timedout");
17807c478bd9Sstevel@tonic-gate 				}
17817c478bd9Sstevel@tonic-gate 				free(pfe);
17827c478bd9Sstevel@tonic-gate 			}
17837c478bd9Sstevel@tonic-gate 			free_link_cookies(cookie);
17847c478bd9Sstevel@tonic-gate 			return (FALSE);
17857c478bd9Sstevel@tonic-gate 		}
17867c478bd9Sstevel@tonic-gate 	} while (linksize > 0);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	free_link_cookies(cookie);
17897c478bd9Sstevel@tonic-gate 	return (TRUE);
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate /*
17937c478bd9Sstevel@tonic-gate  * prune_dbs() will search all of the open databases looking for records
17947c478bd9Sstevel@tonic-gate  * that have not been accessed in the last 'prune_timeout' seconds.
17957c478bd9Sstevel@tonic-gate  * This search is done on the primary records and a list of potential
17967c478bd9Sstevel@tonic-gate  * timeout candidates is built.  The reason for doing this is to not
17977c478bd9Sstevel@tonic-gate  * disturb the underlying dbm_firstkey()/dbm_nextkey() sequence; we
17987c478bd9Sstevel@tonic-gate  * want to search all of the records in the database.
17997c478bd9Sstevel@tonic-gate  * Once we have our candidate list built, we examine each of those
18007c478bd9Sstevel@tonic-gate  * item's links to check if the links have been accessed within the
18017c478bd9Sstevel@tonic-gate  * 'prune_timeout' seconds.  If neither the primary nor any its links
18027c478bd9Sstevel@tonic-gate  * have been accessed, then all of those records are removed/deleted
18037c478bd9Sstevel@tonic-gate  * from the database.
18047c478bd9Sstevel@tonic-gate  */
18057c478bd9Sstevel@tonic-gate int
18067c478bd9Sstevel@tonic-gate prune_dbs(char *fhpath)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	struct db_list		*pdb;
18097c478bd9Sstevel@tonic-gate 	datum			key;
18107c478bd9Sstevel@tonic-gate 	db_record		*ptr;
18117c478bd9Sstevel@tonic-gate 	struct fhlist_ent 	*pfe;
18127c478bd9Sstevel@tonic-gate 	int			error, linkerr, linksize;
18137c478bd9Sstevel@tonic-gate 	time_t			cur_time = time(0);
18147c478bd9Sstevel@tonic-gate 	fh_secondary_key	linkkey;
18157c478bd9Sstevel@tonic-gate 	struct thelist {
18167c478bd9Sstevel@tonic-gate 		struct thelist *next;
18177c478bd9Sstevel@tonic-gate 		db_record *ptr;
18187c478bd9Sstevel@tonic-gate 	} 			thelist, *ptl;
18197c478bd9Sstevel@tonic-gate 	int	cnt = 0;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	if (fhpath != NULL)
18227c478bd9Sstevel@tonic-gate 		(void) db_get_all_databases(fhpath, TRUE);
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	thelist.next = NULL;
18257c478bd9Sstevel@tonic-gate 	/*
18267c478bd9Sstevel@tonic-gate 	 * Search each of the open databases
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	for (pdb = db_fs_list; pdb; pdb = pdb->next) {
18297c478bd9Sstevel@tonic-gate 	    do {
18307c478bd9Sstevel@tonic-gate 		/* Check each record in the database */
18317c478bd9Sstevel@tonic-gate 		for (key = dbm_firstkey(pdb->db); key.dptr != NULL;
18327c478bd9Sstevel@tonic-gate 		    key = dbm_nextkey(pdb->db)) {
18337c478bd9Sstevel@tonic-gate 			/* We're only interested in primary records */
18347c478bd9Sstevel@tonic-gate 			if (key.dsize != NFS_FHMAXDATA)
18357c478bd9Sstevel@tonic-gate 				continue;	/* probably a link record */
18367c478bd9Sstevel@tonic-gate 			ptr = fetch_record(pdb, key.dptr, key.dsize,
18377c478bd9Sstevel@tonic-gate 					NULL, &error, "dump_db");
18387c478bd9Sstevel@tonic-gate 			if (ptr == NULL)
18397c478bd9Sstevel@tonic-gate 				continue;
18407c478bd9Sstevel@tonic-gate 			/*
18417c478bd9Sstevel@tonic-gate 			 * If this record is a primary record and it is
18427c478bd9Sstevel@tonic-gate 			 * not an export point or a public file handle path,
18437c478bd9Sstevel@tonic-gate 			 * check it for a ancient access time.
18447c478bd9Sstevel@tonic-gate 			 */
18457c478bd9Sstevel@tonic-gate 			if ((ptr->fhlist_rec.flags &
18467c478bd9Sstevel@tonic-gate 				    (EXPORT_POINT | PUBLIC_PATH)) ||
18477c478bd9Sstevel@tonic-gate 			    (difftime(cur_time, ptr->fhlist_rec.atime) <=
18487c478bd9Sstevel@tonic-gate 					prune_timeout)) {
18497c478bd9Sstevel@tonic-gate 				/* Keep this record in the database */
18507c478bd9Sstevel@tonic-gate 				free(ptr);
18517c478bd9Sstevel@tonic-gate 			} else {
18527c478bd9Sstevel@tonic-gate 				/* Found one?  Save off info about it */
18537c478bd9Sstevel@tonic-gate 				ptl = malloc(sizeof (struct thelist));
18547c478bd9Sstevel@tonic-gate 				if (ptl == NULL) {
18557c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, gettext(
18567c478bd9Sstevel@tonic-gate 				"prune_dbs: malloc failed, error %s\n"),
18577c478bd9Sstevel@tonic-gate 						strerror(errno));
18587c478bd9Sstevel@tonic-gate 					break;
18597c478bd9Sstevel@tonic-gate 				}
18607c478bd9Sstevel@tonic-gate 				ptl->ptr = ptr;
18617c478bd9Sstevel@tonic-gate 				ptl->next = thelist.next;
18627c478bd9Sstevel@tonic-gate 				thelist.next = ptl;
18637c478bd9Sstevel@tonic-gate 				cnt++;	/* count how many records allocated */
18647c478bd9Sstevel@tonic-gate 				if (cnt > MAX_PRUNE_REC_CNT) {
18657c478bd9Sstevel@tonic-gate 					/* Limit number of records malloc'd */
18667c478bd9Sstevel@tonic-gate 					if (debug)
18677c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
18687c478bd9Sstevel@tonic-gate 				"prune_dbs: halt search - too many records\n");
18697c478bd9Sstevel@tonic-gate 					break;
18707c478bd9Sstevel@tonic-gate 				}
18717c478bd9Sstevel@tonic-gate 			}
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		/*
18757c478bd9Sstevel@tonic-gate 		 * Take the saved records and check their links to make
18767c478bd9Sstevel@tonic-gate 		 * sure that they have not been accessed as well.
18777c478bd9Sstevel@tonic-gate 		 */
18787c478bd9Sstevel@tonic-gate 		for (ptl = thelist.next; ptl; ptl = thelist.next) {
18797c478bd9Sstevel@tonic-gate 			thelist.next = ptl->next;
18807c478bd9Sstevel@tonic-gate 			/* Everything timed out? */
18817c478bd9Sstevel@tonic-gate 			pfe = &(ptl->ptr->fhlist_rec);
18827c478bd9Sstevel@tonic-gate 			if (links_timedout(pdb,	pfe, cur_time)) {
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 				/*
18857c478bd9Sstevel@tonic-gate 				 * Iterate until we run out of links.
18867c478bd9Sstevel@tonic-gate 				 * We have to do this since there can be
18877c478bd9Sstevel@tonic-gate 				 * multiple links to a primary record and
18887c478bd9Sstevel@tonic-gate 				 * we need to delete one at a time.
18897c478bd9Sstevel@tonic-gate 				 */
18907c478bd9Sstevel@tonic-gate 				/* Delete the link and get the next */
18917c478bd9Sstevel@tonic-gate 				linkerr = delete_link(pdb,
18927c478bd9Sstevel@tonic-gate 						&pfe->dfh, pfe->name, linkkey,
18937c478bd9Sstevel@tonic-gate 						&linksize, &error, "dump_db");
18947c478bd9Sstevel@tonic-gate 				while ((linksize > 0) && !(error || linkerr)) {
18957c478bd9Sstevel@tonic-gate 					/* Delete the link and get the next */
18967c478bd9Sstevel@tonic-gate 					linkerr = delete_link_by_key(pdb,
18977c478bd9Sstevel@tonic-gate 						linkkey, &linksize,
18987c478bd9Sstevel@tonic-gate 						&error, "dump_db");
18997c478bd9Sstevel@tonic-gate 					if (error || linkerr) {
19007c478bd9Sstevel@tonic-gate 						break;
19017c478bd9Sstevel@tonic-gate 					}
19027c478bd9Sstevel@tonic-gate 				}
19037c478bd9Sstevel@tonic-gate 				if (linkerr) {
19047c478bd9Sstevel@tonic-gate 					/* link not in database, primary is */
19057c478bd9Sstevel@tonic-gate 					/* Should never happen */
19067c478bd9Sstevel@tonic-gate 					if (debug > 1) {
19077c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
19087c478bd9Sstevel@tonic-gate 					"prune_dbs: Error primary exists ");
19097c478bd9Sstevel@tonic-gate 						debug_opaque_print(stderr,
19107c478bd9Sstevel@tonic-gate 							(void *)&pfe->fh,
19117c478bd9Sstevel@tonic-gate 							sizeof (pfe->fh));
19127c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "\n");
19137c478bd9Sstevel@tonic-gate 					}
19147c478bd9Sstevel@tonic-gate 					if (debug)
19157c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR, gettext(
19167c478bd9Sstevel@tonic-gate 					"prune_dbs: Error primary exists\n"));
19177c478bd9Sstevel@tonic-gate 					(void) delete_record(pdb,
19187c478bd9Sstevel@tonic-gate 					&pfe->fh.fh_data, pfe->fh.fh_len,
19197c478bd9Sstevel@tonic-gate 					"prune_dbs: fh delete");
19207c478bd9Sstevel@tonic-gate 				}
19217c478bd9Sstevel@tonic-gate 			}
19227c478bd9Sstevel@tonic-gate 			/* Make sure to free the pointers used in the list */
19237c478bd9Sstevel@tonic-gate 			free(ptl->ptr);
19247c478bd9Sstevel@tonic-gate 			free(ptl);
19257c478bd9Sstevel@tonic-gate 			cnt--;
19267c478bd9Sstevel@tonic-gate 		}
19277c478bd9Sstevel@tonic-gate 		thelist.next = NULL;
19287c478bd9Sstevel@tonic-gate 	    } while (key.dptr != NULL);
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 	return (0);
19317c478bd9Sstevel@tonic-gate }
1932