xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cfsd/cfsd_subr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Various support routines.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <errno.h>
38*0Sstevel@tonic-gate #include <dirent.h>
39*0Sstevel@tonic-gate #include <wait.h>
40*0Sstevel@tonic-gate #include <stdarg.h>
41*0Sstevel@tonic-gate #include <limits.h>
42*0Sstevel@tonic-gate #include <rpc/rpc.h>
43*0Sstevel@tonic-gate #include <rpc/pmap_clnt.h> /* for pmap_unset */
44*0Sstevel@tonic-gate #include <string.h> /* strcmp */
45*0Sstevel@tonic-gate #include <signal.h>
46*0Sstevel@tonic-gate #include <unistd.h> /* setsid */
47*0Sstevel@tonic-gate #include <sys/utsname.h>
48*0Sstevel@tonic-gate #include <sys/param.h>
49*0Sstevel@tonic-gate #include <sys/mnttab.h>
50*0Sstevel@tonic-gate #include <sys/vfstab.h>
51*0Sstevel@tonic-gate #include <sys/types.h>
52*0Sstevel@tonic-gate #include <sys/stat.h>
53*0Sstevel@tonic-gate #include <memory.h>
54*0Sstevel@tonic-gate #include <stropts.h>
55*0Sstevel@tonic-gate #include <netconfig.h>
56*0Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */
57*0Sstevel@tonic-gate #include <thread.h>
58*0Sstevel@tonic-gate #include <synch.h>
59*0Sstevel@tonic-gate #include <mdbug/mdbug.h>
60*0Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
61*0Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
62*0Sstevel@tonic-gate #include <sys/fs/cachefs_ioctl.h>
63*0Sstevel@tonic-gate #include "cfsd.h"
64*0Sstevel@tonic-gate #include "cfsd_kmod.h"
65*0Sstevel@tonic-gate #include "cfsd_maptbl.h"
66*0Sstevel@tonic-gate #include "cfsd_logfile.h"
67*0Sstevel@tonic-gate #include "cfsd_fscache.h"
68*0Sstevel@tonic-gate #include "cfsd_cache.h"
69*0Sstevel@tonic-gate #include "cfsd_all.h"
70*0Sstevel@tonic-gate #include <common/cachefsd.h>
71*0Sstevel@tonic-gate #include <common/subr.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /* forward references */
74*0Sstevel@tonic-gate void *subr_mount_thread(void *datap);
75*0Sstevel@tonic-gate int subr_fsck_cache(const char *cachedirp);
76*0Sstevel@tonic-gate void subr_doexec(const char *fstype, char *newargv[], const char *progp);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  *			subr_add_mount
80*0Sstevel@tonic-gate  *
81*0Sstevel@tonic-gate  * Description:
82*0Sstevel@tonic-gate  *	Adds the specified file system to the data structures.
83*0Sstevel@tonic-gate  * Arguments:
84*0Sstevel@tonic-gate  *	allp	ptr to set of data structures
85*0Sstevel@tonic-gate  *	dirp	ptr to name of cache directory
86*0Sstevel@tonic-gate  *	idp	ptr to id of file system cache in dirp
87*0Sstevel@tonic-gate  * Returns:
88*0Sstevel@tonic-gate  * Preconditions:
89*0Sstevel@tonic-gate  *	precond(allp)
90*0Sstevel@tonic-gate  *	precond(dirp)
91*0Sstevel@tonic-gate  *	precond(idp)
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate void
subr_add_mount(cfsd_all_object_t * all_object_p,const char * dirp,const char * idp)94*0Sstevel@tonic-gate subr_add_mount(cfsd_all_object_t *all_object_p,
95*0Sstevel@tonic-gate 	const char *dirp,
96*0Sstevel@tonic-gate 	const char *idp)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	int xx;
99*0Sstevel@tonic-gate 	thread_t new_thread;
100*0Sstevel@tonic-gate 	cfsd_cache_object_t *cache_object_p;
101*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	dbug_enter("subr_add_mount");
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	dbug_precond(all_object_p);
106*0Sstevel@tonic-gate 	dbug_precond(dirp);
107*0Sstevel@tonic-gate 	dbug_precond(idp);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	dbug_print(("info", "cachedir %s, cacheid %s", dirp, idp));
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	/* find or create the cache object */
112*0Sstevel@tonic-gate 	all_lock(all_object_p);
113*0Sstevel@tonic-gate 	cache_object_p = all_cachelist_find(all_object_p, dirp);
114*0Sstevel@tonic-gate 	if (cache_object_p == NULL) {
115*0Sstevel@tonic-gate 		/* make the cache object */
116*0Sstevel@tonic-gate 		cache_object_p = cfsd_cache_create();
117*0Sstevel@tonic-gate 		xx = all_object_p->i_nextcacheid;
118*0Sstevel@tonic-gate 		xx = cache_setup(cache_object_p, dirp, xx);
119*0Sstevel@tonic-gate 		if (xx == 0) {
120*0Sstevel@tonic-gate 			dbug_print(("error", "invalid cache %s", dirp));
121*0Sstevel@tonic-gate 			cfsd_cache_destroy(cache_object_p);
122*0Sstevel@tonic-gate 			all_unlock(all_object_p);
123*0Sstevel@tonic-gate 			dbug_leave("subr_add_mount");
124*0Sstevel@tonic-gate 			return;
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		all_cachelist_add(all_object_p, cache_object_p);
127*0Sstevel@tonic-gate 		all_cachefstab_update(all_object_p);
128*0Sstevel@tonic-gate 	}
129*0Sstevel@tonic-gate 	cache_lock(cache_object_p);
130*0Sstevel@tonic-gate 	cache_object_p->i_refcnt++;
131*0Sstevel@tonic-gate 	cache_unlock(cache_object_p);
132*0Sstevel@tonic-gate 	all_unlock(all_object_p);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/* find or create the fscache object */
135*0Sstevel@tonic-gate 	cache_lock(cache_object_p);
136*0Sstevel@tonic-gate 	fscache_object_p = cache_fscachelist_find(cache_object_p, idp);
137*0Sstevel@tonic-gate 	if (fscache_object_p == NULL) {
138*0Sstevel@tonic-gate 		/* make the fscache object and add it to the list */
139*0Sstevel@tonic-gate 		xx = cache_object_p->i_nextfscacheid;
140*0Sstevel@tonic-gate 		fscache_object_p = cfsd_fscache_create(idp, dirp, xx);
141*0Sstevel@tonic-gate 		cache_fscachelist_add(cache_object_p, fscache_object_p);
142*0Sstevel@tonic-gate 	} else {
143*0Sstevel@tonic-gate 		/* don't do any more if already mounted */
144*0Sstevel@tonic-gate 		fscache_lock(fscache_object_p);
145*0Sstevel@tonic-gate 		if (fscache_object_p->i_mounted) {
146*0Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
147*0Sstevel@tonic-gate 			fscache_unlock(fscache_object_p);
148*0Sstevel@tonic-gate 			cache_unlock(cache_object_p);
149*0Sstevel@tonic-gate 			dbug_print(("info", "fscache already mounted"));
150*0Sstevel@tonic-gate 			dbug_leave("subr_add_mount");
151*0Sstevel@tonic-gate 			return;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 		fscache_unlock(fscache_object_p);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
157*0Sstevel@tonic-gate 	fscache_object_p->i_refcnt++;
158*0Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
159*0Sstevel@tonic-gate 	cache_unlock(cache_object_p);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	/* init the fscache object with mount information */
162*0Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
163*0Sstevel@tonic-gate 	fscache_setup(fscache_object_p);
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	/* start the disconnect thread if necessary */
166*0Sstevel@tonic-gate 	if (fscache_object_p->i_disconnectable &&
167*0Sstevel@tonic-gate 	    fscache_object_p->i_mounted &&
168*0Sstevel@tonic-gate 	    (fscache_object_p->i_threaded == 0) &&
169*0Sstevel@tonic-gate 	    (strcmp(fscache_object_p->i_name, "rootcache") != 0)) {
170*0Sstevel@tonic-gate 		fscache_object_p->i_refcnt++;
171*0Sstevel@tonic-gate 		fscache_object_p->i_threaded = 1;
172*0Sstevel@tonic-gate 		xx = thr_create(NULL, 0, subr_mount_thread, fscache_object_p,
173*0Sstevel@tonic-gate 		    THR_DETACHED | THR_NEW_LWP, &new_thread);
174*0Sstevel@tonic-gate 		if (xx) {
175*0Sstevel@tonic-gate 			/* XXX cachefs kmod cannot allow transition */
176*0Sstevel@tonic-gate 			dbug_print(("error", "mount thr_create failed %d", xx));
177*0Sstevel@tonic-gate 			fscache_object_p->i_refcnt--;
178*0Sstevel@tonic-gate 			fscache_object_p->i_threaded = 0;
179*0Sstevel@tonic-gate 		}
180*0Sstevel@tonic-gate 		fscache_object_p->i_threadid = new_thread;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	fscache_object_p->i_refcnt--;
183*0Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	cache_lock(cache_object_p);
186*0Sstevel@tonic-gate 	cache_object_p->i_refcnt--;
187*0Sstevel@tonic-gate 	cache_unlock(cache_object_p);
188*0Sstevel@tonic-gate 	dbug_leave("subr_add_mount");
189*0Sstevel@tonic-gate }
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate  * ------------------------------------------------------------
193*0Sstevel@tonic-gate  *			subr_mount_thread
194*0Sstevel@tonic-gate  *
195*0Sstevel@tonic-gate  * Description:
196*0Sstevel@tonic-gate  *	Called when a thread is created via thr_create to process
197*0Sstevel@tonic-gate  *	an fscache.
198*0Sstevel@tonic-gate  * Arguments:
199*0Sstevel@tonic-gate  *	datap	ptr to cfsd_fscache to process
200*0Sstevel@tonic-gate  * Returns:
201*0Sstevel@tonic-gate  *	Returns NULL.
202*0Sstevel@tonic-gate  * Preconditions:
203*0Sstevel@tonic-gate  *	precond(datap)
204*0Sstevel@tonic-gate  */
205*0Sstevel@tonic-gate void *
subr_mount_thread(void * datap)206*0Sstevel@tonic-gate subr_mount_thread(void *datap)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	dbug_enter("subr_mount_thread");
211*0Sstevel@tonic-gate 	dbug_precond(datap);
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	fscache_object_p = (cfsd_fscache_object_t *)datap;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	fscache_process(fscache_object_p);
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	fscache_lock(fscache_object_p);
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/* close down the message file descriptor */
220*0Sstevel@tonic-gate 	if (fscache_object_p->i_ofd >= 0) {
221*0Sstevel@tonic-gate 		if (close(fscache_object_p->i_ofd))
222*0Sstevel@tonic-gate 			dbug_print(("error", "cannot close fscache fd error %d",
223*0Sstevel@tonic-gate 			    errno));
224*0Sstevel@tonic-gate 		fscache_object_p->i_ofd = -1;
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	fscache_object_p->i_threaded = 0;
228*0Sstevel@tonic-gate 	fscache_object_p->i_refcnt--;
229*0Sstevel@tonic-gate 	fscache_unlock(fscache_object_p);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	dbug_leave("subr_mount_thread");
232*0Sstevel@tonic-gate 	return (NULL);
233*0Sstevel@tonic-gate }
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate /*
236*0Sstevel@tonic-gate  * ------------------------------------------------------------
237*0Sstevel@tonic-gate  *			subr_cache_setup
238*0Sstevel@tonic-gate  *
239*0Sstevel@tonic-gate  * Description:
240*0Sstevel@tonic-gate  *	Called once when the daemon starts up to get the current state
241*0Sstevel@tonic-gate  *	of caches reflected in the daemon.
242*0Sstevel@tonic-gate  * Arguments:
243*0Sstevel@tonic-gate  *	allp
244*0Sstevel@tonic-gate  * Returns:
245*0Sstevel@tonic-gate  * Preconditions:
246*0Sstevel@tonic-gate  *	precond(allp)
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate void
subr_cache_setup(cfsd_all_object_t * all_object_p)249*0Sstevel@tonic-gate subr_cache_setup(cfsd_all_object_t *all_object_p)
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	cfsd_cache_object_t *cache_object_p;
252*0Sstevel@tonic-gate 	int fixcachefstab = 0;
253*0Sstevel@tonic-gate 	int xx;
254*0Sstevel@tonic-gate 	FILE *fin;
255*0Sstevel@tonic-gate 	char buf[MAXPATHLEN];
256*0Sstevel@tonic-gate 	struct mnttab minfo;
257*0Sstevel@tonic-gate 	struct mnttab mpref;
258*0Sstevel@tonic-gate 	char *cp;
259*0Sstevel@tonic-gate 	char *xcp;
260*0Sstevel@tonic-gate 	struct vfstab vinfo;
261*0Sstevel@tonic-gate 	struct vfstab vpref;
262*0Sstevel@tonic-gate 	size_t index;
263*0Sstevel@tonic-gate 	int lockfd;
264*0Sstevel@tonic-gate 	DIR *dirp;
265*0Sstevel@tonic-gate 	char pathname[MAXPATHLEN];
266*0Sstevel@tonic-gate 	int len;
267*0Sstevel@tonic-gate 	struct dirent64 *entp;
268*0Sstevel@tonic-gate 	struct stat64 sinfo;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	dbug_enter("subr_cache_setup");
271*0Sstevel@tonic-gate 	dbug_precond(all_object_p);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	all_lock(all_object_p);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	/* find all the caches indicated in the CACHEFSTAB file */
276*0Sstevel@tonic-gate 	fin = fopen(CACHEFSTAB, "r");
277*0Sstevel@tonic-gate 	if (fin == NULL) {
278*0Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", CACHEFSTAB));
279*0Sstevel@tonic-gate 	} else {
280*0Sstevel@tonic-gate 		while (fgets(buf, sizeof (buf), fin) != NULL) {
281*0Sstevel@tonic-gate 			if (strlen(buf) == 1)
282*0Sstevel@tonic-gate 				continue;
283*0Sstevel@tonic-gate 			/*
284*0Sstevel@tonic-gate 			 * if the line did not fit in the buffer
285*0Sstevel@tonic-gate 			 * it is invalid (i.e. no newline char)
286*0Sstevel@tonic-gate 			 */
287*0Sstevel@tonic-gate 			dbug_precond(buf[(strlen(buf) - 1)] == '\n');
288*0Sstevel@tonic-gate 			if (buf[(strlen(buf) - 1)] != '\n') {
289*0Sstevel@tonic-gate #if 0
290*0Sstevel@tonic-gate 				/*
291*0Sstevel@tonic-gate 				 * if the line is invalid read until
292*0Sstevel@tonic-gate 				 * you get to the next line.
293*0Sstevel@tonic-gate 				 * we only need to do this if we are
294*0Sstevel@tonic-gate 				 * going to continue
295*0Sstevel@tonic-gate 				 */
296*0Sstevel@tonic-gate 				do {
297*0Sstevel@tonic-gate 					cp = fgets(buf, sizeof (buf), fin);
298*0Sstevel@tonic-gate 				} while ((cp != NULL) &&
299*0Sstevel@tonic-gate 				    (buf[(strlen(buf) - 1)] != '\n'));
300*0Sstevel@tonic-gate #endif
301*0Sstevel@tonic-gate 				break;
302*0Sstevel@tonic-gate 			}
303*0Sstevel@tonic-gate 			buf[strlen(buf) - 1] = '\0';
304*0Sstevel@tonic-gate 			dbug_print(("info", "cachefstab cache \"%s\"", buf));
305*0Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, buf);
306*0Sstevel@tonic-gate 			if (cache_object_p == NULL) {
307*0Sstevel@tonic-gate 				/* make the cache object */
308*0Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
309*0Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
310*0Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, buf, xx);
311*0Sstevel@tonic-gate 				if (xx == 0) {
312*0Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
313*0Sstevel@tonic-gate 					fixcachefstab++;
314*0Sstevel@tonic-gate 				} else {
315*0Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
316*0Sstevel@tonic-gate 					    cache_object_p);
317*0Sstevel@tonic-gate 				}
318*0Sstevel@tonic-gate 			} else {
319*0Sstevel@tonic-gate 				fixcachefstab++;
320*0Sstevel@tonic-gate 			}
321*0Sstevel@tonic-gate 		}
322*0Sstevel@tonic-gate 		if (fclose(fin))
323*0Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
324*0Sstevel@tonic-gate 			    CACHEFSTAB, errno));
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	/* read the mnttab file looking for caches we may have missed */
328*0Sstevel@tonic-gate 	fin = fopen(MNTTAB, "r");
329*0Sstevel@tonic-gate 	if (fin == NULL) {
330*0Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", MNTTAB));
331*0Sstevel@tonic-gate 	} else {
332*0Sstevel@tonic-gate 		mpref.mnt_special = NULL;
333*0Sstevel@tonic-gate 		mpref.mnt_mountp = NULL;
334*0Sstevel@tonic-gate 		mpref.mnt_fstype = "cachefs";
335*0Sstevel@tonic-gate 		mpref.mnt_mntopts = NULL;
336*0Sstevel@tonic-gate 		mpref.mnt_time = NULL;
337*0Sstevel@tonic-gate 		while ((xx = getmntany(fin, &minfo, &mpref)) != -1) {
338*0Sstevel@tonic-gate 			if (xx != 0)
339*0Sstevel@tonic-gate 				continue;
340*0Sstevel@tonic-gate 			cp = hasmntopt(&minfo, "cachedir=");
341*0Sstevel@tonic-gate 			if (cp == NULL)
342*0Sstevel@tonic-gate 				cp = "/cache"; /* XXX define in mount.c */
343*0Sstevel@tonic-gate 			else {
344*0Sstevel@tonic-gate 				cp += 9;
345*0Sstevel@tonic-gate 				xcp = strchr(cp, ',');
346*0Sstevel@tonic-gate 				if (xcp)
347*0Sstevel@tonic-gate 					*xcp = '\0';
348*0Sstevel@tonic-gate 			}
349*0Sstevel@tonic-gate 			dbug_print(("info", "mnttab cache \"%s\"", cp));
350*0Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, cp);
351*0Sstevel@tonic-gate 			if (cache_object_p == NULL) {
352*0Sstevel@tonic-gate 				/* make the cache object */
353*0Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
354*0Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
355*0Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, cp, xx);
356*0Sstevel@tonic-gate 				if (xx == 0) {
357*0Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
358*0Sstevel@tonic-gate 					fixcachefstab++;
359*0Sstevel@tonic-gate 				} else {
360*0Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
361*0Sstevel@tonic-gate 					    cache_object_p);
362*0Sstevel@tonic-gate 				}
363*0Sstevel@tonic-gate 			} else {
364*0Sstevel@tonic-gate 				fixcachefstab++;
365*0Sstevel@tonic-gate 			}
366*0Sstevel@tonic-gate 		}
367*0Sstevel@tonic-gate 		if (fclose(fin))
368*0Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
369*0Sstevel@tonic-gate 			    MNTTAB, errno));
370*0Sstevel@tonic-gate 	}
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	/* read the vfstab file looking for caches we may have missed */
373*0Sstevel@tonic-gate 	fin = fopen(VFSTAB, "r");
374*0Sstevel@tonic-gate 	if (fin == NULL) {
375*0Sstevel@tonic-gate 		dbug_print(("info", "%s does not exist", VFSTAB));
376*0Sstevel@tonic-gate 	} else {
377*0Sstevel@tonic-gate 		vpref.vfs_special = NULL;
378*0Sstevel@tonic-gate 		vpref.vfs_fsckdev = NULL;
379*0Sstevel@tonic-gate 		vpref.vfs_mountp = NULL;
380*0Sstevel@tonic-gate 		vpref.vfs_fstype = "cachefs";
381*0Sstevel@tonic-gate 		vpref.vfs_fsckpass = NULL;
382*0Sstevel@tonic-gate 		vpref.vfs_automnt = NULL;
383*0Sstevel@tonic-gate 		vpref.vfs_mntopts = NULL;
384*0Sstevel@tonic-gate 		while ((xx = getvfsany(fin, &vinfo, &vpref)) != -1) {
385*0Sstevel@tonic-gate 			if (xx != 0)
386*0Sstevel@tonic-gate 				continue;
387*0Sstevel@tonic-gate 			cp = strstr(vinfo.vfs_mntopts, "cachedir=");
388*0Sstevel@tonic-gate 			if (cp == NULL)
389*0Sstevel@tonic-gate 				cp = "/cache"; /* XXX define in mount.c */
390*0Sstevel@tonic-gate 			else {
391*0Sstevel@tonic-gate 				cp += 9;
392*0Sstevel@tonic-gate 				xcp = strchr(cp, ',');
393*0Sstevel@tonic-gate 				if (xcp)
394*0Sstevel@tonic-gate 					*xcp = '\0';
395*0Sstevel@tonic-gate 			}
396*0Sstevel@tonic-gate 			dbug_print(("info", "vfstab cache \"%s\"", cp));
397*0Sstevel@tonic-gate 			cache_object_p = all_cachelist_find(all_object_p, cp);
398*0Sstevel@tonic-gate 			if (cache_object_p == NULL) {
399*0Sstevel@tonic-gate 				/* make the cache object */
400*0Sstevel@tonic-gate 				cache_object_p = cfsd_cache_create();
401*0Sstevel@tonic-gate 				xx = all_object_p->i_nextcacheid;
402*0Sstevel@tonic-gate 				xx = cache_setup(cache_object_p, cp, xx);
403*0Sstevel@tonic-gate 				if (xx == 0) {
404*0Sstevel@tonic-gate 					cfsd_cache_destroy(cache_object_p);
405*0Sstevel@tonic-gate 				} else {
406*0Sstevel@tonic-gate 					all_cachelist_add(all_object_p,
407*0Sstevel@tonic-gate 					    cache_object_p);
408*0Sstevel@tonic-gate 					fixcachefstab++;
409*0Sstevel@tonic-gate 				}
410*0Sstevel@tonic-gate 			}
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 		if (fclose(fin))
413*0Sstevel@tonic-gate 			dbug_print(("err", "cannot close %s, %d",
414*0Sstevel@tonic-gate 			    VFSTAB, errno));
415*0Sstevel@tonic-gate 	}
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	/* fix up the CACHEFSTAB file if it is out of date */
418*0Sstevel@tonic-gate 	if (fixcachefstab)
419*0Sstevel@tonic-gate 		all_cachefstab_update(all_object_p);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	/*
422*0Sstevel@tonic-gate 	 * now for each cache we found,
423*0Sstevel@tonic-gate 	 * find all the file systems in the cache
424*0Sstevel@tonic-gate 	 */
425*0Sstevel@tonic-gate 	for (index = 0; index < all_object_p->i_cachecount; index++) {
426*0Sstevel@tonic-gate 		cache_object_p = all_cachelist_at(all_object_p, index);
427*0Sstevel@tonic-gate 		dbug_assert(cache_object_p);
428*0Sstevel@tonic-gate 		cache_lock(cache_object_p);
429*0Sstevel@tonic-gate 		cache_object_p->i_refcnt++;
430*0Sstevel@tonic-gate 		cache_unlock(cache_object_p);
431*0Sstevel@tonic-gate 		all_unlock(all_object_p);
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 		/* fix up the cache if necessary */
434*0Sstevel@tonic-gate 		xx = subr_fsck_cache(cache_object_p->i_cachedir);
435*0Sstevel@tonic-gate 		if (xx != 0) {
436*0Sstevel@tonic-gate 			dbug_print(("error", "could not fix up cache %d",
437*0Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
438*0Sstevel@tonic-gate 			all_lock(all_object_p);
439*0Sstevel@tonic-gate 			cache_lock(cache_object_p);
440*0Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
441*0Sstevel@tonic-gate 			cache_unlock(cache_object_p);
442*0Sstevel@tonic-gate 			continue;
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 		/* lock out activity on the cache */
446*0Sstevel@tonic-gate 		lockfd = cachefs_dir_lock(cache_object_p->i_cachedir, 0);
447*0Sstevel@tonic-gate 		if (lockfd < 0) {
448*0Sstevel@tonic-gate 			dbug_print(("error", "cannot aquire cache lock on %s",
449*0Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
450*0Sstevel@tonic-gate 			all_lock(all_object_p);
451*0Sstevel@tonic-gate 			cache_lock(cache_object_p);
452*0Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
453*0Sstevel@tonic-gate 			cache_unlock(cache_object_p);
454*0Sstevel@tonic-gate 			continue;
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 		/* open the cache directory */
458*0Sstevel@tonic-gate 		dirp = opendir(cache_object_p->i_cachedir);
459*0Sstevel@tonic-gate 		if (dirp == NULL) {
460*0Sstevel@tonic-gate 			dbug_print(("error", "cannot open dir %s",
461*0Sstevel@tonic-gate 			    cache_object_p->i_cachedir));
462*0Sstevel@tonic-gate 			cachefs_dir_unlock(lockfd);
463*0Sstevel@tonic-gate 			all_lock(all_object_p);
464*0Sstevel@tonic-gate 			cache_lock(cache_object_p);
465*0Sstevel@tonic-gate 			cache_object_p->i_refcnt--;
466*0Sstevel@tonic-gate 			cache_unlock(cache_object_p);
467*0Sstevel@tonic-gate 			continue;
468*0Sstevel@tonic-gate 		}
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 		strlcpy(pathname, cache_object_p->i_cachedir,
471*0Sstevel@tonic-gate 		    sizeof (pathname));
472*0Sstevel@tonic-gate 		strlcat(pathname, "/", sizeof (pathname));
473*0Sstevel@tonic-gate 		len = strlen(pathname);
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 		/* read the directory entries */
476*0Sstevel@tonic-gate 		while ((entp = readdir64(dirp)) != NULL) {
477*0Sstevel@tonic-gate 			/* skip . and .. */
478*0Sstevel@tonic-gate 			if ((strcmp(entp->d_name, ".") == 0) ||
479*0Sstevel@tonic-gate 			    (strcmp(entp->d_name, "..") == 0))
480*0Sstevel@tonic-gate 				continue;
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 			pathname[len] = '\0';
483*0Sstevel@tonic-gate 			strlcat(pathname, entp->d_name, sizeof (pathname));
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 			/* get info on the file */
486*0Sstevel@tonic-gate 			xx = lstat64(pathname, &sinfo);
487*0Sstevel@tonic-gate 			if (xx != 0) {
488*0Sstevel@tonic-gate 				dbug_print(("error",
489*0Sstevel@tonic-gate 				    "cannot stat %s %d", pathname, errno));
490*0Sstevel@tonic-gate 				continue;
491*0Sstevel@tonic-gate 			}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 			/* skip unless a symbolic link */
494*0Sstevel@tonic-gate 			if (!S_ISLNK(sinfo.st_mode))
495*0Sstevel@tonic-gate 				continue;
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 			/* add this file system to the list */
498*0Sstevel@tonic-gate 			subr_add_mount(all_object_p, cache_object_p->i_cachedir,
499*0Sstevel@tonic-gate 			    entp->d_name);
500*0Sstevel@tonic-gate 		}
501*0Sstevel@tonic-gate 		if (closedir(dirp))
502*0Sstevel@tonic-gate 			dbug_print(("err", "cannot close dir, %d", errno));
503*0Sstevel@tonic-gate 		cachefs_dir_unlock(lockfd);
504*0Sstevel@tonic-gate 		all_lock(all_object_p);
505*0Sstevel@tonic-gate 		cache_lock(cache_object_p);
506*0Sstevel@tonic-gate 		cache_object_p->i_refcnt--;
507*0Sstevel@tonic-gate 		cache_unlock(cache_object_p);
508*0Sstevel@tonic-gate 	}
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	all_unlock(all_object_p);
511*0Sstevel@tonic-gate 	dbug_leave("subr_cache_setup");
512*0Sstevel@tonic-gate }
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate /*
515*0Sstevel@tonic-gate  * ------------------------------------------------------------
516*0Sstevel@tonic-gate  *			subr_fsck_cache
517*0Sstevel@tonic-gate  *
518*0Sstevel@tonic-gate  * Description:
519*0Sstevel@tonic-gate  *	Fixes the cache if necessary.
520*0Sstevel@tonic-gate  * Arguments:
521*0Sstevel@tonic-gate  *	cachedirp
522*0Sstevel@tonic-gate  * Returns:
523*0Sstevel@tonic-gate  *	Returns 0 for success !0 if the cache is not fixed.
524*0Sstevel@tonic-gate  * Preconditions:
525*0Sstevel@tonic-gate  *	precond(cachedirp)
526*0Sstevel@tonic-gate  */
527*0Sstevel@tonic-gate int
subr_fsck_cache(const char * cachedirp)528*0Sstevel@tonic-gate subr_fsck_cache(const char *cachedirp)
529*0Sstevel@tonic-gate {
530*0Sstevel@tonic-gate 	char *fsck_argv[4];
531*0Sstevel@tonic-gate 	int status = 0;
532*0Sstevel@tonic-gate 	pid_t pid;
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	dbug_enter("subr_fsck_cache");
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	dbug_precond(cachedirp);
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	fsck_argv[1] = "fsck";
539*0Sstevel@tonic-gate 	fsck_argv[2] = (char *)cachedirp;
540*0Sstevel@tonic-gate 	fsck_argv[3] = NULL;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	dbug_print(("info", "about to fsck %s", cachedirp));
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	/* fork */
545*0Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
546*0Sstevel@tonic-gate 		dbug_print(("error", "could not fork fsck %d", errno));
547*0Sstevel@tonic-gate 		dbug_leave("subr_fsck_cache");
548*0Sstevel@tonic-gate 		return (1);
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	if (pid == 0) {
552*0Sstevel@tonic-gate 		/* do the fsck */
553*0Sstevel@tonic-gate 		subr_doexec("cachefs", fsck_argv, "fsck");
554*0Sstevel@tonic-gate 	} else {
555*0Sstevel@tonic-gate 		/* wait for the child to exit */
556*0Sstevel@tonic-gate 		if (waitpid(pid, &status, 0) == -1) {
557*0Sstevel@tonic-gate 			dbug_print(("error", "fsck wait failed %d", errno));
558*0Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
559*0Sstevel@tonic-gate 			return (1);
560*0Sstevel@tonic-gate 		}
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 		if (!WIFEXITED(status)) {
563*0Sstevel@tonic-gate 			dbug_print(("error", "fsck did not exit"));
564*0Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
565*0Sstevel@tonic-gate 			return (1);
566*0Sstevel@tonic-gate 		}
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		if (WEXITSTATUS(status) != 0) {
569*0Sstevel@tonic-gate 			dbug_print(("error", "fsck failed"));
570*0Sstevel@tonic-gate 			dbug_leave("subr_fsck_cache");
571*0Sstevel@tonic-gate 			return (1);
572*0Sstevel@tonic-gate 		}
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 	dbug_leave("subr_fsck_cache");
575*0Sstevel@tonic-gate 	return (0);
576*0Sstevel@tonic-gate }
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate /*
579*0Sstevel@tonic-gate  * ------------------------------------------------------------
580*0Sstevel@tonic-gate  *			subr_doexec
581*0Sstevel@tonic-gate  *
582*0Sstevel@tonic-gate  * Description:
583*0Sstevel@tonic-gate  *	Execs the specified program with the specified command line arguments.
584*0Sstevel@tonic-gate  *	This function never returns.
585*0Sstevel@tonic-gate  * Arguments:
586*0Sstevel@tonic-gate  *	fstype	type of file system
587*0Sstevel@tonic-gate  *	newargv	command line arguments
588*0Sstevel@tonic-gate  *	progp	name of program to exec
589*0Sstevel@tonic-gate  * Returns:
590*0Sstevel@tonic-gate  * Preconditions:
591*0Sstevel@tonic-gate  *	precond(fstype)
592*0Sstevel@tonic-gate  *	precond(newargv)
593*0Sstevel@tonic-gate  *	precond(progp)
594*0Sstevel@tonic-gate  */
595*0Sstevel@tonic-gate void
subr_doexec(const char * fstype,char * newargv[],const char * progp)596*0Sstevel@tonic-gate subr_doexec(const char *fstype, char *newargv[], const char *progp)
597*0Sstevel@tonic-gate {
598*0Sstevel@tonic-gate #define	VFS_PATH	"/usr/lib/fs"
599*0Sstevel@tonic-gate #define	ALT_PATH	"/etc/fs"
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	char	full_path[MAXPATHLEN];
602*0Sstevel@tonic-gate 	char	alter_path[MAXPATHLEN];
603*0Sstevel@tonic-gate 	char	*vfs_path = VFS_PATH;
604*0Sstevel@tonic-gate 	char	*alt_path = ALT_PATH;
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	dbug_enter("subr_doexec");
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	dbug_precond(fstype);
609*0Sstevel@tonic-gate 	dbug_precond(newargv);
610*0Sstevel@tonic-gate 	dbug_precond(progp);
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	/* build the full pathname of the fstype dependent command. */
613*0Sstevel@tonic-gate 	snprintf(full_path, sizeof (full_path), "%s/%s/%s", vfs_path,
614*0Sstevel@tonic-gate 	    fstype, progp);
615*0Sstevel@tonic-gate 	snprintf(alter_path, sizeof (alter_path), "%s/%s/%s", alt_path,
616*0Sstevel@tonic-gate 	    fstype, progp);
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 	/* if the program exists */
619*0Sstevel@tonic-gate 	if (access(full_path, X_OK) == 0) {
620*0Sstevel@tonic-gate 		/* invoke the program */
621*0Sstevel@tonic-gate 		execv(full_path, &newargv[1]);
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 		/* if wrong permissions */
624*0Sstevel@tonic-gate 		if (errno == EACCES) {
625*0Sstevel@tonic-gate 			dbug_print(("error", "cannot execute %s %s",
626*0Sstevel@tonic-gate 			    full_path, strerror(errno)));
627*0Sstevel@tonic-gate 		}
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate #ifdef OBSOLETE
630*0Sstevel@tonic-gate 		/* if it did not work and the shell might make it */
631*0Sstevel@tonic-gate 		if (errno == ENOEXEC) {
632*0Sstevel@tonic-gate 			newargv[0] = "sh";
633*0Sstevel@tonic-gate 			newargv[1] = full_path;
634*0Sstevel@tonic-gate 			execv("/sbin/sh", &newargv[0]);
635*0Sstevel@tonic-gate 		}
636*0Sstevel@tonic-gate #endif
637*0Sstevel@tonic-gate 	}
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate #ifdef OBSOLETE
640*0Sstevel@tonic-gate 	/* try the alternate path */
641*0Sstevel@tonic-gate 	execv(alter_path, &newargv[1]);
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate 	/* if wrong permissions */
644*0Sstevel@tonic-gate 	if (errno == EACCES) {
645*0Sstevel@tonic-gate 		dbug_print(("error", "cannot execute %s %s",
646*0Sstevel@tonic-gate 		    alter_path, strerror(errno)));
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	/* if it did not work and the shell might make it */
650*0Sstevel@tonic-gate 	if (errno == ENOEXEC) {
651*0Sstevel@tonic-gate 		newargv[0] = "sh";
652*0Sstevel@tonic-gate 		newargv[1] = alter_path;
653*0Sstevel@tonic-gate 		execv("/sbin/sh", &newargv[0]);
654*0Sstevel@tonic-gate 	}
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate 	dbug_print(("error", "operation not applicable to FSType %s", fstype));
657*0Sstevel@tonic-gate #endif
658*0Sstevel@tonic-gate 	dbug_leave("subr_doexec");
659*0Sstevel@tonic-gate 	_exit(1);
660*0Sstevel@tonic-gate }
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate /*
663*0Sstevel@tonic-gate  * ------------------------------------------------------------
664*0Sstevel@tonic-gate  *			pr_err
665*0Sstevel@tonic-gate  *
666*0Sstevel@tonic-gate  * Description:
667*0Sstevel@tonic-gate  * Arguments:
668*0Sstevel@tonic-gate  *	fmt
669*0Sstevel@tonic-gate  * Returns:
670*0Sstevel@tonic-gate  * Preconditions:
671*0Sstevel@tonic-gate  *	precond(fmt)
672*0Sstevel@tonic-gate  */
673*0Sstevel@tonic-gate void
pr_err(char * fmt,...)674*0Sstevel@tonic-gate pr_err(char *fmt, ...)
675*0Sstevel@tonic-gate {
676*0Sstevel@tonic-gate 	va_list ap;
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 	va_start(ap, fmt);
679*0Sstevel@tonic-gate 	(void) fprintf(stderr, "cachefsd -F cachefs: ");
680*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
681*0Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
682*0Sstevel@tonic-gate 	va_end(ap);
683*0Sstevel@tonic-gate }
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate /*
687*0Sstevel@tonic-gate  *			subr_strdup
688*0Sstevel@tonic-gate  *
689*0Sstevel@tonic-gate  * Description:
690*0Sstevel@tonic-gate  *	Returns the string dupped.  Returns NULL if passed NULL.
691*0Sstevel@tonic-gate  *	Calls new to allocate memory.
692*0Sstevel@tonic-gate  * Arguments:
693*0Sstevel@tonic-gate  *	strp
694*0Sstevel@tonic-gate  * Returns:
695*0Sstevel@tonic-gate  * Preconditions:
696*0Sstevel@tonic-gate  */
697*0Sstevel@tonic-gate char *
subr_strdup(const char * strp)698*0Sstevel@tonic-gate subr_strdup(const char *strp)
699*0Sstevel@tonic-gate {
700*0Sstevel@tonic-gate 	char *retp = NULL;
701*0Sstevel@tonic-gate 	int len;
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	if (strp) {
704*0Sstevel@tonic-gate 		len = strlen(strp) + 1;
705*0Sstevel@tonic-gate 		retp = cfsd_calloc(len);
706*0Sstevel@tonic-gate 		if (retp)
707*0Sstevel@tonic-gate 			strlcpy(retp, strp, len);
708*0Sstevel@tonic-gate 	}
709*0Sstevel@tonic-gate 	return (retp);
710*0Sstevel@tonic-gate }
711*0Sstevel@tonic-gate /*
712*0Sstevel@tonic-gate  * -----------------------------------------------------------------
713*0Sstevel@tonic-gate  *			cfsd_calloc
714*0Sstevel@tonic-gate  *
715*0Sstevel@tonic-gate  * Description:
716*0Sstevel@tonic-gate  *	allocates memory of a given size, will retry if error
717*0Sstevel@tonic-gate  * Arguments:
718*0Sstevel@tonic-gate  *	size
719*0Sstevel@tonic-gate  * Returns:
720*0Sstevel@tonic-gate  *	pointer to memory
721*0Sstevel@tonic-gate  * Preconditions:
722*0Sstevel@tonic-gate  *	precond(size)
723*0Sstevel@tonic-gate  */
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate void *
cfsd_calloc(int size)726*0Sstevel@tonic-gate cfsd_calloc(int size)
727*0Sstevel@tonic-gate {
728*0Sstevel@tonic-gate 	void *alloc_ptr;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	dbug_enter("cfsd_calloc");
731*0Sstevel@tonic-gate 	dbug_precond(size);
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 	/* allocate memory, if calloc fails sleep and retry */
734*0Sstevel@tonic-gate 	while ((alloc_ptr = calloc(size, 1)) == NULL) {
735*0Sstevel@tonic-gate 		cfsd_sleep(5);
736*0Sstevel@tonic-gate 	}
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	dbug_leave("cfsd_calloc");
739*0Sstevel@tonic-gate 	return (alloc_ptr);
740*0Sstevel@tonic-gate }
741*0Sstevel@tonic-gate /*
742*0Sstevel@tonic-gate  * -----------------------------------------------------------------
743*0Sstevel@tonic-gate  *			cfsd_free
744*0Sstevel@tonic-gate  *
745*0Sstevel@tonic-gate  * Description:
746*0Sstevel@tonic-gate  *	frees memory allocated from cfsd_calloc
747*0Sstevel@tonic-gate  * Arguments:
748*0Sstevel@tonic-gate  *	pointer to memeory
749*0Sstevel@tonic-gate  * Returns:
750*0Sstevel@tonic-gate  *	none
751*0Sstevel@tonic-gate  * Preconditions:
752*0Sstevel@tonic-gate  *	precond(size)
753*0Sstevel@tonic-gate  */
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate void
cfsd_free(void * free_ptr)756*0Sstevel@tonic-gate cfsd_free(void *free_ptr)
757*0Sstevel@tonic-gate {
758*0Sstevel@tonic-gate 	dbug_enter("cfsd_free");
759*0Sstevel@tonic-gate 	dbug_precond(free_ptr);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	/* free memory */
762*0Sstevel@tonic-gate 	if (free_ptr)
763*0Sstevel@tonic-gate 		free(free_ptr);
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 	dbug_leave("cfsd_free");
766*0Sstevel@tonic-gate }
767*0Sstevel@tonic-gate /*
768*0Sstevel@tonic-gate  * -----------------------------------------------------------------
769*0Sstevel@tonic-gate  *			cfsd_sleep
770*0Sstevel@tonic-gate  *
771*0Sstevel@tonic-gate  * Description:
772*0Sstevel@tonic-gate  *	A reimplemenation of the sleep(3c) function call using
773*0Sstevel@tonic-gate  *	cond_timedwait.
774*0Sstevel@tonic-gate  *	Problem withe sleep(3c) hanging. May return early.
775*0Sstevel@tonic-gate  * Arguments:
776*0Sstevel@tonic-gate  *	sec	number of seconds to sleep for
777*0Sstevel@tonic-gate  * Returns:
778*0Sstevel@tonic-gate  * Preconditions:
779*0Sstevel@tonic-gate  */
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate void
cfsd_sleep(int sec)782*0Sstevel@tonic-gate cfsd_sleep(int sec)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	cond_t cv;
785*0Sstevel@tonic-gate 	mutex_t mt;
786*0Sstevel@tonic-gate 	timestruc_t reltime;
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	dbug_enter("cfsd_sleep");
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	if (sec > 0) {
791*0Sstevel@tonic-gate 		mutex_init(&mt, USYNC_THREAD, NULL);
792*0Sstevel@tonic-gate 		cond_init(&cv, USYNC_THREAD, 0);
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 		reltime.tv_sec = sec;
795*0Sstevel@tonic-gate 		reltime.tv_nsec = 0;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 		mutex_lock(&mt);
798*0Sstevel@tonic-gate 		cond_reltimedwait(&cv, &mt, &reltime);
799*0Sstevel@tonic-gate 		mutex_unlock(&mt);
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 		cond_destroy(&cv);
802*0Sstevel@tonic-gate 		mutex_destroy(&mt);
803*0Sstevel@tonic-gate 	}
804*0Sstevel@tonic-gate 	dbug_leave("cfsd_sleep");
805*0Sstevel@tonic-gate }
806