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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #ifndef _RDC_BITMAP_H
  27 #define _RDC_BITMAP_H
  28 
  29 #ifdef  __cplusplus
  30 extern "C" {
  31 #endif
  32 
  33 #ifdef _KERNEL
  34 
  35 extern int rdc_bitmap_mode;     /* property from rdc.conf */
  36 
  37 /*
  38  * Possible values of rdc_bitmap_mode - integer flag.
  39  */
  40 #define RDC_BMP_AUTO    0x0     /* auto detect bitmap mode */
  41 #define RDC_BMP_ALWAYS  0x1     /* always write the bitmap */
  42 #define RDC_BMP_NEVER   0x2     /* never write the bitmap */
  43 
  44 #endif  /* _KERNEL */
  45 
  46 /*
  47  * Public bitmap interface
  48  * The bitmaps are maintained on 32 Kbyte segments
  49  */
  50 
  51 #define LOG_SHFT                15
  52 #define IND_BYTE(ind)           ((ind) >> 3)
  53 #define IND_BIT(ind)            (1 << ((ind) & 0x7))
  54 
  55 #define FBA_LOG_SHFT            (LOG_SHFT - FBA_SHFT)
  56 #define FBA_TO_LOG_NUM(x)       ((x) >> FBA_LOG_SHFT)
  57 #define LOG_TO_FBA_NUM(x)       ((x) << FBA_LOG_SHFT)
  58 #define FBA_TO_LOG_LEN(x)       (FBA_TO_LOG_NUM((x)-1) + 1)
  59 
  60 #define BMAP_LOG_BYTES(fbas)    (IND_BYTE(FBA_TO_LOG_NUM((fbas)-1))+1)
  61 
  62 #define BITS_IN_BYTE            8
  63 
  64 /*
  65  * Private macros for bitmap manipulation
  66  */
  67 
  68 #define BMAP_BIT_SET(bmap, ind) ((bmap)[IND_BYTE(ind)] |= IND_BIT(ind))
  69 #define BMAP_BIT_CLR(bmap, ind) ((bmap)[IND_BYTE(ind)] &= ~IND_BIT(ind))
  70 #define BMAP_BIT_ISSET(bmap, ind) \
  71                                 ((bmap)[IND_BYTE(ind)] & IND_BIT(ind))
  72 
  73 #define BIT_TO_FBA(b)           (FBA_NUM(b) >> 3)
  74 
  75 #define BMAP_REF_SET(krdc, ind) (((krdc)->bm_refs->bmap_ref_set)(krdc, ind))
  76 #define BMAP_REF_CLR(krdc, ind) (((krdc)->bm_refs->bmap_ref_clr)(krdc, ind))
  77 #define BMAP_REF_ISSET(krdc, ind) (((krdc)->bm_refs->bmap_ref_isset)(krdc, ind))
  78 #define BMAP_REF_FORCE(krdc, ind, val) \
  79                         (((krdc)->bm_refs->bmap_ref_force)(krdc, ind, val))
  80 #define BMAP_REF_MAXVAL(krdc) (((krdc)->bm_refs->bmap_ref_maxval)(krdc))
  81 #define BMAP_REF_SIZE(krdc)     ((krdc)->bm_refs->bmap_ref_size)
  82 #define BMAP_REF_PREF_SIZE      (sizeof (unsigned int))
  83 
  84 #ifndef _KERNEL
  85 
  86 struct bm_ref_ops {
  87         void            (*bmap_ref_set)(void *, int);
  88         void            (*bmap_ref_clr)(void *, int);
  89         unsigned int    (*bmap_ref_isset)(void *, int);
  90         void            (*bmap_ref_force)(void *, int, unsigned int);
  91         unsigned int    (*bmap_ref_maxval)(void *);
  92         size_t          bmap_ref_size;
  93 };
  94 
  95 #else
  96 
  97 struct bm_ref_ops {
  98         void            (*bmap_ref_set)(rdc_k_info_t *, int);
  99         void            (*bmap_ref_clr)(rdc_k_info_t *, int);
 100         unsigned int    (*bmap_ref_isset)(rdc_k_info_t *, int);
 101         void            (*bmap_ref_force)(rdc_k_info_t *, int, unsigned int);
 102         unsigned int    (*bmap_ref_maxval)(rdc_k_info_t *);
 103         size_t          bmap_ref_size;
 104 };
 105 
 106 
 107 /* convert fba to block number */
 108 #define _BNUM(x)                (FBA_TO_LOG_NUM(x))
 109 
 110 /* force reference clear during sync */
 111 #define RDC_BIT_BUMP    0x0
 112 #define RDC_BIT_FORCE   0x1
 113 #define RDC_BIT_FLUSHER 0x2
 114 
 115 /* check for overlap, taking account of blocking factor */
 116 #define RDC_OVERLAP(p1, l1, p2, l2)     \
 117                     ((_BNUM(((p1) + (l1) - 1)) >= _BNUM((p2))) && \
 118                     (_BNUM((p1)) <= _BNUM(((p2) + (l2) - 1))))
 119 
 120 struct rdc_bitmap_ops {
 121         int     (*set_bitmap)(rdc_k_info_t *, const nsc_off_t, const nsc_size_t,
 122             uint_t *);
 123         void    (*clr_bitmap)(rdc_k_info_t *, const nsc_off_t, const nsc_size_t,
 124             const uint_t, const int);
 125         int     (*count_dirty)(rdc_k_info_t *);
 126         int     (*bit_isset)(rdc_k_info_t *, const int);
 127         int     (*fill_bitmap)(rdc_k_info_t *, const int);
 128         void    (*zero_bitmap)(rdc_k_info_t *);
 129         int     (*net_bmap)(const struct bmap6 *);
 130         int     (*net_b_data)(const struct net_bdata6 *);
 131         void    (*zero_bitref)(rdc_k_info_t *);
 132         void    (*set_bitmask)(const nsc_off_t, const nsc_size_t, uint_t *);
 133         void    (*check_bit)(rdc_k_info_t *, nsc_off_t, nsc_size_t);
 134 };
 135 
 136 extern struct rdc_bitmap_ops *rdc_bitmap_ops;
 137 
 138 #define RDC_SET_BITMAP(krdc, pos, len, bitmaskp) \
 139                 (*rdc_bitmap_ops->set_bitmap)(krdc, pos, len, bitmaskp)
 140 #define RDC_CLR_BITMAP(krdc, pos, len, bitmask, flag) \
 141                 (*rdc_bitmap_ops->clr_bitmap)(krdc, pos, len, bitmask, flag)
 142 #define RDC_COUNT_BITMAP(krdc) \
 143                 (*rdc_bitmap_ops->count_dirty)(krdc)
 144 #define RDC_BIT_ISSET(krdc, bit) \
 145                 (*rdc_bitmap_ops->bit_isset)(krdc, bit)
 146 #define RDC_FILL_BITMAP(krdc, write) \
 147                 (*rdc_bitmap_ops->fill_bitmap)(krdc, write)
 148 #define RDC_ZERO_BITMAP(krdc) \
 149                 (*rdc_bitmap_ops->zero_bitmap)(krdc)
 150 #define RDC_SEND_BITMAP(argp) \
 151                 (*rdc_bitmap_ops->net_bmap)(argp)
 152 #define RDC_OR_BITMAP(argp) \
 153                 (*rdc_bitmap_ops->net_b_data)(argp)
 154 #define RDC_ZERO_BITREF(krdc) \
 155                 (*rdc_bitmap_ops->zero_bitref)(krdc)
 156 #define RDC_SET_BITMASK(off, len, maskp) \
 157                 (*rdc_bitmap_ops->set_bitmask)(off, len, maskp)
 158 #define RDC_CHECK_BIT(krdc, pos, len) \
 159                 (*rdc_bitmap_ops->check_bit)(krdc, pos, len)
 160 
 161 /*
 162  * Functions
 163  */
 164 
 165 extern void rdc_bitmap_init(void);
 166 extern int rdc_move_bitmap(rdc_k_info_t *, char *);
 167 extern int rdc_enable_bitmap(rdc_k_info_t *, int);
 168 extern int rdc_resume_bitmap(rdc_k_info_t *);
 169 extern int rdc_reset_bitmap(rdc_k_info_t *);
 170 extern void rdc_free_bitmap(rdc_k_info_t *, int);
 171 extern void rdc_close_bitmap(rdc_k_info_t *);
 172 extern int rdc_write_bitmap(rdc_k_info_t *);
 173 extern int rdc_write_bitmap_fill(rdc_k_info_t *);
 174 extern void rdc_set_bitmap_many(rdc_k_info_t *, nsc_off_t, nsc_size_t);
 175 extern void rdc_merge_bitmaps(rdc_k_info_t *, rdc_k_info_t *);
 176 
 177 extern int rdc_read_state(rdc_k_info_t *, int *, int *);
 178 extern int rdc_clear_state(rdc_k_info_t *);
 179 extern void rdc_write_state(rdc_u_info_t *);
 180 extern int rdc_ns_io(nsc_fd_t *, int, nsc_off_t, uchar_t *, nsc_size_t);
 181 extern int rdc_read_refcount(rdc_k_info_t *);
 182 extern int rdc_write_refcount(rdc_k_info_t *);
 183 extern size_t rdc_refcntsize(rdc_k_info_t *);
 184 
 185 #endif /* _KERNEL */
 186 
 187 #ifdef  __cplusplus
 188 }
 189 #endif
 190 
 191 #endif /* _RDC_BITMAP_H */