xref: /titanic_41/usr/src/cmd/fs.d/cachefs/cfsd/cfsd_cache.c (revision 8e50dcc9f00b393d43e6aa42b820bcbf1d3e1ce4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Methods of the cfsd_cache class.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <thread.h>
37 #include <synch.h>
38 #include <locale.h>
39 #include <sys/utsname.h>
40 #include <sys/stat.h>
41 #include <mdbug/mdbug.h>
42 #include <sys/fs/cachefs_fs.h>
43 #include <sys/fs/cachefs_dlog.h>
44 #include <sys/fs/cachefs_ioctl.h>
45 #include "cfsd.h"
46 #include "cfsd_kmod.h"
47 #include "cfsd_maptbl.h"
48 #include "cfsd_logfile.h"
49 #include "cfsd_fscache.h"
50 #include "cfsd_cache.h"
51 
52 /*
53  * -----------------------------------------------------------------
54  *			cfsd_cache_create
55  *
56  * Description:
57  * Arguments:
58  * Returns:
59  * Preconditions:
60  */
61 
62 cfsd_cache_object_t *
63 cfsd_cache_create(void)
64 {
65 	cfsd_cache_object_t *cache_object_p;
66 	int xx;
67 
68 	dbug_enter("cfsd_cache_create");
69 
70 	cache_object_p = cfsd_calloc(sizeof (cfsd_cache_object_t));
71 	strlcpy(cache_object_p->i_cachedir, gettext("unknown"),
72 	    sizeof (cache_object_p->i_cachedir));
73 	cache_object_p->i_refcnt = 0;
74 	cache_object_p->i_nextfscacheid = 0;
75 	cache_object_p->i_cacheid = 0;
76 	cache_object_p->i_modify = 1;
77 	cache_object_p->i_fscachelist = NULL;
78 	cache_object_p->i_fscachecount = 0;
79 
80 	/* initialize the locking mutex */
81 	xx = mutex_init(&cache_object_p->i_lock, USYNC_THREAD, NULL);
82 
83 	dbug_assert(xx == 0);
84 	dbug_leave("cfsd_cache_create");
85 	return (cache_object_p);
86 }
87 
88 /*
89  * -----------------------------------------------------------------
90  *			cfsd_cache_destroy
91  *
92  * Description:
93  * Arguments:
94  * Returns:
95  * Preconditions:
96  */
97 
98 
99 void
100 cfsd_cache_destroy(cfsd_cache_object_t *cache_object_p)
101 {
102 
103 	cfsd_fscache_object_t *fscache_object_p;
104 	cfsd_fscache_object_t *tmp_fscache_object_p;
105 	int xx;
106 
107 	dbug_enter("cfsd_cache_destroy");
108 
109 	/* get rid of any fscache objects */
110 	fscache_object_p = cache_object_p->i_fscachelist;
111 
112 	while (fscache_object_p != NULL) {
113 		tmp_fscache_object_p = fscache_object_p->i_next;
114 		cfsd_fscache_destroy(fscache_object_p);
115 		fscache_object_p = tmp_fscache_object_p;
116 	}
117 
118 	/* destroy the locking mutex */
119 	xx = mutex_destroy(&cache_object_p->i_lock);
120 	dbug_assert(xx == 0);
121 	cfsd_free(cache_object_p);
122 	dbug_leave("cfsd_cache_destroy");
123 }
124 
125 /*
126  * -----------------------------------------------------------------
127  *			cache_setup
128  *
129  * Description:
130  *	Performs setup for the cache.
131  * Arguments:
132  *	cachedirp
133  *	cacheid
134  * Returns:
135  * Preconditions:
136  *	precond(cachedirp)
137  */
138 
139 int
140 cache_setup(cfsd_cache_object_t *cache_object_p, const char *cachedirp,
141 	int cacheid)
142 {
143 
144 	/* XXX either need to prevent multiple calls to this or */
145 	/*  clean up here. */
146 
147 	int ret;
148 	struct stat64 sinfo;
149 	dbug_enter("cache_setup");
150 
151 	if ((stat64(cachedirp, &sinfo) == -1) ||
152 	    (!S_ISDIR(sinfo.st_mode)) ||
153 	    (*cachedirp != '/')) {
154 		dbug_print(("info", "%s is not a cache directory", cachedirp));
155 		ret = 0;
156 	} else {
157 		strlcpy(cache_object_p->i_cachedir, cachedirp,
158 		    sizeof (cache_object_p->i_cachedir));
159 		ret = 1;
160 	}
161 
162 	cache_object_p->i_cacheid = cacheid;
163 	cache_object_p->i_modify++;
164 
165 	dbug_leave("cache_setup");
166 	/* return result */
167 	return (ret);
168 }
169 /*
170  * -----------------------------------------------------------------
171  *			cache_lock
172  *
173  * Description:
174  * Arguments:
175  * Returns:
176  * Preconditions:
177  */
178 
179 void
180 cache_lock(cfsd_cache_object_t *cache_object_p)
181 {
182 	dbug_enter("cache_lock");
183 
184 	mutex_lock(&cache_object_p->i_lock);
185 	dbug_leave("cache_lock");
186 }
187 
188 /*
189  * -----------------------------------------------------------------
190  *			cache_unlock
191  *
192  * Description:
193  * Arguments:
194  * Returns:
195  * Preconditions:
196  */
197 
198 void
199 cache_unlock(cfsd_cache_object_t *cache_object_p)
200 {
201 	dbug_enter("cache_unlock");
202 
203 	mutex_unlock(&cache_object_p->i_lock);
204 	dbug_leave("cache_unlock");
205 }
206 /*
207  * -----------------------------------------------------------------
208  *			cache_fscachelist_at
209  *
210  * Description:
211  * Arguments:
212  *	index
213  * Returns:
214  *	Returns ...
215  * Preconditions:
216  */
217 
218 cfsd_fscache_object_t *
219 cache_fscachelist_at(cfsd_cache_object_t *cache_object_p, size_t index)
220 {
221 	cfsd_fscache_object_t *fscache_object_p;
222 	int i = 0;
223 
224 	dbug_enter("cache_fscachelist_at");
225 
226 	/* find the correct cache object */
227 	fscache_object_p = cache_object_p->i_fscachelist;
228 
229 	while ((fscache_object_p != NULL) && (i++ < index)) {
230 		fscache_object_p = fscache_object_p->i_next;
231 	}
232 
233 	dbug_leave("cache_fscachelist_at");
234 	return (fscache_object_p);
235 }
236 
237 /*
238  * -----------------------------------------------------------------
239  *			cache_fscachelist_add
240  *
241  * Description:
242  * Arguments:
243  *	cachep
244  * Returns:
245  * Preconditions:
246  *	precond(fscachep)
247  */
248 
249 void
250 cache_fscachelist_add(cfsd_cache_object_t *cache_object_p,
251 	cfsd_fscache_object_t *fscache_object_p)
252 {
253 	dbug_enter("cache_fscachelist_add");
254 
255 	dbug_precond(fscache_object_p);
256 
257 	fscache_object_p->i_next = cache_object_p->i_fscachelist;
258 	cache_object_p->i_fscachelist = fscache_object_p;
259 	cache_object_p->i_modify++;
260 	cache_object_p->i_fscachecount++;
261 	dbug_leave("cache_fscachelist_add");
262 }
263 
264 /*
265  * -----------------------------------------------------------------
266  *			cache_fscachelist_find
267  *
268  * Description:
269  * Arguments:
270  *	namep
271  * Returns:
272  *	Returns ...
273  * Preconditions:
274  *	precond(namep)
275  */
276 
277 cfsd_fscache_object_t *
278 cache_fscachelist_find(cfsd_cache_object_t *cache_object_p,
279 	const char *namep)
280 {
281 	cfsd_fscache_object_t *fscache_object_p;
282 
283 	dbug_enter("cache_fscachelist_find");
284 
285 	dbug_precond(namep);
286 
287 	/* see if the fscache exists */
288 	fscache_object_p = cache_object_p->i_fscachelist;
289 
290 	while ((fscache_object_p != NULL) &&
291 		strcmp(namep, fscache_object_p->i_name)) {
292 		fscache_object_p = fscache_object_p->i_next;
293 	}
294 
295 	dbug_leave("cache_fscachelist_find");
296 	return (fscache_object_p);
297 }
298