1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
223f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2310fbdecMatthew Ahrens * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
24aad0257Saso Kiselkov * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25fa9e406ahrens */
26fa9e406ahrens
27fa9e406ahrens#ifndef	_SYS_ARC_H
28fa9e406ahrens#define	_SYS_ARC_H
29fa9e406ahrens
30fa9e406ahrens#include <sys/zfs_context.h>
31fa9e406ahrens
32fa9e406ahrens#ifdef	__cplusplus
33fa9e406ahrensextern "C" {
34fa9e406ahrens#endif
35fa9e406ahrens
36fa9e406ahrens#include <sys/zio.h>
37e45ce72ahrens#include <sys/dmu.h>
38fa94a07brendan#include <sys/spa.h>
39fa9e406ahrens
40244781fPrakash Surya/*
41244781fPrakash Surya * Used by arc_flush() to inform arc_evict_state() that it should evict
42244781fPrakash Surya * all available buffers from the arc state being passed in.
43244781fPrakash Surya */
44244781fPrakash Surya#define	ARC_EVICT_ALL	-1ULL
45244781fPrakash Surya
46dcbf3bdGeorge Wilson#define	HDR_SET_LSIZE(hdr, x) do { \
47dcbf3bdGeorge Wilson	ASSERT(IS_P2ALIGNED(x, 1U << SPA_MINBLOCKSHIFT)); \
48dcbf3bdGeorge Wilson	(hdr)->b_lsize = ((x) >> SPA_MINBLOCKSHIFT); \
49dcbf3bdGeorge Wilson_NOTE(CONSTCOND) } while (0)
50dcbf3bdGeorge Wilson
51dcbf3bdGeorge Wilson#define	HDR_SET_PSIZE(hdr, x) do { \
52dcbf3bdGeorge Wilson	ASSERT(IS_P2ALIGNED((x), 1U << SPA_MINBLOCKSHIFT)); \
53dcbf3bdGeorge Wilson	(hdr)->b_psize = ((x) >> SPA_MINBLOCKSHIFT); \
54dcbf3bdGeorge Wilson_NOTE(CONSTCOND) } while (0)
55dcbf3bdGeorge Wilson
56dcbf3bdGeorge Wilson#define	HDR_GET_LSIZE(hdr)	((hdr)->b_lsize << SPA_MINBLOCKSHIFT)
57dcbf3bdGeorge Wilson#define	HDR_GET_PSIZE(hdr)	((hdr)->b_psize << SPA_MINBLOCKSHIFT)
58dcbf3bdGeorge Wilson
59fa9e406ahrenstypedef struct arc_buf_hdr arc_buf_hdr_t;
60fa9e406ahrenstypedef struct arc_buf arc_buf_t;
61eb63303Tom Caputi
62eb63303Tom Caputi/*
63eb63303Tom Caputi * Because the ARC can store encrypted data, errors (not due to bugs) may arise
64eb63303Tom Caputi * while transforming data into its desired format - specifically, when
65eb63303Tom Caputi * decrypting, the key may not be present, or the HMAC may not be correct
66eb63303Tom Caputi * which signifies deliberate tampering with the on-disk state
67eb63303Tom Caputi * (assuming that the checksum was correct). The "error" parameter will be
68eb63303Tom Caputi * nonzero in this case, even if there is no associated zio.
69eb63303Tom Caputi */
70a3874b8Toomas Soometypedef void arc_read_done_func_t(zio_t *zio, const zbookmark_phys_t *zb,
71eb63303Tom Caputi    const blkptr_t *bp, arc_buf_t *buf, void *priv);
72a3874b8Toomas Soometypedef void arc_write_done_func_t(zio_t *zio, arc_buf_t *buf, void *priv);
73fa9e406ahrens
74fa9e406ahrens/* generic arc_done_func_t's which you can use */
75a3874b8Toomas Soomearc_read_done_func_t arc_bcopy_func;
76a3874b8Toomas Soomearc_read_done_func_t arc_getbuf_func;
77fa9e406ahrens
787adb730George Wilsontypedef enum arc_flags
797adb730George Wilson{
807adb730George Wilson	/*
817adb730George Wilson	 * Public flags that can be passed into the ARC by external consumers.
827adb730George Wilson	 */
83dcbf3bdGeorge Wilson	ARC_FLAG_WAIT			= 1 << 0,	/* perform sync I/O */
84dcbf3bdGeorge Wilson	ARC_FLAG_NOWAIT			= 1 << 1,	/* perform async I/O */
85dcbf3bdGeorge Wilson	ARC_FLAG_PREFETCH		= 1 << 2,	/* I/O is a prefetch */
86dcbf3bdGeorge Wilson	ARC_FLAG_CACHED			= 1 << 3,	/* I/O was in cache */
87dcbf3bdGeorge Wilson	ARC_FLAG_L2CACHE		= 1 << 4,	/* cache in L2ARC */
88dcbf3bdGeorge Wilson	ARC_FLAG_PREDICTIVE_PREFETCH	= 1 << 5,	/* I/O from zfetch */
89a3874b8Toomas Soome	ARC_FLAG_PRESCIENT_PREFETCH	= 1 << 6,	/* long min lifespan */
907adb730George Wilson
917adb730George Wilson	/*
927adb730George Wilson	 * Private ARC flags.  These flags are private ARC only flags that
937adb730George Wilson	 * will show up in b_flags in the arc_hdr_buf_t. These flags should
947adb730George Wilson	 * only be set by ARC code.
957adb730George Wilson	 */
96a3874b8Toomas Soome	ARC_FLAG_IN_HASH_TABLE		= 1 << 7,	/* buffer is hashed */
97a3874b8Toomas Soome	ARC_FLAG_IO_IN_PROGRESS		= 1 << 8,	/* I/O in progress */
98a3874b8Toomas Soome	ARC_FLAG_IO_ERROR		= 1 << 9,	/* I/O failed for buf */
99a3874b8Toomas Soome	ARC_FLAG_INDIRECT		= 1 << 10,	/* indirect block */
100cf6106cMatthew Ahrens	/* Indicates that block was read with ASYNC priority. */
101a3874b8Toomas Soome	ARC_FLAG_PRIO_ASYNC_READ	= 1 << 11,
102a3874b8Toomas Soome	ARC_FLAG_L2_WRITING		= 1 << 12,	/* write in progress */
103a3874b8Toomas Soome	ARC_FLAG_L2_EVICTED		= 1 << 13,	/* evicted during I/O */
104a3874b8Toomas Soome	ARC_FLAG_L2_WRITE_HEAD		= 1 << 14,	/* head of write list */
105eb63303Tom Caputi	/*
106eb63303Tom Caputi	 * Encrypted or authenticated on disk (may be plaintext in memory).
107eb63303Tom Caputi	 * This header has b_crypt_hdr allocated. Does not include indirect
108eb63303Tom Caputi	 * blocks with checksums of MACs which will also have their X
109eb63303Tom Caputi	 * (encrypted) bit set in the bp.
110eb63303Tom Caputi	 */
111eb63303Tom Caputi	ARC_FLAG_PROTECTED		= 1 << 15,
112eb63303Tom Caputi	/* data has not been authenticated yet */
113eb63303Tom Caputi	ARC_FLAG_NOAUTH			= 1 << 16,
11489c86e3Chris Williamson	/* indicates that the buffer contains metadata (otherwise, data) */
115eb63303Tom Caputi	ARC_FLAG_BUFC_METADATA		= 1 << 17,
11689c86e3Chris Williamson
11789c86e3Chris Williamson	/* Flags specifying whether optional hdr struct fields are defined */
118eb63303Tom Caputi	ARC_FLAG_HAS_L1HDR		= 1 << 18,
119eb63303Tom Caputi	ARC_FLAG_HAS_L2HDR		= 1 << 19,
120dcbf3bdGeorge Wilson
121dcbf3bdGeorge Wilson	/*
122dcbf3bdGeorge Wilson	 * Indicates the arc_buf_hdr_t's b_pdata matches the on-disk data.
123dcbf3bdGeorge Wilson	 * This allows the l2arc to use the blkptr's checksum to verify
124dcbf3bdGeorge Wilson	 * the data without having to store the checksum in the hdr.
125dcbf3bdGeorge Wilson	 */
126eb63303Tom Caputi	ARC_FLAG_COMPRESSED_ARC		= 1 << 20,
127eb63303Tom Caputi	ARC_FLAG_SHARED_DATA		= 1 << 21,
128dcbf3bdGeorge Wilson
129dcbf3bdGeorge Wilson	/*
130dcbf3bdGeorge Wilson	 * The arc buffer's compression mode is stored in the top 7 bits of the
131dcbf3bdGeorge Wilson	 * flags field, so these dummy flags are included so that MDB can
132dcbf3bdGeorge Wilson	 * interpret the enum properly.
133dcbf3bdGeorge Wilson	 */
134dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_0		= 1 << 24,
135dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_1		= 1 << 25,
136dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_2		= 1 << 26,
137dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_3		= 1 << 27,
138dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_4		= 1 << 28,
139dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_5		= 1 << 29,
140dcbf3bdGeorge Wilson	ARC_FLAG_COMPRESS_6		= 1 << 30
141dcbf3bdGeorge Wilson
1427adb730George Wilson} arc_flags_t;
1437adb730George Wilson
1445602294Dan Kimmeltypedef enum arc_buf_flags {
1455602294Dan Kimmel	ARC_BUF_FLAG_SHARED		= 1 << 0,
146eb63303Tom Caputi	ARC_BUF_FLAG_COMPRESSED		= 1 << 1,
147eb63303Tom Caputi	/*
148eb63303Tom Caputi	 * indicates whether this arc_buf_t is encrypted, regardless of
149eb63303Tom Caputi	 * state on-disk
150eb63303Tom Caputi	 */
151eb63303Tom Caputi	ARC_BUF_FLAG_ENCRYPTED		= 1 << 2
1525602294Dan Kimmel} arc_buf_flags_t;
1535602294Dan Kimmel
154fa9e406ahrensstruct arc_buf {
155fa9e406ahrens	arc_buf_hdr_t		*b_hdr;
156fa9e406ahrens	arc_buf_t		*b_next;
1573f9d6adLin Ling	kmutex_t		b_evict_lock;
158fa9e406ahrens	void			*b_data;
1595602294Dan Kimmel	arc_buf_flags_t		b_flags;
160fa9e406ahrens};
161fa9e406ahrens
162ad23a2djohansentypedef enum arc_buf_contents {
163dcbf3bdGeorge Wilson	ARC_BUFC_INVALID,			/* invalid type */
164ad23a2djohansen	ARC_BUFC_DATA,				/* buffer contains data */
1650e8c615maybee	ARC_BUFC_METADATA,			/* buffer contains metadata */
1660e8c615maybee	ARC_BUFC_NUMTYPES
167ad23a2djohansen} arc_buf_contents_t;
168fa9e406ahrens
1695a98e54Brendan Gregg - Sun Microsystems/*
1705a98e54Brendan Gregg - Sun Microsystems * The following breakdows of arc_size exist for kstat only.
1715a98e54Brendan Gregg - Sun Microsystems */
1725a98e54Brendan Gregg - Sun Microsystemstypedef enum arc_space_type {
1735a98e54Brendan Gregg - Sun Microsystems	ARC_SPACE_DATA,
1744076b1bPrakash Surya	ARC_SPACE_META,
1755a98e54Brendan Gregg - Sun Microsystems	ARC_SPACE_HDRS,
1765a98e54Brendan Gregg - Sun Microsystems	ARC_SPACE_L2HDRS,
1775a98e54Brendan Gregg - Sun Microsystems	ARC_SPACE_OTHER,
17854811daToomas Soome	ARC_SPACE_BONUS,
1795a98e54Brendan Gregg - Sun Microsystems	ARC_SPACE_NUMTYPES
1805a98e54Brendan Gregg - Sun Microsystems} arc_space_type_t;
1815a98e54Brendan Gregg - Sun Microsystems
1825a98e54Brendan Gregg - Sun Microsystemsvoid arc_space_consume(uint64_t space, arc_space_type_t type);
1835a98e54Brendan Gregg - Sun Microsystemsvoid arc_space_return(uint64_t space, arc_space_type_t type);
1845602294Dan Kimmelboolean_t arc_is_metadata(arc_buf_t *buf);
185eb63303Tom Caputiboolean_t arc_is_encrypted(arc_buf_t *buf);
186eb63303Tom Caputiboolean_t arc_is_unauthenticated(arc_buf_t *buf);
1875602294Dan Kimmelenum zio_compress arc_get_compression(arc_buf_t *buf);
188eb63303Tom Caputivoid arc_get_raw_params(arc_buf_t *buf, boolean_t *byteorder, uint8_t *salt,
189eb63303Tom Caputi    uint8_t *iv, uint8_t *mac);
190eb63303Tom Caputiint arc_untransform(arc_buf_t *buf, spa_t *spa, const zbookmark_phys_t *zb,
191eb63303Tom Caputi    boolean_t in_place);
192eb63303Tom Caputivoid arc_convert_to_raw(arc_buf_t *buf, uint64_t dsobj, boolean_t byteorder,
193eb63303Tom Caputi    dmu_object_type_t ot, const uint8_t *salt, const uint8_t *iv,
194eb63303Tom Caputi    const uint8_t *mac);
1955602294Dan Kimmelarc_buf_t *arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type,
1965602294Dan Kimmel    int32_t size);
1975602294Dan Kimmelarc_buf_t *arc_alloc_compressed_buf(spa_t *spa, void *tag,
1985602294Dan Kimmel    uint64_t psize, uint64_t lsize, enum zio_compress compression_type);
199eb63303Tom Caputiarc_buf_t *arc_alloc_raw_buf(spa_t *spa, void *tag, uint64_t dsobj,
200eb63303Tom Caputi    boolean_t byteorder, const uint8_t *salt, const uint8_t *iv,
201eb63303Tom Caputi    const uint8_t *mac, dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
202eb63303Tom Caputi    enum zio_compress compression_type);
2035602294Dan Kimmelarc_buf_t *arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size);
2045602294Dan Kimmelarc_buf_t *arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize,
2055602294Dan Kimmel    enum zio_compress compression_type);
206eb63303Tom Caputiarc_buf_t *arc_loan_raw_buf(spa_t *spa, uint64_t dsobj, boolean_t byteorder,
207eb63303Tom Caputi    const uint8_t *salt, const uint8_t *iv, const uint8_t *mac,
208eb63303Tom Caputi    dmu_object_type_t ot, uint64_t psize, uint64_t lsize,
209eb63303Tom Caputi    enum zio_compress compression_type);
2102fdbea2Aleksandr Guzovskiyvoid arc_return_buf(arc_buf_t *buf, void *tag);
211c242f9achunli zhang - Sun Microsystems - Irvine United Statesvoid arc_loan_inuse_buf(arc_buf_t *buf, void *tag);
212dcbf3bdGeorge Wilsonvoid arc_buf_destroy(arc_buf_t *buf, void *tag);
213fa9e406ahrensint arc_buf_size(arc_buf_t *buf);
2145602294Dan Kimmelint arc_buf_lsize(arc_buf_t *buf);
2157b38fabAlexander Motinvoid arc_buf_access(arc_buf_t *buf);
216fa9e406ahrensvoid arc_release(arc_buf_t *buf, void *tag);
217fa9e406ahrensint arc_released(arc_buf_t *buf);
2186b4acc8ahrensvoid arc_buf_freeze(arc_buf_t *buf);
2196b4acc8ahrensvoid arc_buf_thaw(arc_buf_t *buf);
220ea8dc4beschrock#ifdef ZFS_DEBUG
221ea8dc4beschrockint arc_referenced(arc_buf_t *buf);
222ea8dc4beschrock#endif
223fa9e406ahrens
2241b912ecGeorge Wilsonint arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
225a3874b8Toomas Soome    arc_read_done_func_t *done, void *private, zio_priority_t priority,
226a3874b8Toomas Soome    int flags, arc_flags_t *arc_flags, const zbookmark_phys_t *zb);
227b24ab67Jeff Bonwickzio_t *arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
228dcbf3bdGeorge Wilson    blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
229a3874b8Toomas Soome    arc_write_done_func_t *ready, arc_write_done_func_t *child_ready,
230a3874b8Toomas Soome    arc_write_done_func_t *physdone, arc_write_done_func_t *done,
2318df0bcfPaul Dagnelie    void *private, zio_priority_t priority, int zio_flags,
2328df0bcfPaul Dagnelie    const zbookmark_phys_t *zb);
2336e6d586Matthew Ahrensvoid arc_freed(spa_t *spa, const blkptr_t *bp);
234fa9e406ahrens
235244781fPrakash Suryavoid arc_flush(spa_t *spa, boolean_t retry);
2361ab7f2dmaybeevoid arc_tempreserve_clear(uint64_t reserve);
237abe1fd0Don Bradyint arc_tempreserve_space(spa_t *spa, uint64_t reserve, uint64_t txg);
238fa9e406ahrens
239af1d63aPaul Dagnelieuint64_t arc_all_memory(void);
240dcbf3bdGeorge Wilsonuint64_t arc_max_bytes(void);
241fa9e406ahrensvoid arc_init(void);
242fa9e406ahrensvoid arc_fini(void);
243fa9e406ahrens
244fa94a07brendan/*
245fa94a07brendan * Level 2 ARC
246fa94a07brendan */
247fa94a07brendan
248573ca77George Wilsonvoid l2arc_add_vdev(spa_t *spa, vdev_t *vd);
249fa94a07brendanvoid l2arc_remove_vdev(vdev_t *vd);
250c5904d1eschrockboolean_t l2arc_vdev_present(vdev_t *vd);
251fa94a07brendanvoid l2arc_init(void);
252fa94a07brendanvoid l2arc_fini(void);
253e14bb32Jeff Bonwickvoid l2arc_start(void);
254e14bb32Jeff Bonwickvoid l2arc_stop(void);
255fa94a07brendan
256cd1c8b8Matthew Ahrens#ifndef _KERNEL
257cd1c8b8Matthew Ahrensextern boolean_t arc_watch;
258cd1c8b8Matthew Ahrensextern int arc_procfd;
259cd1c8b8Matthew Ahrens#endif
260cd1c8b8Matthew Ahrens
261fa9e406ahrens#ifdef	__cplusplus
262fa9e406ahrens}
263fa9e406ahrens#endif
264fa9e406ahrens
265fa9e406ahrens#endif /* _SYS_ARC_H */
266