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