xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_readdir.c (revision 3391:2e4fef544e31)
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
52170Sevanl  * Common Development and Distribution License (the "License").
62170Sevanl  * 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*3391Ssemery  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  *	autod_readdir.c
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <syslog.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <pwd.h>
400Sstevel@tonic-gate #include <locale.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <assert.h>
440Sstevel@tonic-gate #include <fcntl.h>
450Sstevel@tonic-gate #include "automount.h"
460Sstevel@tonic-gate 
472170Sevanl static void build_dir_entry_list(struct autofs_rddir_cache *rdcp,
480Sstevel@tonic-gate 				struct dir_entry *list);
492170Sevanl static int autofs_rddir_cache_enter(char *map, ulong_t bucket_size,
502170Sevanl 				struct autofs_rddir_cache **rdcpp);
512170Sevanl int autofs_rddir_cache_lookup(char *map, struct autofs_rddir_cache **rdcpp);
522170Sevanl static int autofs_rddir_cache_delete(struct autofs_rddir_cache *rdcp);
532170Sevanl static int create_dirents(struct autofs_rddir_cache *rdcp, ulong_t offset,
540Sstevel@tonic-gate 				autofs_rddirres *res);
550Sstevel@tonic-gate struct dir_entry *rddir_entry_lookup(char *name, struct dir_entry *list);
560Sstevel@tonic-gate static void free_offset_tbl(struct off_tbl *head);
570Sstevel@tonic-gate static void free_dir_list(struct dir_entry *head);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	OFFSET_BUCKET_SIZE	100
600Sstevel@tonic-gate 
612170Sevanl rwlock_t autofs_rddir_cache_lock;		/* readdir cache lock */
622170Sevanl struct autofs_rddir_cache *rddir_head;		/* readdir cache head */
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int
do_readdir(autofs_rddirargs * rda,autofs_rddirres * rd)65*3391Ssemery do_readdir(autofs_rddirargs *rda, autofs_rddirres *rd)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	struct dir_entry *list = NULL, *l;
682170Sevanl 	struct autofs_rddir_cache *rdcp = NULL;
690Sstevel@tonic-gate 	int error;
700Sstevel@tonic-gate 	int cache_time = RDDIR_CACHE_TIME;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (automountd_nobrowse) {
730Sstevel@tonic-gate 		/*
740Sstevel@tonic-gate 		 * Browsability was disabled return an empty list.
750Sstevel@tonic-gate 		 */
760Sstevel@tonic-gate 		rd->rd_status = AUTOFS_OK;
770Sstevel@tonic-gate 		rd->rd_rddir.rddir_size = 0;
780Sstevel@tonic-gate 		rd->rd_rddir.rddir_eof = 1;
790Sstevel@tonic-gate 		rd->rd_rddir.rddir_entries = NULL;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
842170Sevanl 	rw_rdlock(&autofs_rddir_cache_lock);
852170Sevanl 	error = autofs_rddir_cache_lookup(rda->rda_map, &rdcp);
860Sstevel@tonic-gate 	if (error) {
872170Sevanl 		rw_unlock(&autofs_rddir_cache_lock);
882170Sevanl 		rw_wrlock(&autofs_rddir_cache_lock);
892170Sevanl 		error = autofs_rddir_cache_lookup(rda->rda_map, &rdcp);
900Sstevel@tonic-gate 		if (error) {
910Sstevel@tonic-gate 			if (trace > 2)
920Sstevel@tonic-gate 				trace_prt(1,
930Sstevel@tonic-gate 				"map %s not found, adding...\n", rda->rda_map);
940Sstevel@tonic-gate 			/*
950Sstevel@tonic-gate 			 * entry doesn't exist, add it.
960Sstevel@tonic-gate 			 */
972170Sevanl 			error = autofs_rddir_cache_enter(rda->rda_map,
980Sstevel@tonic-gate 					OFFSET_BUCKET_SIZE, &rdcp);
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 	}
1012170Sevanl 	rw_unlock(&autofs_rddir_cache_lock);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if (error)
1040Sstevel@tonic-gate 		return (error);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	assert(rdcp != NULL);
1070Sstevel@tonic-gate 	assert(rdcp->in_use);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (!rdcp->full) {
1100Sstevel@tonic-gate 		rw_wrlock(&rdcp->rwlock);
1110Sstevel@tonic-gate 		if (!rdcp->full) {
1120Sstevel@tonic-gate 			/*
1130Sstevel@tonic-gate 			 * cache entry hasn't been filled up, do it now.
1140Sstevel@tonic-gate 			 */
1150Sstevel@tonic-gate 			char *stack[STACKSIZ];
1160Sstevel@tonic-gate 			char **stkptr;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			/*
1190Sstevel@tonic-gate 			 * Initialize the stack of open files
1200Sstevel@tonic-gate 			 * for this thread
1210Sstevel@tonic-gate 			 */
1220Sstevel@tonic-gate 			stack_op(INIT, NULL, stack, &stkptr);
1230Sstevel@tonic-gate 			(void) getmapkeys(rda->rda_map, &list, &error,
124*3391Ssemery 			    &cache_time, stack, &stkptr, rda->uid);
1250Sstevel@tonic-gate 			if (!error)
1260Sstevel@tonic-gate 				build_dir_entry_list(rdcp, list);
1270Sstevel@tonic-gate 			else if (list) {
1280Sstevel@tonic-gate 				free_dir_list(list);
1290Sstevel@tonic-gate 				list = NULL;
1300Sstevel@tonic-gate 			}
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 	} else
1330Sstevel@tonic-gate 		rw_rdlock(&rdcp->rwlock);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	rd->rd_bufsize = rda->rda_count;
1360Sstevel@tonic-gate 	if (!error) {
1370Sstevel@tonic-gate 		error = create_dirents(rdcp, rda->rda_offset, rd);
1380Sstevel@tonic-gate 		if (error) {
1390Sstevel@tonic-gate 			if (rdcp->offtp) {
1400Sstevel@tonic-gate 				free_offset_tbl(rdcp->offtp);
1410Sstevel@tonic-gate 				rdcp->offtp = NULL;
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 			if (rdcp->entp) {
1440Sstevel@tonic-gate 				free_dir_list(rdcp->entp);
1450Sstevel@tonic-gate 				rdcp->entp = NULL;
1460Sstevel@tonic-gate 			}
1470Sstevel@tonic-gate 			rdcp->full = 0;
1480Sstevel@tonic-gate 			list = NULL;
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (trace > 2) {
1530Sstevel@tonic-gate 		/*
1540Sstevel@tonic-gate 		 * print this list only once
1550Sstevel@tonic-gate 		 */
1560Sstevel@tonic-gate 		for (l = list; l != NULL; l = l->next)
1570Sstevel@tonic-gate 			trace_prt(0, "%s\n", l->name);
1580Sstevel@tonic-gate 		trace_prt(0, "\n");
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!error) {
1620Sstevel@tonic-gate 		rd->rd_status = AUTOFS_OK;
1630Sstevel@tonic-gate 		if (cache_time) {
1640Sstevel@tonic-gate 			/*
1650Sstevel@tonic-gate 			 * keep list of entries for up to
1660Sstevel@tonic-gate 			 * 'cache_time' seconds
1670Sstevel@tonic-gate 			 */
1680Sstevel@tonic-gate 			rdcp->ttl = time((time_t *)NULL) + cache_time;
1690Sstevel@tonic-gate 		} else {
1700Sstevel@tonic-gate 			/*
1710Sstevel@tonic-gate 			 * the underlying name service indicated not
1720Sstevel@tonic-gate 			 * to cache contents.
1730Sstevel@tonic-gate 			 */
1740Sstevel@tonic-gate 			if (rdcp->offtp) {
1750Sstevel@tonic-gate 				free_offset_tbl(rdcp->offtp);
1760Sstevel@tonic-gate 				rdcp->offtp = NULL;
1770Sstevel@tonic-gate 			}
1780Sstevel@tonic-gate 			if (rdcp->entp) {
1790Sstevel@tonic-gate 				free_dir_list(rdcp->entp);
1800Sstevel@tonic-gate 				rdcp->entp = NULL;
1810Sstevel@tonic-gate 			}
1820Sstevel@tonic-gate 			rdcp->full = 0;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	} else {
1850Sstevel@tonic-gate 		/*
1860Sstevel@tonic-gate 		 * return an empty list
1870Sstevel@tonic-gate 		 */
1880Sstevel@tonic-gate 		rd->rd_rddir.rddir_size = 0;
1890Sstevel@tonic-gate 		rd->rd_rddir.rddir_eof = 1;
1900Sstevel@tonic-gate 		rd->rd_rddir.rddir_entries = NULL;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		/*
1930Sstevel@tonic-gate 		 * Invalidate cache and set error
1940Sstevel@tonic-gate 		 */
1950Sstevel@tonic-gate 		switch (error) {
1960Sstevel@tonic-gate 		case ENOENT:
1970Sstevel@tonic-gate 			rd->rd_status = AUTOFS_NOENT;
1980Sstevel@tonic-gate 			break;
1990Sstevel@tonic-gate 		case ENOMEM:
2000Sstevel@tonic-gate 			rd->rd_status = AUTOFS_NOMEM;
2010Sstevel@tonic-gate 			break;
2020Sstevel@tonic-gate 		default:
2030Sstevel@tonic-gate 			rd->rd_status = AUTOFS_ECOMM;
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	rw_unlock(&rdcp->rwlock);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	mutex_lock(&rdcp->lock);
2090Sstevel@tonic-gate 	rdcp->in_use--;
2100Sstevel@tonic-gate 	mutex_unlock(&rdcp->lock);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	assert(rdcp->in_use >= 0);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	return (error);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate #define	roundtoint(x)	(((x) + sizeof (int) - 1) & ~(sizeof (int) - 1))
2180Sstevel@tonic-gate #define	DIRENT64_RECLEN(namelen)	\
2190Sstevel@tonic-gate 	(((int)(((dirent64_t *)0)->d_name) + 1 + (namelen) + 7) & ~ 7)
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static int
create_dirents(struct autofs_rddir_cache * rdcp,ulong_t offset,autofs_rddirres * res)2222170Sevanl create_dirents(
2232170Sevanl 	struct autofs_rddir_cache *rdcp,
2242170Sevanl 	ulong_t offset,
2252170Sevanl 	autofs_rddirres *res)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	uint_t total_bytes_wanted;
2280Sstevel@tonic-gate 	int bufsize;
2290Sstevel@tonic-gate 	ushort_t this_reclen;
2300Sstevel@tonic-gate 	int outcount = 0;
2310Sstevel@tonic-gate 	int namelen;
2320Sstevel@tonic-gate 	struct dir_entry *list = NULL, *l, *nl;
2330Sstevel@tonic-gate 	struct dirent64 *dp;
2340Sstevel@tonic-gate 	char *outbuf;
2350Sstevel@tonic-gate 	struct off_tbl *offtp, *next = NULL;
2360Sstevel@tonic-gate 	int this_bucket = 0;
2370Sstevel@tonic-gate 	int error = 0;
2380Sstevel@tonic-gate 	int x = 0, y = 0;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	assert(RW_LOCK_HELD(&rdcp->rwlock));
2410Sstevel@tonic-gate 	for (offtp = rdcp->offtp; offtp != NULL; offtp = next) {
2420Sstevel@tonic-gate 		x++;
2430Sstevel@tonic-gate 		next = offtp->next;
2440Sstevel@tonic-gate 		this_bucket = (next == NULL);
2450Sstevel@tonic-gate 		if (!this_bucket)
2460Sstevel@tonic-gate 			this_bucket = (offset < next->offset);
2470Sstevel@tonic-gate 		if (this_bucket) {
2480Sstevel@tonic-gate 			/*
2490Sstevel@tonic-gate 			 * has to be in this bucket
2500Sstevel@tonic-gate 			 */
2510Sstevel@tonic-gate 			assert(offset >= offtp->offset);
2520Sstevel@tonic-gate 			list = offtp->first;
2530Sstevel@tonic-gate 			break;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 		/*
2560Sstevel@tonic-gate 		 * loop to look in next bucket
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	for (l = list; l != NULL && l->offset < offset; l = l->next)
2610Sstevel@tonic-gate 		y++;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (l == NULL) {
2640Sstevel@tonic-gate 		/*
2650Sstevel@tonic-gate 		 * reached end of directory
2660Sstevel@tonic-gate 		 */
2670Sstevel@tonic-gate 		error = 0;
2680Sstevel@tonic-gate 		goto empty;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if (trace > 2)
2720Sstevel@tonic-gate 		trace_prt(1, "%s: offset searches (%d, %d)\n", rdcp->map, x, y);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	total_bytes_wanted = res->rd_bufsize;
2750Sstevel@tonic-gate 	bufsize = total_bytes_wanted + sizeof (struct dirent64);
2760Sstevel@tonic-gate 	outbuf = malloc(bufsize);
2770Sstevel@tonic-gate 	if (outbuf == NULL) {
2780Sstevel@tonic-gate 		syslog(LOG_ERR, "memory allocation error\n");
2790Sstevel@tonic-gate 		error = ENOMEM;
2800Sstevel@tonic-gate 		goto empty;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	memset(outbuf, 0, bufsize);
2830Sstevel@tonic-gate 	/* LINTED pointer alignment */
2840Sstevel@tonic-gate 	dp = (struct dirent64 *)outbuf;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	while (l) {
2870Sstevel@tonic-gate 		nl = l->next;
2880Sstevel@tonic-gate 		namelen = strlen(l->name);
2890Sstevel@tonic-gate 		this_reclen = DIRENT64_RECLEN(namelen);
2900Sstevel@tonic-gate 		if (outcount + this_reclen > total_bytes_wanted) {
2910Sstevel@tonic-gate 			break;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 		dp->d_ino = (ino64_t)l->nodeid;
2940Sstevel@tonic-gate 		if (nl) {
2950Sstevel@tonic-gate 			/*
2960Sstevel@tonic-gate 			 * get the next elements offset
2970Sstevel@tonic-gate 			 */
2980Sstevel@tonic-gate 			dp->d_off = (off64_t)nl->offset;
2990Sstevel@tonic-gate 		} else {
3000Sstevel@tonic-gate 			/*
3010Sstevel@tonic-gate 			 * This is the last element
3020Sstevel@tonic-gate 			 * make offset one plus the current.
3030Sstevel@tonic-gate 			 */
3040Sstevel@tonic-gate 			dp->d_off = (off64_t)l->offset + 1;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 		(void) strcpy(dp->d_name, l->name);
3070Sstevel@tonic-gate 		dp->d_reclen = (ushort_t)this_reclen;
3080Sstevel@tonic-gate 		outcount += dp->d_reclen;
3090Sstevel@tonic-gate 		dp = (struct dirent64 *)((int)dp + dp->d_reclen);
3100Sstevel@tonic-gate 		assert(outcount <= total_bytes_wanted);
3110Sstevel@tonic-gate 		l = l->next;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	res->rd_rddir.rddir_size = (long)outcount;
3150Sstevel@tonic-gate 	if (outcount > 0) {
3160Sstevel@tonic-gate 		/*
3170Sstevel@tonic-gate 		 * have some entries
3180Sstevel@tonic-gate 		 */
3190Sstevel@tonic-gate 		res->rd_rddir.rddir_eof = (l == NULL);
3200Sstevel@tonic-gate 		/* LINTED pointer alignment */
3210Sstevel@tonic-gate 		res->rd_rddir.rddir_entries = (struct dirent64 *)outbuf;
3220Sstevel@tonic-gate 		error = 0;
3230Sstevel@tonic-gate 	} else {
3240Sstevel@tonic-gate 		/*
3250Sstevel@tonic-gate 		 * total_bytes_wanted is not large enough for one
3260Sstevel@tonic-gate 		 * directory entry
3270Sstevel@tonic-gate 		 */
3280Sstevel@tonic-gate 		res->rd_rddir.rddir_eof = 0;
3290Sstevel@tonic-gate 		res->rd_rddir.rddir_entries = NULL;
3300Sstevel@tonic-gate 		free(outbuf);
3310Sstevel@tonic-gate 		error = EIO;
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	return (error);
3340Sstevel@tonic-gate 
3352170Sevanl empty:
3362170Sevanl 	res->rd_rddir.rddir_size = 0L;
3370Sstevel@tonic-gate 	res->rd_rddir.rddir_eof = TRUE;
3380Sstevel@tonic-gate 	res->rd_rddir.rddir_entries = NULL;
3390Sstevel@tonic-gate 	return (error);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate  * add new entry to cache for 'map'
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate static int
autofs_rddir_cache_enter(char * map,ulong_t bucket_size,struct autofs_rddir_cache ** rdcpp)3472170Sevanl autofs_rddir_cache_enter(
3482170Sevanl 	char *map,
3492170Sevanl 	ulong_t bucket_size,
3502170Sevanl 	struct autofs_rddir_cache **rdcpp)
3510Sstevel@tonic-gate {
3522170Sevanl 	struct autofs_rddir_cache *p;
3532170Sevanl 	assert(RW_LOCK_HELD(&autofs_rddir_cache_lock));
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * Add to front of the list at this time
3570Sstevel@tonic-gate 	 */
3582170Sevanl 	p = (struct autofs_rddir_cache *)malloc(sizeof (*p));
3590Sstevel@tonic-gate 	if (p == NULL) {
3600Sstevel@tonic-gate 		syslog(LOG_ERR,
3612170Sevanl 			"autofs_rddir_cache_enter: memory allocation failed\n");
3620Sstevel@tonic-gate 		return (ENOMEM);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	memset((char *)p, 0, sizeof (*p));
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	p->map = malloc(strlen(map) + 1);
3670Sstevel@tonic-gate 	if (p->map == NULL) {
3680Sstevel@tonic-gate 		syslog(LOG_ERR,
3692170Sevanl 			"autofs_rddir_cache_enter: memory allocation failed\n");
3700Sstevel@tonic-gate 		free(p);
3710Sstevel@tonic-gate 		return (ENOMEM);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	strcpy(p->map, map);
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	p->bucket_size = bucket_size;
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * no need to grab mutex lock since I haven't yet made the
3780Sstevel@tonic-gate 	 * node visible to the list
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	p->in_use = 1;
3810Sstevel@tonic-gate 	(void) rwlock_init(&p->rwlock, USYNC_THREAD, NULL);
3820Sstevel@tonic-gate 	(void) mutex_init(&p->lock, USYNC_THREAD, NULL);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (rddir_head == NULL)
3850Sstevel@tonic-gate 		rddir_head = p;
3860Sstevel@tonic-gate 	else {
3870Sstevel@tonic-gate 		p->next = rddir_head;
3880Sstevel@tonic-gate 		rddir_head = p;
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 	*rdcpp = p;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * find 'map' in readdir cache
3970Sstevel@tonic-gate  */
3980Sstevel@tonic-gate int
autofs_rddir_cache_lookup(char * map,struct autofs_rddir_cache ** rdcpp)3992170Sevanl autofs_rddir_cache_lookup(char *map, struct autofs_rddir_cache **rdcpp)
4000Sstevel@tonic-gate {
4012170Sevanl 	struct autofs_rddir_cache *p;
4020Sstevel@tonic-gate 
4032170Sevanl 	assert(RW_LOCK_HELD(&autofs_rddir_cache_lock));
4040Sstevel@tonic-gate 	for (p = rddir_head; p != NULL; p = p->next) {
4050Sstevel@tonic-gate 		if (strcmp(p->map, map) == 0) {
4060Sstevel@tonic-gate 			/*
4070Sstevel@tonic-gate 			 * found matching entry
4080Sstevel@tonic-gate 			 */
4090Sstevel@tonic-gate 			*rdcpp = p;
4100Sstevel@tonic-gate 			mutex_lock(&p->lock);
4110Sstevel@tonic-gate 			p->in_use++;
4120Sstevel@tonic-gate 			mutex_unlock(&p->lock);
4130Sstevel@tonic-gate 			return (0);
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * didn't find entry
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	return (ENOENT);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate  * free the offset table
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate static void
free_offset_tbl(struct off_tbl * head)4260Sstevel@tonic-gate free_offset_tbl(struct off_tbl *head)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	struct off_tbl *p, *next = NULL;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	for (p = head; p != NULL; p = next) {
4310Sstevel@tonic-gate 		next = p->next;
4320Sstevel@tonic-gate 		free(p);
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * free the directory entries
4380Sstevel@tonic-gate  */
4390Sstevel@tonic-gate static void
free_dir_list(struct dir_entry * head)4400Sstevel@tonic-gate free_dir_list(struct dir_entry *head)
4410Sstevel@tonic-gate {
4420Sstevel@tonic-gate 	struct dir_entry *p, *next = NULL;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	for (p = head; p != NULL; p = next) {
4450Sstevel@tonic-gate 		next = p->next;
4460Sstevel@tonic-gate 		assert(p->name);
4470Sstevel@tonic-gate 		free(p->name);
4480Sstevel@tonic-gate 		free(p);
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate static void
autofs_rddir_cache_entry_free(struct autofs_rddir_cache * p)4532170Sevanl autofs_rddir_cache_entry_free(struct autofs_rddir_cache *p)
4540Sstevel@tonic-gate {
4552170Sevanl 	assert(RW_LOCK_HELD(&autofs_rddir_cache_lock));
4560Sstevel@tonic-gate 	assert(!p->in_use);
4570Sstevel@tonic-gate 	if (p->map)
4580Sstevel@tonic-gate 		free(p->map);
4590Sstevel@tonic-gate 	if (p->offtp)
4600Sstevel@tonic-gate 		free_offset_tbl(p->offtp);
4610Sstevel@tonic-gate 	if (p->entp)
4620Sstevel@tonic-gate 		free_dir_list(p->entp);
4630Sstevel@tonic-gate 	free(p);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * Remove entry from the rddircache
4682170Sevanl  * the caller must own the autofs_rddir_cache_lock.
4690Sstevel@tonic-gate  */
4700Sstevel@tonic-gate static int
autofs_rddir_cache_delete(struct autofs_rddir_cache * rdcp)4712170Sevanl autofs_rddir_cache_delete(struct autofs_rddir_cache *rdcp)
4720Sstevel@tonic-gate {
4732170Sevanl 	struct autofs_rddir_cache *p, *prev;
4740Sstevel@tonic-gate 
4752170Sevanl 	assert(RW_LOCK_HELD(&autofs_rddir_cache_lock));
4760Sstevel@tonic-gate 	/*
4770Sstevel@tonic-gate 	 * Search cache for entry
4780Sstevel@tonic-gate 	 */
4790Sstevel@tonic-gate 	prev = NULL;
4800Sstevel@tonic-gate 	for (p = rddir_head; p != NULL; p = p->next) {
4810Sstevel@tonic-gate 		if (p == rdcp) {
4820Sstevel@tonic-gate 			/*
4830Sstevel@tonic-gate 			 * entry found, remove from list if not in use
4840Sstevel@tonic-gate 			 */
4850Sstevel@tonic-gate 			if (p->in_use)
4860Sstevel@tonic-gate 				return (EBUSY);
4870Sstevel@tonic-gate 			if (prev)
4880Sstevel@tonic-gate 				prev->next = p->next;
4890Sstevel@tonic-gate 			else
4900Sstevel@tonic-gate 				rddir_head = p->next;
4912170Sevanl 			autofs_rddir_cache_entry_free(p);
4920Sstevel@tonic-gate 			return (0);
4930Sstevel@tonic-gate 		}
4940Sstevel@tonic-gate 		prev = p;
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate 	syslog(LOG_ERR, "Couldn't find entry %x in cache\n", p);
4970Sstevel@tonic-gate 	return (ENOENT);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * Return entry that matches name, NULL otherwise.
5020Sstevel@tonic-gate  * Assumes the readers lock for this list has been grabed.
5030Sstevel@tonic-gate  */
5040Sstevel@tonic-gate struct dir_entry *
rddir_entry_lookup(char * name,struct dir_entry * list)5050Sstevel@tonic-gate rddir_entry_lookup(char *name, struct dir_entry *list)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate 	return (btree_lookup(list, name));
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate static void
build_dir_entry_list(struct autofs_rddir_cache * rdcp,struct dir_entry * list)5112170Sevanl build_dir_entry_list(struct autofs_rddir_cache *rdcp, struct dir_entry *list)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	struct dir_entry *p;
5140Sstevel@tonic-gate 	ulong_t offset = AUTOFS_DAEMONCOOKIE, offset_list = AUTOFS_DAEMONCOOKIE;
5150Sstevel@tonic-gate 	struct off_tbl *offtp, *last = NULL;
5160Sstevel@tonic-gate 	ino_t inonum = 4;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	assert(RW_LOCK_HELD(&rdcp->rwlock));
5190Sstevel@tonic-gate 	assert(rdcp->entp == NULL);
5200Sstevel@tonic-gate 	rdcp->entp = list;
5210Sstevel@tonic-gate 	for (p = list; p != NULL; p = p->next) {
5220Sstevel@tonic-gate 		p->nodeid = inonum;
5230Sstevel@tonic-gate 		p->offset = offset;
5240Sstevel@tonic-gate 		if (offset >= offset_list) {
5250Sstevel@tonic-gate 			/*
5260Sstevel@tonic-gate 			 * add node to index table
5270Sstevel@tonic-gate 			 */
5280Sstevel@tonic-gate 			offtp = (struct off_tbl *)
5290Sstevel@tonic-gate 				malloc(sizeof (struct off_tbl));
5300Sstevel@tonic-gate 			if (offtp != NULL) {
5310Sstevel@tonic-gate 				offtp->offset = offset;
5320Sstevel@tonic-gate 				offtp->first = p;
5330Sstevel@tonic-gate 				offtp->next = NULL;
5340Sstevel@tonic-gate 				offset_list += rdcp->bucket_size;
5350Sstevel@tonic-gate 			} else {
5360Sstevel@tonic-gate 				syslog(LOG_ERR,
5370Sstevel@tonic-gate "WARNING: build_dir_entry_list: could not add offset to index table\n");
5380Sstevel@tonic-gate 				continue;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 			/*
5410Sstevel@tonic-gate 			 * add to cache
5420Sstevel@tonic-gate 			 */
5430Sstevel@tonic-gate 			if (rdcp->offtp == NULL)
5440Sstevel@tonic-gate 				rdcp->offtp = offtp;
5450Sstevel@tonic-gate 			else
5460Sstevel@tonic-gate 				last->next = offtp;
5470Sstevel@tonic-gate 			last = offtp;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		offset++;
5500Sstevel@tonic-gate 		inonum += 2;		/* use even numbers in daemon */
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	rdcp->full = 1;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate mutex_t cleanup_lock;
5560Sstevel@tonic-gate cond_t cleanup_start_cv;
5570Sstevel@tonic-gate cond_t cleanup_done_cv;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate  * cache cleanup thread starting point
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate void
cache_cleanup(void)5630Sstevel@tonic-gate cache_cleanup(void)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	timestruc_t reltime;
5662170Sevanl 	struct autofs_rddir_cache *p, *next = NULL;
5670Sstevel@tonic-gate 	int error;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	mutex_init(&cleanup_lock, USYNC_THREAD, NULL);
5700Sstevel@tonic-gate 	cond_init(&cleanup_start_cv, USYNC_THREAD, NULL);
5710Sstevel@tonic-gate 	cond_init(&cleanup_done_cv, USYNC_THREAD, NULL);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
5740Sstevel@tonic-gate 	for (;;) {
5750Sstevel@tonic-gate 		reltime.tv_sec = RDDIR_CACHE_TIME/2;
5760Sstevel@tonic-gate 		reltime.tv_nsec = 0;
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		/*
5790Sstevel@tonic-gate 		 * delay RDDIR_CACHE_TIME seconds, or until some other thread
5800Sstevel@tonic-gate 		 * requests that I cleanup the caches
5810Sstevel@tonic-gate 		 */
5820Sstevel@tonic-gate 		if (error = cond_reltimedwait(
5830Sstevel@tonic-gate 		    &cleanup_start_cv, &cleanup_lock, &reltime)) {
5840Sstevel@tonic-gate 			if (error != ETIME) {
5850Sstevel@tonic-gate 				if (trace > 1)
5860Sstevel@tonic-gate 					trace_prt(1,
5870Sstevel@tonic-gate 					"cleanup thread wakeup (%d)\n", error);
5880Sstevel@tonic-gate 				continue;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 		}
5910Sstevel@tonic-gate 		mutex_unlock(&cleanup_lock);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		/*
5940Sstevel@tonic-gate 		 * Perform the cache cleanup
5950Sstevel@tonic-gate 		 */
5962170Sevanl 		rw_wrlock(&autofs_rddir_cache_lock);
5970Sstevel@tonic-gate 		for (p = rddir_head; p != NULL; p = next) {
5980Sstevel@tonic-gate 			next = p->next;
5990Sstevel@tonic-gate 			if (p->in_use > 0) {
6000Sstevel@tonic-gate 				/*
6010Sstevel@tonic-gate 				 * cache entry busy, skip it
6020Sstevel@tonic-gate 				 */
6030Sstevel@tonic-gate 				if (trace > 1) {
6040Sstevel@tonic-gate 					trace_prt(1,
6050Sstevel@tonic-gate 					"%s cache in use\n", p->map);
6060Sstevel@tonic-gate 				}
6070Sstevel@tonic-gate 				continue;
6080Sstevel@tonic-gate 			}
6090Sstevel@tonic-gate 			/*
6100Sstevel@tonic-gate 			 * Cache entry is not in use, and nobody can grab a
6112170Sevanl 			 * new reference since I'm holding the
6122170Sevanl 			 * autofs_rddir_cache_lock
6130Sstevel@tonic-gate 			 */
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 			/*
6160Sstevel@tonic-gate 			 * error will be zero if some thread signaled us asking
6170Sstevel@tonic-gate 			 * that the caches be freed. In such case, free caches
6180Sstevel@tonic-gate 			 * even if they're still valid and nobody is referencing
6190Sstevel@tonic-gate 			 * them at this time. Otherwise, free caches only
6200Sstevel@tonic-gate 			 * if their time to live (ttl) has expired.
6210Sstevel@tonic-gate 			 */
6220Sstevel@tonic-gate 			if (error == ETIME && (p->ttl > time((time_t *)NULL))) {
6230Sstevel@tonic-gate 				/*
6240Sstevel@tonic-gate 				 * Scheduled cache cleanup, if cache is still
6250Sstevel@tonic-gate 				 * valid don't free.
6260Sstevel@tonic-gate 				 */
6270Sstevel@tonic-gate 				if (trace > 1) {
6280Sstevel@tonic-gate 					trace_prt(1,
6290Sstevel@tonic-gate 					"%s cache still valid\n", p->map);
6300Sstevel@tonic-gate 				}
6310Sstevel@tonic-gate 				continue;
6320Sstevel@tonic-gate 			}
6330Sstevel@tonic-gate 			if (trace > 1)
6340Sstevel@tonic-gate 				trace_prt(1, "%s freeing cache\n", p->map);
6350Sstevel@tonic-gate 			assert(!p->in_use);
6362170Sevanl 			error = autofs_rddir_cache_delete(p);
6370Sstevel@tonic-gate 			assert(!error);
6380Sstevel@tonic-gate 		}
6392170Sevanl 		rw_unlock(&autofs_rddir_cache_lock);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 		/*
6420Sstevel@tonic-gate 		 * wakeup the thread/threads waiting for the
6430Sstevel@tonic-gate 		 * cleanup to finish
6440Sstevel@tonic-gate 		 */
6450Sstevel@tonic-gate 		mutex_lock(&cleanup_lock);
6460Sstevel@tonic-gate 		cond_broadcast(&cleanup_done_cv);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 	/* NOTREACHED */
6490Sstevel@tonic-gate }
650