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