6253 F_GETLK doesn't always return lock owner
[illumos-gate.git] / usr / src / cmd / fs.d / cachefs / cfsd / cfsd_cache.c
blobc27f04425092454b979696812e45f4575fb18e4c
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 of the cfsd_cache 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 <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"
53 * -----------------------------------------------------------------
54 * cfsd_cache_create
56 * Description:
57 * Arguments:
58 * Returns:
59 * Preconditions:
62 cfsd_cache_object_t *
63 cfsd_cache_create(void)
65 cfsd_cache_object_t *cache_object_p;
66 int xx;
68 dbug_enter("cfsd_cache_create");
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;
80 /* initialize the locking mutex */
81 xx = mutex_init(&cache_object_p->i_lock, USYNC_THREAD, NULL);
83 dbug_assert(xx == 0);
84 dbug_leave("cfsd_cache_create");
85 return (cache_object_p);
89 * -----------------------------------------------------------------
90 * cfsd_cache_destroy
92 * Description:
93 * Arguments:
94 * Returns:
95 * Preconditions:
99 void
100 cfsd_cache_destroy(cfsd_cache_object_t *cache_object_p)
103 cfsd_fscache_object_t *fscache_object_p;
104 cfsd_fscache_object_t *tmp_fscache_object_p;
105 int xx;
107 dbug_enter("cfsd_cache_destroy");
109 /* get rid of any fscache objects */
110 fscache_object_p = cache_object_p->i_fscachelist;
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;
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");
126 * -----------------------------------------------------------------
127 * cache_setup
129 * Description:
130 * Performs setup for the cache.
131 * Arguments:
132 * cachedirp
133 * cacheid
134 * Returns:
135 * Preconditions:
136 * precond(cachedirp)
140 cache_setup(cfsd_cache_object_t *cache_object_p, const char *cachedirp,
141 int cacheid)
144 /* XXX either need to prevent multiple calls to this or */
145 /* clean up here. */
147 int ret;
148 struct stat64 sinfo;
149 dbug_enter("cache_setup");
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;
162 cache_object_p->i_cacheid = cacheid;
163 cache_object_p->i_modify++;
165 dbug_leave("cache_setup");
166 /* return result */
167 return (ret);
170 * -----------------------------------------------------------------
171 * cache_lock
173 * Description:
174 * Arguments:
175 * Returns:
176 * Preconditions:
179 void
180 cache_lock(cfsd_cache_object_t *cache_object_p)
182 dbug_enter("cache_lock");
184 mutex_lock(&cache_object_p->i_lock);
185 dbug_leave("cache_lock");
189 * -----------------------------------------------------------------
190 * cache_unlock
192 * Description:
193 * Arguments:
194 * Returns:
195 * Preconditions:
198 void
199 cache_unlock(cfsd_cache_object_t *cache_object_p)
201 dbug_enter("cache_unlock");
203 mutex_unlock(&cache_object_p->i_lock);
204 dbug_leave("cache_unlock");
207 * -----------------------------------------------------------------
208 * cache_fscachelist_at
210 * Description:
211 * Arguments:
212 * index
213 * Returns:
214 * Returns ...
215 * Preconditions:
218 cfsd_fscache_object_t *
219 cache_fscachelist_at(cfsd_cache_object_t *cache_object_p, size_t index)
221 cfsd_fscache_object_t *fscache_object_p;
222 int i = 0;
224 dbug_enter("cache_fscachelist_at");
226 /* find the correct cache object */
227 fscache_object_p = cache_object_p->i_fscachelist;
229 while ((fscache_object_p != NULL) && (i++ < index)) {
230 fscache_object_p = fscache_object_p->i_next;
233 dbug_leave("cache_fscachelist_at");
234 return (fscache_object_p);
238 * -----------------------------------------------------------------
239 * cache_fscachelist_add
241 * Description:
242 * Arguments:
243 * cachep
244 * Returns:
245 * Preconditions:
246 * precond(fscachep)
249 void
250 cache_fscachelist_add(cfsd_cache_object_t *cache_object_p,
251 cfsd_fscache_object_t *fscache_object_p)
253 dbug_enter("cache_fscachelist_add");
255 dbug_precond(fscache_object_p);
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");
265 * -----------------------------------------------------------------
266 * cache_fscachelist_find
268 * Description:
269 * Arguments:
270 * namep
271 * Returns:
272 * Returns ...
273 * Preconditions:
274 * precond(namep)
277 cfsd_fscache_object_t *
278 cache_fscachelist_find(cfsd_cache_object_t *cache_object_p,
279 const char *namep)
281 cfsd_fscache_object_t *fscache_object_p;
283 dbug_enter("cache_fscachelist_find");
285 dbug_precond(namep);
287 /* see if the fscache exists */
288 fscache_object_p = cache_object_p->i_fscachelist;
290 while ((fscache_object_p != NULL) &&
291 strcmp(namep, fscache_object_p->i_name)) {
292 fscache_object_p = fscache_object_p->i_next;
295 dbug_leave("cache_fscachelist_find");
296 return (fscache_object_p);