1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23321552Smav * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24251478Sdelphij * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25168404Spjd */
26168404Spjd
27168404Spjd#ifndef	_SYS_ARC_H
28168404Spjd#define	_SYS_ARC_H
29168404Spjd
30168404Spjd#include <sys/zfs_context.h>
31168404Spjd
32168404Spjd#ifdef	__cplusplus
33168404Spjdextern "C" {
34168404Spjd#endif
35168404Spjd
36168404Spjd#include <sys/zio.h>
37185029Spjd#include <sys/dmu.h>
38185029Spjd#include <sys/spa.h>
39168404Spjd
40286763Smav/*
41286763Smav * Used by arc_flush() to inform arc_evict_state() that it should evict
42286763Smav * all available buffers from the arc state being passed in.
43286763Smav */
44286763Smav#define	ARC_EVICT_ALL	-1ULL
45286763Smav
46307265Smav#define	HDR_SET_LSIZE(hdr, x) do { \
47307265Smav	ASSERT(IS_P2ALIGNED(x, 1U << SPA_MINBLOCKSHIFT)); \
48307265Smav	(hdr)->b_lsize = ((x) >> SPA_MINBLOCKSHIFT); \
49307265Smav_NOTE(CONSTCOND) } while (0)
50307265Smav
51307265Smav#define	HDR_SET_PSIZE(hdr, x) do { \
52307265Smav	ASSERT(IS_P2ALIGNED((x), 1U << SPA_MINBLOCKSHIFT)); \
53307265Smav	(hdr)->b_psize = ((x) >> SPA_MINBLOCKSHIFT); \
54307265Smav_NOTE(CONSTCOND) } while (0)
55307265Smav
56307265Smav#define	HDR_GET_LSIZE(hdr)	((hdr)->b_lsize << SPA_MINBLOCKSHIFT)
57307265Smav#define	HDR_GET_PSIZE(hdr)	((hdr)->b_psize << SPA_MINBLOCKSHIFT)
58307265Smav
59168404Spjdtypedef struct arc_buf_hdr arc_buf_hdr_t;
60168404Spjdtypedef struct arc_buf arc_buf_t;
61339034Sseftypedef void arc_read_done_func_t(zio_t *zio, const zbookmark_phys_t *zb,
62339034Ssef    const blkptr_t *bp, arc_buf_t *buf, void *priv);
63339034Sseftypedef void arc_write_done_func_t(zio_t *zio, arc_buf_t *buf, void *priv);
64168404Spjd
65168404Spjd/* generic arc_done_func_t's which you can use */
66339034Ssefarc_read_done_func_t arc_bcopy_func;
67339034Ssefarc_read_done_func_t arc_getbuf_func;
68168404Spjd
69275811Sdelphijtypedef enum arc_flags
70275811Sdelphij{
71275811Sdelphij	/*
72275811Sdelphij	 * Public flags that can be passed into the ARC by external consumers.
73275811Sdelphij	 */
74307265Smav	ARC_FLAG_WAIT			= 1 << 0,	/* perform sync I/O */
75307265Smav	ARC_FLAG_NOWAIT			= 1 << 1,	/* perform async I/O */
76307265Smav	ARC_FLAG_PREFETCH		= 1 << 2,	/* I/O is a prefetch */
77307265Smav	ARC_FLAG_CACHED			= 1 << 3,	/* I/O was in cache */
78307265Smav	ARC_FLAG_L2CACHE		= 1 << 4,	/* cache in L2ARC */
79307265Smav	ARC_FLAG_PREDICTIVE_PREFETCH	= 1 << 5,	/* I/O from zfetch */
80339034Ssef	ARC_FLAG_PRESCIENT_PREFETCH	= 1 << 6,	/* long min lifespan */
81275811Sdelphij
82275811Sdelphij	/*
83275811Sdelphij	 * Private ARC flags.  These flags are private ARC only flags that
84275811Sdelphij	 * will show up in b_flags in the arc_hdr_buf_t. These flags should
85275811Sdelphij	 * only be set by ARC code.
86275811Sdelphij	 */
87339034Ssef	ARC_FLAG_IN_HASH_TABLE		= 1 << 7,	/* buffer is hashed */
88339034Ssef	ARC_FLAG_IO_IN_PROGRESS		= 1 << 8,	/* I/O in progress */
89339034Ssef	ARC_FLAG_IO_ERROR		= 1 << 9,	/* I/O failed for buf */
90339034Ssef	ARC_FLAG_INDIRECT		= 1 << 10,	/* indirect block */
91287702Sdelphij	/* Indicates that block was read with ASYNC priority. */
92339034Ssef	ARC_FLAG_PRIO_ASYNC_READ	= 1 << 11,
93339034Ssef	ARC_FLAG_L2_WRITING		= 1 << 12,	/* write in progress */
94339034Ssef	ARC_FLAG_L2_EVICTED		= 1 << 13,	/* evicted during I/O */
95339034Ssef	ARC_FLAG_L2_WRITE_HEAD		= 1 << 14,	/* head of write list */
96286570Smav	/* indicates that the buffer contains metadata (otherwise, data) */
97339034Ssef	ARC_FLAG_BUFC_METADATA		= 1 << 15,
98286570Smav
99286570Smav	/* Flags specifying whether optional hdr struct fields are defined */
100339034Ssef	ARC_FLAG_HAS_L1HDR		= 1 << 16,
101339034Ssef	ARC_FLAG_HAS_L2HDR		= 1 << 17,
102307265Smav
103307265Smav	/*
104307265Smav	 * Indicates the arc_buf_hdr_t's b_pdata matches the on-disk data.
105307265Smav	 * This allows the l2arc to use the blkptr's checksum to verify
106307265Smav	 * the data without having to store the checksum in the hdr.
107307265Smav	 */
108339034Ssef	ARC_FLAG_COMPRESSED_ARC		= 1 << 18,
109339034Ssef	ARC_FLAG_SHARED_DATA		= 1 << 19,
110307265Smav
111307265Smav	/*
112307265Smav	 * The arc buffer's compression mode is stored in the top 7 bits of the
113307265Smav	 * flags field, so these dummy flags are included so that MDB can
114307265Smav	 * interpret the enum properly.
115307265Smav	 */
116307265Smav	ARC_FLAG_COMPRESS_0		= 1 << 24,
117307265Smav	ARC_FLAG_COMPRESS_1		= 1 << 25,
118307265Smav	ARC_FLAG_COMPRESS_2		= 1 << 26,
119307265Smav	ARC_FLAG_COMPRESS_3		= 1 << 27,
120307265Smav	ARC_FLAG_COMPRESS_4		= 1 << 28,
121307265Smav	ARC_FLAG_COMPRESS_5		= 1 << 29,
122307265Smav	ARC_FLAG_COMPRESS_6		= 1 << 30
123307265Smav
124275811Sdelphij} arc_flags_t;
125275811Sdelphij
126321535Smavtypedef enum arc_buf_flags {
127321535Smav	ARC_BUF_FLAG_SHARED		= 1 << 0,
128321535Smav	ARC_BUF_FLAG_COMPRESSED		= 1 << 1
129321535Smav} arc_buf_flags_t;
130321535Smav
131168404Spjdstruct arc_buf {
132168404Spjd	arc_buf_hdr_t		*b_hdr;
133168404Spjd	arc_buf_t		*b_next;
134219089Spjd	kmutex_t		b_evict_lock;
135168404Spjd	void			*b_data;
136321535Smav	arc_buf_flags_t		b_flags;
137168404Spjd};
138168404Spjd
139168404Spjdtypedef enum arc_buf_contents {
140307265Smav	ARC_BUFC_INVALID,			/* invalid type */
141206797Spjd	ARC_BUFC_DATA,				/* buffer contains data */
142205231Skmacy	ARC_BUFC_METADATA,			/* buffer contains metadata */
143185029Spjd	ARC_BUFC_NUMTYPES
144168404Spjd} arc_buf_contents_t;
145168404Spjd
146208373Smm/*
147208373Smm * The following breakdows of arc_size exist for kstat only.
148208373Smm */
149208373Smmtypedef enum arc_space_type {
150208373Smm	ARC_SPACE_DATA,
151286574Smav	ARC_SPACE_META,
152208373Smm	ARC_SPACE_HDRS,
153208373Smm	ARC_SPACE_L2HDRS,
154208373Smm	ARC_SPACE_OTHER,
155208373Smm	ARC_SPACE_NUMTYPES
156208373Smm} arc_space_type_t;
157208373Smm
158208373Smmvoid arc_space_consume(uint64_t space, arc_space_type_t type);
159208373Smmvoid arc_space_return(uint64_t space, arc_space_type_t type);
160321535Smavboolean_t arc_is_metadata(arc_buf_t *buf);
161321535Smavenum zio_compress arc_get_compression(arc_buf_t *buf);
162321535Smavint arc_decompress(arc_buf_t *buf);
163321535Smavarc_buf_t *arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type,
164321535Smav    int32_t size);
165321535Smavarc_buf_t *arc_alloc_compressed_buf(spa_t *spa, void *tag,
166321535Smav    uint64_t psize, uint64_t lsize, enum zio_compress compression_type);
167321535Smavarc_buf_t *arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size);
168321535Smavarc_buf_t *arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize,
169321535Smav    enum zio_compress compression_type);
170209962Smmvoid arc_return_buf(arc_buf_t *buf, void *tag);
171219089Spjdvoid arc_loan_inuse_buf(arc_buf_t *buf, void *tag);
172307265Smavvoid arc_buf_destroy(arc_buf_t *buf, void *tag);
173168404Spjdint arc_buf_size(arc_buf_t *buf);
174321535Smavint arc_buf_lsize(arc_buf_t *buf);
175332785Smavvoid arc_buf_access(arc_buf_t *buf);
176168404Spjdvoid arc_release(arc_buf_t *buf, void *tag);
177168404Spjdint arc_released(arc_buf_t *buf);
178168404Spjdvoid arc_buf_freeze(arc_buf_t *buf);
179168404Spjdvoid arc_buf_thaw(arc_buf_t *buf);
180168404Spjd#ifdef ZFS_DEBUG
181168404Spjdint arc_referenced(arc_buf_t *buf);
182168404Spjd#endif
183168404Spjd
184246666Smmint arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
185339034Ssef    arc_read_done_func_t *done, void *priv, zio_priority_t priority,
186339034Ssef    int flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb);
187219089Spjdzio_t *arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
188307265Smav    blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
189339034Ssef    arc_write_done_func_t *ready, arc_write_done_func_t *child_ready,
190339034Ssef    arc_write_done_func_t *physdone, arc_write_done_func_t *done,
191304138Savg    void *priv, zio_priority_t priority, int zio_flags,
192304138Savg    const zbookmark_phys_t *zb);
193251520Sdelphijvoid arc_freed(spa_t *spa, const blkptr_t *bp);
194168404Spjd
195286763Smavvoid arc_flush(spa_t *spa, boolean_t retry);
196185029Spjdvoid arc_tempreserve_clear(uint64_t reserve);
197339141Smavint arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg);
198168404Spjd
199307265Smavuint64_t arc_max_bytes(void);
200168404Spjdvoid arc_init(void);
201168404Spjdvoid arc_fini(void);
202168404Spjd
203185029Spjd/*
204185029Spjd * Level 2 ARC
205185029Spjd */
206185029Spjd
207219089Spjdvoid l2arc_add_vdev(spa_t *spa, vdev_t *vd);
208185029Spjdvoid l2arc_remove_vdev(vdev_t *vd);
209185029Spjdboolean_t l2arc_vdev_present(vdev_t *vd);
210185029Spjdvoid l2arc_init(void);
211185029Spjdvoid l2arc_fini(void);
212185029Spjdvoid l2arc_start(void);
213185029Spjdvoid l2arc_stop(void);
214185029Spjd
215240133Smm#ifdef illumos
216240133Smm#ifndef _KERNEL
217240133Smmextern boolean_t arc_watch;
218240133Smmextern int arc_procfd;
219240133Smm#endif
220240133Smm#endif /* illumos */
221240133Smm
222168404Spjd#ifdef	__cplusplus
223168404Spjd}
224168404Spjd#endif
225168404Spjd
226168404Spjd#endif /* _SYS_ARC_H */
227