6253 F_GETLK doesn't always return lock owner
[illumos-gate.git] / usr / src / cmd / fs.d / cachefs / cfsd / cfsd_all.c
blob6cdc1fe58d01a30df33c0255f8a56b7f9c708968
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1994-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Methods for the cfsd_all class.
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"
57 * ------------------------------------------------------------
58 * cfsd_all_create
60 * Description:
61 * Arguments:
62 * Returns:
63 * Preconditions:
65 cfsd_all_object_t *
66 cfsd_all_create(void)
69 /* get the host name */
70 struct utsname info;
71 cfsd_all_object_t *all_object_p;
72 int xx;
73 char buffer[MAXPATHLEN];
75 dbug_enter("cfsd_all_create");
77 all_object_p =
78 (cfsd_all_object_t *)cfsd_calloc(sizeof (cfsd_all_object_t));
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));
90 /* initialize the locking mutex */
91 xx = mutex_init(&all_object_p->i_lock, USYNC_THREAD, NULL);
92 dbug_assert(xx == 0);
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;
99 /* all_object_p->i_hoardp = NULL; */
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);
109 * ------------------------------------------------------------
110 * cfsd_all_destroy
112 * Description:
113 * Arguments:
114 * Returns:
115 * Preconditions:
117 void
118 cfsd_all_destroy(cfsd_all_object_t *all_object_p)
120 cfsd_cache_object_t *cache_object_p;
121 cfsd_cache_object_t *tmp_cache_object_p;
122 int xx;
124 dbug_enter("cfsd_all_destroy");
126 /* dbug_assert(all_object_p->i_hoardp == NULL); */
128 /* get rid of any cache objects */
129 cache_object_p = all_object_p->i_cachelist;
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;
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");
145 * ------------------------------------------------------------
146 * all_lock
148 * Description:
149 * Arguments:
150 * Returns:
151 * Preconditions:
153 void
154 all_lock(cfsd_all_object_t *all_object_p)
156 dbug_enter("all_lock");
158 mutex_lock(&all_object_p->i_lock);
159 dbug_leave("all_lock");
163 * ------------------------------------------------------------
164 * all_unlock
166 * Description:
167 * Arguments:
168 * Returns:
169 * Preconditions:
171 void
172 all_unlock(cfsd_all_object_t *all_object_p)
174 dbug_enter("all_unlock");
176 mutex_unlock(&all_object_p->i_lock);
177 dbug_leave("all_unlock");
181 * ------------------------------------------------------------
182 * all_cachelist_at
184 * Description:
185 * Arguments:
186 * index
187 * Returns:
188 * Returns ...
189 * Preconditions:
191 cfsd_cache_object_t *
192 all_cachelist_at(cfsd_all_object_t *all_object_p, size_t index)
194 cfsd_cache_object_t *cache_object_p;
195 int i = 0;
197 dbug_enter("all_cachelist_at");
199 /* find the correct cache object */
200 cache_object_p = all_object_p->i_cachelist;
202 while ((cache_object_p != NULL) && (i++ < index)) {
203 cache_object_p = cache_object_p->i_next;
206 dbug_leave("all_cachelist_at");
207 return (cache_object_p);
211 * ------------------------------------------------------------
212 * all_cachelist_add
214 * Description:
215 * Arguments:
216 * cachep
217 * Returns:
218 * Preconditions:
219 * precond(cachep)
221 void
222 all_cachelist_add(cfsd_all_object_t *all_object_p,
223 cfsd_cache_object_t *cache_object_p)
225 dbug_enter("all_cachelist_add");
227 dbug_precond(cache_object_p);
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");
237 * ------------------------------------------------------------
238 * all_cachelist_find
240 * Description:
241 * Arguments:
242 * namep
243 * Returns:
244 * Returns ...
245 * Preconditions:
246 * precond(namep)
248 cfsd_cache_object_t *
249 all_cachelist_find(cfsd_all_object_t *all_object_p, const char *namep)
251 cfsd_cache_object_t *cache_object_p;
253 dbug_enter("all_cachelist_find");
255 dbug_precond(namep);
257 /* find the correct cache object */
258 cache_object_p = all_object_p->i_cachelist;
260 while ((cache_object_p != NULL) &&
261 strcmp(namep, cache_object_p->i_cachedir)) {
262 cache_object_p = cache_object_p->i_next;
265 dbug_leave("all_cachelist_find");
266 return (cache_object_p);
270 * ------------------------------------------------------------
271 * all_cachefstab_update
273 * Description:
274 * Arguments:
275 * Returns:
276 * Preconditions:
278 void
279 all_cachefstab_update(cfsd_all_object_t *all_object_p)
281 cfsd_cache_object_t *cache_object_p;
282 FILE *fout;
284 dbug_enter("all_cachefstab_update");
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;
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;
297 if (fclose(fout))
298 dbug_print(("error", "cannot close %s error %d",
299 CACHEFSTAB, errno));
301 dbug_leave("all_cachefstab_update");