1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
  24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  25  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
  26  */
  27 
  28 #ifndef _SYS_ARC_H
  29 #define _SYS_ARC_H
  30 
  31 #include <sys/zfs_context.h>
  32 
  33 #ifdef  __cplusplus
  34 extern "C" {
  35 #endif
  36 
  37 #include <sys/kreplication_common.h>
  38 #include <sys/zio.h>
  39 #include <sys/dmu.h>
  40 #include <sys/spa.h>
  41 
  42 /*
  43  * Used by arc_flush() to inform arc_evict_state() that it should evict
  44  * all available buffers from the arc state being passed in.
  45  */
  46 #define ARC_EVICT_ALL   -1ULL
  47 
  48 #define HDR_SET_LSIZE(hdr, x) do { \
  49         ASSERT(IS_P2ALIGNED(x, 1U << SPA_MINBLOCKSHIFT)); \
  50         (hdr)->b_lsize = ((x) >> SPA_MINBLOCKSHIFT); \
  51 _NOTE(CONSTCOND) } while (0)
  52 
  53 #define HDR_SET_PSIZE(hdr, x) do { \
  54         ASSERT(IS_P2ALIGNED((x), 1U << SPA_MINBLOCKSHIFT)); \
  55         (hdr)->b_psize = ((x) >> SPA_MINBLOCKSHIFT); \
  56 _NOTE(CONSTCOND) } while (0)
  57 
  58 #define HDR_GET_LSIZE(hdr)      ((hdr)->b_lsize << SPA_MINBLOCKSHIFT)
  59 #define HDR_GET_PSIZE(hdr)      ((hdr)->b_psize << SPA_MINBLOCKSHIFT)
  60 
  61 typedef struct arc_buf_hdr arc_buf_hdr_t;
  62 typedef struct arc_buf arc_buf_t;
  63 typedef void arc_done_func_t(zio_t *zio, arc_buf_t *buf, void *private);
  64 
  65 /* generic arc_done_func_t's which you can use */
  66 arc_done_func_t arc_bcopy_func;
  67 arc_done_func_t arc_getbuf_func;
  68 
  69 typedef enum arc_flags
  70 {
  71         /*
  72          * Public flags that can be passed into the ARC by external consumers.
  73          */
  74         ARC_FLAG_WAIT                   = 1 << 0, /* perform sync I/O */
  75         ARC_FLAG_NOWAIT                 = 1 << 1, /* perform async I/O */
  76         ARC_FLAG_PREFETCH               = 1 << 2, /* I/O is a prefetch */
  77         ARC_FLAG_CACHED                 = 1 << 3, /* I/O was in cache */
  78         ARC_FLAG_L2CACHE                = 1 << 4, /* cache in L2ARC */
  79         ARC_FLAG_PREDICTIVE_PREFETCH    = 1 << 5, /* I/O from zfetch */
  80 
  81         /*
  82          * Private ARC flags.  These flags are private ARC only flags that
  83          * will show up in b_flags in the arc_hdr_buf_t. These flags should
  84          * only be set by ARC code.
  85          */
  86         ARC_FLAG_IN_HASH_TABLE          = 1 << 6, /* buffer is hashed */
  87         ARC_FLAG_IO_IN_PROGRESS         = 1 << 7, /* I/O in progress */
  88         ARC_FLAG_IO_ERROR               = 1 << 8, /* I/O failed for buf */
  89         ARC_FLAG_INDIRECT               = 1 << 9, /* indirect block */
  90         /* Indicates that block was read with ASYNC priority. */
  91         ARC_FLAG_PRIO_ASYNC_READ        = 1 << 10,
  92         ARC_FLAG_L2_WRITING             = 1 << 11,        /* write in progress */
  93         ARC_FLAG_L2_EVICTED             = 1 << 12,        /* evicted during I/O */
  94         ARC_FLAG_L2_WRITE_HEAD          = 1 << 13,        /* head of write list */
  95 
  96         /*
  97          * Below BUFC flags indicate that either the buffer contains
  98          * metadata or DDT metadata. If both of these are not set then the
  99          * buffer contains data.
 100          * ARC_FLAG_BUFC_DDT is only used when zfs_arc_segregate_ddt is true.
 101          * If this tunable is zero ARC_FLAG_BUFC_METADATA is used for both DDT
 102          * and regular metadata.
 103          */
 104         ARC_FLAG_BUFC_METADATA          = 1 << 14,
 105 
 106         /* Flags specifying whether optional hdr struct fields are defined */
 107         ARC_FLAG_HAS_L1HDR              = 1 << 15,
 108         ARC_FLAG_HAS_L2HDR              = 1 << 16,
 109 
 110         /*
 111          * Indicates the arc_buf_hdr_t's b_pdata matches the on-disk data.
 112          * This allows the l2arc to use the blkptr's checksum to verify
 113          * the data without having to store the checksum in the hdr.
 114          */
 115         ARC_FLAG_COMPRESSED_ARC         = 1 << 17,
 116         ARC_FLAG_SHARED_DATA            = 1 << 18,
 117 
 118         ARC_FLAG_BUFC_DDT               = 1 << 19,        /* DDT buf */
 119 
 120         /*
 121          * The arc buffer's compression mode is stored in the top 7 bits of the
 122          * flags field, so these dummy flags are included so that MDB can
 123          * interpret the enum properly.
 124          */
 125         ARC_FLAG_COMPRESS_0             = 1 << 24,
 126         ARC_FLAG_COMPRESS_1             = 1 << 25,
 127         ARC_FLAG_COMPRESS_2             = 1 << 26,
 128         ARC_FLAG_COMPRESS_3             = 1 << 27,
 129         ARC_FLAG_COMPRESS_4             = 1 << 28,
 130         ARC_FLAG_COMPRESS_5             = 1 << 29,
 131         ARC_FLAG_COMPRESS_6             = 1 << 30
 132 } arc_flags_t;
 133 
 134 typedef enum arc_buf_flags {
 135         ARC_BUF_FLAG_SHARED             = 1 << 0,
 136         ARC_BUF_FLAG_COMPRESSED         = 1 << 1
 137 } arc_buf_flags_t;
 138 
 139 struct arc_buf {
 140         arc_buf_hdr_t           *b_hdr;
 141         arc_buf_t               *b_next;
 142         kmutex_t                b_evict_lock;
 143         void                    *b_data;
 144         arc_buf_flags_t         b_flags;
 145 };
 146 
 147 typedef enum arc_buf_contents {
 148         ARC_BUFC_INVALID,                       /* invalid type */
 149         ARC_BUFC_DATA,                          /* buffer contains data */
 150         ARC_BUFC_METADATA,                      /* buffer contains metadata */
 151         ARC_BUFC_DDT,                           /* buffer contains ddt */
 152         ARC_BUFC_NUMTYPES
 153 } arc_buf_contents_t;
 154 
 155 /*
 156  * The following breakdows of arc_size exist for kstat only.
 157  */
 158 typedef enum arc_space_type {
 159         ARC_SPACE_DATA,
 160         ARC_SPACE_META,
 161         ARC_SPACE_DDT,
 162         ARC_SPACE_HDRS,
 163         ARC_SPACE_L2HDRS,
 164         ARC_SPACE_OTHER,
 165         ARC_SPACE_NUMTYPES
 166 } arc_space_type_t;
 167 
 168 /* see spa_misc.c and zio_ddt_write() */
 169 extern uint64_t zfs_ddt_byte_ceiling;
 170 typedef enum zfs_ddt_limit {
 171         DDT_NO_LIMIT = 0,
 172         DDT_LIMIT_TO_ARC = 1,
 173         DDT_LIMIT_TO_L2ARC = 2,
 174 } zfs_ddt_limit_t;
 175 extern zfs_ddt_limit_t zfs_ddt_limit_type;
 176 extern boolean_t zfs_arc_segregate_ddt;
 177 extern uint64_t const * arc_ddt_evict_threshold;
 178 
 179 
 180 void arc_space_consume(uint64_t space, arc_space_type_t type);
 181 void arc_space_return(uint64_t space, arc_space_type_t type);
 182 boolean_t arc_is_metadata(arc_buf_t *buf);
 183 enum zio_compress arc_get_compression(arc_buf_t *buf);
 184 int arc_decompress(arc_buf_t *buf);
 185 arc_buf_t *arc_alloc_buf(spa_t *spa, void *tag, arc_buf_contents_t type,
 186     int32_t size);
 187 arc_buf_t *arc_alloc_compressed_buf(spa_t *spa, void *tag,
 188     uint64_t psize, uint64_t lsize, enum zio_compress compression_type);
 189 arc_buf_t *arc_loan_buf(spa_t *spa, boolean_t is_metadata, int size);
 190 arc_buf_t *arc_loan_compressed_buf(spa_t *spa, uint64_t psize, uint64_t lsize,
 191     enum zio_compress compression_type);
 192 void arc_return_buf(arc_buf_t *buf, void *tag);
 193 void arc_loan_inuse_buf(arc_buf_t *buf, void *tag);
 194 void arc_buf_destroy(arc_buf_t *buf, void *tag);
 195 int arc_buf_size(arc_buf_t *buf);
 196 int arc_buf_lsize(arc_buf_t *buf);
 197 void arc_buf_access(arc_buf_t *buf);
 198 void arc_release(arc_buf_t *buf, void *tag);
 199 int arc_released(arc_buf_t *buf);
 200 void arc_buf_freeze(arc_buf_t *buf);
 201 void arc_buf_thaw(arc_buf_t *buf);
 202 #ifdef ZFS_DEBUG
 203 int arc_referenced(arc_buf_t *buf);
 204 #endif
 205 
 206 int arc_io_bypass(spa_t *spa, const blkptr_t *bp,
 207     arc_bypass_io_func func, void *arg);
 208 int arc_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
 209     arc_done_func_t *done, void *private, zio_priority_t priority, int flags,
 210     arc_flags_t *arc_flags, const zbookmark_phys_t *zb);
 211 zio_t *arc_write(zio_t *pio, spa_t *spa, uint64_t txg,
 212     blkptr_t *bp, arc_buf_t *buf, boolean_t l2arc, const zio_prop_t *zp,
 213     arc_done_func_t *ready, arc_done_func_t *child_ready,
 214     arc_done_func_t *physdone, arc_done_func_t *done,
 215     void *private, zio_priority_t priority, int zio_flags,
 216     const zbookmark_phys_t *zb, const zio_smartcomp_info_t *smartcomp);
 217 void arc_freed(spa_t *spa, const blkptr_t *bp);
 218 
 219 void arc_flush(spa_t *spa, boolean_t retry);
 220 void arc_tempreserve_clear(uint64_t reserve);
 221 int arc_tempreserve_space(uint64_t reserve, uint64_t txg);
 222 
 223 uint64_t arc_max_bytes(void);
 224 void arc_init(void);
 225 void arc_fini(void);
 226 
 227 /*
 228  * Level 2 ARC
 229  */
 230 
 231 void l2arc_add_vdev(spa_t *spa, vdev_t *vd, boolean_t rebuild);
 232 void l2arc_remove_vdev(vdev_t *vd);
 233 boolean_t l2arc_vdev_present(vdev_t *vd);
 234 void l2arc_init(void);
 235 void l2arc_fini(void);
 236 void l2arc_start(void);
 237 void l2arc_stop(void);
 238 void l2arc_spa_rebuild_start(spa_t *spa);
 239 
 240 #ifndef _KERNEL
 241 extern boolean_t arc_watch;
 242 extern int arc_procfd;
 243 #endif
 244 
 245 #ifdef  __cplusplus
 246 }
 247 #endif
 248 
 249 #endif /* _SYS_ARC_H */