10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1835Sgt29601 * Common Development and Distribution License (the "License").
6*1835Sgt29601 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1835Sgt29601 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Code to maintain the runtime and on-disk filehandle mapping table for
300Sstevel@tonic-gate * nfslog.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <nfs/nfs.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <stddef.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <strings.h>
410Sstevel@tonic-gate #include <syslog.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <dirent.h>
440Sstevel@tonic-gate #include <ndbm.h>
450Sstevel@tonic-gate #include <time.h>
460Sstevel@tonic-gate #include <libintl.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <nfs/nfs.h>
490Sstevel@tonic-gate #include <nfs/nfs_log.h>
500Sstevel@tonic-gate #include "fhtab.h"
510Sstevel@tonic-gate #include "nfslogd.h"
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define ROUNDUP32(val) (((val) + 3) & ~3)
540Sstevel@tonic-gate
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * It is important that this string not match the length of the
570Sstevel@tonic-gate * file handle key length NFS_FHMAXDATA
580Sstevel@tonic-gate */
590Sstevel@tonic-gate #define DB_VERSION_STRING "NFSLOG_DB_VERSION"
600Sstevel@tonic-gate #define DB_VERSION "1"
610Sstevel@tonic-gate
620Sstevel@tonic-gate #define MAX_PRUNE_REC_CNT 100000
630Sstevel@tonic-gate
640Sstevel@tonic-gate fhandle_t public_fh = { 0 };
650Sstevel@tonic-gate
660Sstevel@tonic-gate struct db_list {
670Sstevel@tonic-gate fsid_t fsid; /* filesystem fsid */
680Sstevel@tonic-gate char *path; /* dbm filepair path */
690Sstevel@tonic-gate DBM *db; /* open dbm database */
700Sstevel@tonic-gate bool_t getall; /* TRUE if all dbm for prefix open */
710Sstevel@tonic-gate struct db_list *next; /* next db */
720Sstevel@tonic-gate };
730Sstevel@tonic-gate
740Sstevel@tonic-gate static struct db_list *db_fs_list = NULL;
75*1835Sgt29601 static char err_str[] = "DB I/O error has occurred";
760Sstevel@tonic-gate struct link_keys {
770Sstevel@tonic-gate fh_secondary_key lnkey;
780Sstevel@tonic-gate int lnsize;
790Sstevel@tonic-gate struct link_keys *next;
800Sstevel@tonic-gate };
810Sstevel@tonic-gate extern int debug;
820Sstevel@tonic-gate extern time_t mapping_update_interval;
830Sstevel@tonic-gate extern time_t prune_timeout;
840Sstevel@tonic-gate
850Sstevel@tonic-gate static int fill_link_key(char *linkkey, fhandle_t *dfh, char *name);
860Sstevel@tonic-gate static struct db_list *db_get_db(char *fhpath, fsid_t *fsid, int *errorp,
870Sstevel@tonic-gate int create_flag);
880Sstevel@tonic-gate static struct db_list *db_get_all_databases(char *fhpath, bool_t getall);
890Sstevel@tonic-gate static void debug_print_fhlist(FILE *fp, fhlist_ent *fhrecp);
900Sstevel@tonic-gate static void debug_print_linkinfo(FILE *fp, linkinfo_ent *fhrecp);
910Sstevel@tonic-gate static void debug_print_key(FILE *fp, char *str1, char *str2, char *key,
920Sstevel@tonic-gate int ksize);
930Sstevel@tonic-gate static void debug_print_key_and_data(FILE *fp, char *str1, char *str2,
940Sstevel@tonic-gate char *key, int ksize, char *data, int dsize);
950Sstevel@tonic-gate static int store_record(struct db_list *dbp, void *keyaddr, int keysize,
960Sstevel@tonic-gate void *dataaddr, int datasize, char *str);
970Sstevel@tonic-gate static void *fetch_record(struct db_list *dbp, void *keyaddr, int keysize,
980Sstevel@tonic-gate void *dataaddr, int *errorp, char *str);
990Sstevel@tonic-gate static int delete_record(struct db_list *dbp, void *keyaddr, int keysize,
1000Sstevel@tonic-gate char *str);
1010Sstevel@tonic-gate static int db_update_fhrec(struct db_list *dbp, void *keyaddr, int keysize,
1020Sstevel@tonic-gate fhlist_ent *fhrecp, char *str);
1030Sstevel@tonic-gate static int db_update_linkinfo(struct db_list *dbp, void *keyaddr, int keysize,
1040Sstevel@tonic-gate linkinfo_ent *linkp, char *str);
1050Sstevel@tonic-gate static fhlist_ent *create_primary_struct(struct db_list *dbp, fhandle_t *dfh,
1060Sstevel@tonic-gate char *name, fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp,
1070Sstevel@tonic-gate int *errorp);
1080Sstevel@tonic-gate static fhlist_ent *db_add_primary(struct db_list *dbp, fhandle_t *dfh,
1090Sstevel@tonic-gate char *name, fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp,
1100Sstevel@tonic-gate int *errorp);
1110Sstevel@tonic-gate static linkinfo_ent *get_next_link(struct db_list *dbp, char *linkkey,
1120Sstevel@tonic-gate int *linksizep, linkinfo_ent *linkp, void **cookiep,
1130Sstevel@tonic-gate int *errorp, char *msg);
1140Sstevel@tonic-gate static void free_link_cookies(void *cookie);
1150Sstevel@tonic-gate static void add_mc_path(struct db_list *dbp, fhandle_t *dfh, char *name,
1160Sstevel@tonic-gate fhlist_ent *fhrecp, linkinfo_ent *linkp, int *errorp);
1170Sstevel@tonic-gate static linkinfo_ent *create_link_struct(struct db_list *dbp, fhandle_t *dfh,
1180Sstevel@tonic-gate char *name, fhlist_ent *fhrecp, int *errorp);
1190Sstevel@tonic-gate static int db_add_secondary(struct db_list *dbp, fhandle_t *dfh, char *name,
1200Sstevel@tonic-gate fhandle_t *fh, fhlist_ent *fhrecp);
1210Sstevel@tonic-gate static linkinfo_ent *update_next_link(struct db_list *dbp, char *nextkey,
1220Sstevel@tonic-gate int nextsize, char *prevkey, int prevsize, int *errorp);
1230Sstevel@tonic-gate static int update_prev_link(struct db_list *dbp, char *nextkey, int nextsize,
1240Sstevel@tonic-gate char *prevkey, int prevsize);
1250Sstevel@tonic-gate static linkinfo_ent *update_linked_list(struct db_list *dbp, char *nextkey,
1260Sstevel@tonic-gate int nextsize, char *prevkey, int prevsize, int *errorp);
1270Sstevel@tonic-gate static int db_update_primary_new_head(struct db_list *dbp,
1280Sstevel@tonic-gate linkinfo_ent *dellinkp, linkinfo_ent *nextlinkp, fhlist_ent *fhrecp);
1290Sstevel@tonic-gate static int delete_link_by_key(struct db_list *dbp, char *linkkey,
1300Sstevel@tonic-gate int *linksizep, int *errorp, char *errstr);
1310Sstevel@tonic-gate static int delete_link(struct db_list *dbp, fhandle_t *dfh, char *name,
1320Sstevel@tonic-gate char *nextlinkkey, int *nextlinksizep, int *errorp, char *errstr);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * The following functions do the actual database I/O. Currently use DBM.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * The "db_*" functions are functions that access the database using
1400Sstevel@tonic-gate * database-specific calls. Currently the only database supported is
1410Sstevel@tonic-gate * dbm. Because of the limitations of this database, in particular when
1420Sstevel@tonic-gate * it comes to manipulating records with the same key, or using multiple keys,
1430Sstevel@tonic-gate * the following design decisions have been made:
1440Sstevel@tonic-gate *
1450Sstevel@tonic-gate * Each file system has a separate dbm file, which are kept open as
1460Sstevel@tonic-gate * accessed, listed in a linked list.
1470Sstevel@tonic-gate * Two possible access mode are available for each file - either by
1480Sstevel@tonic-gate * file handle, or by directory file handle and name. Since
1490Sstevel@tonic-gate * dbm does not allow multiple keys, we will have a primary
1500Sstevel@tonic-gate * and secondary key for each file/link.
1510Sstevel@tonic-gate * The primary key is the pair (inode,gen) which can be obtained
1520Sstevel@tonic-gate * from the file handle. This points to a record with
1530Sstevel@tonic-gate * the full file handle and the secondary key (dfh-key,name)
1540Sstevel@tonic-gate * for one of the links.
1550Sstevel@tonic-gate * The secondary key is the pair (dfh-key,name) where dfh-key is
1560Sstevel@tonic-gate * the primary key for the directory and the name is the
1570Sstevel@tonic-gate * link name. It points to a record that contains the primary
1580Sstevel@tonic-gate * key for the file and to the previous and next hard link
1590Sstevel@tonic-gate * found for this file (if they exist).
1600Sstevel@tonic-gate *
1610Sstevel@tonic-gate * Summary of operations:
1620Sstevel@tonic-gate * Adding a new file: Create the primary record and secondary (link)
1630Sstevel@tonic-gate * record and add both to the database. The link record
1640Sstevel@tonic-gate * would have prev and next links set to NULL.
1650Sstevel@tonic-gate *
1660Sstevel@tonic-gate * Adding a link to a file in the database: Add the link record,
1670Sstevel@tonic-gate * to the head of the links list (i.e. prev = NULL, next =
1680Sstevel@tonic-gate * secondary key recorded in the primary record). Update
1690Sstevel@tonic-gate * the primary record to point to the new link, and the
1700Sstevel@tonic-gate * secondary record for the old head of list to point to new.
1710Sstevel@tonic-gate *
1720Sstevel@tonic-gate * Deleting a file: Delete the link record. If it is the last link
1730Sstevel@tonic-gate * then mark the primary record as deleted but don't delete
1740Sstevel@tonic-gate * that one from the database (in case some clients still
1750Sstevel@tonic-gate * hold the file handle). If there are other links, and the
1760Sstevel@tonic-gate * deleted link is the head of the list (in the primary
1770Sstevel@tonic-gate * record), update the primary record with the new head.
1780Sstevel@tonic-gate *
1790Sstevel@tonic-gate * Renaming a file: Add the new link and then delete the old one.
1800Sstevel@tonic-gate *
1810Sstevel@tonic-gate * Lookup by file handle (read, write, lookup, etc.) - fetch primary rec.
1820Sstevel@tonic-gate * Lookup by dir info (delete, link, rename) - fetch secondary rec.
1830Sstevel@tonic-gate *
1840Sstevel@tonic-gate * XXX NOTE: The code is written single-threaded. To make it multi-
1850Sstevel@tonic-gate * threaded, the following considerations must be made:
1860Sstevel@tonic-gate * 1. Changes/access to the db list must be atomic.
1870Sstevel@tonic-gate * 2. Changes/access for a specific file handle must be atomic
1880Sstevel@tonic-gate * (example: deleting a link may affect up to 4 separate database
1890Sstevel@tonic-gate * entries: the deleted link, the prev and next links if exist,
1900Sstevel@tonic-gate * and the filehandle entry, if it points to the deleted link -
1910Sstevel@tonic-gate * these changes must be atomic).
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate * Create a link key given directory fh and name
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate static int
fill_link_key(char * linkkey,fhandle_t * dfh,char * name)1980Sstevel@tonic-gate fill_link_key(char *linkkey, fhandle_t *dfh, char *name)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate int linksize, linksize32;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate (void) memcpy(linkkey, &dfh->fh_data, dfh->fh_len);
2030Sstevel@tonic-gate (void) strcpy(&linkkey[dfh->fh_len], name);
2040Sstevel@tonic-gate linksize = dfh->fh_len + strlen(name) + 1;
2050Sstevel@tonic-gate linksize32 = ROUNDUP32(linksize);
2060Sstevel@tonic-gate if (linksize32 > linksize)
2070Sstevel@tonic-gate bzero(&linkkey[linksize], linksize32 - linksize);
2080Sstevel@tonic-gate return (linksize32);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * db_get_db - gets the database for the filesystem, or creates one
2130Sstevel@tonic-gate * if none exists. Return the pointer for the database in *dbpp if success.
2140Sstevel@tonic-gate * Return 0 for success, error code otherwise.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate static struct db_list *
db_get_db(char * fhpath,fsid_t * fsid,int * errorp,int create_flag)2170Sstevel@tonic-gate db_get_db(char *fhpath, fsid_t *fsid, int *errorp, int create_flag)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate struct db_list *p, *newp;
2200Sstevel@tonic-gate char fsidstr[30];
2210Sstevel@tonic-gate datum key, data;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate *errorp = 0;
2240Sstevel@tonic-gate for (p = db_fs_list;
2250Sstevel@tonic-gate (p != NULL) && memcmp(&p->fsid, fsid, sizeof (*fsid));
2260Sstevel@tonic-gate p = p->next);
2270Sstevel@tonic-gate if (p != NULL) {
2280Sstevel@tonic-gate /* Found it */
2290Sstevel@tonic-gate return (p);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate /* Create it */
2320Sstevel@tonic-gate if ((newp = calloc(1, sizeof (*newp))) == NULL) {
2330Sstevel@tonic-gate *errorp = errno;
2340Sstevel@tonic-gate syslog(LOG_ERR, gettext(
2350Sstevel@tonic-gate "db_get_db: malloc db failed: Error %s"),
2360Sstevel@tonic-gate strerror(*errorp));
2370Sstevel@tonic-gate return (NULL);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate (void) sprintf(fsidstr, "%08x%08x", fsid->val[0], fsid->val[1]);
2400Sstevel@tonic-gate if ((newp->path = malloc(strlen(fhpath) + 2 + strlen(fsidstr)))
2410Sstevel@tonic-gate == NULL) {
2420Sstevel@tonic-gate *errorp = errno;
2430Sstevel@tonic-gate syslog(LOG_ERR, gettext(
2440Sstevel@tonic-gate "db_get_db: malloc dbpath failed: Error %s"),
2450Sstevel@tonic-gate strerror(*errorp));
2460Sstevel@tonic-gate goto err_exit;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate (void) sprintf(newp->path, "%s.%s", fhpath, fsidstr);
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * The open mode is masked by UMASK.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate if ((newp->db = dbm_open(newp->path, create_flag | O_RDWR, 0666))
2530Sstevel@tonic-gate == NULL) {
2540Sstevel@tonic-gate *errorp = errno;
2550Sstevel@tonic-gate syslog(LOG_ERR, gettext(
2560Sstevel@tonic-gate "db_get_db: dbm_open db '%s' failed: Error %s"),
2570Sstevel@tonic-gate newp->path, strerror(*errorp));
2580Sstevel@tonic-gate if (*errorp == 0) /* should not happen but may */
2590Sstevel@tonic-gate *errorp = -1;
2600Sstevel@tonic-gate goto err_exit;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate * Add the version identifier (have to check first in the
2640Sstevel@tonic-gate * case the db exists)
2650Sstevel@tonic-gate */
2660Sstevel@tonic-gate key.dptr = DB_VERSION_STRING;
2670Sstevel@tonic-gate key.dsize = strlen(DB_VERSION_STRING);
2680Sstevel@tonic-gate data = dbm_fetch(newp->db, key);
2690Sstevel@tonic-gate if (data.dptr == NULL) {
2700Sstevel@tonic-gate data.dptr = DB_VERSION;
2710Sstevel@tonic-gate data.dsize = strlen(DB_VERSION);
2720Sstevel@tonic-gate (void) dbm_store(newp->db, key, data, DBM_INSERT);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate (void) memcpy(&newp->fsid, fsid, sizeof (*fsid));
2760Sstevel@tonic-gate newp->next = db_fs_list;
2770Sstevel@tonic-gate db_fs_list = newp;
2780Sstevel@tonic-gate if (debug > 1) {
2790Sstevel@tonic-gate (void) printf("db_get_db: db %s opened\n", newp->path);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate return (newp);
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate err_exit:
2840Sstevel@tonic-gate if (newp != NULL) {
2850Sstevel@tonic-gate if (newp->db != NULL) {
2860Sstevel@tonic-gate dbm_close(newp->db);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate if (newp->path != NULL) {
2890Sstevel@tonic-gate free(newp->path);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate free(newp);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate return (NULL);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate * db_get_all_databases - gets the database for any filesystem. This is used
2980Sstevel@tonic-gate * when any database will do - typically to retrieve the path for the
2990Sstevel@tonic-gate * public filesystem. If any database is open - return the first one,
3000Sstevel@tonic-gate * otherwise, search for it using fhpath. If getall is TRUE, open all
3010Sstevel@tonic-gate * matching databases, and mark them (to indicate that all such were opened).
3020Sstevel@tonic-gate * Return the pointer for a matching database if success.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate static struct db_list *
db_get_all_databases(char * fhpath,bool_t getall)3050Sstevel@tonic-gate db_get_all_databases(char *fhpath, bool_t getall)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate char *dirptr, *fhdir, *fhpathname;
3080Sstevel@tonic-gate int len, error;
3090Sstevel@tonic-gate DIR *dirp;
3100Sstevel@tonic-gate struct dirent *dp;
3110Sstevel@tonic-gate fsid_t fsid;
3120Sstevel@tonic-gate struct db_list *dbp, *ret_dbp;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate for (dbp = db_fs_list; dbp != NULL; dbp = dbp->next) {
3150Sstevel@tonic-gate if (strncmp(fhpath, dbp->path, strlen(fhpath)) == 0)
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate if (dbp != NULL) {
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate * if one database for that prefix is open, and either only
3210Sstevel@tonic-gate * one is needed, or already opened all such databases,
3220Sstevel@tonic-gate * return here without exhaustive search
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate if (!getall || dbp->getall)
3250Sstevel@tonic-gate return (dbp);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate if ((fhdir = strdup(fhpath)) == NULL) {
3280Sstevel@tonic-gate syslog(LOG_ERR, gettext(
3290Sstevel@tonic-gate "db_get_all_databases: strdup '%s' Error '%s*'"),
3300Sstevel@tonic-gate fhpath, strerror(errno));
3310Sstevel@tonic-gate return (NULL);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate fhpathname = NULL;
3340Sstevel@tonic-gate ret_dbp = NULL;
3350Sstevel@tonic-gate if ((dirptr = strrchr(fhdir, '/')) == NULL) {
3360Sstevel@tonic-gate /* no directory */
3370Sstevel@tonic-gate goto exit;
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate if ((fhpathname = strdup(&dirptr[1])) == NULL) {
3400Sstevel@tonic-gate syslog(LOG_ERR, gettext(
3410Sstevel@tonic-gate "db_get_all_databases: strdup '%s' Error '%s*'"),
3420Sstevel@tonic-gate &dirptr[1], strerror(errno));
3430Sstevel@tonic-gate goto exit;
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate /* Terminate fhdir string at last '/' */
3460Sstevel@tonic-gate dirptr[1] = '\0';
3470Sstevel@tonic-gate /* Search the directory */
3480Sstevel@tonic-gate if (debug > 2) {
3490Sstevel@tonic-gate (void) printf("db_get_all_databases: search '%s' for '%s*'\n",
3500Sstevel@tonic-gate fhdir, fhpathname);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate if ((dirp = opendir(fhdir)) == NULL) {
3530Sstevel@tonic-gate syslog(LOG_ERR, gettext(
3540Sstevel@tonic-gate "db_get_all_databases: opendir '%s' Error '%s*'"),
3550Sstevel@tonic-gate fhdir, strerror(errno));
3560Sstevel@tonic-gate goto exit;
3570Sstevel@tonic-gate }
3580Sstevel@tonic-gate len = strlen(fhpathname);
3590Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
3600Sstevel@tonic-gate if (strncmp(fhpathname, dp->d_name, len) == 0) {
3610Sstevel@tonic-gate dirptr = &dp->d_name[len + 1];
3620Sstevel@tonic-gate if (*(dirptr - 1) != '.') {
3630Sstevel@tonic-gate continue;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate (void) sscanf(dirptr, "%08lx%08lx",
3660Sstevel@tonic-gate (ulong_t *)&fsid.val[0], (ulong_t *)&fsid.val[1]);
3670Sstevel@tonic-gate dbp = db_get_db(fhpath, &fsid, &error, 0);
3680Sstevel@tonic-gate if (dbp != NULL) {
3690Sstevel@tonic-gate ret_dbp = dbp;
3700Sstevel@tonic-gate if (!getall)
3710Sstevel@tonic-gate break;
3720Sstevel@tonic-gate dbp->getall = TRUE;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate (void) closedir(dirp);
3770Sstevel@tonic-gate exit:
3780Sstevel@tonic-gate if (fhpathname != NULL)
3790Sstevel@tonic-gate free(fhpathname);
3800Sstevel@tonic-gate if (fhdir != NULL)
3810Sstevel@tonic-gate free(fhdir);
3820Sstevel@tonic-gate return (ret_dbp);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate static void
debug_print_key(FILE * fp,char * str1,char * str2,char * key,int ksize)3860Sstevel@tonic-gate debug_print_key(FILE *fp, char *str1, char *str2, char *key, int ksize)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate (void) fprintf(fp, "%s: %s key (%d) ", str1, str2, ksize);
3890Sstevel@tonic-gate debug_opaque_print(fp, key, ksize);
3900Sstevel@tonic-gate /* may be inode,name - try to print the fields */
3910Sstevel@tonic-gate if (ksize >= NFS_FHMAXDATA) {
3920Sstevel@tonic-gate (void) fprintf(fp, ": inode ");
3930Sstevel@tonic-gate debug_opaque_print(fp, &key[2], sizeof (int));
3940Sstevel@tonic-gate (void) fprintf(fp, ", gen ");
3950Sstevel@tonic-gate debug_opaque_print(fp, &key[2 + sizeof (int)], sizeof (int));
3960Sstevel@tonic-gate if (ksize > NFS_FHMAXDATA) {
3970Sstevel@tonic-gate (void) fprintf(fp, ", name '%s'", &key[NFS_FHMAXDATA]);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate (void) fprintf(fp, "\n");
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate static void
debug_print_linkinfo(FILE * fp,linkinfo_ent * linkp)4040Sstevel@tonic-gate debug_print_linkinfo(FILE *fp, linkinfo_ent *linkp)
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate if (linkp == NULL)
4070Sstevel@tonic-gate return;
4080Sstevel@tonic-gate (void) fprintf(fp, "linkinfo:\ndfh: ");
4090Sstevel@tonic-gate debug_opaque_print(fp, (void *)&linkp->dfh, sizeof (linkp->dfh));
4100Sstevel@tonic-gate (void) fprintf(fp, "\nname: '%s'", LN_NAME(linkp));
4110Sstevel@tonic-gate (void) fprintf(fp, "\nmtime 0x%x, atime 0x%x, flags 0x%x, reclen %d\n",
4120Sstevel@tonic-gate linkp->mtime, linkp->atime, linkp->flags, linkp->reclen);
4130Sstevel@tonic-gate (void) fprintf(fp, "offsets: fhkey %d, name %d, next %d, prev %d\n",
4140Sstevel@tonic-gate linkp->fhkey_offset, linkp->name_offset, linkp->next_offset,
4150Sstevel@tonic-gate linkp->prev_offset);
4160Sstevel@tonic-gate debug_print_key(fp, "fhkey", "", LN_FHKEY(linkp), LN_FHKEY_LEN(linkp));
4170Sstevel@tonic-gate debug_print_key(fp, "next", "", LN_NEXT(linkp), LN_NEXT_LEN(linkp));
4180Sstevel@tonic-gate debug_print_key(fp, "prev", "", LN_PREV(linkp), LN_PREV_LEN(linkp));
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate static void
debug_print_fhlist(FILE * fp,fhlist_ent * fhrecp)4220Sstevel@tonic-gate debug_print_fhlist(FILE *fp, fhlist_ent *fhrecp)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate if (fhrecp == NULL)
4250Sstevel@tonic-gate return;
4260Sstevel@tonic-gate (void) fprintf(fp, "fhrec:\nfh: ");
4270Sstevel@tonic-gate debug_opaque_print(fp, (void *)&fhrecp->fh, sizeof (fhrecp->fh));
4280Sstevel@tonic-gate (void) fprintf(fp, "name '%s', dfh: ", fhrecp->name);
4290Sstevel@tonic-gate debug_opaque_print(fp, (void *)&fhrecp->dfh, sizeof (fhrecp->dfh));
4300Sstevel@tonic-gate (void) fprintf(fp, "\nmtime 0x%x, atime 0x%x, flags 0x%x, reclen %d\n",
4310Sstevel@tonic-gate fhrecp->mtime, fhrecp->atime, fhrecp->flags, fhrecp->reclen);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate static void
debug_print_key_and_data(FILE * fp,char * str1,char * str2,char * key,int ksize,char * data,int dsize)4350Sstevel@tonic-gate debug_print_key_and_data(FILE *fp, char *str1, char *str2, char *key,
4360Sstevel@tonic-gate int ksize, char *data, int dsize)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate debug_print_key(fp, str1, str2, key, ksize);
4390Sstevel@tonic-gate (void) fprintf(fp, " ==> (%p,%d)\n", (void *)data, dsize);
4400Sstevel@tonic-gate if (ksize > NFS_FHMAXDATA) {
4410Sstevel@tonic-gate linkinfo_ent inf;
4420Sstevel@tonic-gate /* probably a link struct */
4430Sstevel@tonic-gate (void) memcpy(&inf, data, sizeof (linkinfo_ent));
4440Sstevel@tonic-gate debug_print_linkinfo(fp, &inf);
4450Sstevel@tonic-gate } else if (ksize == NFS_FHMAXDATA) {
4460Sstevel@tonic-gate fhlist_ent inf;
4470Sstevel@tonic-gate /* probably an fhlist struct */
4480Sstevel@tonic-gate (void) memcpy(&inf, data, sizeof (linkinfo_ent));
4490Sstevel@tonic-gate debug_print_fhlist(fp, &inf);
4500Sstevel@tonic-gate } else {
4510Sstevel@tonic-gate /* don't know... */
4520Sstevel@tonic-gate debug_opaque_print(fp, data, dsize);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate * store_record - store the record in the database and return 0 for success
4580Sstevel@tonic-gate * or error code otherwise.
4590Sstevel@tonic-gate */
4600Sstevel@tonic-gate static int
store_record(struct db_list * dbp,void * keyaddr,int keysize,void * dataaddr,int datasize,char * str)4610Sstevel@tonic-gate store_record(struct db_list *dbp, void *keyaddr, int keysize, void *dataaddr,
4620Sstevel@tonic-gate int datasize, char *str)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate datum key, data;
4650Sstevel@tonic-gate int error;
4660Sstevel@tonic-gate char *errfmt = "store_record: dbm_store failed, Error: %s\n";
4670Sstevel@tonic-gate char *err;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate errno = 0;
4700Sstevel@tonic-gate key.dptr = keyaddr;
4710Sstevel@tonic-gate key.dsize = keysize;
4720Sstevel@tonic-gate data.dptr = dataaddr;
4730Sstevel@tonic-gate data.dsize = datasize;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (debug > 2) {
4760Sstevel@tonic-gate debug_print_key_and_data(stdout, str, "dbm_store:\n ",
4770Sstevel@tonic-gate key.dptr, key.dsize, data.dptr, data.dsize);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate if (dbm_store(dbp->db, key, data, DBM_REPLACE) < 0) {
4800Sstevel@tonic-gate /* Could not store */
4810Sstevel@tonic-gate error = dbm_error(dbp->db);
4820Sstevel@tonic-gate dbm_clearerr(dbp->db);
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate if (error) {
4850Sstevel@tonic-gate if (errno)
4860Sstevel@tonic-gate err = strerror(errno);
4870Sstevel@tonic-gate else {
4880Sstevel@tonic-gate err = err_str;
4890Sstevel@tonic-gate errno = EIO;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate } else { /* should not happen but sometimes does */
4920Sstevel@tonic-gate err = err_str;
4930Sstevel@tonic-gate errno = -1;
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate if (debug) {
4960Sstevel@tonic-gate debug_print_key(stderr, str, "store_record:"
4970Sstevel@tonic-gate "dbm_store:\n", key.dptr, key.dsize);
4980Sstevel@tonic-gate (void) fprintf(stderr, errfmt, err);
4990Sstevel@tonic-gate } else
5000Sstevel@tonic-gate syslog(LOG_ERR, gettext(errfmt), err);
5010Sstevel@tonic-gate return (errno);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate return (0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate * fetch_record - fetch the record from the database and return 0 for success
5080Sstevel@tonic-gate * and errno for failure.
5090Sstevel@tonic-gate * dataaddr is an optional valid address for the result. If dataaddr
5100Sstevel@tonic-gate * is non-null, then that memory is already alloc'd. Else, alloc it, and
5110Sstevel@tonic-gate * the caller must free the returned struct when done.
5120Sstevel@tonic-gate */
5130Sstevel@tonic-gate static void *
fetch_record(struct db_list * dbp,void * keyaddr,int keysize,void * dataaddr,int * errorp,char * str)5140Sstevel@tonic-gate fetch_record(struct db_list *dbp, void *keyaddr, int keysize, void *dataaddr,
5150Sstevel@tonic-gate int *errorp, char *str)
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate datum key, data;
5180Sstevel@tonic-gate char *errfmt = "fetch_record: dbm_fetch failed, Error: %s\n";
5190Sstevel@tonic-gate char *err;
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate errno = 0;
5220Sstevel@tonic-gate *errorp = 0;
5230Sstevel@tonic-gate key.dptr = keyaddr;
5240Sstevel@tonic-gate key.dsize = keysize;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate data = dbm_fetch(dbp->db, key);
5270Sstevel@tonic-gate if (data.dptr == NULL) {
528*1835Sgt29601 /* see if there is a database error */
529*1835Sgt29601 if (dbm_error(dbp->db)) {
530*1835Sgt29601 /* clear and report the database error */
531*1835Sgt29601 dbm_clearerr(dbp->db);
532*1835Sgt29601 *errorp = EIO;
533*1835Sgt29601 err = strerror(*errorp);
534*1835Sgt29601 syslog(LOG_ERR, gettext(errfmt), err);
535*1835Sgt29601 } else {
536*1835Sgt29601 /* primary record not in database */
537*1835Sgt29601 *errorp = ENOENT;
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate if (debug > 3) {
540*1835Sgt29601 err = strerror(*errorp);
5410Sstevel@tonic-gate debug_print_key(stderr, str, "fetch_record:"
5420Sstevel@tonic-gate "dbm_fetch:\n", key.dptr, key.dsize);
5430Sstevel@tonic-gate (void) fprintf(stderr, errfmt, err);
544*1835Sgt29601 }
5450Sstevel@tonic-gate return (NULL);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate /* copy to local struct because dbm may return non-aligned pointers */
5490Sstevel@tonic-gate if ((dataaddr == NULL) &&
5500Sstevel@tonic-gate ((dataaddr = malloc(data.dsize)) == NULL)) {
5510Sstevel@tonic-gate *errorp = errno;
5520Sstevel@tonic-gate syslog(LOG_ERR, gettext(
5530Sstevel@tonic-gate "%s: dbm_fetch - malloc %ld: Error %s"),
5540Sstevel@tonic-gate str, data.dsize, strerror(*errorp));
5550Sstevel@tonic-gate return (NULL);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate (void) memcpy(dataaddr, data.dptr, data.dsize);
5580Sstevel@tonic-gate if (debug > 3) {
5590Sstevel@tonic-gate debug_print_key_and_data(stdout, str, "fetch_record:"
5600Sstevel@tonic-gate "dbm_fetch:\n", key.dptr, key.dsize,
5610Sstevel@tonic-gate dataaddr, data.dsize);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate *errorp = 0;
5640Sstevel@tonic-gate return (dataaddr);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate * delete_record - delete the record from the database and return 0 for success
5690Sstevel@tonic-gate * or error code for failure.
5700Sstevel@tonic-gate */
5710Sstevel@tonic-gate static int
delete_record(struct db_list * dbp,void * keyaddr,int keysize,char * str)5720Sstevel@tonic-gate delete_record(struct db_list *dbp, void *keyaddr, int keysize, char *str)
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate datum key;
5750Sstevel@tonic-gate int error = 0;
5760Sstevel@tonic-gate char *errfmt = "delete_record: dbm_delete failed, Error: %s\n";
5770Sstevel@tonic-gate char *err;
5780Sstevel@tonic-gate
5790Sstevel@tonic-gate errno = 0;
5800Sstevel@tonic-gate key.dptr = keyaddr;
5810Sstevel@tonic-gate key.dsize = keysize;
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate if (debug > 2) {
5840Sstevel@tonic-gate debug_print_key(stdout, str, "delete_record:"
5850Sstevel@tonic-gate "dbm_delete:\n", key.dptr, key.dsize);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate if (dbm_delete(dbp->db, key) < 0) {
5880Sstevel@tonic-gate error = dbm_error(dbp->db);
5890Sstevel@tonic-gate dbm_clearerr(dbp->db);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate if (error) {
5920Sstevel@tonic-gate if (errno)
5930Sstevel@tonic-gate err = strerror(errno);
5940Sstevel@tonic-gate else {
5950Sstevel@tonic-gate err = err_str;
5960Sstevel@tonic-gate errno = EIO;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate } else { /* should not happen but sometimes does */
5990Sstevel@tonic-gate err = err_str;
6000Sstevel@tonic-gate errno = -1;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate if (debug) {
6030Sstevel@tonic-gate debug_print_key(stderr, str, "delete_record:"
6040Sstevel@tonic-gate "dbm_delete:\n", key.dptr, key.dsize);
6050Sstevel@tonic-gate (void) fprintf(stderr, errfmt, err);
6060Sstevel@tonic-gate } else
6070Sstevel@tonic-gate syslog(LOG_ERR, gettext(errfmt), err);
6080Sstevel@tonic-gate }
6090Sstevel@tonic-gate return (errno);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate * db_update_fhrec - puts fhrec in db with updated atime if more than
6140Sstevel@tonic-gate * mapping_update_interval seconds passed. Return 0 if success, error otherwise.
6150Sstevel@tonic-gate */
6160Sstevel@tonic-gate static int
db_update_fhrec(struct db_list * dbp,void * keyaddr,int keysize,fhlist_ent * fhrecp,char * str)6170Sstevel@tonic-gate db_update_fhrec(struct db_list *dbp, void *keyaddr, int keysize,
6180Sstevel@tonic-gate fhlist_ent *fhrecp, char *str)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate time_t cur_time = time(0);
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate if (difftime(cur_time, fhrecp->atime) >= mapping_update_interval) {
6230Sstevel@tonic-gate fhrecp->atime = cur_time;
6240Sstevel@tonic-gate return (store_record(dbp, keyaddr, keysize,
6250Sstevel@tonic-gate fhrecp, fhrecp->reclen, str));
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate return (0);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate * db_update_linkinfo - puts linkinfo in db with updated atime if more than
6320Sstevel@tonic-gate * mapping_update_interval seconds passed. Return 0 if success, error otherwise.
6330Sstevel@tonic-gate */
6340Sstevel@tonic-gate static int
db_update_linkinfo(struct db_list * dbp,void * keyaddr,int keysize,linkinfo_ent * linkp,char * str)6350Sstevel@tonic-gate db_update_linkinfo(struct db_list *dbp, void *keyaddr, int keysize,
6360Sstevel@tonic-gate linkinfo_ent *linkp, char *str)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate time_t cur_time = time(0);
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate if (difftime(cur_time, linkp->atime) >= mapping_update_interval) {
6410Sstevel@tonic-gate linkp->atime = cur_time;
6420Sstevel@tonic-gate return (store_record(dbp, keyaddr, keysize,
6430Sstevel@tonic-gate linkp, linkp->reclen, str));
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate return (0);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate * create_primary_struct - add primary record to the database.
6500Sstevel@tonic-gate * Database must be open when this function is called.
6510Sstevel@tonic-gate * If success, return the added database entry. fhrecp may be used to
6520Sstevel@tonic-gate * provide an existing memory area, else malloc it. If failed, *errorp
6530Sstevel@tonic-gate * contains the error code and return NULL.
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate static fhlist_ent *
create_primary_struct(struct db_list * dbp,fhandle_t * dfh,char * name,fhandle_t * fh,uint_t flags,fhlist_ent * fhrecp,int * errorp)6560Sstevel@tonic-gate create_primary_struct(struct db_list *dbp, fhandle_t *dfh, char *name,
6570Sstevel@tonic-gate fhandle_t *fh, uint_t flags, fhlist_ent *fhrecp, int *errorp)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate int reclen, reclen1;
6600Sstevel@tonic-gate fhlist_ent *new_fhrecp = fhrecp;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate reclen1 = offsetof(fhlist_ent, name) + strlen(name) + 1;
6630Sstevel@tonic-gate reclen = ROUNDUP32(reclen1);
6640Sstevel@tonic-gate if (fhrecp == NULL) { /* allocated the memory */
6650Sstevel@tonic-gate if ((new_fhrecp = malloc(reclen)) == NULL) {
6660Sstevel@tonic-gate *errorp = errno;
6670Sstevel@tonic-gate syslog(LOG_ERR, gettext(
6680Sstevel@tonic-gate "create_primary_struct: malloc %d Error %s"),
6690Sstevel@tonic-gate reclen, strerror(*errorp));
6700Sstevel@tonic-gate return (NULL);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate /* Fill in the fields */
6740Sstevel@tonic-gate (void) memcpy(&new_fhrecp->fh, fh, sizeof (*fh));
6750Sstevel@tonic-gate (void) memcpy(&new_fhrecp->dfh, dfh, sizeof (*dfh));
6760Sstevel@tonic-gate new_fhrecp->flags = flags;
6770Sstevel@tonic-gate if (dfh == &public_fh)
6780Sstevel@tonic-gate new_fhrecp->flags |= PUBLIC_PATH;
6790Sstevel@tonic-gate else
6800Sstevel@tonic-gate new_fhrecp->flags &= ~PUBLIC_PATH;
6810Sstevel@tonic-gate new_fhrecp->mtime = time(0);
6820Sstevel@tonic-gate new_fhrecp->atime = new_fhrecp->mtime;
6830Sstevel@tonic-gate (void) strcpy(new_fhrecp->name, name);
6840Sstevel@tonic-gate if (reclen1 < reclen) {
6850Sstevel@tonic-gate bzero((char *)((uintptr_t)new_fhrecp + reclen1),
6860Sstevel@tonic-gate reclen - reclen1);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate new_fhrecp->reclen = reclen;
6890Sstevel@tonic-gate *errorp = store_record(dbp, &fh->fh_data, fh->fh_len, new_fhrecp,
6900Sstevel@tonic-gate new_fhrecp->reclen, "create_primary_struct");
6910Sstevel@tonic-gate if (*errorp != 0) {
6920Sstevel@tonic-gate /* Could not store */
6930Sstevel@tonic-gate if (fhrecp == NULL) /* caller did not supply pointer */
6940Sstevel@tonic-gate free(new_fhrecp);
6950Sstevel@tonic-gate return (NULL);
6960Sstevel@tonic-gate }
6970Sstevel@tonic-gate return (new_fhrecp);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate * db_add_primary - add primary record to the database.
7020Sstevel@tonic-gate * If record already in and live, return it (even if for a different link).
7030Sstevel@tonic-gate * If in database but marked deleted, replace it. If not in database, add it.
7040Sstevel@tonic-gate * Database must be open when this function is called.
7050Sstevel@tonic-gate * If success, return the added database entry. fhrecp may be used to
7060Sstevel@tonic-gate * provide an existing memory area, else malloc it. If failed, *errorp
7070Sstevel@tonic-gate * contains the error code and return NULL.
7080Sstevel@tonic-gate */
7090Sstevel@tonic-gate static fhlist_ent *
db_add_primary(struct db_list * dbp,fhandle_t * dfh,char * name,fhandle_t * fh,uint_t flags,fhlist_ent * fhrecp,int * errorp)7100Sstevel@tonic-gate db_add_primary(struct db_list *dbp, fhandle_t *dfh, char *name, fhandle_t *fh,
7110Sstevel@tonic-gate uint_t flags, fhlist_ent *fhrecp, int *errorp)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate fhlist_ent *new_fhrecp;
7140Sstevel@tonic-gate fh_primary_key fhkey;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate if (debug > 2)
7170Sstevel@tonic-gate (void) printf("db_add_primary entered: name '%s'\n", name);
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate bcopy(&fh->fh_data, fhkey, fh->fh_len);
7200Sstevel@tonic-gate new_fhrecp = fetch_record(dbp, fhkey, fh->fh_len, (void *)fhrecp,
7210Sstevel@tonic-gate errorp, "db_add_primary");
7220Sstevel@tonic-gate if (new_fhrecp != NULL) {
7230Sstevel@tonic-gate /* primary record is in the database */
7240Sstevel@tonic-gate /* Update atime if needed */
7250Sstevel@tonic-gate *errorp = db_update_fhrec(dbp, fhkey, fh->fh_len, new_fhrecp,
7260Sstevel@tonic-gate "db_add_primary put fhrec");
7270Sstevel@tonic-gate if (debug > 2)
7280Sstevel@tonic-gate (void) printf("db_add_primary exits(2): name '%s'\n",
7290Sstevel@tonic-gate name);
7300Sstevel@tonic-gate return (new_fhrecp);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate /* primary record not in database - create it */
7330Sstevel@tonic-gate new_fhrecp = create_primary_struct(dbp, dfh, name, fh, flags,
7340Sstevel@tonic-gate fhrecp, errorp);
7350Sstevel@tonic-gate if (new_fhrecp == NULL) {
7360Sstevel@tonic-gate /* Could not store */
7370Sstevel@tonic-gate if (debug > 2)
7380Sstevel@tonic-gate (void) printf(
7390Sstevel@tonic-gate "db_add_primary exits(1): name '%s' Error %s\n",
7400Sstevel@tonic-gate name, ((*errorp >= 0) ? strerror(*errorp) :
7410Sstevel@tonic-gate "Unknown"));
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate return (NULL);
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate if (debug > 2)
7460Sstevel@tonic-gate (void) printf("db_add_primary exits(0): name '%s'\n", name);
7470Sstevel@tonic-gate return (new_fhrecp);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate * get_next_link - get and check the next link in the chain.
7520Sstevel@tonic-gate * Re-use space if linkp param non-null. Also set *linkkey and *linksizep
7530Sstevel@tonic-gate * to values for next link (*linksizep set to 0 if last link).
7540Sstevel@tonic-gate * cookie is used to detect corrupted link entries XXXXXXX
7550Sstevel@tonic-gate * Return the link pointer or NULL if none.
7560Sstevel@tonic-gate */
7570Sstevel@tonic-gate static linkinfo_ent *
get_next_link(struct db_list * dbp,char * linkkey,int * linksizep,linkinfo_ent * linkp,void ** cookiep,int * errorp,char * msg)7580Sstevel@tonic-gate get_next_link(struct db_list *dbp, char *linkkey, int *linksizep,
7590Sstevel@tonic-gate linkinfo_ent *linkp, void **cookiep, int *errorp, char *msg)
7600Sstevel@tonic-gate {
7610Sstevel@tonic-gate int linksize, nextsize;
7620Sstevel@tonic-gate char *nextkey;
7630Sstevel@tonic-gate linkinfo_ent *new_linkp = linkp;
7640Sstevel@tonic-gate struct link_keys *lnp;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate linksize = *linksizep;
7670Sstevel@tonic-gate if (linksize == 0)
7680Sstevel@tonic-gate return (NULL);
7690Sstevel@tonic-gate *linksizep = 0;
7700Sstevel@tonic-gate new_linkp = fetch_record(dbp, linkkey, linksize, (void *)linkp,
7710Sstevel@tonic-gate errorp, msg);
7720Sstevel@tonic-gate if (new_linkp == NULL)
7730Sstevel@tonic-gate return (NULL);
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate /* Set linkkey to point to next record */
7760Sstevel@tonic-gate nextsize = LN_NEXT_LEN(new_linkp);
7770Sstevel@tonic-gate if (nextsize == 0)
7780Sstevel@tonic-gate return (new_linkp);
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate /* Add this key to the cookie list */
7810Sstevel@tonic-gate if ((lnp = malloc(sizeof (struct link_keys))) == NULL) {
7820Sstevel@tonic-gate syslog(LOG_ERR, gettext("get_next_key: malloc error %s\n"),
7830Sstevel@tonic-gate strerror(errno));
7840Sstevel@tonic-gate if ((new_linkp != NULL) && (linkp == NULL))
7850Sstevel@tonic-gate free(new_linkp);
7860Sstevel@tonic-gate return (NULL);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate (void) memcpy(lnp->lnkey, linkkey, linksize);
7890Sstevel@tonic-gate lnp->lnsize = linksize;
7900Sstevel@tonic-gate lnp->next = *(struct link_keys **)cookiep;
7910Sstevel@tonic-gate *cookiep = (void *)lnp;
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate /* Make sure record does not point to itself or other internal loops */
7940Sstevel@tonic-gate nextkey = LN_NEXT(new_linkp);
7950Sstevel@tonic-gate for (; lnp != NULL; lnp = lnp->next) {
7960Sstevel@tonic-gate if ((nextsize == lnp->lnsize) && (memcmp(
7970Sstevel@tonic-gate lnp->lnkey, nextkey, nextsize) == 0)) {
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * XXX This entry's next pointer points to
8010Sstevel@tonic-gate * itself. This is only a work-around, remove
8020Sstevel@tonic-gate * this check once bug 4203186 is fixed.
8030Sstevel@tonic-gate */
8040Sstevel@tonic-gate if (debug) {
8050Sstevel@tonic-gate (void) fprintf(stderr,
8060Sstevel@tonic-gate "%s: get_next_link: last record invalid.\n",
8070Sstevel@tonic-gate msg);
8080Sstevel@tonic-gate debug_print_key_and_data(stderr, msg,
8090Sstevel@tonic-gate "invalid rec:\n ", linkkey, linksize,
8100Sstevel@tonic-gate (char *)new_linkp, new_linkp->reclen);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate /* Return as if this is the last link */
8130Sstevel@tonic-gate return (new_linkp);
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate (void) memcpy(linkkey, nextkey, nextsize);
8170Sstevel@tonic-gate *linksizep = nextsize;
8180Sstevel@tonic-gate return (new_linkp);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate /*
8220Sstevel@tonic-gate * free_link_cookies - free the cookie list
8230Sstevel@tonic-gate */
8240Sstevel@tonic-gate static void
free_link_cookies(void * cookie)8250Sstevel@tonic-gate free_link_cookies(void *cookie)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate struct link_keys *dellnp, *lnp;
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate lnp = (struct link_keys *)cookie;
8300Sstevel@tonic-gate while (lnp != NULL) {
8310Sstevel@tonic-gate dellnp = lnp;
8320Sstevel@tonic-gate lnp = lnp->next;
8330Sstevel@tonic-gate free(dellnp);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate /*
8380Sstevel@tonic-gate * add_mc_path - add a mc link to a file that has other links. Add it at end
8390Sstevel@tonic-gate * of linked list. Called when it's known there are other links.
8400Sstevel@tonic-gate */
8410Sstevel@tonic-gate static void
add_mc_path(struct db_list * dbp,fhandle_t * dfh,char * name,fhlist_ent * fhrecp,linkinfo_ent * linkp,int * errorp)8420Sstevel@tonic-gate add_mc_path(struct db_list *dbp, fhandle_t *dfh, char *name,
8430Sstevel@tonic-gate fhlist_ent *fhrecp, linkinfo_ent *linkp, int *errorp)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate fh_secondary_key linkkey;
8460Sstevel@tonic-gate int linksize, len;
8470Sstevel@tonic-gate linkinfo_ent lastlink, *lastlinkp;
8480Sstevel@tonic-gate void *cookie;
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate linksize = fill_link_key(linkkey, &fhrecp->dfh, fhrecp->name);
8510Sstevel@tonic-gate cookie = NULL;
8520Sstevel@tonic-gate do {
8530Sstevel@tonic-gate lastlinkp = get_next_link(dbp, linkkey, &linksize, &lastlink,
8540Sstevel@tonic-gate &cookie, errorp, "add_mc_path");
8550Sstevel@tonic-gate } while (linksize > 0);
8560Sstevel@tonic-gate free_link_cookies(cookie);
8570Sstevel@tonic-gate /* reached end of list */
8580Sstevel@tonic-gate if (lastlinkp == NULL) {
8590Sstevel@tonic-gate /* nothing to do */
8600Sstevel@tonic-gate if (debug > 1) {
8610Sstevel@tonic-gate (void) fprintf(stderr, "add_mc_path link is null\n");
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate return;
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate /* Add new link after last link */
8660Sstevel@tonic-gate /*
8670Sstevel@tonic-gate * next - link key for the next in the list - add at end so null.
8680Sstevel@tonic-gate * prev - link key for the previous link in the list.
8690Sstevel@tonic-gate */
8700Sstevel@tonic-gate linkp->prev_offset = linkp->next_offset; /* aligned */
8710Sstevel@tonic-gate linksize = fill_link_key(LN_PREV(linkp), &lastlinkp->dfh,
8720Sstevel@tonic-gate LN_NAME(lastlinkp));
8730Sstevel@tonic-gate linkp->reclen = linkp->prev_offset + linksize; /* aligned */
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate /* Add the link information to the database */
8760Sstevel@tonic-gate linksize = fill_link_key(linkkey, dfh, name);
8770Sstevel@tonic-gate *errorp = store_record(dbp, linkkey, linksize,
8780Sstevel@tonic-gate linkp, linkp->reclen, "add_mc_path");
8790Sstevel@tonic-gate if (*errorp != 0)
8800Sstevel@tonic-gate return;
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate /* Now update previous last link to point forward to new link */
8830Sstevel@tonic-gate /* Copy prev link out since it's going to be overwritten */
8840Sstevel@tonic-gate linksize = LN_PREV_LEN(lastlinkp);
8850Sstevel@tonic-gate (void) memcpy(linkkey, LN_PREV(lastlinkp), linksize);
8860Sstevel@tonic-gate /* Update previous last link to point to new one */
8870Sstevel@tonic-gate len = fill_link_key(LN_NEXT(lastlinkp), dfh, name);
8880Sstevel@tonic-gate lastlinkp->prev_offset = lastlinkp->next_offset + len; /* aligned */
8890Sstevel@tonic-gate (void) memcpy(LN_PREV(lastlinkp), linkkey, linksize);
8900Sstevel@tonic-gate lastlinkp->reclen = lastlinkp->prev_offset + linksize;
8910Sstevel@tonic-gate /* Update the link information to the database */
8920Sstevel@tonic-gate linksize = fill_link_key(linkkey, &lastlinkp->dfh, LN_NAME(lastlinkp));
8930Sstevel@tonic-gate *errorp = store_record(dbp, linkkey, linksize,
8940Sstevel@tonic-gate lastlinkp, lastlinkp->reclen, "add_mc_path prev");
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate * create_link_struct - create the secondary struct.
8990Sstevel@tonic-gate * (dfh,name) is the secondary key, fhrec is the primary record for the file
9000Sstevel@tonic-gate * and linkpp is a place holder for the record (could be null).
9010Sstevel@tonic-gate * Insert the record to the database.
9020Sstevel@tonic-gate * Return 0 if success, error otherwise.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate static linkinfo_ent *
create_link_struct(struct db_list * dbp,fhandle_t * dfh,char * name,fhlist_ent * fhrecp,int * errorp)9050Sstevel@tonic-gate create_link_struct(struct db_list *dbp, fhandle_t *dfh, char *name,
9060Sstevel@tonic-gate fhlist_ent *fhrecp, int *errorp)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate fh_secondary_key linkkey;
9090Sstevel@tonic-gate int len, linksize;
9100Sstevel@tonic-gate linkinfo_ent *linkp;
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate if ((linkp = malloc(sizeof (linkinfo_ent))) == NULL) {
9130Sstevel@tonic-gate *errorp = errno;
9140Sstevel@tonic-gate syslog(LOG_ERR, gettext(
9150Sstevel@tonic-gate "create_link_struct: malloc failed: Error %s"),
9160Sstevel@tonic-gate strerror(*errorp));
9170Sstevel@tonic-gate return (NULL);
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate if (dfh == &public_fh)
9200Sstevel@tonic-gate linkp->flags |= PUBLIC_PATH;
9210Sstevel@tonic-gate else
9220Sstevel@tonic-gate linkp->flags &= ~PUBLIC_PATH;
9230Sstevel@tonic-gate (void) memcpy(&linkp->dfh, dfh, sizeof (*dfh));
9240Sstevel@tonic-gate linkp->mtime = time(0);
9250Sstevel@tonic-gate linkp->atime = linkp->mtime;
9260Sstevel@tonic-gate /* Calculate offsets of variable fields */
9270Sstevel@tonic-gate /* fhkey - primary key (inode/gen) */
9280Sstevel@tonic-gate /* name - component name (in directory dfh) */
9290Sstevel@tonic-gate linkp->fhkey_offset = ROUNDUP32(offsetof(linkinfo_ent, varbuf));
9300Sstevel@tonic-gate len = fill_link_key(LN_FHKEY(linkp), &fhrecp->fh, name);
9310Sstevel@tonic-gate linkp->name_offset = linkp->fhkey_offset + fhrecp->fh.fh_len;
9320Sstevel@tonic-gate linkp->next_offset = linkp->fhkey_offset + len; /* aligned */
9330Sstevel@tonic-gate /*
9340Sstevel@tonic-gate * next - link key for the next link in the list - NULL if it's
9350Sstevel@tonic-gate * the first link. If this is the public fs, only one link allowed.
9360Sstevel@tonic-gate * Avoid setting a multi-component path as primary path,
9370Sstevel@tonic-gate * unless no choice.
9380Sstevel@tonic-gate */
9390Sstevel@tonic-gate len = 0;
9400Sstevel@tonic-gate if (memcmp(&fhrecp->dfh, dfh, sizeof (*dfh)) ||
9410Sstevel@tonic-gate strcmp(fhrecp->name, name)) {
9420Sstevel@tonic-gate /* different link than the one that's in the record */
9430Sstevel@tonic-gate if (dfh == &public_fh) {
9440Sstevel@tonic-gate /* parent is public fh - either multi-comp or root */
9450Sstevel@tonic-gate if (memcmp(&fhrecp->fh, &public_fh,
9460Sstevel@tonic-gate sizeof (public_fh))) {
9470Sstevel@tonic-gate /* multi-comp path */
9480Sstevel@tonic-gate add_mc_path(dbp, dfh, name, fhrecp, linkp,
9490Sstevel@tonic-gate errorp);
9500Sstevel@tonic-gate if (*errorp != 0) {
9510Sstevel@tonic-gate free(linkp);
9520Sstevel@tonic-gate return (NULL);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate return (linkp);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate } else {
9570Sstevel@tonic-gate /* new link to a file with a different one already */
9580Sstevel@tonic-gate len = fill_link_key(LN_NEXT(linkp), &fhrecp->dfh,
9590Sstevel@tonic-gate fhrecp->name);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate /*
9630Sstevel@tonic-gate * prev - link key for the previous link in the list - since we
9640Sstevel@tonic-gate * always insert at the front of the list, it's always initially NULL.
9650Sstevel@tonic-gate */
9660Sstevel@tonic-gate linkp->prev_offset = linkp->next_offset + len; /* aligned */
9670Sstevel@tonic-gate linkp->reclen = linkp->prev_offset;
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /* Add the link information to the database */
9700Sstevel@tonic-gate linksize = fill_link_key(linkkey, dfh, name);
9710Sstevel@tonic-gate *errorp = store_record(dbp, linkkey, linksize, linkp, linkp->reclen,
9720Sstevel@tonic-gate "create_link_struct");
9730Sstevel@tonic-gate if (*errorp != 0) {
9740Sstevel@tonic-gate free(linkp);
9750Sstevel@tonic-gate return (NULL);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate return (linkp);
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate * db_add_secondary - add secondary record to the database (for the directory
9820Sstevel@tonic-gate * information).
9830Sstevel@tonic-gate * Assumes this is a new link, not yet in the database, and that the primary
9840Sstevel@tonic-gate * record is already in.
9850Sstevel@tonic-gate * If fhrecp is non-null, then fhrecp is the primary record.
9860Sstevel@tonic-gate * Database must be open when this function is called.
9870Sstevel@tonic-gate * Return 0 if success, error code otherwise.
9880Sstevel@tonic-gate */
9890Sstevel@tonic-gate static int
db_add_secondary(struct db_list * dbp,fhandle_t * dfh,char * name,fhandle_t * fh,fhlist_ent * fhrecp)9900Sstevel@tonic-gate db_add_secondary(struct db_list *dbp, fhandle_t *dfh, char *name,
9910Sstevel@tonic-gate fhandle_t *fh, fhlist_ent *fhrecp)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate int nextsize, len, error;
9940Sstevel@tonic-gate linkinfo_ent nextlink, *newlinkp, *nextlinkp;
9950Sstevel@tonic-gate uint_t fhflags;
9960Sstevel@tonic-gate char *nextaddr;
9970Sstevel@tonic-gate fhlist_ent *new_fhrecp = fhrecp;
9980Sstevel@tonic-gate fh_primary_key fhkey;
9990Sstevel@tonic-gate
10000Sstevel@tonic-gate if (debug > 2)
10010Sstevel@tonic-gate (void) printf("db_add_secondary entered: name '%s'\n", name);
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate bcopy(&fh->fh_data, fhkey, fh->fh_len);
10040Sstevel@tonic-gate if (fhrecp == NULL) {
10050Sstevel@tonic-gate /* Fetch the primary record */
10060Sstevel@tonic-gate new_fhrecp = fetch_record(dbp, fhkey, fh->fh_len, NULL,
10070Sstevel@tonic-gate &error, "db_add_secondary primary");
10080Sstevel@tonic-gate if (new_fhrecp == NULL) {
10090Sstevel@tonic-gate return (error);
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate /* Update fhrec atime if needed */
10130Sstevel@tonic-gate error = db_update_fhrec(dbp, fhkey, fh->fh_len, new_fhrecp,
10140Sstevel@tonic-gate "db_add_secondary primary");
10150Sstevel@tonic-gate fhflags = new_fhrecp->flags;
10160Sstevel@tonic-gate /* now create and insert the secondary record */
10170Sstevel@tonic-gate newlinkp = create_link_struct(dbp, dfh, name, new_fhrecp, &error);
10180Sstevel@tonic-gate if (fhrecp == NULL) {
10190Sstevel@tonic-gate free(new_fhrecp);
10200Sstevel@tonic-gate new_fhrecp = NULL;
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate if (newlinkp == NULL) {
10230Sstevel@tonic-gate if (debug > 2)
10240Sstevel@tonic-gate (void) printf("create_link_struct '%s' Error %s\n",
10250Sstevel@tonic-gate name, ((error >= 0) ? strerror(error) :
10260Sstevel@tonic-gate "Unknown"));
10270Sstevel@tonic-gate return (error);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate nextsize = LN_NEXT_LEN(newlinkp);
10300Sstevel@tonic-gate if (nextsize == 0) {
10310Sstevel@tonic-gate /* No next - can exit now */
10320Sstevel@tonic-gate if (debug > 2)
10330Sstevel@tonic-gate (void) printf("db_add_secondary: no next link\n");
10340Sstevel@tonic-gate free(newlinkp);
10350Sstevel@tonic-gate return (0);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate * Update the linked list to point to new head: replace head of
10400Sstevel@tonic-gate * list in the primary record, then update previous secondary record
10410Sstevel@tonic-gate * to point to new head
10420Sstevel@tonic-gate */
10430Sstevel@tonic-gate new_fhrecp = create_primary_struct(dbp, dfh, name, fh, fhflags,
10440Sstevel@tonic-gate new_fhrecp, &error);
10450Sstevel@tonic-gate if (new_fhrecp == NULL) {
10460Sstevel@tonic-gate if (debug > 2)
10470Sstevel@tonic-gate (void) printf(
10480Sstevel@tonic-gate "db_add_secondary: replace primary failed\n");
10490Sstevel@tonic-gate free(newlinkp);
10500Sstevel@tonic-gate return (error);
10510Sstevel@tonic-gate } else if (fhrecp == NULL) {
10520Sstevel@tonic-gate free(new_fhrecp);
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate /*
10560Sstevel@tonic-gate * newlink is the new head of the list, with its "next" pointing to
10570Sstevel@tonic-gate * the old head, and its "prev" pointing to NULL. We now need to
10580Sstevel@tonic-gate * modify the "next" entry to have its "prev" point to the new entry.
10590Sstevel@tonic-gate */
10600Sstevel@tonic-gate nextaddr = LN_NEXT(newlinkp);
10610Sstevel@tonic-gate if (debug > 2) {
10620Sstevel@tonic-gate debug_print_key(stderr, "db_add_secondary", "next key\n ",
10630Sstevel@tonic-gate nextaddr, nextsize);
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate /* Get the next link entry from the database */
10660Sstevel@tonic-gate nextlinkp = fetch_record(dbp, nextaddr, nextsize, (void *)&nextlink,
10670Sstevel@tonic-gate &error, "db_add_secondary next link");
10680Sstevel@tonic-gate if (nextlinkp == NULL) {
10690Sstevel@tonic-gate if (debug > 2)
10700Sstevel@tonic-gate (void) printf(
10710Sstevel@tonic-gate "db_add_secondary: fetch next link failed\n");
10720Sstevel@tonic-gate free(newlinkp);
10730Sstevel@tonic-gate return (error);
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate /*
10770Sstevel@tonic-gate * since the "prev" field is the only field to be changed, and it's
10780Sstevel@tonic-gate * the last in the link record, we only need to modify it (and reclen).
10790Sstevel@tonic-gate * Re-use link to update the next record.
10800Sstevel@tonic-gate */
10810Sstevel@tonic-gate len = fill_link_key(LN_PREV(nextlinkp), dfh, name);
10820Sstevel@tonic-gate nextlinkp->reclen = nextlinkp->prev_offset + len;
10830Sstevel@tonic-gate error = store_record(dbp, nextaddr, nextsize, nextlinkp,
10840Sstevel@tonic-gate nextlinkp->reclen, "db_add_secondary");
10850Sstevel@tonic-gate if (debug > 2)
10860Sstevel@tonic-gate (void) printf(
10870Sstevel@tonic-gate "db_add_secondary exits(%d): name '%s'\n", error, name);
10880Sstevel@tonic-gate free(newlinkp);
10890Sstevel@tonic-gate return (error);
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate * Update the next link to point to the new prev.
10940Sstevel@tonic-gate * Return 0 for success, error code otherwise.
10950Sstevel@tonic-gate * If successful, and nextlinkpp is non-null,
10960Sstevel@tonic-gate * *nextlinkpp contains the record for the next link, since
10970Sstevel@tonic-gate * we may will it if the primary record should be updated.
10980Sstevel@tonic-gate */
10990Sstevel@tonic-gate static linkinfo_ent *
update_next_link(struct db_list * dbp,char * nextkey,int nextsize,char * prevkey,int prevsize,int * errorp)11000Sstevel@tonic-gate update_next_link(struct db_list *dbp, char *nextkey, int nextsize,
11010Sstevel@tonic-gate char *prevkey, int prevsize, int *errorp)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate linkinfo_ent *nextlinkp, *linkp1;
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate if ((nextlinkp = malloc(sizeof (linkinfo_ent))) == NULL) {
11060Sstevel@tonic-gate *errorp = errno;
11070Sstevel@tonic-gate syslog(LOG_ERR, gettext(
11080Sstevel@tonic-gate "update_next_link: malloc next Error %s"),
11090Sstevel@tonic-gate strerror(*errorp));
11100Sstevel@tonic-gate return (NULL);
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate linkp1 = nextlinkp;
11130Sstevel@tonic-gate nextlinkp = fetch_record(dbp, nextkey, nextsize, nextlinkp,
11140Sstevel@tonic-gate errorp, "update next");
11150Sstevel@tonic-gate /* if there is no next record - ok */
11160Sstevel@tonic-gate if (nextlinkp == NULL) {
11170Sstevel@tonic-gate /* Return no error */
11180Sstevel@tonic-gate *errorp = 0;
11190Sstevel@tonic-gate free(linkp1);
11200Sstevel@tonic-gate return (NULL);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate /* Set its prev to the prev of the deleted record */
11230Sstevel@tonic-gate nextlinkp->reclen = ROUNDUP32(nextlinkp->reclen -
11240Sstevel@tonic-gate LN_PREV_LEN(nextlinkp) + prevsize);
11250Sstevel@tonic-gate /* Change the len and set prev */
11260Sstevel@tonic-gate if (prevsize > 0) {
11270Sstevel@tonic-gate (void) memcpy(LN_PREV(nextlinkp), prevkey, prevsize);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate /* No other changes needed because prev is last field */
11300Sstevel@tonic-gate *errorp = store_record(dbp, nextkey, nextsize, nextlinkp,
11310Sstevel@tonic-gate nextlinkp->reclen, "update_next");
11320Sstevel@tonic-gate if (*errorp != 0) {
11330Sstevel@tonic-gate free(nextlinkp);
11340Sstevel@tonic-gate nextlinkp = NULL;
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate return (nextlinkp);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate /*
11400Sstevel@tonic-gate * Update the prev link to point to the new next.
11410Sstevel@tonic-gate * Return 0 for success, error code otherwise.
11420Sstevel@tonic-gate */
11430Sstevel@tonic-gate static int
update_prev_link(struct db_list * dbp,char * nextkey,int nextsize,char * prevkey,int prevsize)11440Sstevel@tonic-gate update_prev_link(struct db_list *dbp, char *nextkey, int nextsize,
11450Sstevel@tonic-gate char *prevkey, int prevsize)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate linkinfo_ent prevlink, *prevlinkp;
11480Sstevel@tonic-gate int diff, error;
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate /* Update its next to the given one */
11510Sstevel@tonic-gate prevlinkp = fetch_record(dbp, prevkey, prevsize, &prevlink, &error,
11520Sstevel@tonic-gate "update prev");
11530Sstevel@tonic-gate /* if error there is no next record - ok */
11540Sstevel@tonic-gate if (prevlinkp == NULL) {
11550Sstevel@tonic-gate return (0);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate diff = nextsize - LN_NEXT_LEN(prevlinkp);
11580Sstevel@tonic-gate prevlinkp->reclen = ROUNDUP32(prevlinkp->reclen + diff);
11590Sstevel@tonic-gate /* Change the len and set next - may push prev */
11600Sstevel@tonic-gate if (diff != 0) {
11610Sstevel@tonic-gate char *ptr = LN_PREV(prevlinkp);
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate prevlinkp->prev_offset += diff;
11640Sstevel@tonic-gate (void) memcpy(LN_PREV(prevlinkp), ptr, LN_PREV_LEN(prevlinkp));
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate if (nextsize > 0) {
11670Sstevel@tonic-gate (void) memcpy(LN_NEXT(prevlinkp), nextkey, nextsize);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate /* Store updated record */
11700Sstevel@tonic-gate error = store_record(dbp, prevkey, prevsize, prevlinkp,
11710Sstevel@tonic-gate prevlinkp->reclen, "update_prev");
11720Sstevel@tonic-gate return (error);
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate * update_linked_list - update the next link to point back to prev, and vice
11770Sstevel@tonic-gate * versa. Normally called by delete_link to drop the deleted link from the
11780Sstevel@tonic-gate * linked list of hard links for the file. next and prev are the keys of next
11790Sstevel@tonic-gate * and previous links for the deleted link in the list (could be NULL).
11800Sstevel@tonic-gate * Return 0 for success, error code otherwise.
11810Sstevel@tonic-gate * If successful, and nextlinkpp is non-null,
11820Sstevel@tonic-gate * return the record for the next link, since
11830Sstevel@tonic-gate * if the primary record should be updated we'll need it. In this case,
11840Sstevel@tonic-gate * actually allocate the space for it because we can't tell otherwise.
11850Sstevel@tonic-gate */
11860Sstevel@tonic-gate static linkinfo_ent *
update_linked_list(struct db_list * dbp,char * nextkey,int nextsize,char * prevkey,int prevsize,int * errorp)11870Sstevel@tonic-gate update_linked_list(struct db_list *dbp, char *nextkey, int nextsize,
11880Sstevel@tonic-gate char *prevkey, int prevsize, int *errorp)
11890Sstevel@tonic-gate {
11900Sstevel@tonic-gate linkinfo_ent *nextlinkp = NULL;
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate *errorp = 0;
11930Sstevel@tonic-gate if (nextsize > 0) {
11940Sstevel@tonic-gate nextlinkp = update_next_link(dbp, nextkey, nextsize,
11950Sstevel@tonic-gate prevkey, prevsize, errorp);
11960Sstevel@tonic-gate if (nextlinkp == NULL) {
11970Sstevel@tonic-gate /* not an error if no next link */
11980Sstevel@tonic-gate if (*errorp != 0) {
11990Sstevel@tonic-gate if (debug > 1) {
12000Sstevel@tonic-gate (void) fprintf(stderr,
12010Sstevel@tonic-gate "update_next_link Error %s\n",
12020Sstevel@tonic-gate ((*errorp >= 0) ? strerror(*errorp) :
12030Sstevel@tonic-gate "Unknown"));
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate return (NULL);
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate }
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate if (prevsize > 0) {
12100Sstevel@tonic-gate *errorp = update_prev_link(dbp, nextkey, nextsize,
12110Sstevel@tonic-gate prevkey, prevsize);
12120Sstevel@tonic-gate if (*errorp != 0) {
12130Sstevel@tonic-gate if (debug > 1) {
12140Sstevel@tonic-gate (void) fprintf(stderr,
12150Sstevel@tonic-gate "update_prev_link Error %s\n",
12160Sstevel@tonic-gate ((*errorp >= 0) ? strerror(*errorp) :
12170Sstevel@tonic-gate "Unknown"));
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate if (nextlinkp != NULL)
12200Sstevel@tonic-gate free(nextlinkp);
12210Sstevel@tonic-gate nextlinkp = NULL;
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate return (nextlinkp);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * db_update_primary_new_head - Update a primary record that the head of
12290Sstevel@tonic-gate * the list is deleted. Similar to db_add_primary, but the primary record
12300Sstevel@tonic-gate * must exist, and is always replaced with one pointing to the new link,
12310Sstevel@tonic-gate * unless it does not point to the deleted link. If the link we deleted
12320Sstevel@tonic-gate * was the last link, the delete the primary record as well.
12330Sstevel@tonic-gate * Return 0 for success, error code otherwise.
12340Sstevel@tonic-gate */
12350Sstevel@tonic-gate static int
db_update_primary_new_head(struct db_list * dbp,linkinfo_ent * dellinkp,linkinfo_ent * nextlinkp,fhlist_ent * fhrecp)12360Sstevel@tonic-gate db_update_primary_new_head(struct db_list *dbp, linkinfo_ent *dellinkp,
12370Sstevel@tonic-gate linkinfo_ent *nextlinkp, fhlist_ent *fhrecp)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate int error;
12400Sstevel@tonic-gate char *name, *next_name;
12410Sstevel@tonic-gate fhandle_t *dfh;
12420Sstevel@tonic-gate fh_primary_key fhkey;
12430Sstevel@tonic-gate
12440Sstevel@tonic-gate dfh = &dellinkp->dfh;
12450Sstevel@tonic-gate name = LN_NAME(dellinkp);
12460Sstevel@tonic-gate /* If the deleted link was not the head of the list, we are done */
12470Sstevel@tonic-gate if (memcmp(&fhrecp->dfh, dfh, sizeof (*dfh)) ||
12480Sstevel@tonic-gate strcmp(fhrecp->name, name)) {
12490Sstevel@tonic-gate /* should never be here... */
12500Sstevel@tonic-gate if (debug > 1) {
12510Sstevel@tonic-gate (void) fprintf(stderr,
12520Sstevel@tonic-gate "db_update_primary_new_head: primary "
12530Sstevel@tonic-gate "is for [%s,", name);
12540Sstevel@tonic-gate debug_opaque_print(stderr, (void *)dfh, sizeof (*dfh));
12550Sstevel@tonic-gate (void) fprintf(stderr, "], not [%s,", fhrecp->name);
12560Sstevel@tonic-gate debug_opaque_print(stderr, (void *)&fhrecp->dfh,
12570Sstevel@tonic-gate sizeof (fhrecp->dfh));
12580Sstevel@tonic-gate (void) fprintf(stderr, "]\n");
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate return (0); /* not head of list so done */
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate /* Set the head to nextkey if exists. Otherwise, mark file as deleted */
12630Sstevel@tonic-gate bcopy(&fhrecp->fh.fh_data, fhkey, fhrecp->fh.fh_len);
12640Sstevel@tonic-gate if (nextlinkp == NULL) {
12650Sstevel@tonic-gate /* last link */
12660Sstevel@tonic-gate /* remove primary record from database */
12670Sstevel@tonic-gate (void) delete_record(dbp,
12680Sstevel@tonic-gate fhkey, fhrecp->fh.fh_len,
12690Sstevel@tonic-gate "db_update_primary_new_head: fh delete");
12700Sstevel@tonic-gate return (0);
12710Sstevel@tonic-gate } else {
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate * There are still "live" links, so update the primary record.
12740Sstevel@tonic-gate */
12750Sstevel@tonic-gate next_name = LN_NAME(nextlinkp);
12760Sstevel@tonic-gate fhrecp->reclen = ROUNDUP32(offsetof(fhlist_ent, name) +
12770Sstevel@tonic-gate strlen(next_name) + 1);
12780Sstevel@tonic-gate /* Replace link data with the info for the next link */
12790Sstevel@tonic-gate (void) memcpy(&fhrecp->dfh, &nextlinkp->dfh,
12800Sstevel@tonic-gate sizeof (nextlinkp->dfh));
12810Sstevel@tonic-gate (void) strcpy(fhrecp->name, next_name);
12820Sstevel@tonic-gate }
12830Sstevel@tonic-gate /* not last link */
12840Sstevel@tonic-gate fhrecp->mtime = time(0);
12850Sstevel@tonic-gate fhrecp->atime = fhrecp->mtime;
12860Sstevel@tonic-gate error = store_record(dbp,
12870Sstevel@tonic-gate fhkey, fhrecp->fh.fh_len, fhrecp,
12880Sstevel@tonic-gate fhrecp->reclen, "db_update_primary_new_head: fh");
12890Sstevel@tonic-gate return (error);
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate
12920Sstevel@tonic-gate /*
12930Sstevel@tonic-gate * Exported functions
12940Sstevel@tonic-gate */
12950Sstevel@tonic-gate
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate * db_add - add record to the database. If dfh, fh and name are all here,
12980Sstevel@tonic-gate * add both primary and secondary records. If fh is not available, don't
12990Sstevel@tonic-gate * add anything...
13000Sstevel@tonic-gate * Assumes this is a new file, not yet in the database and that the record
13010Sstevel@tonic-gate * for fh is already in.
13020Sstevel@tonic-gate * Return 0 for success, error code otherwise.
13030Sstevel@tonic-gate */
13040Sstevel@tonic-gate int
db_add(char * fhpath,fhandle_t * dfh,char * name,fhandle_t * fh,uint_t flags)13050Sstevel@tonic-gate db_add(char *fhpath, fhandle_t *dfh, char *name, fhandle_t *fh, uint_t flags)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate struct db_list *dbp = NULL;
13080Sstevel@tonic-gate fhlist_ent fhrec, *fhrecp;
13090Sstevel@tonic-gate int error = 0;
13100Sstevel@tonic-gate
13110Sstevel@tonic-gate if (fh == NULL) {
13120Sstevel@tonic-gate /* nothing to add */
13130Sstevel@tonic-gate return (EINVAL);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate if (fh == &public_fh) {
13160Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, FALSE);
13170Sstevel@tonic-gate } else {
13180Sstevel@tonic-gate dbp = db_get_db(fhpath, &fh->fh_fsid, &error, O_CREAT);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate for (; dbp != NULL; dbp = ((fh != &public_fh) ? NULL : dbp->next)) {
13210Sstevel@tonic-gate if (debug > 3) {
13220Sstevel@tonic-gate (void) printf("db_add: name '%s', db '%s'\n",
13230Sstevel@tonic-gate name, dbp->path);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate fhrecp = db_add_primary(dbp, dfh, name, fh, flags,
13260Sstevel@tonic-gate &fhrec, &error);
13270Sstevel@tonic-gate if (fhrecp == NULL) {
13280Sstevel@tonic-gate continue;
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate if ((dfh == NULL) || (name == NULL)) {
13310Sstevel@tonic-gate /* Can't add link information */
13320Sstevel@tonic-gate syslog(LOG_ERR, gettext(
13330Sstevel@tonic-gate "db_add: dfh %p, name %p - invalid"),
13340Sstevel@tonic-gate (void *)dfh, (void *)name);
13350Sstevel@tonic-gate error = EINVAL;
13360Sstevel@tonic-gate continue;
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate if (fh == &public_fh) {
13390Sstevel@tonic-gate while ((fhrecp != NULL) && strcmp(name, fhrecp->name)) {
13400Sstevel@tonic-gate /* Replace the public fh rather than add link */
13410Sstevel@tonic-gate error = db_delete_link(fhpath, dfh,
13420Sstevel@tonic-gate fhrecp->name);
13430Sstevel@tonic-gate fhrecp = db_add_primary(dbp, dfh, name, fh,
13440Sstevel@tonic-gate flags, &fhrec, &error);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate if (fhrecp == NULL) {
13470Sstevel@tonic-gate continue;
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate }
13500Sstevel@tonic-gate error = db_add_secondary(dbp, dfh, name, fh, fhrecp);
13510Sstevel@tonic-gate if (fhrecp != &fhrec) {
13520Sstevel@tonic-gate free(fhrecp);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate return (error);
13560Sstevel@tonic-gate }
13570Sstevel@tonic-gate
13580Sstevel@tonic-gate /*
13590Sstevel@tonic-gate * db_lookup - search the database for the file identified by fh.
13600Sstevel@tonic-gate * Return the entry in *fhrecpp if found, or NULL with error set otherwise.
13610Sstevel@tonic-gate */
13620Sstevel@tonic-gate fhlist_ent *
db_lookup(char * fhpath,fhandle_t * fh,fhlist_ent * fhrecp,int * errorp)13630Sstevel@tonic-gate db_lookup(char *fhpath, fhandle_t *fh, fhlist_ent *fhrecp, int *errorp)
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate struct db_list *dbp;
13660Sstevel@tonic-gate fh_primary_key fhkey;
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate if ((fhpath == NULL) || (fh == NULL) || (errorp == NULL)) {
13690Sstevel@tonic-gate if (errorp != NULL)
13700Sstevel@tonic-gate *errorp = EINVAL;
13710Sstevel@tonic-gate return (NULL);
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate *errorp = 0;
13740Sstevel@tonic-gate if (fh == &public_fh) {
13750Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, FALSE);
13760Sstevel@tonic-gate } else {
13770Sstevel@tonic-gate dbp = db_get_db(fhpath, &fh->fh_fsid, errorp, O_CREAT);
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate if (dbp == NULL) {
13800Sstevel@tonic-gate /* Could not get or create database */
13810Sstevel@tonic-gate return (NULL);
13820Sstevel@tonic-gate }
13830Sstevel@tonic-gate bcopy(&fh->fh_data, fhkey, fh->fh_len);
13840Sstevel@tonic-gate fhrecp = fetch_record(dbp, fhkey, fh->fh_len, fhrecp,
13850Sstevel@tonic-gate errorp, "db_lookup");
13860Sstevel@tonic-gate /* Update fhrec atime if needed */
13870Sstevel@tonic-gate if (fhrecp != NULL) {
13880Sstevel@tonic-gate *errorp = db_update_fhrec(dbp, fhkey, fh->fh_len, fhrecp,
13890Sstevel@tonic-gate "db_lookup");
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate return (fhrecp);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate /*
13950Sstevel@tonic-gate * db_lookup_link - search the database for the file identified by (dfh,name).
13960Sstevel@tonic-gate * If the link was found, use it to search for the primary record.
13970Sstevel@tonic-gate * Return 0 and set the entry in *fhrecpp if found, return error otherwise.
13980Sstevel@tonic-gate */
13990Sstevel@tonic-gate fhlist_ent *
db_lookup_link(char * fhpath,fhandle_t * dfh,char * name,fhlist_ent * fhrecp,int * errorp)14000Sstevel@tonic-gate db_lookup_link(char *fhpath, fhandle_t *dfh, char *name, fhlist_ent *fhrecp,
14010Sstevel@tonic-gate int *errorp)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate struct db_list *dbp;
14040Sstevel@tonic-gate fh_secondary_key linkkey;
14050Sstevel@tonic-gate linkinfo_ent *linkp;
14060Sstevel@tonic-gate int linksize, fhkeysize;
14070Sstevel@tonic-gate char *fhkey;
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate if ((fhpath == NULL) || (dfh == NULL) || (name == NULL) ||
14100Sstevel@tonic-gate (errorp == NULL)) {
14110Sstevel@tonic-gate if (errorp != NULL)
14120Sstevel@tonic-gate *errorp = EINVAL;
14130Sstevel@tonic-gate return (NULL);
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate *errorp = 0;
14160Sstevel@tonic-gate if (dfh == &public_fh) {
14170Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, FALSE);
14180Sstevel@tonic-gate } else {
14190Sstevel@tonic-gate dbp = db_get_db(fhpath, &dfh->fh_fsid, errorp, O_CREAT);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate if (dbp == NULL) {
14220Sstevel@tonic-gate /* Could not get or create database */
14230Sstevel@tonic-gate return (NULL);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate /* Get the link record */
14260Sstevel@tonic-gate linksize = fill_link_key(linkkey, dfh, name);
14270Sstevel@tonic-gate linkp = fetch_record(dbp, linkkey, linksize, NULL, errorp,
14280Sstevel@tonic-gate "db_lookup_link link");
14290Sstevel@tonic-gate if (linkp != NULL) {
14300Sstevel@tonic-gate /* Now use link to search for fh entry */
14310Sstevel@tonic-gate fhkeysize = LN_FHKEY_LEN(linkp);
14320Sstevel@tonic-gate fhkey = LN_FHKEY(linkp);
14330Sstevel@tonic-gate fhrecp = fetch_record(dbp, fhkey, fhkeysize,
14340Sstevel@tonic-gate (void *)fhrecp, errorp, "db_lookup_link fh");
14350Sstevel@tonic-gate /* Update fhrec atime if needed */
14360Sstevel@tonic-gate if (fhrecp != NULL) {
14370Sstevel@tonic-gate *errorp = db_update_fhrec(dbp, fhkey, fhkeysize, fhrecp,
14380Sstevel@tonic-gate "db_lookup_link fhrec");
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate /* Update link atime if needed */
14410Sstevel@tonic-gate *errorp = db_update_linkinfo(dbp, linkkey, linksize, linkp,
14420Sstevel@tonic-gate "db_lookup_link link");
14430Sstevel@tonic-gate free(linkp);
14440Sstevel@tonic-gate } else {
14450Sstevel@tonic-gate fhrecp = NULL;
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate return (fhrecp);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate * delete_link - delete the requested link from the database. If it's the
14520Sstevel@tonic-gate * last link in the database for that file then remove the primary record
14530Sstevel@tonic-gate * as well. *errorp contains the returned error code.
14540Sstevel@tonic-gate * Return ENOENT if link not in database and 0 otherwise.
14550Sstevel@tonic-gate */
14560Sstevel@tonic-gate static int
delete_link_by_key(struct db_list * dbp,char * linkkey,int * linksizep,int * errorp,char * errstr)14570Sstevel@tonic-gate delete_link_by_key(struct db_list *dbp, char *linkkey, int *linksizep,
14580Sstevel@tonic-gate int *errorp, char *errstr)
14590Sstevel@tonic-gate {
14600Sstevel@tonic-gate int nextsize, prevsize, fhkeysize, linksize;
14610Sstevel@tonic-gate char *nextkey, *prevkey, *fhkey;
14620Sstevel@tonic-gate linkinfo_ent *dellinkp, *nextlinkp;
14630Sstevel@tonic-gate fhlist_ent *fhrecp, fhrec;
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate *errorp = 0;
14660Sstevel@tonic-gate linksize = *linksizep;
14670Sstevel@tonic-gate /* Get the link record */
14680Sstevel@tonic-gate dellinkp = fetch_record(dbp, linkkey, linksize, NULL, errorp, errstr);
14690Sstevel@tonic-gate if (dellinkp == NULL) {
14700Sstevel@tonic-gate /*
14710Sstevel@tonic-gate * Link not in database.
14720Sstevel@tonic-gate */
14730Sstevel@tonic-gate if (debug > 2) {
14740Sstevel@tonic-gate debug_print_key(stderr, errstr,
14750Sstevel@tonic-gate "link not in database\n",
14760Sstevel@tonic-gate linkkey, linksize);
14770Sstevel@tonic-gate }
14780Sstevel@tonic-gate *linksizep = 0;
14790Sstevel@tonic-gate return (ENOENT);
14800Sstevel@tonic-gate }
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate * Possibilities:
14830Sstevel@tonic-gate * 1. Normal case - only one link to delete: the link next and
14840Sstevel@tonic-gate * prev should be NULL, and fhrec's name/dfh are same
14850Sstevel@tonic-gate * as the link. Remove the link and fhrec.
14860Sstevel@tonic-gate * 2. Multiple hard links, and the deleted link is the head of
14870Sstevel@tonic-gate * the list. Remove the link and replace the link key in
14880Sstevel@tonic-gate * the primary record to point to the new head.
14890Sstevel@tonic-gate * 3. Multiple hard links, and the deleted link is not the
14900Sstevel@tonic-gate * head of the list (not the same as in fhrec) - just
14910Sstevel@tonic-gate * delete the link and update the previous and next records
14920Sstevel@tonic-gate * in the links linked list.
14930Sstevel@tonic-gate */
14940Sstevel@tonic-gate
14950Sstevel@tonic-gate /* Get next and prev keys for linked list updates */
14960Sstevel@tonic-gate nextsize = LN_NEXT_LEN(dellinkp);
14970Sstevel@tonic-gate nextkey = ((nextsize > 0) ? LN_NEXT(dellinkp) : NULL);
14980Sstevel@tonic-gate prevsize = LN_PREV_LEN(dellinkp);
14990Sstevel@tonic-gate prevkey = ((prevsize > 0) ? LN_PREV(dellinkp) : NULL);
15000Sstevel@tonic-gate /* Update the linked list for the file */
15010Sstevel@tonic-gate nextlinkp = update_linked_list(dbp, nextkey, nextsize,
15020Sstevel@tonic-gate prevkey, prevsize, errorp);
15030Sstevel@tonic-gate if ((nextlinkp == NULL) && (*errorp != 0)) {
15040Sstevel@tonic-gate free(dellinkp);
15050Sstevel@tonic-gate *linksizep = 0;
15060Sstevel@tonic-gate return (0);
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate /* Delete link record */
15090Sstevel@tonic-gate *errorp = delete_record(dbp, linkkey, linksize, errstr);
15100Sstevel@tonic-gate /* Get the primary key */
15110Sstevel@tonic-gate fhkeysize = LN_FHKEY_LEN(dellinkp);
15120Sstevel@tonic-gate fhkey = LN_FHKEY(dellinkp);
15130Sstevel@tonic-gate fhrecp = fetch_record(dbp, fhkey, fhkeysize,
15140Sstevel@tonic-gate &fhrec, errorp, errstr);
15150Sstevel@tonic-gate if (fhrecp == NULL) {
15160Sstevel@tonic-gate /* Should never happen */
15170Sstevel@tonic-gate if (debug > 1) {
15180Sstevel@tonic-gate debug_print_key(stderr, errstr,
15190Sstevel@tonic-gate "fetch primary for ", linkkey, linksize);
15200Sstevel@tonic-gate (void) fprintf(stderr, " Error %s\n",
15210Sstevel@tonic-gate ((*errorp >= 0) ? strerror(*errorp) : "Unknown"));
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate } else if ((*errorp == 0) && (prevsize <= 0)) {
15240Sstevel@tonic-gate /* This is the head of the list update primary record */
15250Sstevel@tonic-gate *errorp = db_update_primary_new_head(dbp, dellinkp,
15260Sstevel@tonic-gate nextlinkp, fhrecp);
15270Sstevel@tonic-gate } else {
15280Sstevel@tonic-gate /* Update fhrec atime if needed */
15290Sstevel@tonic-gate *errorp = db_update_fhrec(dbp, fhkey, fhkeysize, fhrecp,
15300Sstevel@tonic-gate errstr);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate *linksizep = nextsize;
15330Sstevel@tonic-gate if (nextsize > 0)
15340Sstevel@tonic-gate (void) memcpy(linkkey, nextkey, nextsize);
15350Sstevel@tonic-gate if (nextlinkp != NULL)
15360Sstevel@tonic-gate free(nextlinkp);
15370Sstevel@tonic-gate free(dellinkp);
15380Sstevel@tonic-gate return (0);
15390Sstevel@tonic-gate }
15400Sstevel@tonic-gate
15410Sstevel@tonic-gate /*
15420Sstevel@tonic-gate * delete_link - delete the requested link from the database. If it's the
15430Sstevel@tonic-gate * last link in the database for that file then remove the primary record
15440Sstevel@tonic-gate * as well. If nextlinkkey/sizep are non-null, copy the key and key size of
15450Sstevel@tonic-gate * the next link in the chain into them (this would save a dbm_fetch op).
15460Sstevel@tonic-gate * Return ENOENT if link not in database and 0 otherwise, with *errorp
15470Sstevel@tonic-gate * containing the returned error if any from the delete_link ops.
15480Sstevel@tonic-gate */
15490Sstevel@tonic-gate static int
delete_link(struct db_list * dbp,fhandle_t * dfh,char * name,char * nextlinkkey,int * nextlinksizep,int * errorp,char * errstr)15500Sstevel@tonic-gate delete_link(struct db_list *dbp, fhandle_t *dfh, char *name,
15510Sstevel@tonic-gate char *nextlinkkey, int *nextlinksizep, int *errorp, char *errstr)
15520Sstevel@tonic-gate {
15530Sstevel@tonic-gate int linkerr;
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate *errorp = 0;
15560Sstevel@tonic-gate if ((nextlinkkey != NULL) && (nextlinksizep != NULL)) {
15570Sstevel@tonic-gate *nextlinksizep = fill_link_key(nextlinkkey, dfh, name);
15580Sstevel@tonic-gate linkerr = delete_link_by_key(dbp, nextlinkkey, nextlinksizep,
15590Sstevel@tonic-gate errorp, errstr);
15600Sstevel@tonic-gate } else {
15610Sstevel@tonic-gate int linksize;
15620Sstevel@tonic-gate fh_secondary_key linkkey;
15630Sstevel@tonic-gate
15640Sstevel@tonic-gate linksize = fill_link_key(linkkey, dfh, name);
15650Sstevel@tonic-gate linkerr = delete_link_by_key(dbp, linkkey, &linksize,
15660Sstevel@tonic-gate errorp, errstr);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate return (linkerr);
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate /*
15720Sstevel@tonic-gate * db_delete_link - search the database for the file system for link.
15730Sstevel@tonic-gate * Delete the link from the database. If this is the "primary" link,
15740Sstevel@tonic-gate * set the primary record for the next link. If it's the last one,
15750Sstevel@tonic-gate * delete the primary record.
15760Sstevel@tonic-gate * Return 0 for success, error code otherwise.
15770Sstevel@tonic-gate */
15780Sstevel@tonic-gate int
db_delete_link(char * fhpath,fhandle_t * dfh,char * name)15790Sstevel@tonic-gate db_delete_link(char *fhpath, fhandle_t *dfh, char *name)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate struct db_list *dbp;
15820Sstevel@tonic-gate int error = 0;
15830Sstevel@tonic-gate
15840Sstevel@tonic-gate if ((fhpath == NULL) || (dfh == NULL) || (name == NULL)) {
15850Sstevel@tonic-gate return (EINVAL);
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate if (dfh == &public_fh) {
15880Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, TRUE);
15890Sstevel@tonic-gate } else {
15900Sstevel@tonic-gate dbp = db_get_db(fhpath, &dfh->fh_fsid, &error, O_CREAT);
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate for (; dbp != NULL; dbp = ((dfh == &public_fh) ? dbp->next : NULL)) {
15930Sstevel@tonic-gate (void) delete_link(dbp, dfh, name, NULL, NULL, &error,
15940Sstevel@tonic-gate "db_delete_link link");
15950Sstevel@tonic-gate }
15960Sstevel@tonic-gate return (error);
15970Sstevel@tonic-gate }
15980Sstevel@tonic-gate
15990Sstevel@tonic-gate #ifdef DEBUG
16000Sstevel@tonic-gate /*
16010Sstevel@tonic-gate * db_delete - Deletes the fhrec corresponding to the fh. Use only
16020Sstevel@tonic-gate * for repairing the fhtable, not for normal handling.
16030Sstevel@tonic-gate * Return 0 for success, error code otherwise.
16040Sstevel@tonic-gate */
16050Sstevel@tonic-gate int
db_delete(char * fhpath,fhandle_t * fh)16060Sstevel@tonic-gate db_delete(char *fhpath, fhandle_t *fh)
16070Sstevel@tonic-gate {
16080Sstevel@tonic-gate struct db_list *dbp;
16090Sstevel@tonic-gate int error = 0;
16100Sstevel@tonic-gate
16110Sstevel@tonic-gate if ((fhpath == NULL) || (fh == NULL)) {
16120Sstevel@tonic-gate return (EINVAL);
16130Sstevel@tonic-gate }
16140Sstevel@tonic-gate if (fh == &public_fh) {
16150Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, TRUE);
16160Sstevel@tonic-gate } else {
16170Sstevel@tonic-gate dbp = db_get_db(fhpath, &fh->fh_fsid, &error, O_CREAT);
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate for (; dbp != NULL; dbp = ((fh == &public_fh) ? dbp->next : NULL)) {
16200Sstevel@tonic-gate /* Get the link record */
16210Sstevel@tonic-gate (void) delete_record(dbp, &fh->fh_data, fh->fh_len,
16220Sstevel@tonic-gate "db_delete: fh delete");
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate return (error);
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate #endif /* DEBUG */
16270Sstevel@tonic-gate
16280Sstevel@tonic-gate /*
16290Sstevel@tonic-gate * db_rename_link - search the database for the file system for link.
16300Sstevel@tonic-gate * Add the new link and delete the old link from the database.
16310Sstevel@tonic-gate * Return 0 for success, error code otherwise.
16320Sstevel@tonic-gate */
16330Sstevel@tonic-gate int
db_rename_link(char * fhpath,fhandle_t * from_dfh,char * from_name,fhandle_t * to_dfh,char * to_name)16340Sstevel@tonic-gate db_rename_link(char *fhpath, fhandle_t *from_dfh, char *from_name,
16350Sstevel@tonic-gate fhandle_t *to_dfh, char *to_name)
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate int error;
16380Sstevel@tonic-gate struct db_list *dbp;
16390Sstevel@tonic-gate fhlist_ent fhrec, *fhrecp;
16400Sstevel@tonic-gate
16410Sstevel@tonic-gate if ((fhpath == NULL) || (from_dfh == NULL) || (from_name == NULL) ||
16420Sstevel@tonic-gate (to_dfh == NULL) || (to_name == NULL)) {
16430Sstevel@tonic-gate return (EINVAL);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate if (from_dfh == &public_fh) {
16460Sstevel@tonic-gate dbp = db_get_all_databases(fhpath, FALSE);
16470Sstevel@tonic-gate } else {
16480Sstevel@tonic-gate dbp = db_get_db(fhpath, &from_dfh->fh_fsid, &error, O_CREAT);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate for (; dbp != NULL;
16510Sstevel@tonic-gate dbp = ((from_dfh != &public_fh) ? NULL : dbp->next)) {
16520Sstevel@tonic-gate /* find existing link */
16530Sstevel@tonic-gate fhrecp = db_lookup_link(fhpath, from_dfh, from_name, &fhrec,
16540Sstevel@tonic-gate &error);
16550Sstevel@tonic-gate if (fhrecp == NULL) {
16560Sstevel@tonic-gate /* Could not find the link */
16570Sstevel@tonic-gate continue;
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate /* Delete the old link (if last primary record not deleted) */
16600Sstevel@tonic-gate error = db_delete_link(fhpath, from_dfh, from_name);
16610Sstevel@tonic-gate if (error == 0) {
16620Sstevel@tonic-gate error = db_add(fhpath, to_dfh, to_name, &fhrecp->fh,
16630Sstevel@tonic-gate fhrecp->flags);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate return (error);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate
16690Sstevel@tonic-gate /*
16700Sstevel@tonic-gate * db_print_all_keys: prints all keys for a given filesystem. If fsidp is
16710Sstevel@tonic-gate * NULL, print for all filesystems covered by fhpath.
16720Sstevel@tonic-gate */
16730Sstevel@tonic-gate void
db_print_all_keys(char * fhpath,fsid_t * fsidp,FILE * fp)16740Sstevel@tonic-gate db_print_all_keys(char *fhpath, fsid_t *fsidp, FILE *fp)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate struct db_list *dbp;
16770Sstevel@tonic-gate datum key;
16780Sstevel@tonic-gate int error, len;
16790Sstevel@tonic-gate char strkey[NFS_FHMAXDATA + MAXNAMELEN];
16800Sstevel@tonic-gate db_record rec;
16810Sstevel@tonic-gate void *ptr;
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate if ((fhpath == NULL) ||
16840Sstevel@tonic-gate ((fsidp != NULL) && (fsidp == &public_fh.fh_fsid)))
16850Sstevel@tonic-gate return;
16860Sstevel@tonic-gate if (fsidp == NULL) {
16870Sstevel@tonic-gate (void) db_get_all_databases(fhpath, TRUE);
16880Sstevel@tonic-gate dbp = db_fs_list;
16890Sstevel@tonic-gate } else {
16900Sstevel@tonic-gate dbp = db_get_db(fhpath, fsidp, &error, 0);
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate if (dbp == NULL) {
16930Sstevel@tonic-gate /* Could not get or create database */
16940Sstevel@tonic-gate return;
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate len = strlen(fhpath);
16970Sstevel@tonic-gate for (; dbp != NULL; dbp = ((fsidp != NULL) ? NULL : dbp->next)) {
16980Sstevel@tonic-gate if (strncmp(fhpath, dbp->path, len))
16990Sstevel@tonic-gate continue;
17000Sstevel@tonic-gate (void) fprintf(fp,
17010Sstevel@tonic-gate "\nStart print database for fsid 0x%x 0x%x\n",
17020Sstevel@tonic-gate dbp->fsid.val[0], dbp->fsid.val[1]);
17030Sstevel@tonic-gate (void) fprintf(fp, "=============================\n");
17040Sstevel@tonic-gate for (key = dbm_firstkey(dbp->db); key.dptr != NULL;
17050Sstevel@tonic-gate key = dbm_nextkey(dbp->db)) {
17060Sstevel@tonic-gate (void) memcpy(strkey, key.dptr, key.dsize);
17070Sstevel@tonic-gate debug_print_key(fp, "", "", strkey, key.dsize);
17080Sstevel@tonic-gate if (debug < 2)
17090Sstevel@tonic-gate continue;
17100Sstevel@tonic-gate ptr = fetch_record(dbp, key.dptr, key.dsize,
17110Sstevel@tonic-gate (void *)&rec, &error, "db_prt_keys");
17120Sstevel@tonic-gate if (ptr == NULL)
17130Sstevel@tonic-gate continue;
17140Sstevel@tonic-gate if (key.dsize == NFS_FHMAXDATA) {
17150Sstevel@tonic-gate /* fhrec */
17160Sstevel@tonic-gate debug_print_fhlist(fp, &rec.fhlist_rec);
17170Sstevel@tonic-gate } else if (key.dsize > NFS_FHMAXDATA) {
17180Sstevel@tonic-gate /* linkinfo */
17190Sstevel@tonic-gate debug_print_linkinfo(fp, &rec.link_rec);
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate (void) fprintf(fp, "-----------------------------\n");
17220Sstevel@tonic-gate }
17230Sstevel@tonic-gate (void) fprintf(fp, "End print database for fsid 0x%x 0x%x\n",
17240Sstevel@tonic-gate dbp->fsid.val[0], dbp->fsid.val[1]);
17250Sstevel@tonic-gate }
17260Sstevel@tonic-gate }
17270Sstevel@tonic-gate
17280Sstevel@tonic-gate void
debug_opaque_print(FILE * fp,void * buf,int size)17290Sstevel@tonic-gate debug_opaque_print(FILE *fp, void *buf, int size)
17300Sstevel@tonic-gate {
17310Sstevel@tonic-gate int bufoffset = 0;
17320Sstevel@tonic-gate char debug_str[200];
17330Sstevel@tonic-gate
17340Sstevel@tonic-gate if ((buf == NULL) || (size <= 0))
17350Sstevel@tonic-gate return;
17360Sstevel@tonic-gate
17370Sstevel@tonic-gate nfslog_opaque_print_buf(buf, size, debug_str, &bufoffset, 200);
17380Sstevel@tonic-gate (void) fprintf(fp, debug_str);
17390Sstevel@tonic-gate }
17400Sstevel@tonic-gate
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate * links_timedout() takes a primary records and searches all of its
17430Sstevel@tonic-gate * links to see if they all have access times that are older than
17440Sstevel@tonic-gate * the 'prune_timeout' value. TRUE if all links are old and FALSE
17450Sstevel@tonic-gate * if there is just one link that has an access time which is recent.
17460Sstevel@tonic-gate */
17470Sstevel@tonic-gate static int
links_timedout(struct db_list * pdb,fhlist_ent * pfe,time_t ts)17480Sstevel@tonic-gate links_timedout(struct db_list *pdb, fhlist_ent *pfe, time_t ts)
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate fh_secondary_key linkkey;
17510Sstevel@tonic-gate linkinfo_ent *linkp, link_st;
17520Sstevel@tonic-gate int error;
17530Sstevel@tonic-gate int linksize;
17540Sstevel@tonic-gate void *cookie;
17550Sstevel@tonic-gate
17560Sstevel@tonic-gate /* Get the link record */
17570Sstevel@tonic-gate linksize = fill_link_key(linkkey, &pfe->dfh, pfe->name);
17580Sstevel@tonic-gate cookie = NULL;
17590Sstevel@tonic-gate do {
17600Sstevel@tonic-gate linkp = get_next_link(pdb, linkkey, &linksize, &link_st,
17610Sstevel@tonic-gate &cookie, &error, "links_timedout");
17620Sstevel@tonic-gate if ((linkp != NULL) &&
17630Sstevel@tonic-gate (difftime(ts, linkp->atime) <= prune_timeout)) {
17640Sstevel@tonic-gate /* update primary record to have an uptodate time */
17650Sstevel@tonic-gate pfe = fetch_record(pdb, (void *)&pfe->fh.fh_data,
17660Sstevel@tonic-gate pfe->fh.fh_len, NULL, &error,
17670Sstevel@tonic-gate "links_timedout");
17680Sstevel@tonic-gate if (pfe == NULL) {
17690Sstevel@tonic-gate syslog(LOG_ERR, gettext(
17700Sstevel@tonic-gate "links_timedout: fetch fhrec error %s\n"),
17710Sstevel@tonic-gate strerror(error));
17720Sstevel@tonic-gate } else {
17730Sstevel@tonic-gate if (difftime(pfe->atime, linkp->atime) < 0) {
17740Sstevel@tonic-gate /* update fhrec atime */
17750Sstevel@tonic-gate pfe->atime = linkp->atime;
17760Sstevel@tonic-gate (void) store_record(pdb,
17770Sstevel@tonic-gate (void *)&pfe->fh.fh_data,
17780Sstevel@tonic-gate pfe->fh.fh_len, pfe,
17790Sstevel@tonic-gate pfe->reclen, "links_timedout");
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate free(pfe);
17820Sstevel@tonic-gate }
17830Sstevel@tonic-gate free_link_cookies(cookie);
17840Sstevel@tonic-gate return (FALSE);
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate } while (linksize > 0);
17870Sstevel@tonic-gate
17880Sstevel@tonic-gate free_link_cookies(cookie);
17890Sstevel@tonic-gate return (TRUE);
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate
17920Sstevel@tonic-gate /*
17930Sstevel@tonic-gate * prune_dbs() will search all of the open databases looking for records
17940Sstevel@tonic-gate * that have not been accessed in the last 'prune_timeout' seconds.
17950Sstevel@tonic-gate * This search is done on the primary records and a list of potential
17960Sstevel@tonic-gate * timeout candidates is built. The reason for doing this is to not
17970Sstevel@tonic-gate * disturb the underlying dbm_firstkey()/dbm_nextkey() sequence; we
17980Sstevel@tonic-gate * want to search all of the records in the database.
17990Sstevel@tonic-gate * Once we have our candidate list built, we examine each of those
18000Sstevel@tonic-gate * item's links to check if the links have been accessed within the
18010Sstevel@tonic-gate * 'prune_timeout' seconds. If neither the primary nor any its links
18020Sstevel@tonic-gate * have been accessed, then all of those records are removed/deleted
18030Sstevel@tonic-gate * from the database.
18040Sstevel@tonic-gate */
18050Sstevel@tonic-gate int
prune_dbs(char * fhpath)18060Sstevel@tonic-gate prune_dbs(char *fhpath)
18070Sstevel@tonic-gate {
18080Sstevel@tonic-gate struct db_list *pdb;
18090Sstevel@tonic-gate datum key;
18100Sstevel@tonic-gate db_record *ptr;
18110Sstevel@tonic-gate struct fhlist_ent *pfe;
18120Sstevel@tonic-gate int error, linkerr, linksize;
18130Sstevel@tonic-gate time_t cur_time = time(0);
18140Sstevel@tonic-gate fh_secondary_key linkkey;
18150Sstevel@tonic-gate struct thelist {
18160Sstevel@tonic-gate struct thelist *next;
18170Sstevel@tonic-gate db_record *ptr;
18180Sstevel@tonic-gate } thelist, *ptl;
18190Sstevel@tonic-gate int cnt = 0;
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate if (fhpath != NULL)
18220Sstevel@tonic-gate (void) db_get_all_databases(fhpath, TRUE);
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate thelist.next = NULL;
18250Sstevel@tonic-gate /*
18260Sstevel@tonic-gate * Search each of the open databases
18270Sstevel@tonic-gate */
18280Sstevel@tonic-gate for (pdb = db_fs_list; pdb; pdb = pdb->next) {
18290Sstevel@tonic-gate do {
18300Sstevel@tonic-gate /* Check each record in the database */
18310Sstevel@tonic-gate for (key = dbm_firstkey(pdb->db); key.dptr != NULL;
18320Sstevel@tonic-gate key = dbm_nextkey(pdb->db)) {
18330Sstevel@tonic-gate /* We're only interested in primary records */
18340Sstevel@tonic-gate if (key.dsize != NFS_FHMAXDATA)
18350Sstevel@tonic-gate continue; /* probably a link record */
18360Sstevel@tonic-gate ptr = fetch_record(pdb, key.dptr, key.dsize,
18370Sstevel@tonic-gate NULL, &error, "dump_db");
18380Sstevel@tonic-gate if (ptr == NULL)
18390Sstevel@tonic-gate continue;
18400Sstevel@tonic-gate /*
18410Sstevel@tonic-gate * If this record is a primary record and it is
18420Sstevel@tonic-gate * not an export point or a public file handle path,
18430Sstevel@tonic-gate * check it for a ancient access time.
18440Sstevel@tonic-gate */
18450Sstevel@tonic-gate if ((ptr->fhlist_rec.flags &
18460Sstevel@tonic-gate (EXPORT_POINT | PUBLIC_PATH)) ||
18470Sstevel@tonic-gate (difftime(cur_time, ptr->fhlist_rec.atime) <=
18480Sstevel@tonic-gate prune_timeout)) {
18490Sstevel@tonic-gate /* Keep this record in the database */
18500Sstevel@tonic-gate free(ptr);
18510Sstevel@tonic-gate } else {
18520Sstevel@tonic-gate /* Found one? Save off info about it */
18530Sstevel@tonic-gate ptl = malloc(sizeof (struct thelist));
18540Sstevel@tonic-gate if (ptl == NULL) {
18550Sstevel@tonic-gate syslog(LOG_ERR, gettext(
18560Sstevel@tonic-gate "prune_dbs: malloc failed, error %s\n"),
18570Sstevel@tonic-gate strerror(errno));
18580Sstevel@tonic-gate break;
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate ptl->ptr = ptr;
18610Sstevel@tonic-gate ptl->next = thelist.next;
18620Sstevel@tonic-gate thelist.next = ptl;
18630Sstevel@tonic-gate cnt++; /* count how many records allocated */
18640Sstevel@tonic-gate if (cnt > MAX_PRUNE_REC_CNT) {
18650Sstevel@tonic-gate /* Limit number of records malloc'd */
18660Sstevel@tonic-gate if (debug)
18670Sstevel@tonic-gate (void) fprintf(stderr,
18680Sstevel@tonic-gate "prune_dbs: halt search - too many records\n");
18690Sstevel@tonic-gate break;
18700Sstevel@tonic-gate }
18710Sstevel@tonic-gate }
18720Sstevel@tonic-gate }
18730Sstevel@tonic-gate
18740Sstevel@tonic-gate /*
18750Sstevel@tonic-gate * Take the saved records and check their links to make
18760Sstevel@tonic-gate * sure that they have not been accessed as well.
18770Sstevel@tonic-gate */
18780Sstevel@tonic-gate for (ptl = thelist.next; ptl; ptl = thelist.next) {
18790Sstevel@tonic-gate thelist.next = ptl->next;
18800Sstevel@tonic-gate /* Everything timed out? */
18810Sstevel@tonic-gate pfe = &(ptl->ptr->fhlist_rec);
18820Sstevel@tonic-gate if (links_timedout(pdb, pfe, cur_time)) {
18830Sstevel@tonic-gate
18840Sstevel@tonic-gate /*
18850Sstevel@tonic-gate * Iterate until we run out of links.
18860Sstevel@tonic-gate * We have to do this since there can be
18870Sstevel@tonic-gate * multiple links to a primary record and
18880Sstevel@tonic-gate * we need to delete one at a time.
18890Sstevel@tonic-gate */
18900Sstevel@tonic-gate /* Delete the link and get the next */
18910Sstevel@tonic-gate linkerr = delete_link(pdb,
18920Sstevel@tonic-gate &pfe->dfh, pfe->name, linkkey,
18930Sstevel@tonic-gate &linksize, &error, "dump_db");
18940Sstevel@tonic-gate while ((linksize > 0) && !(error || linkerr)) {
18950Sstevel@tonic-gate /* Delete the link and get the next */
18960Sstevel@tonic-gate linkerr = delete_link_by_key(pdb,
18970Sstevel@tonic-gate linkkey, &linksize,
18980Sstevel@tonic-gate &error, "dump_db");
18990Sstevel@tonic-gate if (error || linkerr) {
19000Sstevel@tonic-gate break;
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate if (linkerr) {
19040Sstevel@tonic-gate /* link not in database, primary is */
19050Sstevel@tonic-gate /* Should never happen */
19060Sstevel@tonic-gate if (debug > 1) {
19070Sstevel@tonic-gate (void) fprintf(stderr,
19080Sstevel@tonic-gate "prune_dbs: Error primary exists ");
19090Sstevel@tonic-gate debug_opaque_print(stderr,
19100Sstevel@tonic-gate (void *)&pfe->fh,
19110Sstevel@tonic-gate sizeof (pfe->fh));
19120Sstevel@tonic-gate (void) fprintf(stderr, "\n");
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate if (debug)
19150Sstevel@tonic-gate syslog(LOG_ERR, gettext(
19160Sstevel@tonic-gate "prune_dbs: Error primary exists\n"));
19170Sstevel@tonic-gate (void) delete_record(pdb,
19180Sstevel@tonic-gate &pfe->fh.fh_data, pfe->fh.fh_len,
19190Sstevel@tonic-gate "prune_dbs: fh delete");
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate }
19220Sstevel@tonic-gate /* Make sure to free the pointers used in the list */
19230Sstevel@tonic-gate free(ptl->ptr);
19240Sstevel@tonic-gate free(ptl);
19250Sstevel@tonic-gate cnt--;
19260Sstevel@tonic-gate }
19270Sstevel@tonic-gate thelist.next = NULL;
19280Sstevel@tonic-gate } while (key.dptr != NULL);
19290Sstevel@tonic-gate }
19300Sstevel@tonic-gate return (0);
19310Sstevel@tonic-gate }
1932