xref: /titanic_41/usr/src/cmd/fs.d/cachefs/cfsd/cfsd_all.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
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 for the cfsd_all 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 <errno.h>
40 #include <sys/utsname.h>
41 #include <sys/param.h>
42 #include <sys/mnttab.h>
43 #include <sys/vfstab.h>
44 #include <mdbug/mdbug.h>
45 #include <sys/fs/cachefs_fs.h>
46 #include <sys/fs/cachefs_dlog.h>
47 #include <sys/fs/cachefs_ioctl.h>
48 #include "cfsd.h"
49 #include "cfsd_kmod.h"
50 #include "cfsd_maptbl.h"
51 #include "cfsd_logfile.h"
52 #include "cfsd_fscache.h"
53 #include "cfsd_cache.h"
54 #include "cfsd_all.h"
55 
56 /*
57  * ------------------------------------------------------------
58  *			cfsd_all_create
59  *
60  * Description:
61  * Arguments:
62  * Returns:
63  * Preconditions:
64  */
65 cfsd_all_object_t *
66 cfsd_all_create(void)
67 {
68 
69 	/* get the host name */
70 	struct utsname info;
71 	cfsd_all_object_t *all_object_p;
72 	int xx;
73 	char buffer[MAXPATHLEN];
74 
75 	dbug_enter("cfsd_all_create");
76 
77 	all_object_p =
78 	    (cfsd_all_object_t *)cfsd_calloc(sizeof (cfsd_all_object_t));
79 
80 	xx = uname(&info);
81 	if (xx == -1) {
82 		dbug_print(("error", "cannot get host name"));
83 		strlcpy(all_object_p->i_machname, gettext("unknown"),
84 		    sizeof (all_object_p->i_machname));
85 	} else {
86 		strlcpy(all_object_p->i_machname, info.nodename,
87 		    sizeof (all_object_p->i_machname));
88 	}
89 
90 	/* initialize the locking mutex */
91 	xx = mutex_init(&all_object_p->i_lock, USYNC_THREAD, NULL);
92 	dbug_assert(xx == 0);
93 
94 	all_object_p->i_nextcacheid = 0;
95 	all_object_p->i_modify = 1;
96 	all_object_p->i_cachelist = NULL;
97 	all_object_p->i_cachecount = 0;
98 
99 	/* all_object_p->i_hoardp = NULL; */
100 
101 	snprintf(buffer, sizeof (buffer), gettext("host name is \"%s\""),
102 	    all_object_p->i_machname);
103 	dbug_print(("info", buffer));
104 	dbug_leave("cfsd_all_create");
105 	return (all_object_p);
106 }
107 
108 /*
109  * ------------------------------------------------------------
110  *			cfsd_all_destroy
111  *
112  * Description:
113  * Arguments:
114  * Returns:
115  * Preconditions:
116  */
117 void
118 cfsd_all_destroy(cfsd_all_object_t *all_object_p)
119 {
120 	cfsd_cache_object_t *cache_object_p;
121 	cfsd_cache_object_t *tmp_cache_object_p;
122 	int xx;
123 
124 	dbug_enter("cfsd_all_destroy");
125 
126 	/* dbug_assert(all_object_p->i_hoardp == NULL); */
127 
128 	/* get rid of any cache objects */
129 	cache_object_p = all_object_p->i_cachelist;
130 
131 	while (cache_object_p != NULL) {
132 		tmp_cache_object_p = cache_object_p->i_next;
133 		cfsd_cache_destroy(cache_object_p);
134 		cache_object_p = tmp_cache_object_p;
135 	}
136 
137 	/* destroy the locking mutex */
138 	xx = mutex_destroy(&all_object_p->i_lock);
139 	dbug_assert(xx == 0);
140 	cfsd_free(all_object_p);
141 	dbug_leave("cfsd_all_destroy");
142 }
143 
144 /*
145  * ------------------------------------------------------------
146  *			all_lock
147  *
148  * Description:
149  * Arguments:
150  * Returns:
151  * Preconditions:
152  */
153 void
154 all_lock(cfsd_all_object_t *all_object_p)
155 {
156 	dbug_enter("all_lock");
157 
158 	mutex_lock(&all_object_p->i_lock);
159 	dbug_leave("all_lock");
160 }
161 
162 /*
163  * ------------------------------------------------------------
164  *			all_unlock
165  *
166  * Description:
167  * Arguments:
168  * Returns:
169  * Preconditions:
170  */
171 void
172 all_unlock(cfsd_all_object_t *all_object_p)
173 {
174 	dbug_enter("all_unlock");
175 
176 	mutex_unlock(&all_object_p->i_lock);
177 	dbug_leave("all_unlock");
178 }
179 
180 /*
181  * ------------------------------------------------------------
182  *			all_cachelist_at
183  *
184  * Description:
185  * Arguments:
186  *	index
187  * Returns:
188  *	Returns ...
189  * Preconditions:
190  */
191 cfsd_cache_object_t *
192 all_cachelist_at(cfsd_all_object_t *all_object_p, size_t index)
193 {
194 	cfsd_cache_object_t *cache_object_p;
195 	int i = 0;
196 
197 	dbug_enter("all_cachelist_at");
198 
199 	/* find the correct cache object */
200 	cache_object_p = all_object_p->i_cachelist;
201 
202 	while ((cache_object_p != NULL) && (i++ < index)) {
203 		cache_object_p = cache_object_p->i_next;
204 	}
205 
206 	dbug_leave("all_cachelist_at");
207 	return (cache_object_p);
208 }
209 
210 /*
211  * ------------------------------------------------------------
212  *			all_cachelist_add
213  *
214  * Description:
215  * Arguments:
216  *	cachep
217  * Returns:
218  * Preconditions:
219  *	precond(cachep)
220  */
221 void
222 all_cachelist_add(cfsd_all_object_t *all_object_p,
223 	cfsd_cache_object_t *cache_object_p)
224 {
225 	dbug_enter("all_cachelist_add");
226 
227 	dbug_precond(cache_object_p);
228 
229 	cache_object_p->i_next = all_object_p->i_cachelist;
230 	all_object_p->i_cachelist = cache_object_p;
231 	all_object_p->i_modify++;
232 	all_object_p->i_cachecount++;
233 	dbug_leave("all_cachelist_add");
234 }
235 
236 /*
237  * ------------------------------------------------------------
238  *			all_cachelist_find
239  *
240  * Description:
241  * Arguments:
242  *	namep
243  * Returns:
244  *	Returns ...
245  * Preconditions:
246  *	precond(namep)
247  */
248 cfsd_cache_object_t *
249 all_cachelist_find(cfsd_all_object_t *all_object_p, const char *namep)
250 {
251 	cfsd_cache_object_t *cache_object_p;
252 
253 	dbug_enter("all_cachelist_find");
254 
255 	dbug_precond(namep);
256 
257 	/* find the correct cache object */
258 	cache_object_p = all_object_p->i_cachelist;
259 
260 	while ((cache_object_p != NULL) &&
261 		strcmp(namep, cache_object_p->i_cachedir)) {
262 		cache_object_p = cache_object_p->i_next;
263 	}
264 
265 	dbug_leave("all_cachelist_find");
266 	return (cache_object_p);
267 }
268 
269 /*
270  * ------------------------------------------------------------
271  *			all_cachefstab_update
272  *
273  * Description:
274  * Arguments:
275  * Returns:
276  * Preconditions:
277  */
278 void
279 all_cachefstab_update(cfsd_all_object_t *all_object_p)
280 {
281 	cfsd_cache_object_t *cache_object_p;
282 	FILE *fout;
283 
284 	dbug_enter("all_cachefstab_update");
285 
286 	fout = fopen(CACHEFSTAB, "w");
287 	if (fout == NULL) {
288 		dbug_print(("error", "cannot write %s", CACHEFSTAB));
289 	} else {
290 		cache_object_p = all_object_p->i_cachelist;
291 
292 		while (cache_object_p != NULL) {
293 			dbug_assert(cache_object_p);
294 			fprintf(fout, "%s\n", cache_object_p->i_cachedir);
295 			cache_object_p = cache_object_p->i_next;
296 		}
297 		if (fclose(fout))
298 			dbug_print(("error", "cannot close %s error %d",
299 			    CACHEFSTAB, errno));
300 	}
301 	dbug_leave("all_cachefstab_update");
302 }
303