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