xref: /freebsd/sys/geom/cache/g_cache.h (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
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, ...) \
45     _GEOM_DEBUG("GEOM_CACHE", g_cache_debug, (lvl), NULL, __VA_ARGS__)
46 #define G_CACHE_LOGREQ(bp, ...) \
47     _GEOM_DEBUG("GEOM_CACHE", g_cache_debug, 2, (bp), __VA_ARGS__)
48 
49 #define	G_CACHE_BUCKETS		(1 << 3)
50 #define	G_CACHE_BUCKET(bno)	((bno) & (G_CACHE_BUCKETS - 1))
51 
52 struct g_cache_softc {
53 	struct g_geom	*sc_geom;
54 	int		sc_type;
55 	u_int		sc_bshift;
56 	u_int		sc_bsize;
57 	off_t		sc_tail;
58 	struct mtx	sc_mtx;
59 	struct callout	sc_callout;
60 	LIST_HEAD(, g_cache_desc) sc_desclist[G_CACHE_BUCKETS];
61 	TAILQ_HEAD(, g_cache_desc) sc_usedlist;
62 	uma_zone_t	sc_zone;
63 
64 	u_int		sc_maxent;		/* max entries */
65 	u_int		sc_nent;		/* allocated entries */
66 	u_int		sc_nused;		/* re-useable entries */
67 	u_int		sc_invalid;		/* invalid entries */
68 
69 	uintmax_t	sc_reads;		/* #reads */
70 	uintmax_t	sc_readbytes;		/* bytes read */
71 	uintmax_t	sc_cachereads;		/* #reads from cache */
72 	uintmax_t	sc_cachereadbytes;	/* bytes read from cache */
73 	uintmax_t	sc_cachehits;		/* cache hits */
74 	uintmax_t	sc_cachemisses;		/* cache misses */
75 	uintmax_t	sc_cachefull;		/* #times a cache was full */
76 	uintmax_t	sc_writes;		/* #writes */
77 	uintmax_t	sc_wrotebytes;		/* bytes written */
78 };
79 #define	sc_name	sc_geom->name
80 
81 struct g_cache_desc {
82 	off_t		d_bno;			/* block number */
83 	caddr_t		d_data;			/* data area */
84 	struct bio	*d_biolist;		/* waiters */
85 	time_t		d_atime;		/* access time */
86 	int		d_flags;		/* flags */
87 #define	D_FLAG_USED	(1 << 0)			/* can be reused */
88 #define	D_FLAG_INVALID	(1 << 1)			/* invalid */
89 	LIST_ENTRY(g_cache_desc) d_next;	/* list */
90 	TAILQ_ENTRY(g_cache_desc) d_used;	/* used list */
91 };
92 
93 #define	G_CACHE_NEXT_BIO1(bp)	(bp)->bio_driver1
94 #define	G_CACHE_NEXT_BIO2(bp)	(bp)->bio_driver2
95 #define	G_CACHE_DESC1(bp)	(bp)->bio_caller1
96 #define	G_CACHE_DESC2(bp)	(bp)->bio_caller2
97 
98 #endif	/* _KERNEL */
99 
100 struct g_cache_metadata {
101 	char		md_magic[16];		/* Magic value. */
102 	uint32_t	md_version;		/* Version number. */
103 	char		md_name[16];		/* Cache value. */
104 	uint32_t	md_bsize;		/* Cache block size. */
105 	uint32_t	md_size;		/* Cache size. */
106 	uint64_t	md_provsize;		/* Provider's size. */
107 };
108 
109 static __inline void
110 cache_metadata_encode(const struct g_cache_metadata *md, u_char *data)
111 {
112 
113 	bcopy(md->md_magic, data, sizeof(md->md_magic));
114 	le32enc(data + 16, md->md_version);
115 	bcopy(md->md_name, data + 20, sizeof(md->md_name));
116 	le32enc(data + 36, md->md_bsize);
117 	le32enc(data + 40, md->md_size);
118 	le64enc(data + 44, md->md_provsize);
119 }
120 
121 static __inline void
122 cache_metadata_decode(const u_char *data, struct g_cache_metadata *md)
123 {
124 
125 	bcopy(data, md->md_magic, sizeof(md->md_magic));
126 	md->md_version = le32dec(data + 16);
127 	bcopy(data + 20, md->md_name, sizeof(md->md_name));
128 	md->md_bsize = le32dec(data + 36);
129 	md->md_size = le32dec(data + 40);
130 	md->md_provsize = le64dec(data + 44);
131 }
132 
133 #endif	/* _G_CACHE_H_ */
134