xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cfsd/cfsd_cache.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 (c) 1994-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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  * Methods of the cfsd_cache class.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <thread.h>
37*0Sstevel@tonic-gate #include <synch.h>
38*0Sstevel@tonic-gate #include <locale.h>
39*0Sstevel@tonic-gate #include <sys/utsname.h>
40*0Sstevel@tonic-gate #include <sys/stat.h>
41*0Sstevel@tonic-gate #include <mdbug/mdbug.h>
42*0Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
43*0Sstevel@tonic-gate #include <sys/fs/cachefs_dlog.h>
44*0Sstevel@tonic-gate #include <sys/fs/cachefs_ioctl.h>
45*0Sstevel@tonic-gate #include "cfsd.h"
46*0Sstevel@tonic-gate #include "cfsd_kmod.h"
47*0Sstevel@tonic-gate #include "cfsd_maptbl.h"
48*0Sstevel@tonic-gate #include "cfsd_logfile.h"
49*0Sstevel@tonic-gate #include "cfsd_fscache.h"
50*0Sstevel@tonic-gate #include "cfsd_cache.h"
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * -----------------------------------------------------------------
54*0Sstevel@tonic-gate  *			cfsd_cache_create
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  * Description:
57*0Sstevel@tonic-gate  * Arguments:
58*0Sstevel@tonic-gate  * Returns:
59*0Sstevel@tonic-gate  * Preconditions:
60*0Sstevel@tonic-gate  */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate cfsd_cache_object_t *
cfsd_cache_create(void)63*0Sstevel@tonic-gate cfsd_cache_create(void)
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	cfsd_cache_object_t *cache_object_p;
66*0Sstevel@tonic-gate 	int xx;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	dbug_enter("cfsd_cache_create");
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	cache_object_p = cfsd_calloc(sizeof (cfsd_cache_object_t));
71*0Sstevel@tonic-gate 	strlcpy(cache_object_p->i_cachedir, gettext("unknown"),
72*0Sstevel@tonic-gate 	    sizeof (cache_object_p->i_cachedir));
73*0Sstevel@tonic-gate 	cache_object_p->i_refcnt = 0;
74*0Sstevel@tonic-gate 	cache_object_p->i_nextfscacheid = 0;
75*0Sstevel@tonic-gate 	cache_object_p->i_cacheid = 0;
76*0Sstevel@tonic-gate 	cache_object_p->i_modify = 1;
77*0Sstevel@tonic-gate 	cache_object_p->i_fscachelist = NULL;
78*0Sstevel@tonic-gate 	cache_object_p->i_fscachecount = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/* initialize the locking mutex */
81*0Sstevel@tonic-gate 	xx = mutex_init(&cache_object_p->i_lock, USYNC_THREAD, NULL);
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	dbug_assert(xx == 0);
84*0Sstevel@tonic-gate 	dbug_leave("cfsd_cache_create");
85*0Sstevel@tonic-gate 	return (cache_object_p);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * -----------------------------------------------------------------
90*0Sstevel@tonic-gate  *			cfsd_cache_destroy
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * Description:
93*0Sstevel@tonic-gate  * Arguments:
94*0Sstevel@tonic-gate  * Returns:
95*0Sstevel@tonic-gate  * Preconditions:
96*0Sstevel@tonic-gate  */
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate void
cfsd_cache_destroy(cfsd_cache_object_t * cache_object_p)100*0Sstevel@tonic-gate cfsd_cache_destroy(cfsd_cache_object_t *cache_object_p)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
104*0Sstevel@tonic-gate 	cfsd_fscache_object_t *tmp_fscache_object_p;
105*0Sstevel@tonic-gate 	int xx;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	dbug_enter("cfsd_cache_destroy");
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	/* get rid of any fscache objects */
110*0Sstevel@tonic-gate 	fscache_object_p = cache_object_p->i_fscachelist;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	while (fscache_object_p != NULL) {
113*0Sstevel@tonic-gate 		tmp_fscache_object_p = fscache_object_p->i_next;
114*0Sstevel@tonic-gate 		cfsd_fscache_destroy(fscache_object_p);
115*0Sstevel@tonic-gate 		fscache_object_p = tmp_fscache_object_p;
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	/* destroy the locking mutex */
119*0Sstevel@tonic-gate 	xx = mutex_destroy(&cache_object_p->i_lock);
120*0Sstevel@tonic-gate 	dbug_assert(xx == 0);
121*0Sstevel@tonic-gate 	cfsd_free(cache_object_p);
122*0Sstevel@tonic-gate 	dbug_leave("cfsd_cache_destroy");
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * -----------------------------------------------------------------
127*0Sstevel@tonic-gate  *			cache_setup
128*0Sstevel@tonic-gate  *
129*0Sstevel@tonic-gate  * Description:
130*0Sstevel@tonic-gate  *	Performs setup for the cache.
131*0Sstevel@tonic-gate  * Arguments:
132*0Sstevel@tonic-gate  *	cachedirp
133*0Sstevel@tonic-gate  *	cacheid
134*0Sstevel@tonic-gate  * Returns:
135*0Sstevel@tonic-gate  * Preconditions:
136*0Sstevel@tonic-gate  *	precond(cachedirp)
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate int
cache_setup(cfsd_cache_object_t * cache_object_p,const char * cachedirp,int cacheid)140*0Sstevel@tonic-gate cache_setup(cfsd_cache_object_t *cache_object_p, const char *cachedirp,
141*0Sstevel@tonic-gate 	int cacheid)
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/* XXX either need to prevent multiple calls to this or */
145*0Sstevel@tonic-gate 	/*  clean up here. */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	int ret;
148*0Sstevel@tonic-gate 	struct stat64 sinfo;
149*0Sstevel@tonic-gate 	dbug_enter("cache_setup");
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if ((stat64(cachedirp, &sinfo) == -1) ||
152*0Sstevel@tonic-gate 	    (!S_ISDIR(sinfo.st_mode)) ||
153*0Sstevel@tonic-gate 	    (*cachedirp != '/')) {
154*0Sstevel@tonic-gate 		dbug_print(("info", "%s is not a cache directory", cachedirp));
155*0Sstevel@tonic-gate 		ret = 0;
156*0Sstevel@tonic-gate 	} else {
157*0Sstevel@tonic-gate 		strlcpy(cache_object_p->i_cachedir, cachedirp,
158*0Sstevel@tonic-gate 		    sizeof (cache_object_p->i_cachedir));
159*0Sstevel@tonic-gate 		ret = 1;
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 	cache_object_p->i_cacheid = cacheid;
163*0Sstevel@tonic-gate 	cache_object_p->i_modify++;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	dbug_leave("cache_setup");
166*0Sstevel@tonic-gate 	/* return result */
167*0Sstevel@tonic-gate 	return (ret);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate  * -----------------------------------------------------------------
171*0Sstevel@tonic-gate  *			cache_lock
172*0Sstevel@tonic-gate  *
173*0Sstevel@tonic-gate  * Description:
174*0Sstevel@tonic-gate  * Arguments:
175*0Sstevel@tonic-gate  * Returns:
176*0Sstevel@tonic-gate  * Preconditions:
177*0Sstevel@tonic-gate  */
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate void
cache_lock(cfsd_cache_object_t * cache_object_p)180*0Sstevel@tonic-gate cache_lock(cfsd_cache_object_t *cache_object_p)
181*0Sstevel@tonic-gate {
182*0Sstevel@tonic-gate 	dbug_enter("cache_lock");
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	mutex_lock(&cache_object_p->i_lock);
185*0Sstevel@tonic-gate 	dbug_leave("cache_lock");
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * -----------------------------------------------------------------
190*0Sstevel@tonic-gate  *			cache_unlock
191*0Sstevel@tonic-gate  *
192*0Sstevel@tonic-gate  * Description:
193*0Sstevel@tonic-gate  * Arguments:
194*0Sstevel@tonic-gate  * Returns:
195*0Sstevel@tonic-gate  * Preconditions:
196*0Sstevel@tonic-gate  */
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate void
cache_unlock(cfsd_cache_object_t * cache_object_p)199*0Sstevel@tonic-gate cache_unlock(cfsd_cache_object_t *cache_object_p)
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	dbug_enter("cache_unlock");
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	mutex_unlock(&cache_object_p->i_lock);
204*0Sstevel@tonic-gate 	dbug_leave("cache_unlock");
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * -----------------------------------------------------------------
208*0Sstevel@tonic-gate  *			cache_fscachelist_at
209*0Sstevel@tonic-gate  *
210*0Sstevel@tonic-gate  * Description:
211*0Sstevel@tonic-gate  * Arguments:
212*0Sstevel@tonic-gate  *	index
213*0Sstevel@tonic-gate  * Returns:
214*0Sstevel@tonic-gate  *	Returns ...
215*0Sstevel@tonic-gate  * Preconditions:
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate cfsd_fscache_object_t *
cache_fscachelist_at(cfsd_cache_object_t * cache_object_p,size_t index)219*0Sstevel@tonic-gate cache_fscachelist_at(cfsd_cache_object_t *cache_object_p, size_t index)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
222*0Sstevel@tonic-gate 	int i = 0;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	dbug_enter("cache_fscachelist_at");
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	/* find the correct cache object */
227*0Sstevel@tonic-gate 	fscache_object_p = cache_object_p->i_fscachelist;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	while ((fscache_object_p != NULL) && (i++ < index)) {
230*0Sstevel@tonic-gate 		fscache_object_p = fscache_object_p->i_next;
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	dbug_leave("cache_fscachelist_at");
234*0Sstevel@tonic-gate 	return (fscache_object_p);
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate /*
238*0Sstevel@tonic-gate  * -----------------------------------------------------------------
239*0Sstevel@tonic-gate  *			cache_fscachelist_add
240*0Sstevel@tonic-gate  *
241*0Sstevel@tonic-gate  * Description:
242*0Sstevel@tonic-gate  * Arguments:
243*0Sstevel@tonic-gate  *	cachep
244*0Sstevel@tonic-gate  * Returns:
245*0Sstevel@tonic-gate  * Preconditions:
246*0Sstevel@tonic-gate  *	precond(fscachep)
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate void
cache_fscachelist_add(cfsd_cache_object_t * cache_object_p,cfsd_fscache_object_t * fscache_object_p)250*0Sstevel@tonic-gate cache_fscachelist_add(cfsd_cache_object_t *cache_object_p,
251*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	dbug_enter("cache_fscachelist_add");
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	dbug_precond(fscache_object_p);
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	fscache_object_p->i_next = cache_object_p->i_fscachelist;
258*0Sstevel@tonic-gate 	cache_object_p->i_fscachelist = fscache_object_p;
259*0Sstevel@tonic-gate 	cache_object_p->i_modify++;
260*0Sstevel@tonic-gate 	cache_object_p->i_fscachecount++;
261*0Sstevel@tonic-gate 	dbug_leave("cache_fscachelist_add");
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /*
265*0Sstevel@tonic-gate  * -----------------------------------------------------------------
266*0Sstevel@tonic-gate  *			cache_fscachelist_find
267*0Sstevel@tonic-gate  *
268*0Sstevel@tonic-gate  * Description:
269*0Sstevel@tonic-gate  * Arguments:
270*0Sstevel@tonic-gate  *	namep
271*0Sstevel@tonic-gate  * Returns:
272*0Sstevel@tonic-gate  *	Returns ...
273*0Sstevel@tonic-gate  * Preconditions:
274*0Sstevel@tonic-gate  *	precond(namep)
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate cfsd_fscache_object_t *
cache_fscachelist_find(cfsd_cache_object_t * cache_object_p,const char * namep)278*0Sstevel@tonic-gate cache_fscachelist_find(cfsd_cache_object_t *cache_object_p,
279*0Sstevel@tonic-gate 	const char *namep)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	cfsd_fscache_object_t *fscache_object_p;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	dbug_enter("cache_fscachelist_find");
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	dbug_precond(namep);
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	/* see if the fscache exists */
288*0Sstevel@tonic-gate 	fscache_object_p = cache_object_p->i_fscachelist;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	while ((fscache_object_p != NULL) &&
291*0Sstevel@tonic-gate 		strcmp(namep, fscache_object_p->i_name)) {
292*0Sstevel@tonic-gate 		fscache_object_p = fscache_object_p->i_next;
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	dbug_leave("cache_fscachelist_find");
296*0Sstevel@tonic-gate 	return (fscache_object_p);
297*0Sstevel@tonic-gate }
298