xref: /titanic_44/usr/src/cmd/fs.d/cachefs/common/stats_dbm.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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) 1996-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  *
31*7c478bd9Sstevel@tonic-gate  *			stats_dbm.c
32*7c478bd9Sstevel@tonic-gate  *
33*7c478bd9Sstevel@tonic-gate  * Routines for dbm access.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <stddef.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
41*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
42*7c478bd9Sstevel@tonic-gate #include <libintl.h>
43*7c478bd9Sstevel@tonic-gate #include <time.h>
44*7c478bd9Sstevel@tonic-gate #include <string.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_fs.h>
46*7c478bd9Sstevel@tonic-gate #include "stats.h"
47*7c478bd9Sstevel@tonic-gate #include <assert.h>
48*7c478bd9Sstevel@tonic-gate #include <ndbm.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate void
stats_dbm_open(stats_cookie_t * st)51*7c478bd9Sstevel@tonic-gate stats_dbm_open(stats_cookie_t *st)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	char *tmpdir;
54*7c478bd9Sstevel@tonic-gate 	pid_t	getpid();
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
57*7c478bd9Sstevel@tonic-gate 	assert(! (st->st_flags & ST_DBMOPEN));
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if ((tmpdir = getenv("TMPDIR")) == NULL)
60*7c478bd9Sstevel@tonic-gate 		tmpdir = "/tmp";
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	(void) snprintf(st->st_dbm_name, sizeof (st->st_dbm_name), "%s/%s-%d",
63*7c478bd9Sstevel@tonic-gate 	    tmpdir, st->st_progname, getpid());
64*7c478bd9Sstevel@tonic-gate 	st->st_dbm = dbm_open(st->st_dbm_name, O_RDWR | O_CREAT, 0666);
65*7c478bd9Sstevel@tonic-gate 	if (st->st_dbm == NULL) {
66*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_FILE,
67*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot open dbm file %s"), st->st_dbm_name);
68*7c478bd9Sstevel@tonic-gate 		return;
69*7c478bd9Sstevel@tonic-gate 	}
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	st->st_flags |= ST_DBMOPEN;
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate void
stats_dbm_rm(stats_cookie_t * st)75*7c478bd9Sstevel@tonic-gate stats_dbm_rm(stats_cookie_t *st)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	char buffy[MAXPATHLEN], *eobase;
78*7c478bd9Sstevel@tonic-gate 	int unlink(), buflen, eobaselen;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (! (st->st_flags & ST_DBMOPEN))
83*7c478bd9Sstevel@tonic-gate 		return;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (strlcpy(buffy, st->st_dbm_name, sizeof (buffy)) >
86*7c478bd9Sstevel@tonic-gate 	    ((sizeof (buffy)) - (sizeof (".xxx"))))
87*7c478bd9Sstevel@tonic-gate 		return; /* No space for the file extensions */
88*7c478bd9Sstevel@tonic-gate 	buflen = strlen(buffy);
89*7c478bd9Sstevel@tonic-gate 	eobase = buffy + buflen;
90*7c478bd9Sstevel@tonic-gate 	eobaselen = (sizeof (buffy)) - buflen;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(eobase, ".dir", eobaselen);
93*7c478bd9Sstevel@tonic-gate 	(void) unlink(buffy);
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(eobase, ".pag", eobaselen);
96*7c478bd9Sstevel@tonic-gate 	(void) unlink(buffy);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate void
stats_dbm_close(stats_cookie_t * st)100*7c478bd9Sstevel@tonic-gate stats_dbm_close(stats_cookie_t *st)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (! (st->st_flags & ST_DBMOPEN))
105*7c478bd9Sstevel@tonic-gate 		return;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	st->st_flags &= ~ST_DBMOPEN;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (st->st_dbm == NULL)
110*7c478bd9Sstevel@tonic-gate 		return;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	dbm_close(st->st_dbm);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate fid_info *
stats_dbm_fetch_byfid(stats_cookie_t * st,cfs_fid_t * fidp)116*7c478bd9Sstevel@tonic-gate stats_dbm_fetch_byfid(stats_cookie_t *st, cfs_fid_t *fidp)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	datum key, value;
119*7c478bd9Sstevel@tonic-gate 	fid_info *rc;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
122*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	key.dptr = (char *)fidp;
125*7c478bd9Sstevel@tonic-gate 	key.dsize = sizeof (*fidp);
126*7c478bd9Sstevel@tonic-gate 	value = dbm_fetch(st->st_dbm, key);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	assert((value.dptr == NULL) || (value.dsize == sizeof (fid_info)));
129*7c478bd9Sstevel@tonic-gate 	if (value.dptr == NULL)
130*7c478bd9Sstevel@tonic-gate 		return (NULL);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if ((rc = malloc(sizeof (*rc))) == NULL) {
133*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_NOMEM,
134*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot malloc memory for fid_info record"));
135*7c478bd9Sstevel@tonic-gate 		return (NULL);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	memcpy(rc, value.dptr, sizeof (*rc));
139*7c478bd9Sstevel@tonic-gate 	if (rc->fi_magic != FI_MAGIC) {
140*7c478bd9Sstevel@tonic-gate 		free(rc);
141*7c478bd9Sstevel@tonic-gate 		return (NULL);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	return (rc);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate void
stats_dbm_store_byfid(stats_cookie_t * st,cfs_fid_t * fidp,fid_info * fi)148*7c478bd9Sstevel@tonic-gate stats_dbm_store_byfid(stats_cookie_t *st, cfs_fid_t *fidp, fid_info *fi)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	datum key, value;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
153*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	fi->fi_magic = FI_MAGIC;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	key.dptr = (char *)fidp;
158*7c478bd9Sstevel@tonic-gate 	key.dsize = sizeof (*fidp);
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	value.dptr = (char *)fi;
161*7c478bd9Sstevel@tonic-gate 	value.dsize = sizeof (*fi);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (dbm_store(st->st_dbm, key, value, DBM_REPLACE) != 0) {
164*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_FILE,
165*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot store fid info"));
166*7c478bd9Sstevel@tonic-gate 		return;
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate mount_info *
stats_dbm_fetch_byvfsp(stats_cookie_t * st,caddr_t vfsp)171*7c478bd9Sstevel@tonic-gate stats_dbm_fetch_byvfsp(stats_cookie_t *st, caddr_t vfsp)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	mount_info *rc, *mi;
174*7c478bd9Sstevel@tonic-gate 	int len1, len2, size;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	datum key, value;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
179*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	key.dptr = (char *)&vfsp;
182*7c478bd9Sstevel@tonic-gate 	key.dsize = sizeof (vfsp);
183*7c478bd9Sstevel@tonic-gate 	value = dbm_fetch(st->st_dbm, key);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (value.dptr == NULL)
186*7c478bd9Sstevel@tonic-gate 		return (NULL);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	mi = (mount_info *)value.dptr;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	len1 = strlen(mi->mi_path);
191*7c478bd9Sstevel@tonic-gate 	len2 = strlen(mi->mi_path + len1 + 1);
192*7c478bd9Sstevel@tonic-gate 	size = sizeof (*rc) + len1 + len2 - CLPAD(mount_info, mi_path);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	if ((rc = malloc(size)) == NULL) {
195*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_NOMEM,
196*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot malloc memory for mountinfo"));
197*7c478bd9Sstevel@tonic-gate 		return (NULL);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	memcpy(rc, mi, size);
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	if (rc->mi_magic != MI_MAGIC) {
202*7c478bd9Sstevel@tonic-gate 		free(rc);
203*7c478bd9Sstevel@tonic-gate 		return (NULL);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (rc);
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate void
stats_dbm_store_byvfsp(stats_cookie_t * st,caddr_t vfsp,mount_info * mi)210*7c478bd9Sstevel@tonic-gate stats_dbm_store_byvfsp(stats_cookie_t *st, caddr_t vfsp, mount_info *mi)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	datum key, value;
213*7c478bd9Sstevel@tonic-gate 	int len1, len2;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
216*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	mi->mi_magic = MI_MAGIC;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	key.dptr = (char *)&vfsp;
221*7c478bd9Sstevel@tonic-gate 	key.dsize = sizeof (vfsp);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	len1 = strlen(mi->mi_path);
224*7c478bd9Sstevel@tonic-gate 	len2 = strlen(mi->mi_path + len1 + 1);
225*7c478bd9Sstevel@tonic-gate 	value.dptr = (char *)mi;
226*7c478bd9Sstevel@tonic-gate 	value.dsize = sizeof (*mi) +
227*7c478bd9Sstevel@tonic-gate 	    len1 + len2 -
228*7c478bd9Sstevel@tonic-gate 	    CLPAD(mount_info, mi_path);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	if (dbm_store(st->st_dbm, key, value, DBM_REPLACE) != 0) {
231*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_FILE,
232*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot store mount info"));
233*7c478bd9Sstevel@tonic-gate 		return;
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate void
stats_dbm_delete_byvfsp(stats_cookie_t * st,caddr_t vfsp)238*7c478bd9Sstevel@tonic-gate stats_dbm_delete_byvfsp(stats_cookie_t *st, caddr_t vfsp)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	datum key;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
243*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	key.dptr = (caddr_t)&vfsp;
246*7c478bd9Sstevel@tonic-gate 	key.dsize = sizeof (vfsp);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	(void) dbm_delete(st->st_dbm, key);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate datum
stats_dbm_firstkey(stats_cookie_t * st)252*7c478bd9Sstevel@tonic-gate stats_dbm_firstkey(stats_cookie_t *st)
253*7c478bd9Sstevel@tonic-gate {
254*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
255*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	return (dbm_firstkey(st->st_dbm));
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate datum
stats_dbm_nextkey(stats_cookie_t * st)261*7c478bd9Sstevel@tonic-gate stats_dbm_nextkey(stats_cookie_t *st)
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
264*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 	return (dbm_nextkey(st->st_dbm));
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate /*
270*7c478bd9Sstevel@tonic-gate  * count var will be non-zero only for the record type CACHEFS_LOG_MDCREATE
271*7c478bd9Sstevel@tonic-gate  * and count can't be >2GB because it refers to the number of entries in
272*7c478bd9Sstevel@tonic-gate  * the attribute cache file.
273*7c478bd9Sstevel@tonic-gate  */
274*7c478bd9Sstevel@tonic-gate size_t
stats_dbm_attrcache_addsize(stats_cookie_t * st,mount_info * mi,ino64_t fileno,uint_t count)275*7c478bd9Sstevel@tonic-gate stats_dbm_attrcache_addsize(stats_cookie_t *st, mount_info *mi,
276*7c478bd9Sstevel@tonic-gate     ino64_t fileno, uint_t count)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	char keystring[BUFSIZ];
279*7c478bd9Sstevel@tonic-gate 	datum key, value;
280*7c478bd9Sstevel@tonic-gate 	char *cacheid;
281*7c478bd9Sstevel@tonic-gate 	fg_info fg, *fgp = NULL;
282*7c478bd9Sstevel@tonic-gate 	size_t size = 0, overhead = 0;
283*7c478bd9Sstevel@tonic-gate 	uchar_t tbits;
284*7c478bd9Sstevel@tonic-gate 	int i;
285*7c478bd9Sstevel@tonic-gate 	uint_t gfileno;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	assert(stats_good(st));
288*7c478bd9Sstevel@tonic-gate 	assert(st->st_flags & ST_DBMOPEN);
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	/* look up any known data about this filegrp */
291*7c478bd9Sstevel@tonic-gate 	cacheid = mi->mi_path + strlen(mi->mi_path) + 1;
292*7c478bd9Sstevel@tonic-gate 	(void) snprintf(keystring, sizeof (keystring), "%s.%lld", cacheid,
293*7c478bd9Sstevel@tonic-gate 	    fileno / (ino64_t)mi->mi_filegrp_size);
294*7c478bd9Sstevel@tonic-gate 	gfileno = (uint_t)(fileno % (ino64_t)mi->mi_filegrp_size);
295*7c478bd9Sstevel@tonic-gate 	key.dsize = strlen(keystring); /* no need to null terminate */
296*7c478bd9Sstevel@tonic-gate 	key.dptr = keystring;
297*7c478bd9Sstevel@tonic-gate 	value = dbm_fetch(st->st_dbm, key);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	size = sizeof (struct attrcache_header);
300*7c478bd9Sstevel@tonic-gate 	size += mi->mi_filegrp_size * sizeof (struct attrcache_index);
301*7c478bd9Sstevel@tonic-gate 	size += mi->mi_filegrp_size / NBBY;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if ((value.dptr != NULL) && (value.dsize == sizeof (fg))) {
304*7c478bd9Sstevel@tonic-gate 		/* align the structure */
305*7c478bd9Sstevel@tonic-gate 		memcpy((char *)&fg, value.dptr, sizeof (fg));
306*7c478bd9Sstevel@tonic-gate 		fgp = &fg;
307*7c478bd9Sstevel@tonic-gate 		if (fgp->fg_magic != FG_MAGIC)
308*7c478bd9Sstevel@tonic-gate 			fgp = NULL; /* oops -- key collision! */
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	/* if we haven't seen this filegrp yet */
312*7c478bd9Sstevel@tonic-gate 	if (fgp == NULL) {
313*7c478bd9Sstevel@tonic-gate 		memset((char *)&fg, '\0', sizeof (fg));
314*7c478bd9Sstevel@tonic-gate 		fgp = &fg;
315*7c478bd9Sstevel@tonic-gate 		fgp->fg_magic = FG_MAGIC;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 		/* filegrp frontfile directory */
318*7c478bd9Sstevel@tonic-gate 		overhead += st->st_loghead.lh_maxbsize;
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	/* high-water the given count (if any) with our known count */
322*7c478bd9Sstevel@tonic-gate 	if (count > fgp->fg_count)
323*7c478bd9Sstevel@tonic-gate 		fgp->fg_count = count;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	/* set a bit for this file */
326*7c478bd9Sstevel@tonic-gate 	if ((gfileno / NBBY) < sizeof (fgp->fg_bits)) {
327*7c478bd9Sstevel@tonic-gate 		tbits = 1 << (gfileno % NBBY);
328*7c478bd9Sstevel@tonic-gate 		if (! (fgp->fg_bits[gfileno / NBBY] & tbits))
329*7c478bd9Sstevel@tonic-gate 			fgp->fg_bcount++;
330*7c478bd9Sstevel@tonic-gate 		fgp->fg_bits[gfileno / NBBY] |= tbits;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/* high-water our derived count with our known count */
334*7c478bd9Sstevel@tonic-gate 	if (fgp->fg_bcount > fgp->fg_count)
335*7c478bd9Sstevel@tonic-gate 		fgp->fg_count = fgp->fg_bcount;
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate 	/* account for the size of all known attrcache entries */
338*7c478bd9Sstevel@tonic-gate 	size += fgp->fg_count * sizeof (struct cachefs_metadata);
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/* round to the ceiling block boundary */
341*7c478bd9Sstevel@tonic-gate 	size += st->st_loghead.lh_maxbsize - 1;
342*7c478bd9Sstevel@tonic-gate 	size &= ~ (st->st_loghead.lh_maxbsize - 1);
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	/* sneaky :-) -- high-water fg_size, and make size the delta */
345*7c478bd9Sstevel@tonic-gate 	size -= fgp->fg_size;
346*7c478bd9Sstevel@tonic-gate 	fgp->fg_size += size;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	value.dptr = (char *)fgp;
349*7c478bd9Sstevel@tonic-gate 	value.dsize = sizeof (*fgp);
350*7c478bd9Sstevel@tonic-gate 	if (dbm_store(st->st_dbm, key, value, DBM_REPLACE) != 0)
351*7c478bd9Sstevel@tonic-gate 		stats_perror(st, SE_FILE,
352*7c478bd9Sstevel@tonic-gate 		    gettext("Cannot store attrcache info"));
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	return (size + overhead);
355*7c478bd9Sstevel@tonic-gate }
356