xref: /freebsd/sys/geom/cache/g_cache.h (revision 3c5ba95ad12285ad37c182a4bfc1b240ec6d18a7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Ruslan Ermilov <ru@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_G_CACHE_H_
32 #define	_G_CACHE_H_
33 
34 #include <sys/endian.h>
35 
36 #define	G_CACHE_CLASS_NAME	"CACHE"
37 #define	G_CACHE_MAGIC		"GEOM::CACHE"
38 #define	G_CACHE_VERSION		1
39 
40 #ifdef _KERNEL
41 #define	G_CACHE_TYPE_MANUAL	0
42 #define	G_CACHE_TYPE_AUTOMATIC	1
43 
44 #define	G_CACHE_DEBUG(lvl, ...)	do {					\
45 	if (g_cache_debug >= (lvl)) {					\
46 		printf("GEOM_CACHE");					\
47 		if (g_cache_debug > 0)					\
48 			printf("[%u]", lvl);				\
49 		printf(": ");						\
50 		printf(__VA_ARGS__);					\
51 		printf("\n");						\
52 	}								\
53 } while (0)
54 #define	G_CACHE_LOGREQ(bp, ...)	do {					\
55 	if (g_cache_debug >= 2) {					\
56 		printf("GEOM_CACHE[2]: ");				\
57 		printf(__VA_ARGS__);					\
58 		printf(" ");						\
59 		g_print_bio(bp);					\
60 		printf("\n");						\
61 	}								\
62 } while (0)
63 
64 #define	G_CACHE_BUCKETS		(1 << 3)
65 #define	G_CACHE_BUCKET(bno)	((bno) & (G_CACHE_BUCKETS - 1))
66 
67 struct g_cache_softc {
68 	struct g_geom	*sc_geom;
69 	int		sc_type;
70 	u_int		sc_bshift;
71 	u_int		sc_bsize;
72 	off_t		sc_tail;
73 	struct mtx	sc_mtx;
74 	struct callout	sc_callout;
75 	LIST_HEAD(, g_cache_desc) sc_desclist[G_CACHE_BUCKETS];
76 	TAILQ_HEAD(, g_cache_desc) sc_usedlist;
77 	uma_zone_t	sc_zone;
78 
79 	u_int		sc_maxent;		/* max entries */
80 	u_int		sc_nent;		/* allocated entries */
81 	u_int		sc_nused;		/* re-useable entries */
82 	u_int		sc_invalid;		/* invalid entries */
83 
84 	uintmax_t	sc_reads;		/* #reads */
85 	uintmax_t	sc_readbytes;		/* bytes read */
86 	uintmax_t	sc_cachereads;		/* #reads from cache */
87 	uintmax_t	sc_cachereadbytes;	/* bytes read from cache */
88 	uintmax_t	sc_cachehits;		/* cache hits */
89 	uintmax_t	sc_cachemisses;		/* cache misses */
90 	uintmax_t	sc_cachefull;		/* #times a cache was full */
91 	uintmax_t	sc_writes;		/* #writes */
92 	uintmax_t	sc_wrotebytes;		/* bytes written */
93 };
94 #define	sc_name	sc_geom->name
95 
96 struct g_cache_desc {
97 	off_t		d_bno;			/* block number */
98 	caddr_t		d_data;			/* data area */
99 	struct bio	*d_biolist;		/* waiters */
100 	time_t		d_atime;		/* access time */
101 	int		d_flags;		/* flags */
102 #define	D_FLAG_USED	(1 << 0)			/* can be reused */
103 #define	D_FLAG_INVALID	(1 << 1)			/* invalid */
104 	LIST_ENTRY(g_cache_desc) d_next;	/* list */
105 	TAILQ_ENTRY(g_cache_desc) d_used;	/* used list */
106 };
107 
108 #define	G_CACHE_NEXT_BIO1(bp)	(bp)->bio_driver1
109 #define	G_CACHE_NEXT_BIO2(bp)	(bp)->bio_driver2
110 #define	G_CACHE_DESC1(bp)	(bp)->bio_caller1
111 #define	G_CACHE_DESC2(bp)	(bp)->bio_caller2
112 
113 #endif	/* _KERNEL */
114 
115 struct g_cache_metadata {
116 	char		md_magic[16];		/* Magic value. */
117 	uint32_t	md_version;		/* Version number. */
118 	char		md_name[16];		/* Cache value. */
119 	uint32_t	md_bsize;		/* Cache block size. */
120 	uint32_t	md_size;		/* Cache size. */
121 	uint64_t	md_provsize;		/* Provider's size. */
122 };
123 
124 static __inline void
125 cache_metadata_encode(const struct g_cache_metadata *md, u_char *data)
126 {
127 
128 	bcopy(md->md_magic, data, sizeof(md->md_magic));
129 	le32enc(data + 16, md->md_version);
130 	bcopy(md->md_name, data + 20, sizeof(md->md_name));
131 	le32enc(data + 36, md->md_bsize);
132 	le32enc(data + 40, md->md_size);
133 	le64enc(data + 44, md->md_provsize);
134 }
135 
136 static __inline void
137 cache_metadata_decode(const u_char *data, struct g_cache_metadata *md)
138 {
139 
140 	bcopy(data, md->md_magic, sizeof(md->md_magic));
141 	md->md_version = le32dec(data + 16);
142 	bcopy(data + 20, md->md_name, sizeof(md->md_name));
143 	md->md_bsize = le32dec(data + 36);
144 	md->md_size = le32dec(data + 40);
145 	md->md_provsize = le64dec(data + 44);
146 }
147 
148 #endif	/* _G_CACHE_H_ */
149